KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > wsrp > adapter > WSRPPortletWindowAspect


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.wsrp.adapter;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.servlet.ServletConfig JavaDoc;
22
23 import oasis.names.tc.wsrp.v1.types.LocalizedString;
24
25 import org.apache.avalon.framework.context.Context;
26 import org.apache.avalon.framework.context.ContextException;
27 import org.apache.avalon.framework.context.Contextualizable;
28 import org.apache.avalon.framework.parameters.ParameterException;
29 import org.apache.avalon.framework.parameters.Parameters;
30 import org.apache.cocoon.portal.LinkService;
31 import org.apache.cocoon.portal.PortalService;
32 import org.apache.cocoon.portal.coplet.CopletInstanceData;
33 import org.apache.cocoon.portal.event.Event;
34 import org.apache.cocoon.portal.event.impl.ChangeCopletInstanceAspectDataEvent;
35 import org.apache.cocoon.portal.layout.Layout;
36 import org.apache.cocoon.portal.layout.impl.CopletLayout;
37 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
38 import org.apache.cocoon.portal.layout.renderer.aspect.impl.AbstractAspect;
39 import org.apache.cocoon.portal.wsrp.consumer.ConsumerEnvironmentImpl;
40 import org.apache.cocoon.portal.wsrp.consumer.SimplePortletWindowSession;
41 import org.apache.cocoon.servlet.CocoonServlet;
42 import org.apache.cocoon.xml.XMLUtils;
43 import org.apache.commons.lang.ArrayUtils;
44 import org.apache.wsrp4j.consumer.PortletKey;
45 import org.apache.wsrp4j.consumer.URLGenerator;
46 import org.apache.wsrp4j.consumer.User;
47 import org.apache.wsrp4j.consumer.WSRPPortlet;
48 import org.apache.wsrp4j.exception.WSRPException;
49 import org.apache.wsrp4j.util.Constants;
50 import org.apache.wsrp4j.util.Modes;
51 import org.apache.wsrp4j.util.WindowStates;
52 import org.xml.sax.ContentHandler JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54
55 /**
56  * This aspect draws a portlet window for a wsrp portlet.
57  *
58  * <h2>Example XML:</h2>
59  * <pre>
60  * &lt;window&gt;
61  * &lt;title&gt;title&lt;/title&gt;
62  * &lt;maximize-uri&gt;event&lt;/maximize-uri&gt;
63  * &lt;minimize-uri&gt;event&lt;/minimize-uri&gt;
64  * &lt;fullscreen-uri&gt;event&lt;/fullscreen-uri&gt;
65  * &lt;edit-uri&gt;event&lt;/edit-uri&gt;
66  * &lt;help-uri&gt;event&lt;/help-uri&gt;
67  * &lt;view-uri&gt;event&lt;/view-uri&gt;
68  * &lt;!-- output of following renderers --&gt;
69  * &lt;/window&gt;
70  * </pre>
71  *
72  * <h2>Applicable to:</h2>
73  * <ul>
74  * <li>{@link org.apache.cocoon.portal.layout.impl.CopletLayout}</li>
75  * </ul>
76  *
77  * <h2>Parameters</h2>
78  * <table><tbody>
79  * <tr><th>root-tag</th><td>Should a root tag surrounding the following output
80  * be generated?</td><td></td><td>boolean</td><td><code>true</code></td></tr>
81  * <tr><th>tag-name</th><td>Name of the root tag if requested.
82  * </td><td></td><td>String</td><td><code>"window"</code></td></tr>
83  * </tbody></table>
84  *
85  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
86  * @author <a HREF="mailto:malessandrini@s-und-n.de">Michel Alessandrini</a>
87  *
88  * @version $Id: WSRPPortletWindowAspect.java 264755 2005-08-30 10:29:21Z cziegeler $
89  */

