KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplet > adapter > impl > ApplicationCopletAdapter


1 /*
2  * Copyright 1999-2002,2004 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.coplet.adapter.impl;
17
18 import java.io.UnsupportedEncodingException JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.service.ServiceException;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.components.ContextHelper;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.portal.coplet.CopletData;
28 import org.apache.cocoon.portal.coplet.CopletFactory;
29 import org.apache.cocoon.portal.coplet.CopletInstanceData;
30 import org.apache.cocoon.portal.event.CopletInstanceEvent;
31 import org.apache.cocoon.portal.event.impl.CopletLinkEvent;
32 import org.apache.cocoon.portal.layout.CompositeLayout;
33 import org.apache.cocoon.portal.layout.Item;
34 import org.apache.cocoon.portal.layout.LayoutFactory;
35 import org.apache.cocoon.portal.layout.NamedItem;
36 import org.apache.cocoon.portal.layout.impl.CopletLayout;
37 import org.apache.cocoon.portal.profile.ProfileManager;
38 import org.apache.cocoon.portal.transformation.ProxyTransformer;
39 import org.apache.cocoon.util.NetUtils;
40 import org.apache.cocoon.xml.XMLUtils;
41 import org.xml.sax.ContentHandler JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  * This coplet adapter is used to connect to external applications that are
46  * plugged into the portal.
47  *
48  * @author <a HREF="mailto:gerald.kahrer@rizit.at">Gerald Kahrer</a>
49  *
50  * @version CVS $Id: ApplicationCopletAdapter.java 232769 2005-08-15 07:51:18Z cziegeler $
51  */

