1 16 package org.apache.cocoon.portal.wsrp.consumer; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 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 35 public class URLRewriterImpl 36 extends AbstractLogEnabled 37 implements URLRewriter { 38 39 40 protected URLGenerator urlGenerator; 41 42 45 public void setURLGenerator(URLGenerator urlGenerator) { 46 this.urlGenerator = urlGenerator; 47 } 48 49 55 protected void rewrite(StringBuffer markup, String rewriteURL) { 56 if ( rewriteURL.startsWith(Constants.REWRITE_START + Constants.PARAMS_START) ) { 57 Map params = createParameterMap(rewriteURL); 59 60 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 81 protected Map createParameterMap(String rewriteURL) { 82 83 Map params = new HashMap (); 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 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 subNext = null; 102 103 while (index != -1) { 104 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 150 public String rewriteURLs(String markup) { 151 StringBuffer resultMarkup = new StringBuffer (""); 152 int markupIndex = 0; 153 int rewriteStartPos = -1; 154 int rewriteEndPos = -1; 155 int currentPos = 0; 156 String exprType = null; 157 158 while ( markupIndex < markup.length() ) { 160 rewriteStartPos = -1; 161 rewriteEndPos = -1; 162 163 rewriteStartPos = markup.indexOf(Constants.REWRITE_START,markupIndex); 165 166 if (! ( rewriteStartPos == -1 || 167 ( rewriteStartPos + Constants.REWRITE_START.length() - 1 ) > 168 ( markup.length() - 2 ) ) ) { 169 172 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 rewriteEndPos = rewriteStartPos + Constants.REWRITE_START.length() 179 + Constants.NAMESPACE_START.length() - 1; 180 } else if ( exprType.equals(Constants.PARAMS_START) ) { 181 rewriteEndPos = markup.indexOf(Constants.REWRITE_END,markupIndex); 184 if (rewriteEndPos != -1) { 185 currentPos = rewriteStartPos; 187 188 while ((currentPos != -1) && (currentPos < rewriteEndPos)) { 189 rewriteStartPos = currentPos; 191 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 resultMarkup.append(markup.substring(markupIndex,rewriteStartPos)); 204 rewrite(resultMarkup,markup.substring(rewriteStartPos,rewriteEndPos+1)); 206 markupIndex = rewriteEndPos + 1; 208 } else { 209 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 |