KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > impl > invocation > PortalResponseImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.impl.invocation;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
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 /**
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.10 $
29  */

30 public class PortalResponseImpl extends HttpServletResponseWrapper JavaDoc implements PortalResponse
31 {
32
33    /** The invocation. */
34    protected InvocationImpl invocation;
35
36    /** The content type. */
37    protected String JavaDoc contentType;
38
39    /** The result of the target. */
40    protected Result targetResult;
41
42    /** The various render results. */
43    protected List JavaDoc renderResults;
44
45    public PortalResponseImpl(HttpServletResponse JavaDoc resp, InvocationImpl invocation)
46    {
47       super(resp);
48       this.renderResults = new ArrayList JavaDoc();
49       this.invocation = invocation;
50    }
51
52    public String JavaDoc createURL(ServerURL url, boolean relative)
53    {
54       return createURL(url, null, null, relative);
55    }
56
57    public String JavaDoc createURL(ServerURL url, Boolean JavaDoc wantAuthenticated, Boolean JavaDoc wantSecure, boolean relative) throws IllegalArgumentException JavaDoc
58    {
59       // Arg check
60
if (url == null)
61       {
62          throw new IllegalArgumentException JavaDoc("No null server URL accepted");
63       }
64
65       // Are we authenticated or not
66
boolean authenticated = wantAuthenticated == null ? invocation.req.getRemoteUser() != null : wantAuthenticated.booleanValue();
67
68       // Are we secure or not ?
69
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       // The URL
88
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
89
90       //
91
if (!relative)
92       {
93          buffer.append(invocation.req.getScheme()).append("://").append(invocation.req.getServerName());
94
95          //
96
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       // Append the context path
111
buffer.append(invocation.req.getContextPath());
112
113       // Append the servlet path
114
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       // Append parameters
138
boolean first = appendParameter(true, buffer, "ctrl:", "id", new String JavaDoc[]{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 JavaDoc buffer, String JavaDoc prefix, Parameters parameters)
145    {
146       for (Iterator JavaDoc i = parameters.getParameterMap().entrySet().iterator();i.hasNext();)
147       {
148          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
149          String JavaDoc name = (String JavaDoc)entry.getKey();
150          String JavaDoc[] values = (String JavaDoc[])entry.getValue();
151          first = appendParameter(first, buffer, prefix, name, values);
152       }
153       return first;
154    }
155
156    private boolean appendParameter(boolean first, StringBuffer JavaDoc buffer, String JavaDoc prefix, String JavaDoc name, String JavaDoc[] values)
157    {
158       for (int j = 0;j < values.length;j++)
159       {
160          // julien : check UTF-8 is ok and should not be dependant on the response charset
161
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 JavaDoc contentType)
171    {
172       super.setContentType(contentType);
173       this.contentType = contentType;
174    }
175
176    public String JavaDoc 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 JavaDoc getRenderResults()
197    {
198       return renderResults.iterator();
199    }
200 }
201
Popular Tags