KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > wsrp > consumer > URLGeneratorImpl


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.consumer;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.cocoon.portal.PortalService;
23 import org.apache.cocoon.portal.coplet.CopletInstanceData;
24 import org.apache.cocoon.portal.wsrp.adapter.WSRPAdapter;
25 import org.apache.cocoon.portal.wsrp.adapter.WSRPEventAspect;
26 import org.apache.wsrp4j.consumer.URLGenerator;
27 import org.apache.wsrp4j.util.Constants;
28
29 /**
30  * Implements the URLGenerator interface providing methods
31  * to query the consumer's urls.
32  *
33  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
34  * @author <a HREF="mailto:malessandrini@s-und-n.de">Michel Alessandrini</a>
35  *
36  * @version $Id: URLGeneratorImpl.java 292632 2005-09-30 04:47:07Z antonio $
37  */

38 public class URLGeneratorImpl
39     implements URLGenerator, RequiresPortalService, RequiresWSRPAdapter {
40
41     /** The portal service. */
42     protected PortalService service;
43
44     /** The WSRP Adapter. */
45     protected WSRPAdapter adapter;
46
47     /**
48      * @see org.apache.cocoon.portal.wsrp.consumer.RequiresWSRPAdapter#setWSRPAdapter(org.apache.cocoon.portal.wsrp.adapter.WSRPAdapter)
49      */

50     public void setWSRPAdapter(WSRPAdapter adapter) {
51         this.adapter = adapter;
52     }
53
54     /**
55      * @see org.apache.cocoon.portal.wsrp.consumer.RequiresPortalService#setPortalService(org.apache.cocoon.portal.PortalService)
56      */

57     public void setPortalService(PortalService service) {
58         this.service = service;
59     }
60
61     /**
62      * @see org.apache.wsrp4j.consumer.URLGenerator#getBlockingActionURL(java.util.Map)
63      */

64     public String JavaDoc getBlockingActionURL(Map JavaDoc params) {
65         return this.generateUrl(params);
66     }
67
68     /**
69      * @see org.apache.wsrp4j.consumer.URLGenerator#getRenderURL(java.util.Map)
70      */

71     public String JavaDoc getRenderURL(Map JavaDoc params) {
72         return this.generateUrl(params);
73     }
74
75     /**
76      * @see org.apache.wsrp4j.consumer.URLGenerator#getResourceURL(java.util.Map)
77      */

78     public String JavaDoc getResourceURL(Map JavaDoc params) {
79         // this is a little bit tricky
80
// we create a usual portal link first with
81
// all the infos
82
String JavaDoc portalLink = this.generateUrl(params);
83         
84         // now we replace the portal pipeline with
85
// the resource pipeline
86
int linkEndPos = portalLink.indexOf('?');
87         int pipelineStartPos = portalLink.lastIndexOf('/', linkEndPos);
88
89         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
90         buffer.append(portalLink.substring(0, pipelineStartPos+1));
91         buffer.append("wsrprsc");
92         buffer.append(portalLink.substring(linkEndPos));
93         return buffer.toString();
94     }
95
96     /**
97      * @see org.apache.wsrp4j.consumer.URLGenerator#getNamespacedToken(java.lang.String)
98      */

99     public String JavaDoc getNamespacedToken(String JavaDoc token) {
100         final CopletInstanceData coplet = this.adapter.getCurrentCopletInstanceData();
101         return coplet.getId();
102     }
103
104     /**
105      * Generate the url.<br/>
106      * We simply create a new wsrp event and use the portal link service.<br/>
107      *
108      * @param params Url-parameters
109      * @return portal-url including all required attributes
110      */

111     protected String JavaDoc generateUrl(Map JavaDoc params) {
112         if ( params == null ) {
113             params = new HashMap JavaDoc();
114         }
115         Boolean JavaDoc secureLink = null;
116         if ( "true".equalsIgnoreCase((String JavaDoc)params.get(Constants.SECURE_URL)) ) {
117             secureLink = Boolean.TRUE;
118         }
119         final CopletInstanceData coplet = this.adapter.getCurrentCopletInstanceData();
120         params.put(WSRPEventAspect.REQUEST_PARAMETER_NAME, coplet.getId());
121         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(this.service.getComponentManager().getLinkService().getRefreshLinkURI(secureLink));
122         boolean hasParams = buffer.toString().indexOf("?") > 0;
123         Iterator JavaDoc i = params.entrySet().iterator();
124         while ( i.hasNext() ) {
125             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
126             if ( hasParams ) {
127                 buffer.append('&');
128             } else {
129                 hasParams = true;
130                 buffer.append('?');
131             }
132             buffer.append(entry.getKey()).append('=').append(entry.getValue());
133         }
134         // append consumer parameters
135
Map JavaDoc consumerParameters = (Map JavaDoc)coplet.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_CONSUMER_MAP);
136         if ( consumerParameters != null ) {
137             i = consumerParameters.entrySet().iterator();
138             while (i.hasNext()) {
139                 final Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
140                 if ( hasParams ) {
141                     buffer.append('&');
142                 } else {
143                     hasParams = true;
144                     buffer.append('?');
145                 }
146                 buffer.append(entry.getKey()).append('=').append(entry.getValue());
147             }
148         }
149         return buffer.toString();
150     }
151 }
152
Popular Tags