001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.pluto;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.HashMap;
024: import java.util.Iterator;
025:
026: import javax.portlet.PortletMode;
027: import javax.portlet.WindowState;
028:
029: import org.apache.avalon.framework.CascadingRuntimeException;
030: import org.apache.avalon.framework.service.ServiceException;
031: import org.apache.avalon.framework.service.ServiceManager;
032: import org.apache.cocoon.portal.LinkService;
033: import org.apache.cocoon.portal.PortalService;
034: import org.apache.cocoon.portal.coplet.CopletInstanceData;
035: import org.apache.cocoon.portal.event.CopletInstanceEvent;
036: import org.apache.cocoon.portal.event.Event;
037: import org.apache.cocoon.portal.event.ConvertableEvent;
038: import org.apache.cocoon.portal.event.impl.FullScreenCopletEvent;
039: import org.apache.cocoon.portal.layout.impl.CopletLayout;
040: import org.apache.cocoon.portal.pluto.om.PortletEntityImpl;
041: import org.apache.cocoon.portal.pluto.om.PortletWindowImpl;
042: import org.apache.pluto.om.window.PortletWindow;
043: import org.apache.pluto.services.information.PortletURLProvider;
044:
045: /**
046: * Create the URL for a portlet.
047: *
048: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
049: *
050: * @version CVS $Id: PortletURLProviderImpl.java 433543 2006-08-22 06:22:54Z crossley $
051: */
052: public class PortletURLProviderImpl implements PortletURLProvider,
053: CopletInstanceEvent, ConvertableEvent {
054:
055: /** The portlet window (target) */
056: protected final PortletWindow portletWindow;
057:
058: /** The new portlet mode */
059: protected PortletMode mode;
060:
061: /** The new window state */
062: protected WindowState state;
063:
064: /** Is this an action */
065: protected boolean action;
066:
067: /** Secure link? */
068: protected Boolean secure;
069:
070: /** Clear parameters */
071: protected boolean clearParameters;
072:
073: /** Parameters */
074: protected Map parameters;
075:
076: /** The generated url */
077: protected String generatedURL;
078: private final LinkService linkService;
079: private static final String DEFAULT_PORTLET_URL_REQUEST_PARAM = "url";
080:
081: /**
082: * Constructor
083: */
084: public PortletURLProviderImpl(PortletWindow portletWindow,
085: ServiceManager manager) {
086: this .portletWindow = portletWindow;
087: PortalService service = null;
088: try {
089: service = (PortalService) manager
090: .lookup(PortalService.ROLE);
091: this .linkService = service.getComponentManager()
092: .getLinkService();
093: } catch (ServiceException se) {
094: throw new CascadingRuntimeException(
095: "Unable to lookup portal service.", se);
096: } finally {
097: manager.release(service);
098: }
099: }
100:
101: /**
102: * Constructor for factory
103: * @param service
104: * @param eventData
105: */
106: PortletURLProviderImpl(PortalService service, String eventData) {
107: this .linkService = service.getComponentManager()
108: .getLinkService();
109: PortletURLConverter urlConverter = new PortletURLConverter(
110: eventData);
111: String copletId = urlConverter.getPortletId();
112: CopletInstanceData cid = service.getComponentManager()
113: .getProfileManager().getCopletInstanceData(copletId);
114: this .portletWindow = (PortletWindow) cid
115: .getTemporaryAttribute("window");
116: this .mode = urlConverter.getMode();
117: this .state = urlConverter.getState();
118: this .action = urlConverter.isAction();
119: this .parameters = urlConverter.getParameters();
120: this .clearParameters = false;
121: this .secure = null;
122: }
123:
124: /**
125: * Copy constructor
126: */
127: private PortletURLProviderImpl(PortletURLProviderImpl original) {
128: this .linkService = original.linkService;
129: this .portletWindow = original.portletWindow;
130: this .mode = original.mode;
131: this .state = original.state;
132: this .action = original.action;
133: this .secure = original.secure;
134: this .clearParameters = original.clearParameters;
135: this .generatedURL = original.generatedURL;
136: if (original.parameters != null) {
137: this .parameters = new HashMap(original.parameters.size());
138: this .parameters.putAll(original.parameters);
139: }
140: }
141:
142: /**
143: * Return the window
144: */
145: public PortletWindow getPortletWindow() {
146: return this .portletWindow;
147: }
148:
149: /**
150: * @see org.apache.pluto.services.information.PortletURLProvider#setPortletMode(javax.portlet.PortletMode)
151: */
152: public void setPortletMode(PortletMode mode) {
153: this .mode = mode;
154: }
155:
156: /**
157: * Return the portlet mode
158: */
159: public PortletMode getPortletMode() {
160: return this .mode;
161: }
162:
163: /**
164: * @see org.apache.pluto.services.information.PortletURLProvider#setWindowState(javax.portlet.WindowState)
165: */
166: public void setWindowState(WindowState state) {
167: this .state = state;
168: }
169:
170: /**
171: * Return the portlet mode
172: */
173: public WindowState getWindowState() {
174: return this .state;
175: }
176:
177: /**
178: * @see org.apache.pluto.services.information.PortletURLProvider#setAction()
179: */
180: public void setAction() {
181: this .action = true;
182: }
183:
184: /**
185: * Is this an action?
186: */
187: public boolean isAction() {
188: return this .action;
189: }
190:
191: /**
192: * @see org.apache.pluto.services.information.PortletURLProvider#setSecure()
193: */
194: public void setSecure() {
195: this .secure = Boolean.TRUE;
196: }
197:
198: /**
199: * @see org.apache.pluto.services.information.PortletURLProvider#clearParameters()
200: */
201: public void clearParameters() {
202: this .clearParameters = true;
203: }
204:
205: /**
206: * @see org.apache.pluto.services.information.PortletURLProvider#setParameters(java.util.Map)
207: */
208: public void setParameters(Map parameters) {
209: this .parameters = parameters;
210: }
211:
212: /**
213: * Return the parameters
214: */
215: public Map getParameters() {
216: if (this .parameters == null) {
217: return Collections.EMPTY_MAP;
218: }
219: return this .parameters;
220: }
221:
222: /**
223: * @see java.lang.Object#toString()
224: */
225: public String toString() {
226: return new PortletURLProviderImpl(this ).getURL();
227: }
228:
229: /**
230: * @see java.lang.Object#toString()
231: */
232: private String getURL() {
233: if (this .generatedURL == null) {
234: final PortletWindowImpl impl = (PortletWindowImpl) this .portletWindow;
235: final CopletLayout cl = impl.getLayout();
236: Event sizingEvent = null;
237: if (cl != null) {
238: final CopletInstanceData cid = cl
239: .getCopletInstanceData();
240: String oldStateString = (String) cid
241: .getTemporaryAttribute("window-state");
242: WindowState oldState = null;
243: if (oldStateString != null) {
244: oldState = new WindowState(oldStateString);
245: } else {
246: oldState = WindowState.NORMAL;
247: }
248: if (this .state != null && !this .state.equals(oldState)) {
249: if (oldState.equals(WindowState.MAXIMIZED)) {
250: sizingEvent = new FullScreenCopletEvent(cid,
251: null);
252: } else {
253: if (this .state.equals(WindowState.MAXIMIZED)) {
254: sizingEvent = new FullScreenCopletEvent(
255: cid, cl);
256: }
257: }
258: }
259: }
260:
261: List l = new ArrayList();
262: if (sizingEvent != null) {
263: l.add(sizingEvent);
264: }
265: l.add(this );
266: if (secure == null) {
267: this .generatedURL = this .linkService.getLinkURI(l);
268: } else {
269: this .generatedURL = this .linkService.getLinkURI(l,
270: secure);
271: }
272: }
273: return linkService.encodeURL(this .generatedURL);
274: }
275:
276: /**
277: * @see org.apache.cocoon.portal.event.ActionEvent#getTarget()
278: */
279: public Object getTarget() {
280: return ((PortletEntityImpl) this .portletWindow
281: .getPortletEntity()).getCopletInstanceData();
282: }
283:
284: /**
285: * Return the URL as a String
286: *
287: * @return The URL as a String
288: */
289: public String asString() {
290:
291: final PortletWindowImpl impl = (PortletWindowImpl) this .portletWindow;
292: final CopletLayout cl = impl.getLayout();
293: if (cl == null) {
294: return "";
295: }
296: final CopletInstanceData cid = cl.getCopletInstanceData();
297: PortletURLConverter urlConverter = new PortletURLConverter(cid);
298:
299: if (this .mode != null) {
300: urlConverter.setMode(this .mode);
301: }
302:
303: if (this .state != null) {
304: urlConverter.setState(this .state);
305: }
306:
307: if (this .action) {
308: urlConverter.setAction();
309: }
310:
311: if (this .parameters != null) {
312: Iterator entries = this .parameters.entrySet().iterator();
313: while (entries.hasNext()) {
314: Map.Entry entry = (Map.Entry) entries.next();
315: String name = (String) entry.getKey();
316: Object value = entry.getValue();
317: String[] values = value instanceof String ? new String[] { (String) value }
318: : (String[]) value;
319: urlConverter.setParam(name, values);
320: }
321: }
322:
323: return urlConverter.toString();
324: }
325:
326: /**
327: * The request parameter to be used for this event (if events are not hidden)
328: *
329: * @return The request parameter name for this event.
330: */
331: public String getRequestParameterName() {
332: return DEFAULT_PORTLET_URL_REQUEST_PARAM;
333: }
334:
335: }
|