KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > impl > DefaultLinkService


1 /*
2  * Copyright 1999-2002,2004-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.impl;
17
18 import java.io.UnsupportedEncodingException JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.context.Contextualizable;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.parameters.ParameterException;
28 import org.apache.avalon.framework.parameters.Parameterizable;
29 import org.apache.avalon.framework.parameters.Parameters;
30 import org.apache.avalon.framework.service.ServiceException;
31 import org.apache.avalon.framework.service.ServiceManager;
32 import org.apache.avalon.framework.service.Serviceable;
33 import org.apache.avalon.framework.thread.ThreadSafe;
34 import org.apache.cocoon.components.ContextHelper;
35 import org.apache.cocoon.environment.Request;
36 import org.apache.cocoon.portal.LinkService;
37 import org.apache.cocoon.portal.event.ComparableEvent;
38 import org.apache.cocoon.portal.event.ConvertableEvent;
39 import org.apache.cocoon.portal.event.Event;
40 import org.apache.cocoon.portal.event.EventConverter;
41 import org.apache.cocoon.portal.event.RequestEvent;
42 import org.apache.cocoon.util.NetUtils;
43
44 /**
45  *
46  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
47  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
48  *
49  * @version $Id: DefaultLinkService.java 291586 2005-09-26 09:02:39Z cziegeler $
50  */