90 public final class WSRPPortletWindowAspect
91 extends AbstractAspect
92     implements Contextualizable {
93
94     /** The environment implementation*/
95     protected ConsumerEnvironmentImpl environment;
96
97     /** The wsrp adapter. */
98     protected WSRPAdapter adapter;
99
100     /**
101      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
102      */

103     public void contextualize(Context context) throws ContextException {
104         try {
105             // now get the wsrp adapter
106
ServletConfig JavaDoc servletConfig = (ServletConfig JavaDoc) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
107             this.adapter = (WSRPAdapter)servletConfig.getServletContext().getAttribute(WSRPAdapter.class.getName());
108             if ( this.adapter != null ) {
109                 this.environment = this.adapter.getConsumerEnvironment();
110             }
111         } catch (ContextException ignore) {
112             // we ignore the context exception
113
// this avoids startup errors if the portal is configured for the CLI
114
// environment
115
this.getLogger().warn("The wsrp support is disabled as the servlet context is not available.", ignore);
116         }
117     }
118
119     /**
120      * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#toSAX(org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
121      */

122     public void toSAX(RendererAspectContext context,
123                       Layout layout,
124                       PortalService service,
125                       ContentHandler JavaDoc contenthandler)
126     throws SAXException JavaDoc {
127         final PreparedConfiguration config = (PreparedConfiguration)context.getAspectConfiguration();
128         final CopletInstanceData copletInstanceData = ((CopletLayout)layout).getCopletInstanceData();
129
130         if ( config.rootTag ) {
131             XMLUtils.startElement(contenthandler, config.tagName);
132         }
133         final PortletKey portletKey = (PortletKey)copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_KEY);
134
135         if ( portletKey == null ) {
136             // no portlet window, so use a default behaviour
137
XMLUtils.createElement(contenthandler, "title", copletInstanceData.getTitle());
138         } else {
139             copletInstanceData.setTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_LAYOUT, layout);
140             LocalizedString localizedTitle = (LocalizedString)copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_TITLE);
141             String JavaDoc title;
142             if ( localizedTitle == null ) {
143                 title = copletInstanceData.getTitle();
144             } else {
145                 title = localizedTitle.getValue();
146             }
147             XMLUtils.createElement(contenthandler, "title", title);
148
149             final String JavaDoc portletInstanceKey = (String JavaDoc)copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_INSTANCE_KEY);
150             final User user = (User)copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_USER);
151
152             final WSRPPortlet portlet = this.environment.getPortletRegistry().getPortlet(portletKey);
153             try {
154                 SimplePortletWindowSession windowSession = this.adapter.getSimplePortletWindowSession(portlet, portletInstanceKey, user);
155                 if ( this.environment != null && windowSession != null ) {
156                     this.adapter.setCurrentCopletInstanceData(copletInstanceData);
157                     URLGenerator urlGenerator = this.environment.getURLGenerator();
158
159                     String JavaDoc[] supportedWindowStates = (String JavaDoc[])copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_WINDOWSTATES);
160                     String JavaDoc ws = windowSession.getWindowState();
161                     if ( ws == null ) {
162                         ws = WindowStates._normal;
163                     }
164
165                     if ( !ws.equals(WindowStates._minimized)
166                          && ArrayUtils.contains(supportedWindowStates, WindowStates._minimized)) {
167                         final Map JavaDoc p = new HashMap JavaDoc();
168                         p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER);
169                         p.put(Constants.WINDOW_STATE, WindowStates._minimized);
170                         
171                         final String JavaDoc link = urlGenerator.getRenderURL(p);
172                         XMLUtils.createElement(contenthandler, "minimize-uri", link);
173                     }
174                     if ( !ws.equals(WindowStates._normal)
175                           && ArrayUtils.contains(supportedWindowStates, WindowStates._normal)) {
176                         final Map JavaDoc p = new HashMap JavaDoc();
177                         p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER);
178                         p.put(Constants.WINDOW_STATE, WindowStates._normal);
179
180                         final String JavaDoc link = urlGenerator.getRenderURL(p);
181                         XMLUtils.createElement(contenthandler, "maximize-uri", link);
182                     }
183                     if ( !ws.equals(WindowStates._maximized)
184                           && ArrayUtils.contains(supportedWindowStates, WindowStates._maximized)) {
185                         final Map JavaDoc p = new HashMap JavaDoc();
186                         p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER);
187                         p.put(Constants.WINDOW_STATE, WindowStates._maximized);
188
189                         final String JavaDoc link = urlGenerator.getRenderURL(p);
190                         XMLUtils.createElement(contenthandler, "fullscreen-uri", link);
191                     }
192
193                     String JavaDoc[] supportedModes = (String JavaDoc[])copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_MODES);
194                     String JavaDoc pm = windowSession.getMode();
195                     if ( pm == null ) {
196                         pm = Modes._view;
197                     }
198                     if ( !pm.equals(Modes._edit)
199                          && ArrayUtils.contains(supportedModes, Modes._edit) ) {
200                         final Map JavaDoc p = new HashMap JavaDoc();
201                         p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER);
202                         p.put(Constants.PORTLET_MODE, Modes._edit);
203
204                         final String JavaDoc link = urlGenerator.getRenderURL(p);
205                         XMLUtils.createElement(contenthandler, "edit-uri", link);
206                     }
207                     if ( !pm.equals(Modes._help)
208                         && ArrayUtils.contains(supportedModes, Modes._help) ) {
209                         final Map JavaDoc p = new HashMap JavaDoc();
210                         p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER);
211                         p.put(Constants.PORTLET_MODE, Modes._help);
212
213                         final String JavaDoc link = urlGenerator.getRenderURL(p);
214                         XMLUtils.createElement(contenthandler, "help-uri", link);
215                     }
216                     if ( !pm.equals(Modes._view)
217                         && ArrayUtils.contains(supportedModes, Modes._view) ) {
218                         final Map JavaDoc p = new HashMap JavaDoc();
219                         p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER);
220                         p.put(Constants.PORTLET_MODE, Modes._view);
221
222                         final String JavaDoc link = urlGenerator.getRenderURL(p);
223                         XMLUtils.createElement(contenthandler, "view-uri", link);
224                     }
225                 }
226             } catch (WSRPException ignore) {
227                 // we ignore this
228
} finally {
229                 this.adapter.setCurrentCopletInstanceData(null);
230             }
231
232         }
233
234         context.invokeNext( layout, service, contenthandler );
235         
236         if ( config.rootTag ) {
237             XMLUtils.endElement(contenthandler, config.tagName);
238         }
239     }
240
241     /**
242      * utility-class to get the tags out of the configuration<br/>
243      *
244      * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
245      * @author <a HREF="mailto:malessandrini@s-und-n.de">Michel Alessandrini</a>
246      *
247      * @version $Id: WSRPPortletWindowAspect.java 264755 2005-08-30 10:29:21Z cziegeler $
248      */

