1 16 package org.apache.cocoon.environment.portlet; 17 18 import org.apache.cocoon.util.NetUtils; 19 20 import org.apache.avalon.framework.CascadingRuntimeException; 21 22 import javax.portlet.PortletMode; 23 import javax.portlet.PortletModeException; 24 import javax.portlet.PortletPreferences; 25 import javax.portlet.WindowState; 26 import javax.portlet.WindowStateException; 27 28 import java.io.IOException ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 40 public final class ActionResponse extends PortletResponse { 41 42 private String uri; 43 private ActionRequest request; 44 45 49 protected ActionResponse(javax.portlet.ActionResponse response, 50 PortletPreferences preferences, 51 ActionRequest request, 52 String uri) { 53 super(response, preferences); 54 this.request = request; 55 this.uri = uri; 56 } 57 58 60 65 public void addHeader(String name, String value) { 66 if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) { 67 try { 68 this.getActionResponse().setPortletMode(new PortletMode(value)); 69 } catch (PortletModeException e) { 70 throw new CascadingRuntimeException("Cant set portlet mode '" + value + "'", e); 71 } 72 } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) { 73 try { 74 this.getActionResponse().setWindowState(new WindowState(value)); 75 } catch (WindowStateException e) { 76 throw new CascadingRuntimeException("Cant set window state '" + value + "'", e); 77 } 78 } else { 79 super.addHeader(name, value); 80 } 81 } 82 83 88 public void setHeader(String name, String value) { 89 if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) { 90 try { 91 this.getActionResponse().setPortletMode(new PortletMode(value)); 92 } catch (PortletModeException e) { 93 throw new CascadingRuntimeException("Cant set portlet mode '" + value + "'", e); 94 } 95 } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) { 96 try { 97 this.getActionResponse().setWindowState(new WindowState(value)); 98 } catch (WindowStateException e) { 99 throw new CascadingRuntimeException("Cant set window state '" + value + "'", e); 100 } 101 } else { 102 super.setHeader(name, value); 103 } 104 } 105 106 117 public void sendRedirect(String location) throws IOException { 118 String servletPath = this.request.getServletPath(); 119 120 Map parameters = new HashMap (7); 122 String absLoc = NetUtils.deparameterize(location, parameters); 123 124 if (absLoc.length() > 0) { 126 String base = NetUtils.getPath(uri); 127 absLoc = NetUtils.absolutize(base, absLoc); 128 absLoc = NetUtils.normalize(absLoc); 129 } else { 130 absLoc = uri; 131 } 132 133 if (absLoc.startsWith(servletPath)) { 135 String pathInfo = absLoc.substring(servletPath.length()); 136 137 for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) { 138 Map.Entry me = (Map.Entry )i.next(); 139 String name = (String )me.getKey(); 140 String value = (String )me.getValue(); 141 getActionResponse().setRenderParameter(name, value); 142 } 143 getActionResponse().setRenderParameter(PortletEnvironment.PARAMETER_PATH_INFO, pathInfo); 144 } else { 145 getActionResponse().sendRedirect(location); 146 } 147 148 161 } 162 163 165 170 public javax.portlet.ActionResponse getActionResponse() { 171 return (javax.portlet.ActionResponse) getPortletResponse(); 172 } 173 174 public void setPortletMode(PortletMode mode) throws PortletModeException { 175 getActionResponse().setPortletMode(mode); 176 } 177 178 public void setRenderParameter(String key, String value) { 179 getActionResponse().setRenderParameter(key, value); 180 } 181 182 public void setRenderParameter(String key, String [] values) { 183 getActionResponse().setRenderParameter(key, values); 184 } 185 186 public void setRenderParameters(Map parameters) { 187 getActionResponse().setRenderParameters(parameters); 188 } 189 190 public void setWindowState(WindowState state) throws WindowStateException { 191 getActionResponse().setWindowState(state); 192 } 193 194 195 197 200 boolean isCommitted() { 201 return true; 202 } 203 } 204 | Popular Tags |