51 public class DefaultLinkService
52     extends AbstractLogEnabled
53     implements LinkService,
54                ThreadSafe,
55                Serviceable,
56                Disposable,
57                Contextualizable,
58                Parameterizable {
59     
60     /** The converter used to convert an event into a request parameter */
61     protected EventConverter converter;
62     /** The service manager */
63     protected ServiceManager manager;
64     /** The cocoon context */
65     protected Context context;
66
67     protected Boolean JavaDoc eventsMarshalled;
68
69     /** Default port used for http. */
70     protected int defaultPort = 80;
71     /** Default port used for https. */
72     protected int defaultSecurePort = 443;
73
74     /**
75      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
76      */

77     public void service(ServiceManager manager) throws ServiceException {
78         this.manager = manager;
79         this.converter = (EventConverter)this.manager.lookup(EventConverter.ROLE);
80     }
81
82     /**
83      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
84      */

85     public void parameterize(Parameters params) throws ParameterException {
86         this.defaultPort = params.getParameterAsInteger("defaultPort", this.defaultPort);
87         this.defaultSecurePort = params.getParameterAsInteger("defaultSecurePort", this.defaultSecurePort);
88     }
89
90     /**
91      * Return the current info for the request.
92      * @return A LinkInfo object.
93      */

94     protected LinkInfo getInfo() {
95         final Request request = ContextHelper.getRequest( this.context );
96         LinkInfo info = (LinkInfo)request.getAttribute(DefaultLinkService.class.getName());
97         if ( info == null ) {
98             synchronized ( this ) {
99                 info = (LinkInfo)request.getAttribute(DefaultLinkService.class.getName());
100                 if ( info == null ) {
101                     info = new LinkInfo(request, this.defaultPort, this.defaultSecurePort);
102                     request.setAttribute(DefaultLinkService.class.getName(), info);
103                 }
104             }
105         }
106         return info;
107     }
108
109     /**
110      * @see org.apache.cocoon.portal.LinkService#isSecure()
111      */

112     public boolean isSecure() {
113         return ContextHelper.getRequest(this.context).isSecure();
114     }
115
116     /**
117      * @see org.apache.cocoon.portal.LinkService#encodeURL(String url)
118      */

119     public String JavaDoc encodeURL(String JavaDoc url) {
120         return ContextHelper.getResponse(this.context).encodeURL(url);
121     }
122
123     /**
124      * @see org.apache.cocoon.portal.LinkService#getLinkURI(org.apache.cocoon.portal.event.Event)
125      */

126     public String JavaDoc getLinkURI(Event event) {
127         return this.getLinkURI(event, null);
128     }
129
130     /**
131      * @see org.apache.cocoon.portal.LinkService#getLinkURI(org.apache.cocoon.portal.event.Event, Boolean)
132      */

133     public String JavaDoc getLinkURI(Event event, Boolean JavaDoc secure) {
134         if (event == null) {
135             return this.getRefreshLinkURI(secure);
136         }
137         final LinkInfo info = this.getInfo();
138         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(info.getBase(secure));
139         boolean hasParams = info.hasParameters();
140
141         // add comparable events
142
final boolean comparableEvent = event instanceof ComparableEvent;
143         Iterator JavaDoc iter = info.comparableEvents.iterator();
144         while (iter.hasNext()) {
145             Object JavaDoc[] objects = (Object JavaDoc[]) iter.next();
146             ComparableEvent current = (ComparableEvent) objects[0];
147             if (!comparableEvent || !current.equalsEvent((ComparableEvent) event)) {
148                 if (hasParams) {
149                     buffer.append('&');
150                 } else {
151                     buffer.append('?');
152                 }
153                 try {
154                     buffer.append((String JavaDoc) objects[1]).append('=').append(NetUtils.encode((String JavaDoc) objects[2], "utf-8"));
155                 } catch (UnsupportedEncodingException JavaDoc uee) {
156                     // ignore this as utf-8 is always supported
157
}
158                 hasParams = true;
159             }
160         }
161
162         // now add event
163
hasParams = this.addEvent(buffer, event, hasParams);
164
165         return buffer.toString();
166     }
167
168     /**
169      * Add one event to the buffer
170      * @return Returns true, if the link contains a parameter
171      */

172     protected boolean addEvent(StringBuffer JavaDoc buffer, Event event, boolean hasParams) {
173         if ( hasParams ) {
174             buffer.append('&');
175         } else {
176             buffer.append('?');
177         }
178         StringBuffer JavaDoc value = new StringBuffer JavaDoc("");
179         String JavaDoc parameterName = processEvent(event, value);
180         try {
181             buffer.append(parameterName).append('=').append(NetUtils.encode(value.toString(), "utf-8"));
182         } catch (UnsupportedEncodingException JavaDoc uee) {
183             // ignore this as utf-8 is always supported
184
}
185         return true;
186     }
187     
188     /**
189      * @see org.apache.cocoon.portal.LinkService#getLinkURI(java.util.List)
190      */

191     public String JavaDoc getLinkURI(List JavaDoc events) {
192         return this.getLinkURI(events, null);
193     }
194
195     /**
196      * @see org.apache.cocoon.portal.LinkService#getLinkURI(java.util.List)
197      */

198     public String JavaDoc getLinkURI(List JavaDoc events, Boolean JavaDoc secure) {
199         if (events == null || events.size() == 0) {
200             return this.getRefreshLinkURI(secure);
201         }
202         final LinkInfo info = this.getInfo();
203         boolean hasParams = info.hasParameters();
204         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(info.getBase(secure));
205
206         // add comparable events
207
Iterator JavaDoc iter = info.comparableEvents.iterator();
208         while (iter.hasNext()) {
209             Object JavaDoc[] objects = (Object JavaDoc[]) iter.next();
210             ComparableEvent current = (ComparableEvent) objects[0];
211
212             Iterator JavaDoc eventIterator = events.iterator();
213             boolean found = false;
214             while (!found && eventIterator.hasNext()) {
215                 final Object JavaDoc inEvent = eventIterator.next();
216                 if (inEvent instanceof ComparableEvent
217                     && current.equalsEvent((ComparableEvent) inEvent)) {
218                     found = true;
219                 }
220             }
221             if (!found) {
222                 if (hasParams) {
223                     buffer.append('&');
224                 } else {
225                     buffer.append('?');
226                 }
227                 try {
228                     buffer.append((String JavaDoc) objects[1]).append('=').append(NetUtils.encode((String JavaDoc) objects[2], "utf-8"));
229                 } catch (UnsupportedEncodingException JavaDoc uee) {
230                     // ignore this as utf-8 is always supported
231
}
232                 hasParams = true;
233             }
234         }
235
236         // now add events
237
iter = events.iterator();
238         while (iter.hasNext()) {
239             final Object JavaDoc current = iter.next();
240             if (current instanceof Event) {
241                 hasParams = this.addEvent(buffer, (Event) current, hasParams);
242             } else if ( current instanceof ParameterDescription ) {
243                 if (hasParams) {
244                     buffer.append('&');
245                 } else {
246                     buffer.append('?');
247                     hasParams = true;
248                 }
249                 buffer.append(((ParameterDescription) current).parameters);
250             }
251         }
252         return buffer.toString();
253     }
254
255     /**
256      * @see org.apache.cocoon.portal.LinkService#addEventToLink(org.apache.cocoon.portal.event.Event)
257      */

258     public void addEventToLink(Event event) {
259         if (event == null) {
260             return;
261         }
262         StringBuffer JavaDoc value = new StringBuffer JavaDoc("");
263         String JavaDoc parameterName = processEvent(event, value);
264
265         final LinkInfo info = this.getInfo();
266         if (event instanceof ComparableEvent) {
267             // search if we already have an event for this!
268
final Iterator JavaDoc iter = info.comparableEvents.iterator();
269             boolean found = false;
270             while (!found && iter.hasNext()) {
271                 Object JavaDoc[] objects = (Object JavaDoc[])iter.next();
272                 if (((ComparableEvent) objects[0]).equalsEvent((ComparableEvent) event)) {
273                     found = true;
274                     info.comparableEvents.remove(objects);
275                 }
276             }
277             info.comparableEvents.add(new Object JavaDoc[]{event, parameterName, value.toString()});
278         } else {
279             this.addParameterToLink(parameterName, value.toString());
280         }
281     }
282
283     /**
284      * @see org.apache.cocoon.portal.LinkService#addParameterToLink(java.lang.String, java.lang.String)
285      */

286     public void addParameterToLink(String JavaDoc name, String JavaDoc value) {
287         final LinkInfo info = this.getInfo();
288         info.addParameterToBase(name, value);
289     }
290
291     /**
292      * @see org.apache.cocoon.portal.LinkService#addUniqueParameterToLink(java.lang.String, java.lang.String)
293      */

294     public void addUniqueParameterToLink(String JavaDoc name, String JavaDoc value) {
295         final LinkInfo info = this.getInfo();
296         info.deleteParameterFromBase(name);
297         this.addParameterToLink(name, value);
298     }
299     
300     /**
301      * @see org.apache.cocoon.portal.LinkService#getRefreshLinkURI()
302      */

303     public String JavaDoc getRefreshLinkURI() {
304         return this.getRefreshLinkURI(null);
305     }
306
307     /**
308      * @see org.apache.cocoon.portal.LinkService#getRefreshLinkURI(java.lang.Boolean)
309      */

310     public String JavaDoc getRefreshLinkURI(Boolean JavaDoc secure) {
311         final LinkInfo info = this.getInfo();
312
313         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(info.getBase(secure));
314
315         // add comparable events
316
Iterator JavaDoc iter = info.comparableEvents.iterator();
317         boolean hasParams = info.hasParameters();
318         while (iter.hasNext()) {
319             Object JavaDoc[] objects = (Object JavaDoc[])iter.next();
320             if ( hasParams ) {
321                 buffer.append('&');
322             } else {
323                 buffer.append('?');
324             }
325             try {
326                 buffer.append((String JavaDoc)objects[1]).append('=').append(NetUtils.encode((String JavaDoc)objects[2], "utf-8"));
327             } catch (UnsupportedEncodingException JavaDoc uee) {
328                 // ignore this as utf-8 is always supported
329
}
330             hasParams = true;
331         }
332         return buffer.toString();
333     }
334     
335     /**
336      * @see org.apache.avalon.framework.activity.Disposable#dispose()
337      */

338     public void dispose() {
339         if (this.manager != null) {
340             this.manager.release( this.converter );
341             this.converter = null;
342             this.manager = null;
343         }
344     }
345
346     /**
347      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
348      */

349     public void contextualize(Context context) throws ContextException {
350         this.context = context;
351     }
352
353     private boolean getEventsMarshalled() {
354         if (this.eventsMarshalled == null) {
355             this.eventsMarshalled = new Boolean JavaDoc(this.converter.isMarshallEvents());
356         }
357         return this.eventsMarshalled.booleanValue();
358     }
359
360     private String JavaDoc processEvent(Event event, StringBuffer JavaDoc value) {
361         String JavaDoc parameterName = DEFAULT_REQUEST_EVENT_PARAMETER_NAME;
362         if (event instanceof ConvertableEvent && getEventsMarshalled()) {
363             final String JavaDoc eventParName = ((ConvertableEvent) event).getRequestParameterName();
364             String JavaDoc eventStr = ((ConvertableEvent) event).asString();
365             if (eventStr == null) {
366                 // Could not convert the event
367
value.append(this.converter.encode(event));
368             } else {
369                 parameterName = DEFAULT_CONVERTABLE_EVENT_PARAMETER_NAME;
370                 try {
371                     String JavaDoc eventValue = NetUtils.encode(eventStr, "utf-8");
372                     value.append(eventParName).append('(').append(eventValue).append(')');
373                 } catch (UnsupportedEncodingException JavaDoc uee) {
374                     // ignore this as utf-8 is always supported
375
}
376             }
377         } else {
378             if (event instanceof RequestEvent) {
379                 final String JavaDoc eventParName = ((RequestEvent) event).getRequestParameterName();
380                 if (eventParName != null) {
381                     parameterName = eventParName;
382                 }
383             }
384             value.append(this.converter.encode(event));
385         }
386         return parameterName;
387     }
388 }
389
Popular Tags