|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| RequestContext.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 | // Copyright 2004, 2005 The Apache Software Foundation | |
| 2 | // | |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | // you may not use this file except in compliance with the License. | |
| 5 | // You may obtain a copy of the License at | |
| 6 | // | |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | // | |
| 9 | // Unless required by applicable law or agreed to in writing, software | |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | // See the License for the specific language governing permissions and | |
| 13 | // limitations under the License. | |
| 14 | ||
| 15 | package org.apache.tapestry.request; | |
| 16 | ||
| 17 | import java.util.ArrayList; | |
| 18 | import java.util.Enumeration; | |
| 19 | import java.util.List; | |
| 20 | ||
| 21 | import javax.servlet.http.HttpServletRequest; | |
| 22 | import javax.servlet.http.HttpServletResponse; | |
| 23 | import javax.servlet.http.HttpSession; | |
| 24 | ||
| 25 | /** | |
| 26 | * This class encapsulates all the relevant data for one request cycle of an | |
| 27 | * {@link ApplicationServlet}. This includes: | |
| 28 | * <ul> | |
| 29 | * <li>{@link HttpServletRequest} | |
| 30 | * <li>{@link HttpServletResponse} | |
| 31 | * <li>{@link HttpSession} | |
| 32 | * </ul> | |
| 33 | * <p> | |
| 34 | * This is a limited and crippled version of the RequestContext as it was available in release 3.0, | |
| 35 | * that exists as a bridge for compatibility only. This saves developers from having to modify their | |
| 36 | * classes to have the {@link javax.servlet.http.HttpServletRequest} or (preferrably) | |
| 37 | * {@link org.apache.tapestry.web.WebRequest} injected into their pages, components, or services. It | |
| 38 | * will be removed in the next release of Tapestry. | |
| 39 | * | |
| 40 | * @author Howard Lewis Ship | |
| 41 | * @deprecated To be removed in 4.1. Use injection to gain access to the necessary objects. | |
| 42 | */ | |
| 43 | ||
| 44 | public class RequestContext | |
| 45 | { | |
| 46 | private final HttpServletRequest _request; | |
| 47 | ||
| 48 | private final HttpServletResponse _response; | |
| 49 | ||
| 50 | 396 | public RequestContext(HttpServletRequest request, HttpServletResponse response) |
| 51 | { | |
| 52 | 396 | _request = request; |
| 53 | 396 | _response = response; |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Returns the named parameter from the {@link HttpServletRequest}. | |
| 58 | * <p> | |
| 59 | * Use {@link #getParameters(String)}for parameters that may include multiple values. | |
| 60 | */ | |
| 61 | ||
| 62 | 3 | public String getParameter(String name) |
| 63 | { | |
| 64 | 3 | return _request.getParameter(name); |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Convienience method for getting a {@link HttpServletRequest}attribute. | |
| 69 | * | |
| 70 | * @since 2.3 | |
| 71 | */ | |
| 72 | ||
| 73 | 3 | public Object getAttribute(String name) |
| 74 | { | |
| 75 | 3 | return _request.getAttribute(name); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * For parameters that are, or are possibly, multi-valued, this method returns all the values as | |
| 80 | * an array of Strings. | |
| 81 | * | |
| 82 | * @see #getParameter(String) | |
| 83 | */ | |
| 84 | ||
| 85 | 3 | public String[] getParameters(String name) |
| 86 | { | |
| 87 | 3 | return _request.getParameterValues(name); |
| 88 | } | |
| 89 | ||
| 90 | 3 | public String[] getParameterNames() |
| 91 | { | |
| 92 | 3 | Enumeration e = _request.getParameterNames(); |
| 93 | 3 | List names = new ArrayList(); |
| 94 | ||
| 95 | 3 | while (e.hasMoreElements()) |
| 96 | 6 | names.add(e.nextElement()); |
| 97 | ||
| 98 | 3 | int count = names.size(); |
| 99 | ||
| 100 | 3 | String[] result = new String[count]; |
| 101 | ||
| 102 | 3 | return (String[]) names.toArray(result); |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Returns the request which initiated the current request cycle. Note that the methods | |
| 107 | * {@link #getParameter(String)}and {@link #getParameters(String)}should be used, rather than | |
| 108 | * obtaining parameters directly from the request (since the RequestContext handles the | |
| 109 | * differences between normal and multipart/form requests). | |
| 110 | */ | |
| 111 | ||
| 112 | 3 | public HttpServletRequest getRequest() |
| 113 | { | |
| 114 | 3 | return _request; |
| 115 | } | |
| 116 | ||
| 117 | 3 | public HttpServletResponse getResponse() |
| 118 | { | |
| 119 | 3 | return _response; |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Returns the {@link HttpSession}, if necessary, invoking | |
| 124 | * {@link HttpServletRequest#getSession(boolean)}. However, this method will <em>not</em> | |
| 125 | * create a session. | |
| 126 | */ | |
| 127 | ||
| 128 | 3 | public HttpSession getSession() |
| 129 | { | |
| 130 | 3 | return _request.getSession(false); |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Like {@link #getSession()}, but forces the creation of the {@link HttpSession}, if | |
| 135 | * necessary. | |
| 136 | */ | |
| 137 | ||
| 138 | 3 | public HttpSession createSession() |
| 139 | { | |
| 140 | 3 | return _request.getSession(true); |
| 141 | } | |
| 142 | ||
| 143 | } |
|
||||||||||