001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.request; 016 017 import java.util.ArrayList; 018 import java.util.Enumeration; 019 import java.util.List; 020 021 import javax.servlet.http.HttpServletRequest; 022 import javax.servlet.http.HttpServletResponse; 023 import javax.servlet.http.HttpSession; 024 025 /** 026 * This class encapsulates all the relevant data for one request cycle of an 027 * {@link ApplicationServlet}. This includes: 028 * <ul> 029 * <li>{@link HttpServletRequest} 030 * <li>{@link HttpServletResponse} 031 * <li>{@link HttpSession} 032 * </ul> 033 * <p> 034 * This is a limited and crippled version of the RequestContext as it was available in release 3.0, 035 * that exists as a bridge for compatibility only. This saves developers from having to modify their 036 * classes to have the {@link javax.servlet.http.HttpServletRequest} or 037 * {@link org.apache.tapestry.web.WebRequest}injected into their pages, components, or services. It 038 * will be removed in the next release of Tapestry. 039 * <p> 040 * Interestingly, with the Tapestry 4.0 architecture, a single instance of RequestContext can be 041 * shared by all request cycles (that's because the request and response are, in fact, threaded 042 * proxies). 043 * 044 * @author Howard Lewis Ship 045 * @deprecated To be removed in 4.1. Use injection to gain access to the necessary objects. 046 */ 047 048 public class RequestContext 049 { 050 private final HttpServletRequest _request; 051 052 private final HttpServletResponse _response; 053 054 public RequestContext(HttpServletRequest request, HttpServletResponse response) 055 { 056 057 _request = request; 058 _response = response; 059 } 060 061 /** 062 * Returns the named parameter from the {@link HttpServletRequest}. 063 * <p> 064 * Use {@link #getParameters(String)}for parameters that may include multiple values. 065 */ 066 067 public String getParameter(String name) 068 { 069 return _request.getParameter(name); 070 } 071 072 /** 073 * Convienience method for getting a {@link HttpServletRequest}attribute. 074 * 075 * @since 2.3 076 */ 077 078 public Object getAttribute(String name) 079 { 080 return _request.getAttribute(name); 081 } 082 083 /** 084 * For parameters that are, or are possibly, multi-valued, this method returns all the values as 085 * an array of Strings. 086 * 087 * @see #getParameter(String) 088 */ 089 090 public String[] getParameters(String name) 091 { 092 return _request.getParameterValues(name); 093 } 094 095 public String[] getParameterNames() 096 { 097 Enumeration e = _request.getParameterNames(); 098 List names = new ArrayList(); 099 100 while (e.hasMoreElements()) 101 names.add(e.nextElement()); 102 103 int count = names.size(); 104 105 String[] result = new String[count]; 106 107 return (String[]) names.toArray(result); 108 } 109 110 /** 111 * Returns the request which initiated the current request cycle. Note that the methods 112 * {@link #getParameter(String)}and {@link #getParameters(String)}should be used, rather than 113 * obtaining parameters directly from the request (since the RequestContext handles the 114 * differences between normal and multipart/form requests). 115 */ 116 117 public HttpServletRequest getRequest() 118 { 119 return _request; 120 } 121 122 public HttpServletResponse getResponse() 123 { 124 return _response; 125 } 126 127 /** 128 * Returns the {@link HttpSession}, if necessary, invoking 129 * {@link HttpServletRequest#getSession(boolean)}. However, this method will <em>not</em> 130 * create a session. 131 */ 132 133 public HttpSession getSession() 134 { 135 return _request.getSession(false); 136 } 137 138 /** 139 * Like {@link #getSession()}, but forces the creation of the {@link HttpSession}, if 140 * necessary. 141 */ 142 143 public HttpSession createSession() 144 { 145 return _request.getSession(true); 146 } 147 148 }