249     protected static class PreparedConfiguration {
250         
251         /** name of the element */
252         public String JavaDoc tagName;
253         
254         /** shows if the element is on the highest level */
255         public boolean rootTag;
256         
257         /**
258          * set the configuration to the local attributes<br/>
259          *
260          * @param from the object with the configuration-values
261          */

262         public void takeValues(PreparedConfiguration from) {
263             this.tagName = from.tagName;
264             this.rootTag = from.rootTag;
265         }
266     }
267     
268     /**
269      * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
270      */

271     public Object JavaDoc prepareConfiguration(Parameters configuration)
272     throws ParameterException {
273         PreparedConfiguration pc = new PreparedConfiguration();
274         pc.tagName = configuration.getParameter("tag-name", "window");
275         pc.rootTag = configuration.getParameterAsBoolean("root-tag", true);
276         return pc;
277     }
278
279     /**
280      * provides an link including the wsrp-link and the current event-state<br/>
281      *
282      * @param linkService
283      * @param urlGenerator
284      * @param cid
285      * @param windowStates
286      * @param size
287      * @return String the link for the portal
288      */

289     protected String JavaDoc createLink(LinkService linkService,
290                                 URLGenerator urlGenerator,
291                                 CopletInstanceData cid,
292                                 WindowStates windowStates,
293                                 Integer JavaDoc size) {
294         ChangeCopletInstanceAspectDataEvent event;
295         event = new ChangeCopletInstanceAspectDataEvent(cid, "size", size);
296         
297         return this.createLink(linkService, urlGenerator, cid, windowStates, event);
298     }
299
300     /**
301      * provides an link including the wsrp-link and the current event-state<br/>
302      *
303      * @param linkService
304      * @param urlGenerator
305      * @param cid
306      * @param windowStates
307      * @param event
308      * @return String the link for the portal
309      */

310     protected String JavaDoc createLink(LinkService linkService,
311                                 URLGenerator urlGenerator,
312                                 CopletInstanceData cid,
313                                 WindowStates windowStates,
314                                 Event event) {
315         // create the link for the wsrp window state first
316
final Map JavaDoc p = new HashMap JavaDoc();
317         p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER);
318         p.put(Constants.WINDOW_STATE, windowStates.getValue());
319         final String JavaDoc wsrpLink = urlGenerator.getRenderURL(p);
320
321         String JavaDoc link_event = linkService.getLinkURI(event);
322         int pos = link_event.indexOf('?');
323
324         return wsrpLink + link_event.substring(pos+1);
325     }
326 }
327
Popular Tags