KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > consumer > impl > URLRewriterImpl


1 /*
2 * Copyright 2001-2004 The eXo platform SARL All rights reserved.
3 * Please look at license.txt in info directory for more license detail.
4 */

5
6 package org.exoplatform.services.wsrp.consumer.impl;
7
8
9 import java.util.Map JavaDoc;
10 import java.util.HashMap JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.exoplatform.services.log.LogService;
14 import org.exoplatform.services.wsrp.WSRPConstants;
15 import org.exoplatform.services.wsrp.consumer.URLGenerator;
16 import org.exoplatform.services.wsrp.consumer.URLRewriter;
17 import org.exoplatform.services.wsrp.exceptions.Faults;
18 import org.exoplatform.services.wsrp.exceptions.WSRPException;
19
20 /*
21  * Some part of this class implementation are taken from the WSRP4J project
22  *
23  * @author Mestrallet Benjamin
24  * benjmestrallet@users.sourceforge.net
25  *
26  * @author <a HREF="mailto:stefan.behl@de.ibm.com">Stefan Behl</a>
27  * @author Richard Jacob
28  *
29  * Date: 6 févr. 2004
30  * Time: 15:13:40
31  */

32
33 public class URLRewriterImpl implements URLRewriter {
34
35   private URLGenerator urlGenerator;
36   private Log log;
37
38   public URLRewriterImpl(URLGenerator urlGenerator, LogService logService) {
39     this.urlGenerator = urlGenerator;
40     this.log = logService.getLog("org.exoplatform.services.wsrp.consumer");
41   }
42
43   public String JavaDoc rewriteURLs(String JavaDoc path, String JavaDoc markup) throws WSRPException {
44     log.debug("Rewrite URL : " + markup);
45     StringBuffer JavaDoc resultMarkup = new StringBuffer JavaDoc("");
46     int markupIndex = 0;
47     int rewriteStartPos = -1;
48     int rewriteEndPos = -1;
49     int currentPos = 0;
50     while (markupIndex < markup.length()) {
51       rewriteStartPos = -1;
52       rewriteEndPos = -1;
53       rewriteStartPos = markup.indexOf(WSRPConstants.WSRP_REWITE_PREFIX, markupIndex);
54       if (!(rewriteStartPos == -1 ||
55           (rewriteStartPos + WSRPConstants.WSRP_REWITE_PREFIX.length() - 1) > (markup.length() - 2))) {
56         rewriteEndPos = markup.indexOf(WSRPConstants.WSRP_REWITE_SUFFFIX, markupIndex);
57         if (rewriteEndPos != -1) {
58           currentPos = rewriteStartPos;
59           while ((currentPos != -1) && (currentPos < rewriteEndPos)) {
60             rewriteStartPos = currentPos;
61             currentPos = markup.indexOf(WSRPConstants.WSRP_REWITE_PREFIX,
62                 rewriteStartPos + WSRPConstants.WSRP_REWITE_PREFIX.length());
63           }
64           rewriteEndPos = rewriteEndPos + WSRPConstants.WSRP_REWITE_SUFFFIX.length() - 1;
65         }
66       }
67       if ((rewriteStartPos != -1) && (rewriteEndPos != -1)) {
68         resultMarkup.append(markup.substring(markupIndex, rewriteStartPos));
69         rewrite(path, resultMarkup, markup.substring(rewriteStartPos, rewriteEndPos + 1));
70         markupIndex = rewriteEndPos + 1;
71       } else {
72         resultMarkup.append(markup.substring(markupIndex, markup.length()));
73         markupIndex = markup.length();
74       }
75     }
76     log.debug("Markup returned : " + resultMarkup.toString());
77     return resultMarkup.toString();
78   }
79
80   private void rewrite(String JavaDoc baseURL, StringBuffer JavaDoc markup, String JavaDoc rewriteURL) throws WSRPException {
81     Map JavaDoc params = createParameterMap(rewriteURL);
82     if (rewriteURL.indexOf(WSRPConstants.URL_TYPE_BLOCKINGACTION) != -1) {
83       markup.append(urlGenerator.getBlockingActionURL(baseURL, params));
84     } else if (rewriteURL.indexOf(WSRPConstants.URL_TYPE_RENDER) != -1) {
85       markup.append(urlGenerator.getRenderURL(baseURL, params));
86     } else if (rewriteURL.indexOf(WSRPConstants.URL_TYPE_RESOURCE) != -1) {
87       markup.append(urlGenerator.getResourceURL(baseURL,params));
88     }
89   }
90
91   private Map JavaDoc createParameterMap(String JavaDoc rewriteURL) throws WSRPException {
92     Map JavaDoc params = (Map JavaDoc) new HashMap JavaDoc();
93     if (rewriteURL.indexOf(WSRPConstants.URL_TYPE_BLOCKINGACTION) != -1) {
94       params.put(WSRPConstants.WSRP_URL_TYPE, WSRPConstants.URL_TYPE_BLOCKINGACTION);
95     } else if (rewriteURL.indexOf(WSRPConstants.URL_TYPE_RENDER) != -1) {
96       params.put(WSRPConstants.WSRP_URL_TYPE, WSRPConstants.URL_TYPE_RENDER);
97     } else if (rewriteURL.indexOf(WSRPConstants.URL_TYPE_RESOURCE) != -1) {
98       params.put(WSRPConstants.WSRP_URL_TYPE, WSRPConstants.URL_TYPE_RESOURCE);
99     } else {
100       throw new WSRPException(Faults.OPERATION_FAILED_FAULT);
101     }
102     int equals = 0;
103     int next = 0;
104     int end = rewriteURL.indexOf(WSRPConstants.WSRP_REWITE_SUFFFIX);
105     int index = rewriteURL.indexOf(WSRPConstants.NEXT_PARAM);
106     int lengthNext = 0;
107     String JavaDoc subNext = null;
108     while (index != -1) {
109       subNext = rewriteURL.substring(index, index + WSRPConstants.NEXT_PARAM_AMP.length());
110       if (subNext.equals(WSRPConstants.NEXT_PARAM_AMP)) {
111         lengthNext = WSRPConstants.NEXT_PARAM_AMP.length();
112       } else {
113         lengthNext = WSRPConstants.NEXT_PARAM.length();
114       }
115       equals = rewriteURL.indexOf("=", index + lengthNext);
116       next = rewriteURL.indexOf(WSRPConstants.NEXT_PARAM, equals);
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     return params;
127   }
128
129
130 }
Popular Tags