1 15 package org.apache.tapestry.web; 16 17 import java.io.IOException ; 18 import java.util.List ; 19 import java.util.Locale ; 20 21 import javax.servlet.RequestDispatcher ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.HttpSession ; 26 27 import org.apache.hivemind.ApplicationRuntimeException; 28 import org.apache.hivemind.HiveMind; 29 import org.apache.hivemind.util.Defense; 30 import org.apache.tapestry.describe.DescriptionReceiver; 31 32 39 public class ServletWebRequest implements WebRequest 40 { 41 private final HttpServletRequest _servletRequest; 42 43 private final HttpServletResponse _servletResponse; 44 45 private WebSession _webSession; 46 47 public ServletWebRequest(HttpServletRequest request, HttpServletResponse response) 48 { 49 Defense.notNull(request, "request"); 50 Defense.notNull(response, "response"); 51 52 _servletRequest = request; 53 _servletResponse = response; 54 } 55 56 public List getParameterNames() 57 { 58 return WebUtils.toSortedList(_servletRequest.getParameterNames()); 59 } 60 61 public String getParameterValue(String parameterName) 62 { 63 return _servletRequest.getParameter(parameterName); 64 } 65 66 public String [] getParameterValues(String parameterName) 67 { 68 return _servletRequest.getParameterValues(parameterName); 69 } 70 71 public String getContextPath() 72 { 73 return _servletRequest.getContextPath(); 74 } 75 76 public WebSession getSession(boolean create) 77 { 78 if (_webSession != null) 79 return _webSession; 80 81 HttpSession session = _servletRequest.getSession(create); 82 83 if (session != null) 84 _webSession = new ServletWebSession(session); 85 86 return _webSession; 87 } 88 89 public List getAttributeNames() 90 { 91 return WebUtils.toSortedList(_servletRequest.getAttributeNames()); 92 } 93 94 public Object getAttribute(String name) 95 { 96 return _servletRequest.getAttribute(name); 97 } 98 99 public void setAttribute(String name, Object attribute) 100 { 101 if (attribute == null) 102 _servletRequest.removeAttribute(name); 103 else 104 _servletRequest.setAttribute(name, attribute); 105 } 106 107 public String getScheme() 108 { 109 return _servletRequest.getScheme(); 110 } 111 112 public String getServerName() 113 { 114 return _servletRequest.getServerName(); 115 } 116 117 public int getServerPort() 118 { 119 return _servletRequest.getServerPort(); 120 } 121 122 public String getRequestURI() 123 { 124 return _servletRequest.getRequestURI(); 125 } 126 127 public void forward(String URL) 128 { 129 if (HiveMind.isBlank(URL)) 130 { 131 performForward("/"); 132 return; 133 } 134 135 boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0); 136 137 if (internal) 138 performForward("/" + URL); 139 else 140 sendRedirect(URL); 141 } 142 143 private void sendRedirect(String URL) 144 { 145 String finalURL = _servletResponse.encodeRedirectURL(URL); 146 147 try 148 { 149 _servletResponse.sendRedirect(finalURL); 150 } 151 catch (IOException ex) 152 { 153 throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex); 154 } 155 156 } 157 158 private void performForward(String URL) 159 { 160 RequestDispatcher dispatcher = _servletRequest.getRequestDispatcher(URL); 161 162 if (dispatcher == null) 163 throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL)); 164 165 try 166 { 167 dispatcher.forward(_servletRequest, _servletResponse); 168 } 169 catch (ServletException ex) 170 { 171 throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex); 172 } 173 catch (IOException ex) 174 { 175 throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex); 176 } 177 } 178 179 182 public String getActivationPath() 183 { 184 String servletPath = _servletRequest.getServletPath(); 185 String pathInfo = _servletRequest.getPathInfo(); 186 187 return pathInfo == null ? servletPath : servletPath + pathInfo; 188 } 189 190 public Locale getLocale() 191 { 192 return _servletRequest.getLocale(); 193 } 194 195 public void describeTo(DescriptionReceiver receiver) 196 { 197 receiver.describeAlternate(_servletRequest); 198 } 199 200 public String getHeader(String name) 201 { 202 return _servletRequest.getHeader(name); 203 } 204 } | Popular Tags |