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.faces.component.portlet;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.WindowState;
026: import org.jboss.portal.common.text.FastURLEncoder;
027: import org.jboss.portal.common.util.MarkupInfo;
028: import org.jboss.portal.common.util.MediaType;
029: import org.jboss.portal.portlet.Portlet;
030: import org.jboss.portal.portlet.PortletInvoker;
031: import org.jboss.portal.portlet.PortletInvokerException;
032: import org.jboss.portal.portlet.PortletParameters;
033: import org.jboss.portal.portlet.PortletURL;
034: import org.jboss.portal.portlet.StateString;
035: import org.jboss.portal.portlet.bridge.JBossServletContextProvider;
036: import org.jboss.portal.portlet.impl.PortletRequestEncoder;
037: import org.jboss.portal.portlet.impl.spi.AbstractRequestContext;
038: import org.jboss.portal.portlet.invocation.ActionInvocation;
039: import org.jboss.portal.portlet.invocation.PortletInvocation;
040: import org.jboss.portal.portlet.invocation.RenderInvocation;
041: import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
042: import org.jboss.portal.portlet.spi.ActionContext;
043: import org.jboss.portal.portlet.spi.PortalContext;
044: import org.jboss.portal.portlet.spi.RenderContext;
045: import org.jboss.portal.portlet.spi.RequestContext;
046: import org.jboss.portal.portlet.spi.SecurityContext;
047: import org.jboss.portal.portlet.spi.UserContext;
048: import org.jboss.portal.portlet.spi.WindowContext;
049:
050: import javax.faces.application.ViewHandler;
051: import javax.faces.context.FacesContext;
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054: import java.util.Iterator;
055: import java.util.Map;
056:
057: /**
058: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
059: * @version $Revision: 8811 $
060: */
061: public class JSFInvocation {
062:
063: /** . */
064: final HttpServletRequest clientRequest;
065:
066: /** . */
067: final HttpServletResponse clientResponse;
068:
069: /** . */
070: final FacesContext faces;
071:
072: /** . */
073: final UIPortlet uiportlet;
074:
075: /** . */
076: final PortalContext portalContext;
077:
078: /** . */
079: final RequestContext requestContext;
080:
081: /** . */
082: final SecurityContext securityContext;
083:
084: /** . */
085: final UserContext userContext;
086:
087: /** . */
088: final WindowContext windowContext;
089:
090: public JSFInvocation(FacesContext faces, UIPortlet uiportlet) {
091: // Get request and response
092: JBossServletContextProvider.BridgeInfo info = JBossServletContextProvider
093: .get();
094: PortletInvocation portletInvocation = info.getInvocation();
095: this .clientRequest = portletInvocation.getDispatchedRequest();
096: this .clientResponse = portletInvocation.getDispatchedResponse();
097: this .faces = faces;
098: this .uiportlet = uiportlet;
099:
100: //
101: this .portalContext = new JSFPortalContext(faces, uiportlet);
102: this .requestContext = new AbstractRequestContext(clientRequest,
103: clientResponse);
104: this .securityContext = new JSFSecurityContext(faces);
105: this .userContext = new JSFUserContext(faces);
106: this .windowContext = new JSFWindowContext(faces);
107: }
108:
109: String renderURL(PortletURL portletURL, Boolean wantSecure,
110: Boolean wantAuthenticated, boolean relative) {
111: ViewHandler vh = faces.getApplication().getViewHandler();
112: String viewId = faces.getViewRoot().getViewId();
113: String clientId = uiportlet.getClientId(faces);
114: StringBuffer url = new StringBuffer(vh.getActionURL(faces,
115: viewId));
116:
117: //
118: url.append("&").append(clientId).append("=jbp");
119:
120: //
121: PortletRequestEncoder encoder = new PortletRequestEncoder();
122: encoder.encode(portletURL);
123: for (Iterator i = encoder.getQueryParameters().entrySet()
124: .iterator(); i.hasNext();) {
125: Map.Entry entry = (Map.Entry) i.next();
126: String name = FastURLEncoder.getUTF8Instance().encode(
127: (String) entry.getKey());
128: String[] values = (String[]) entry.getValue();
129: for (int j = 0; j < values.length; j++) {
130: String value = values[j];
131: url.append("&").append(name).append('=');
132: FastURLEncoder.getUTF8Instance().encode(value, url);
133: }
134: }
135:
136: //
137: return url.toString();
138: }
139:
140: public PortletInvocationResponse action(Portlet portlet, Mode mode,
141: WindowState windowState, StateString interactionState)
142: throws PortletInvokerException {
143: //
144: ActionContext actionContext = new JSFActionContext(mode,
145: windowState, interactionState, new MarkupInfo(
146: MediaType.HTML, "UTF-8"), this );
147:
148: //
149: PortletInvocation action = new ActionInvocation(actionContext);
150:
151: //
152: return invoke(portlet, action);
153: }
154:
155: public PortletInvocationResponse render(Portlet portlet, Mode mode,
156: WindowState windowState, PortletParameters navState)
157: throws PortletInvokerException {
158: //
159: RenderContext renderContext = new JSFRenderContext(mode,
160: windowState, navState, new MarkupInfo(MediaType.HTML,
161: "UTF-8"), this );
162:
163: //
164: PortletInvocation render = new RenderInvocation(renderContext);
165:
166: //
167: return invoke(portlet, render);
168: }
169:
170: private PortletInvocationResponse invoke(Portlet portlet,
171: PortletInvocation invocation)
172: throws PortletInvokerException {
173: //
174: invocation.setPortalContext(portalContext);
175: invocation.setSecurityContext(securityContext);
176: invocation.setUserContext(userContext);
177: invocation.setRequestContext(requestContext);
178: invocation.setWindowContext(windowContext);
179:
180: //
181: invocation.setTarget(portlet.getContext());
182: PortletInvoker invoker = uiportlet.getPortletInvoker();
183: return invoker.invoke(invocation);
184: }
185: }
|