KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
20
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.wsrp4j.consumer.URLGenerator;
23 import org.apache.wsrp4j.consumer.URLRewriter;
24 import org.apache.wsrp4j.util.Constants;
25
26 /**
27  * Implements the URLRewriter interface providing a method
28  * to rewrite urls (Consumer URL Rewriting).<br/>
29  *
30  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
31  * @author <a HREF="mailto:malessandrini@s-und-n.de">Michel Alessandrini</a>
32  *
33  * @version $Id: URLRewriterImpl.java 264755 2005-08-30 10:29:21Z cziegeler $
34  */

35 public class URLRewriterImpl
36     extends AbstractLogEnabled
37     implements URLRewriter {
38
39     /** The url generator. */
40     protected URLGenerator urlGenerator;
41
42     /**
43      * @see org.apache.wsrp4j.consumer.URLRewriter#setURLGenerator(org.apache.wsrp4j.consumer.URLGenerator)
44      */

45     public void setURLGenerator(URLGenerator urlGenerator) {
46         this.urlGenerator = urlGenerator;
47     }
48
49     /**
50      * Rewriting: get url from URLGenerator and append it<br/>
51      *
52      * @param markup
53      * @param rewriteURL
54      */

55     protected void rewrite(StringBuffer JavaDoc markup, String JavaDoc rewriteURL) {
56         if ( rewriteURL.startsWith(Constants.REWRITE_START + Constants.PARAMS_START) ) {
57             // handle URL rewriting
58
Map JavaDoc params = createParameterMap(rewriteURL);
59
60             // What kind of link has to be rewritten?
61
if (rewriteURL.indexOf(Constants.URL_TYPE_BLOCKINGACTION) != -1) {
62                 markup.append(urlGenerator.getBlockingActionURL(params));
63             } else if (rewriteURL.indexOf(Constants.URL_TYPE_RENDER) != -1) {
64                 markup.append(urlGenerator.getRenderURL(params));
65             } else if (rewriteURL.indexOf(Constants.URL_TYPE_RESOURCE) != -1) {
66                 markup.append(urlGenerator.getResourceURL(params));
67             }
68         } else if (rewriteURL.startsWith(Constants.REWRITE_START + Constants.NAMESPACE_START) ) {
69             markup.append(urlGenerator.getNamespacedToken(""));
70         } else {
71             this.getLogger().error("No valid rewrite expression found in: " + rewriteURL);
72         }
73     }
74
75     /**
76      * Extracts parameters from url to be rewritten copies them into a map.<br/>
77      *
78      * @param rewriteURL
79      * @return Map
80      */

81     protected Map JavaDoc createParameterMap(String JavaDoc rewriteURL) {
82
83         Map JavaDoc params = new HashMap JavaDoc();
84
85         if (rewriteURL.indexOf(Constants.URL_TYPE_BLOCKINGACTION) != -1) {
86             params.put(Constants.URL_TYPE, Constants.URL_TYPE_BLOCKINGACTION);
87         } else if (rewriteURL.indexOf(Constants.URL_TYPE_RENDER) != -1) {
88             params.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER);
89         } else if (rewriteURL.indexOf(Constants.URL_TYPE_RESOURCE) != -1) {
90             params.put(Constants.URL_TYPE, Constants.URL_TYPE_RESOURCE);
91         } else {
92             this.getLogger().error("no valid url-type: " + rewriteURL);
93         }
94
95         // begin parsing
96
int equals = 0;
97         int next = 0;
98         int end = rewriteURL.indexOf(Constants.REWRITE_END);
99         int index = rewriteURL.indexOf(Constants.NEXT_PARAM);
100         int lengthNext = 0;
101         String JavaDoc subNext = null;
102
103         while (index != -1) {
104             // support "&amp;" as parameter seperator
105
// see if &amp; was used
106
subNext = rewriteURL.substring(index, index + Constants.NEXT_PARAM_AMP.length());
107             if (subNext.equals(Constants.NEXT_PARAM_AMP)) {
108                 lengthNext = Constants.NEXT_PARAM_AMP.length();
109             }
110             else {
111                 lengthNext = Constants.NEXT_PARAM.length();
112             }
113              
114             equals = rewriteURL.indexOf(Constants.EQUALS, index + lengthNext);
115             next = rewriteURL.indexOf(Constants.NEXT_PARAM, equals);
116
117             if (equals != -1) {
118                 if (next != -1) {
119                     params.put(rewriteURL.substring(index + lengthNext, equals), rewriteURL.substring(equals + 1, next));
120                 } else {
121                     params.put(rewriteURL.substring(index + lengthNext, equals), rewriteURL.substring(equals + 1, end));
122                 }
123             }
124             index = next;
125         }
126
127         return params;
128
129     }
130
131     /**
132      * Parses markup and performs URL rewriting.<br/>
133      *
134      * Principle:<br/>
135      * - Iterate over markup-string once and copy processed markup to result
136      * buffer (StringBuffer)<br/>
137      * - If url to be rewritten found (during markup iteration),<br/>
138      * ... append markup before url to result buffer,<br/>
139      * ... perform rewriting (call URLGenerator) and append rewritten url to result buffer.<br/>
140      *
141      * Incomplete rewrite-pairs (e.g. a rewrite-begin-token not followed by a
142      * rewrite-end-token) are considered as 'normal' markup.<br/>
143      *
144      * @param markup String representing the markup to be processed.
145      *
146      * @return String representing the processed markup.
147      *
148      * @see org.apache.wsrp4j.consumer.URLRewriter#rewriteURLs(java.lang.String)
149      */

