1 16 package org.apache.cocoon.environment.portlet; 17 18 import org.apache.cocoon.environment.AbstractEnvironment; 19 import org.apache.cocoon.environment.Context; 20 import org.apache.cocoon.environment.ObjectModelHelper; 21 import org.apache.cocoon.environment.PermanentRedirector; 22 import org.apache.cocoon.environment.Redirector; 23 import org.apache.cocoon.environment.Session; 24 25 import javax.portlet.PortletContext; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 29 37 public class PortletEnvironment extends AbstractEnvironment implements Redirector, PermanentRedirector { 38 39 44 public static final String PARAMETER_PATH_INFO = "cocoon-portlet-path"; 45 46 50 public static final String HEADER_PORTLET_MODE = "X-Portlet-Mode"; 51 52 56 public static final String HEADER_WINDOW_STATE = "X-Window-State"; 57 58 61 public static final String HEADER_PORTLET_TITLE = "X-Portlet-Title"; 62 63 66 public static final String SESSION_APPLICATION_SCOPE = "portlet-application-"; 67 68 71 public static final String SESSION_PORTLET_SCOPE = "portlet-portlet-"; 72 73 74 77 private PortletRequest request; 78 79 82 private PortletResponse response; 83 84 87 private Context context; 88 89 90 94 private String contentType; 95 96 99 private boolean hasRedirected; 100 101 104 private int defaultSessionScope; 105 106 110 public PortletEnvironment(String servletPath, 111 String pathInfo, 112 String uri, 113 String root, 114 javax.portlet.ActionRequest request, 115 javax.portlet.ActionResponse response, 116 PortletContext portletContext, 117 Context context, 118 String containerEncoding, 119 String defaultFormEncoding, 120 int defaultSessionScope) 121 throws IOException { 122 super(uri, null, root, null); 123 124 this.request = new ActionRequest(servletPath, pathInfo, request, this); 125 this.request.setCharacterEncoding(defaultFormEncoding); 126 this.request.setContainerEncoding(containerEncoding); 127 this.response = new ActionResponse(response, request.getPreferences(), (ActionRequest) this.request, uri); 128 this.context = context; 129 this.defaultSessionScope = defaultSessionScope; 130 131 setView(extractView(this.request)); 132 setAction(extractAction(this.request)); 133 134 initObjectModel(request, response, portletContext); 135 } 136 137 141 public PortletEnvironment(String servletPath, 142 String pathInfo, 143 String uri, 144 String root, 145 javax.portlet.RenderRequest request, 146 javax.portlet.RenderResponse response, 147 PortletContext portletContext, 148 Context context, 149 String containerEncoding, 150 String defaultFormEncoding, 151 int defaultSessionScope) 152 throws IOException { 153 super(uri, null, root, null); 154 155 this.request = new RenderRequest(servletPath, pathInfo, request, this); 156 this.request.setCharacterEncoding(defaultFormEncoding); 157 this.request.setContainerEncoding(containerEncoding); 158 this.response = new RenderResponse(response, request.getPreferences()); 159 this.context = context; 160 this.defaultSessionScope = defaultSessionScope; 161 162 setView(extractView(this.request)); 163 setAction(extractAction(this.request)); 164 165 initObjectModel(request, response, portletContext); 166 } 167 168 private void initObjectModel(javax.portlet.PortletRequest portletRequest, 169 javax.portlet.PortletResponse portletResponse, 170 PortletContext portletContext) { 171 this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT, this.request); 172 this.objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, this.response); 173 this.objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, this.context); 174 175 PortletObjectModelHelper.setPortletRequest(this.objectModel, portletRequest); 179 PortletObjectModelHelper.setPortletResponse(this.objectModel, portletResponse); 180 PortletObjectModelHelper.setPortletContext(this.objectModel, portletContext); 181 } 182 183 184 public void redirect(boolean sessionmode, String newURL) throws IOException { 185 this.hasRedirected = true; 186 187 if (sessionmode) { 189 if (getLogger().isDebugEnabled()) { 190 String s = request.getRequestedSessionId(); 191 if (s != null) { 192 getLogger().debug("Session ID in request = " + s + 193 (request.isRequestedSessionIdValid() ? " (valid)" : " (invalid)")); 194 } 195 } 196 197 Session session = request.getSession(true); 199 if (getLogger().isDebugEnabled()) { 200 getLogger().debug("Session ID = " + session.getId()); 201 } 202 } 203 204 String redirect = newURL; 206 if (getLogger().isDebugEnabled()) { 207 getLogger().debug("Sending redirect to '" + redirect + "'"); 208 } 209 210 this.response.sendRedirect(redirect); 211 } 212 213 216 public void permanentRedirect(boolean sessionmode, String newURL) throws IOException { 217 redirect(sessionmode, newURL); 218 } 219 220 public boolean hasRedirected() { 221 return this.hasRedirected; 222 } 223 224 227 public void setStatus(int statusCode) { 228 } 229 230 233 public void sendStatus(int sc) { 234 throw new AbstractMethodError ("Not Implemented"); 235 } 236 237 240 public void setContentType(String contentType) { 241 this.response.setContentType(contentType); 242 this.contentType = contentType; 243 } 244 245 248 public String getContentType() { 249 return this.contentType; 250 } 251 252 256 public void setContentLength(int length) { 257 } 258 259 263 public boolean isResponseModified(long lastModified) { 264 return true; 265 } 266 267 271 public void setResponseIsNotModified() { 272 } 273 274 281 public boolean tryResetResponse() throws IOException { 282 if (!super.tryResetResponse()) { 283 try { 284 if (!this.response.isCommitted()) { 285 this.response.reset(); 286 if (getLogger().isDebugEnabled()) { 287 getLogger().debug("Response successfully reset"); 288 } 289 return true; 290 } 291 } catch (Exception e) { 292 getLogger().warn("Problem resetting response", e); 294 } 295 if (getLogger().isDebugEnabled()) { 296 getLogger().debug("Response wasn't reset"); 297 } 298 return false; 299 } 300 return true; 301 } 302 303 310 public OutputStream getOutputStream(final int bufferSize) throws IOException { 311 if (this.outputStream == null) { 312 this.outputStream = this.response.getOutputStream(); 313 } 314 return super.getOutputStream(bufferSize); 315 } 316 317 320 public boolean isExternal() { 321 return true; 322 } 323 324 329 int getDefaultSessionScope() { 330 return this.defaultSessionScope; 331 } 332 } 333 | Popular Tags |