1 16 package org.apache.cocoon.environment.http; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.net.MalformedURLException ; 21 22 import javax.servlet.ServletContext ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.cocoon.environment.AbstractEnvironment; 27 import org.apache.cocoon.environment.ObjectModelHelper; 28 import org.apache.cocoon.environment.PermanentRedirector; 29 import org.apache.cocoon.environment.Redirector; 30 import org.apache.cocoon.environment.Session; 31 import org.apache.cocoon.util.NetUtils; 32 33 39 public class HttpEnvironment extends AbstractEnvironment 40 implements Redirector, PermanentRedirector { 41 42 public static final String HTTP_REQUEST_OBJECT = "httprequest"; 43 public static final String HTTP_RESPONSE_OBJECT= "httpresponse"; 44 public static final String HTTP_SERVLET_CONTEXT= "httpservletcontext"; 45 46 47 private HttpRequest request; 48 49 50 private HttpResponse response; 51 52 53 private HttpContext webcontext; 54 55 56 private String contentType; 57 58 59 private boolean hasRedirected = false; 60 61 65 public HttpEnvironment(String uri, 66 String root, 67 HttpServletRequest req, 68 HttpServletResponse res, 69 ServletContext servletContext, 70 HttpContext context, 71 String containerEncoding, 72 String defaultFormEncoding) 73 throws MalformedURLException , IOException { 74 super(uri, null, root, null); 75 76 this.request = new HttpRequest(req, this); 77 this.request.setCharacterEncoding(defaultFormEncoding); 78 this.request.setContainerEncoding(containerEncoding); 79 this.response = new HttpResponse(res); 80 this.webcontext = context; 81 82 setView(extractView(this.request)); 83 setAction(extractAction(this.request)); 84 85 this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT, this.request); 86 this.objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, this.response); 87 this.objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, this.webcontext); 88 89 this.objectModel.put(HTTP_REQUEST_OBJECT, req); 93 this.objectModel.put(HTTP_RESPONSE_OBJECT, res); 94 this.objectModel.put(HTTP_SERVLET_CONTEXT, servletContext); 95 } 96 97 public void redirect(boolean sessionmode, String newURL) throws IOException { 98 doRedirect(sessionmode, newURL, false); 99 } 100 101 public void permanentRedirect(boolean sessionmode, String newURL) throws IOException { 102 doRedirect(sessionmode, newURL, true); 103 } 104 105 public void sendStatus(int sc) { 106 setStatus(sc); 107 this.hasRedirected = true; 108 } 109 110 113 private void doRedirect(boolean sessionmode, String newURL, boolean permanent) 114 throws IOException { 115 this.hasRedirected = true; 116 117 if (sessionmode) { 119 String s = request.getRequestedSessionId(); 120 if (getLogger().isDebugEnabled()) { 121 if (s != null) { 122 getLogger().debug("Redirect: Requested session ID <" + s + "> is " + 123 (request.isRequestedSessionIdValid()? "valid" : "invalid")); 124 } else { 125 getLogger().debug("Redirect: No session found in request"); 126 } 127 } 128 129 Session session = request.getSession(); 131 if (getLogger().isDebugEnabled()) { 132 getLogger().debug ("Redirect: Obtained session <" + session.getId() + ">"); 133 } 134 } 135 136 String redirect = this.response.encodeRedirectURL(newURL); 138 139 if (!newURL.startsWith("/") && newURL.indexOf(':') == -1 && redirect.indexOf(':') != -1) { 141 if (getLogger().isDebugEnabled()) { 142 getLogger().debug("Redirect: WebSphere Bug Detected!"); 143 } 144 String base = NetUtils.getPath(request.getRequestURI()); 145 if (base.startsWith("/")) { 146 base = base.substring(1); 147 } 148 redirect = response.encodeRedirectURL(base + '/' + newURL); 149 } 150 151 if (getLogger().isDebugEnabled()) { 152 getLogger().debug("Sending redirect to '" + redirect + "'"); 153 } 154 155 if (permanent) { 156 this.response.sendPermanentRedirect(redirect); 157 } else { 158 this.response.sendRedirect(redirect); 159 } 160 } 161 162 public boolean hasRedirected() { 163 return this.hasRedirected; 164 } 165 166 169 public void setStatus(int statusCode) { 170 this.response.setStatus(statusCode); 171 } 172 173 176 public void setContentType(String contentType) { 177 this.response.setContentType(contentType); 178 this.contentType = contentType; 179 } 180 181 184 public String getContentType() { 185 return this.contentType; 186 } 187 188 191 public void setContentLength(int length) { 192 this.response.setContentLength(length); 193 } 194 195 203 public boolean isResponseModified(long lastModified) { 204 if (lastModified != 0) { 205 long if_modified_since = this.request.getDateHeader("If-Modified-Since"); 206 this.response.setDateHeader("Last-Modified", lastModified); 207 return (if_modified_since / 1000 < lastModified / 1000); 208 } 209 return true; 210 } 211 212 215 public void setResponseIsNotModified() { 216 this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 217 } 218 219 226 public boolean tryResetResponse() 227 throws IOException { 228 if (!super.tryResetResponse()) { 229 try { 230 if (!this.response.isCommitted()) { 231 this.response.reset(); 232 if (getLogger().isDebugEnabled()) { 233 getLogger().debug("Response successfully reset"); 234 } 235 return true; 236 } 237 } catch (Exception e) { 238 getLogger().warn("Problem resetting response", e); 240 } 241 if (getLogger().isDebugEnabled()) { 242 getLogger().debug("Response wasn't reset"); 243 } 244 return false; 245 } 246 return true; 247 } 248 249 250 257 public OutputStream getOutputStream(final int bufferSize) 258 throws IOException { 259 if (this.outputStream == null) { 260 this.outputStream = this.response.getOutputStream(); 261 } 262 return super.getOutputStream(bufferSize); 263 } 264 265 268 public boolean isExternal() { 269 return true; 270 } 271 } 272 | Popular Tags |