1 9 package org.jboss.portal.server.impl.invocation; 10 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.List ; 14 import java.util.ArrayList ; 15 16 import javax.servlet.http.HttpServletResponse ; 17 import javax.servlet.http.HttpServletResponseWrapper ; 18 19 import org.jboss.portal.common.util.Tools; 20 import org.jboss.portal.server.PortalResponse; 21 import org.jboss.portal.server.ServerObjectID; 22 import org.jboss.portal.server.ServerURL; 23 import org.jboss.portal.server.output.Result; 24 import org.jboss.portal.server.util.Parameters; 25 26 30 public class PortalResponseImpl extends HttpServletResponseWrapper implements PortalResponse 31 { 32 33 34 protected InvocationImpl invocation; 35 36 37 protected String contentType; 38 39 40 protected Result targetResult; 41 42 43 protected List renderResults; 44 45 public PortalResponseImpl(HttpServletResponse resp, InvocationImpl invocation) 46 { 47 super(resp); 48 this.renderResults = new ArrayList (); 49 this.invocation = invocation; 50 } 51 52 public String createURL(ServerURL url, boolean relative) 53 { 54 return createURL(url, null, null, relative); 55 } 56 57 public String createURL(ServerURL url, Boolean wantAuthenticated, Boolean wantSecure, boolean relative) throws IllegalArgumentException 58 { 59 if (url == null) 61 { 62 throw new IllegalArgumentException ("No null server URL accepted"); 63 } 64 65 boolean authenticated = wantAuthenticated == null ? invocation.req.getRemoteUser() != null : wantAuthenticated.booleanValue(); 67 68 boolean secure = false; 70 if (wantSecure != null) 71 { 72 secure = wantSecure.booleanValue(); 73 } 74 else 75 { 76 secure = invocation.req.isSecure(); 77 if (url.getSecure() != null) 78 { 79 secure = url.getSecure().booleanValue(); 80 } 81 } 82 83 ServerObjectID target = url.getTarget(); 84 Parameters targetParameters = url.getTargetParameters(); 85 Parameters controlParameters = url.getControlParameters(); 86 87 StringBuffer buffer = new StringBuffer (); 89 90 if (!relative) 92 { 93 buffer.append(invocation.req.getScheme()).append("://").append(invocation.req.getServerName()); 94 95 int port = invocation.req.getServerPort(); 97 if (invocation.req.isSecure()) 98 { 99 if (port != 443) 100 { 101 buffer.append(":").append(port); 102 } 103 } 104 else if (port != 80) 105 { 106 buffer.append(":").append(port); 107 } 108 } 109 110 buffer.append(invocation.req.getContextPath()); 112 113 if (authenticated) 115 { 116 if (secure) 117 { 118 buffer.append("/authsec/index.html"); 119 } 120 else 121 { 122 buffer.append("/auth/index.html"); 123 } 124 } 125 else 126 { 127 if (secure) 128 { 129 buffer.append("/sec/index.html"); 130 } 131 else 132 { 133 buffer.append("/index.html"); 134 } 135 } 136 137 boolean first = appendParameter(true, buffer, "ctrl:", "id", new String []{target.toString()}); 139 first = appendParameters(first, buffer, "ctrl:", controlParameters); 140 first = appendParameters(first, buffer, "", targetParameters); 141 return buffer.toString(); 142 } 143 144 private boolean appendParameters(boolean first, StringBuffer buffer, String prefix, Parameters parameters) 145 { 146 for (Iterator i = parameters.getParameterMap().entrySet().iterator();i.hasNext();) 147 { 148 Map.Entry entry = (Map.Entry )i.next(); 149 String name = (String )entry.getKey(); 150 String [] values = (String [])entry.getValue(); 151 first = appendParameter(first, buffer, prefix, name, values); 152 } 153 return first; 154 } 155 156 private boolean appendParameter(boolean first, StringBuffer buffer, String prefix, String name, String [] values) 157 { 158 for (int j = 0;j < values.length;j++) 159 { 160 buffer.append(first ? "?" : "&").append(prefix). 162 append(Tools.createXWWWFormURLEncoded(name)). 163 append("="). 164 append(Tools.createXWWWFormURLEncoded(values[j])); 165 first = false; 166 } 167 return first; 168 } 169 170 public void setContentType(String contentType) 171 { 172 super.setContentType(contentType); 173 this.contentType = contentType; 174 } 175 176 public String getContentType() 177 { 178 return contentType; 179 } 180 181 public Result getTargetResult() 182 { 183 return targetResult; 184 } 185 186 public void setTargetResult(Result targetResult) 187 { 188 this.targetResult = targetResult; 189 } 190 191 public void addRenderResult(Result result) 192 { 193 renderResults.add(result); 194 } 195 196 public Iterator getRenderResults() 197 { 198 return renderResults.iterator(); 199 } 200 } 201 | Popular Tags |