001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.server.impl;
023:
024: import org.jboss.portal.common.invocation.AbstractInvocationContext;
025: import org.jboss.portal.common.invocation.resolver.PrincipalAttributeResolver;
026: import org.jboss.portal.common.invocation.resolver.RequestAttributeResolver;
027: import org.jboss.portal.common.invocation.resolver.SessionAttributeResolver;
028: import org.jboss.portal.common.text.CharBuffer;
029: import org.jboss.portal.common.text.FastURLEncoder;
030: import org.jboss.portal.common.util.ParameterMap;
031: import org.jboss.portal.server.PortalConstants;
032: import org.jboss.portal.server.ServerInvocation;
033: import org.jboss.portal.server.ServerInvocationContext;
034: import org.jboss.portal.server.ServerURL;
035: import org.jboss.portal.server.request.URLContext;
036: import org.jboss.portal.server.request.URLFormat;
037: import org.jboss.portal.web.WebRequest;
038: import org.jboss.portal.web.Body;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.util.Iterator;
043: import java.util.Map;
044:
045: /**
046: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
047: * @version $Revision: 9044 $
048: */
049: public class ServerInvocationContextImpl extends
050: AbstractInvocationContext implements ServerInvocationContext {
051:
052: /** The fast url encoder. */
053: private static final FastURLEncoder urlEncoder = FastURLEncoder
054: .getUTF8Instance();
055:
056: /** The client request. */
057: private final HttpServletRequest req;
058:
059: /** The client request. */
060: private final WebRequest webReq;
061:
062: /** The client response. */
063: private final HttpServletResponse resp;
064:
065: /** The portal context path. */
066: private String portalContextPath;
067:
068: /** The portal request path. */
069: private String portalRequestPath;
070:
071: /** The portal host. */
072: private String portalHost;
073:
074: /** The url context. */
075: private URLContext urlContext;
076:
077: /** . */
078: private Buffer[] buffers;
079:
080: /** . */
081: private final String requestRelativePrefix;
082:
083: /** . */
084: private final String requestPrefix;
085:
086: public ServerInvocationContextImpl(HttpServletRequest req,
087: HttpServletResponse resp, WebRequest webReq,
088: String portalHost, String portalRequestPath,
089: String portalContextPath, URLContext urlContext) {
090: if (req == null) {
091: throw new IllegalArgumentException();
092: }
093: if (resp == null) {
094: throw new IllegalArgumentException();
095: }
096:
097: //
098: this .req = req;
099: this .webReq = webReq;
100: this .resp = resp;
101: this .portalRequestPath = portalRequestPath;
102: this .portalContextPath = portalContextPath;
103: this .portalHost = portalHost;
104: this .urlContext = urlContext;
105: this .buffers = new Buffer[16];
106:
107: //
108: StringBuffer requestRelativePrefix = new StringBuffer();
109: requestRelativePrefix.append(req.getScheme()).append("://")
110: .append(req.getServerName());
111: if (req.isSecure()) {
112: if (req.getServerPort() != 443) {
113: requestRelativePrefix.append(":").append(
114: Integer.toString(req.getServerPort()));
115: }
116: } else if (req.getServerPort() != 80) {
117: requestRelativePrefix.append(":").append(
118: Integer.toString(req.getServerPort()));
119: }
120: requestRelativePrefix.append(req.getContextPath());
121:
122: //
123: this .requestRelativePrefix = requestRelativePrefix.toString();
124: this .requestPrefix = req.getContextPath();
125:
126: //
127: addResolver(ServerInvocation.REQUEST_SCOPE,
128: new RequestAttributeResolver(req));
129: addResolver(ServerInvocation.SESSION_SCOPE,
130: new SessionAttributeResolver(req,
131: PortalConstants.PORTAL_SESSION_MAP_KEY));
132: addResolver(ServerInvocation.PRINCIPAL_SCOPE,
133: new PrincipalAttributeResolver(req));
134: }
135:
136: public WebRequest getWebRequest() {
137: return webReq;
138: }
139:
140: public HttpServletRequest getClientRequest() {
141: return req;
142: }
143:
144: public HttpServletResponse getClientResponse() {
145: return resp;
146: }
147:
148: public String getMediaType() {
149: return webReq.getMediaType();
150: }
151:
152: public URLContext getURLContext() {
153: return urlContext;
154: }
155:
156: public ParameterMap getQueryParameterMap() {
157: return webReq.getQueryParameterMap();
158: }
159:
160: public ParameterMap getBodyParameterMap() {
161: Body body = webReq.getBody();
162:
163: //
164: if (body instanceof Body.Form) {
165: return ((Body.Form) body).getParameters();
166: }
167:
168: //
169: return null;
170: }
171:
172: public String getPortalRequestPath() {
173: return portalRequestPath;
174: }
175:
176: public String getPortalContextPath() {
177: return portalContextPath;
178: }
179:
180: public String getPortalHost() {
181: return portalHost;
182: }
183:
184: public String renderURL(ServerURL url, URLContext context,
185: URLFormat format) {
186: int index = context.getMask() << 2 | format.getMask();
187: if (buffers[index] == null) {
188: buffers[index] = new Buffer(resp, context, format);
189: }
190: Buffer buffer = buffers[index];
191: return buffer.toString(url);
192: }
193:
194: public class Buffer extends CharBuffer {
195:
196: /** . */
197: private final HttpServletResponse resp;
198:
199: /** . */
200: private final URLFormat format;
201:
202: /** . */
203: private final int prefixLength;
204:
205: public Buffer(HttpServletResponse resp, URLContext context,
206: URLFormat format) {
207: this .resp = resp;
208: this .format = format;
209:
210: //
211: if (!format.isRelative()) {
212: append(requestRelativePrefix);
213: } else {
214: append(requestPrefix);
215: }
216:
217: // Append the servlet path
218: switch (context.getMask()) {
219: case URLContext.AUTH_MASK + URLContext.SEC_MASK:
220: append("/authsec");
221: break;
222: case URLContext.AUTH_MASK:
223: append("/auth");
224: break;
225: case URLContext.SEC_MASK:
226: append("/sec");
227: break;
228: }
229:
230: // Save the prefix length
231: this .prefixLength = length;
232: }
233:
234: public String toString(ServerURL url) {
235: // Reset the prefix length
236: this .length = prefixLength;
237:
238: // julien : check UTF-8 is ok and should not be dependant on the response charset
239: append(url.getPortalRequestPath());
240:
241: //
242: boolean first = true;
243: for (Iterator i = url.getParameterMap().entrySet()
244: .iterator(); i.hasNext();) {
245: Map.Entry parameter = (Map.Entry) i.next();
246: String name = (String) parameter.getKey();
247: String[] values = (String[]) parameter.getValue();
248: for (int j = 0; j < values.length; j++) {
249: String value = values[j];
250: append(first ? '?' : '&');
251: append(name, urlEncoder);
252: append('=');
253: append(value, urlEncoder);
254: first = false;
255: }
256: }
257:
258: // Stringify
259: String s = asString();
260:
261: // Let the servlet rewrite the URL if necessary
262: if (format.isServletEncoded()) {
263: s = resp.encodeURL(s);
264: }
265:
266: //
267: return s;
268: }
269: }
270: }
|