52 public class ApplicationCopletAdapter extends CachingURICopletAdapter {
53
54     /**
55      * @see org.apache.cocoon.portal.coplet.adapter.impl.URICopletAdapter#streamContent(org.apache.cocoon.portal.coplet.CopletInstanceData, java.lang.String, org.xml.sax.ContentHandler)
56      */

57     public void streamContent(final CopletInstanceData coplet,
58                               final String JavaDoc uri,
59                               final ContentHandler JavaDoc contentHandler)
60     throws SAXException JavaDoc {
61         try {
62             super.streamContent(coplet, uri, contentHandler);
63         } catch (SAXException JavaDoc se) {
64             this.getLogger().error(
65                 "ApplicationCopletAdapter: Exception while getting coplet resource",
66                 se);
67
68             this.renderErrorContent(coplet, contentHandler);
69         }
70     }
71
72     /**
73      * This adapter listens for CopletLinkEvents. If it catches one the link uri is saved in
74      * the coplet instance data for further handling in the ProxyTransformer.
75      * There is a special CopletLinkEvent with the uri "createNewCopletInstance", which is the
76      * trigger to create a new instance of the one that is the target of the event.
77      */

78     public void handleCopletInstanceEvent(CopletInstanceEvent e) {
79         super.handleCopletInstanceEvent(e);
80         
81         if ( e instanceof CopletLinkEvent ) {
82             CopletLinkEvent event = (CopletLinkEvent) e;
83             CopletInstanceData coplet = (CopletInstanceData) event.getTarget();
84             String JavaDoc link = event.getLink();
85     
86             if ("createNewCopletInstance".equals(link)) {
87                 try {
88                     createNewInstance(coplet);
89                 } catch (ProcessingException ex) {
90                     getLogger().error("Could not create new coplet instance", ex);
91                 }
92             } else {
93                 // this is a normal link event, so save the url in the instance data
94
// for ProxyTransformer
95
String JavaDoc linkValue = event.getLink();
96                 Boolean JavaDoc addParams = (Boolean JavaDoc)this.getConfiguration(coplet, "appendParameters", Boolean.FALSE);
97                 if ( addParams.booleanValue() ) {
98                     final StringBuffer JavaDoc uri = new StringBuffer JavaDoc(event.getLink());
99                     boolean hasParams = (uri.toString().indexOf("?") != -1);
100                     
101                     // append parameters - if any
102
final Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
103                     final Request r = ObjectModelHelper.getRequest(objectModel);
104                     final Enumeration JavaDoc params = r.getParameterNames();
105                     while (params.hasMoreElements()) {
106                         final String JavaDoc name = (String JavaDoc)params.nextElement();
107                         if (!name.startsWith("cocoon-portal-")) {
108                             final String JavaDoc[] values = r.getParameterValues(name);
109                             for(int i = 0; i < values.length; i++) {
110                                 if ( hasParams ) {
111                                     uri.append('&');
112                                 } else {
113                                     uri.append('?');
114                                     hasParams = true;
115                                 }
116                                 uri.append(name);
117                                 uri.append('=');
118                                 try {
119                                     uri.append(NetUtils.decode(values[i], "utf-8"));
120                                 } catch (UnsupportedEncodingException JavaDoc uee) {
121                                     // ignore this
122
}
123                             }
124                         }
125                     }
126                     linkValue = uri.toString();
127                 }
128                 coplet.setTemporaryAttribute(ProxyTransformer.LINK, linkValue);
129             }
130         }
131     }
132
133     /**
134      * Creates a new instance of the given coplet. Also a new named item in the tab layout is
135      * created to show the data of the new coplet instance in the portal.
136      * @param coplet the coplet instance data
137      * @trows ProcessingException if something fails in the creation process
138      */

139     private void createNewInstance(CopletInstanceData coplet)
140     throws ProcessingException {
141         ProfileManager profileManager = null;
142         try {
143             profileManager =
144                 (ProfileManager) this.manager.lookup(ProfileManager.ROLE);
145
146             CopletData copletData = coplet.getCopletData();
147
148             LayoutFactory lfac =
149                 (LayoutFactory) this.manager.lookup(LayoutFactory.ROLE);
150
151             CopletLayout copletLayout =
152                 (CopletLayout) lfac.newInstance("coplet");
153
154             CopletFactory cfac =
155                 (CopletFactory) manager.lookup(CopletFactory.ROLE);
156
157             CopletInstanceData newCoplet = cfac.newInstance(copletData);
158
159             copletLayout.setCopletInstanceData(newCoplet);
160             profileManager.register(copletLayout);
161
162             NamedItem newItem = new NamedItem();
163             newItem.setLayout(copletLayout);
164
165             CompositeLayout tabLayout =
166                 (CompositeLayout) profileManager.getPortalLayout(
167                     "portalApplications", null);
168
169             newItem.setName(getNewInstanceTabName(tabLayout));
170             tabLayout.addItem(newItem);
171         } catch (ServiceException ce) {
172             throw new ProcessingException(
173                 "Unable to lookup profile manager.",
174                 ce);
175         } catch (Exception JavaDoc e) {
176             throw new ProcessingException(e);
177         } finally {
178             this.manager.release(profileManager);
179         }
180     }
181
182     /**
183      * Returns the name of the new named item in the tab layout
184      * @return String the name of the new item
185      */

186     private String JavaDoc getNewInstanceTabName(CompositeLayout layout) {
187         Integer JavaDoc data = (Integer JavaDoc) layout.getAspectData("tab");
188         Item selectedItem = (NamedItem) layout.getItem(data.intValue());
189
190         if (selectedItem instanceof NamedItem) {
191             return ((NamedItem) selectedItem).getName();
192         }
193         return ("New");
194     }
195
196     /**
197      * Render the error content for a coplet
198      * @param coplet
199      * @param handler
200      * @return True if the error content has been rendered, otherwise false
201      * @throws SAXException
202      */

203     protected boolean renderErrorContent(CopletInstanceData coplet,
204                                          ContentHandler JavaDoc handler)
205     throws SAXException JavaDoc {
206         handler.startDocument();
207         XMLUtils.startElement(handler, "p");
208         XMLUtils.data(
209             handler,
210             "ApplicationCopletAdapter: Can't get content for coplet "
211                 + coplet.getId()
212                 + ". Look up the logs.");
213         XMLUtils.endElement(handler, "p");
214         handler.endDocument();
215
216         return true;
217     }
218
219 }
220
Popular Tags