150     public String JavaDoc rewriteURLs(String JavaDoc markup) {
151         StringBuffer JavaDoc resultMarkup = new StringBuffer JavaDoc("");
152         int markupIndex = 0;
153         int rewriteStartPos = -1;
154         int rewriteEndPos = -1;
155         int currentPos = 0;
156         String JavaDoc exprType = null;
157         
158         // loop through the markup, find rewrite expressions, rewrite them
159
while ( markupIndex < markup.length() ) {
160             rewriteStartPos = -1;
161             rewriteEndPos = -1;
162
163             // get fist occurance of wsrp rewrite expression
164
rewriteStartPos = markup.indexOf(Constants.REWRITE_START,markupIndex);
165             
166             if (! ( rewriteStartPos == -1 ||
167                     ( rewriteStartPos + Constants.REWRITE_START.length() - 1 ) >
168                     ( markup.length() - 2 ) ) ) {
169                 // found a rewrite start token, and token is not at the end of markup so we can
170
// determine the rewrite type, i.e. there is at least 1 char after the rewrite start token
171

172                 // namespace or URL? The single char string after the token decides
173
exprType = markup.substring(rewriteStartPos+Constants.REWRITE_START.length() - 1 + 1,
174                                             rewriteStartPos+Constants.REWRITE_START.length() - 1 + 2);
175             
176                 if ( exprType.equals(Constants.NAMESPACE_START)) {
177                     // ok, we have a namespace rewrite here
178
rewriteEndPos = rewriteStartPos + Constants.REWRITE_START.length()
179                                     + Constants.NAMESPACE_START.length() - 1;
180                 } else if ( exprType.equals(Constants.PARAMS_START) ) {
181                     // ok, we have a URL rewrite here
182
// get the position of the end token
183
rewriteEndPos = markup.indexOf(Constants.REWRITE_END,markupIndex);
184                     if (rewriteEndPos != -1) {
185                         // now let's see if we find a rewrite start token nearer to the end token
186
currentPos = rewriteStartPos;
187
188                         while ((currentPos != -1) && (currentPos < rewriteEndPos)) {
189                              // update rewriteStartPos with position of found rewrite begin token being 'nearer'
190
rewriteStartPos = currentPos;
191                             // look for next URL rewrite start expression
192
currentPos = markup.indexOf(Constants.REWRITE_START+Constants.PARAMS_START,
193                                                         rewriteStartPos + Constants.REWRITE_START.length()
194                                                         + Constants.PARAMS_START.length());
195                         }
196                         rewriteEndPos = rewriteEndPos + Constants.REWRITE_END.length() - 1;
197                     }
198                 }
199             }
200             
201             if ( (rewriteStartPos != -1) && (rewriteEndPos != -1) ) {
202                 // append markup before rewrite expression
203
resultMarkup.append(markup.substring(markupIndex,rewriteStartPos));
204                 // append rewritten expression
205
rewrite(resultMarkup,markup.substring(rewriteStartPos,rewriteEndPos+1));
206                 // set markup index after the last char of the rewriteExpression
207
markupIndex = rewriteEndPos + 1;
208             } else {
209                 // append rest of markup
210
resultMarkup.append(markup.substring(markupIndex,markup.length()));
211                 markupIndex = markup.length();
212             }
213         }
214
215         return resultMarkup.toString();
216     }
217 }
218
219
220
Popular Tags