1 5 6 package org.exoplatform.services.wsrp.consumer.impl; 7 8 9 import java.util.Map ; 10 import java.util.HashMap ; 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 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 rewriteURLs(String path, String markup) throws WSRPException { 44 log.debug("Rewrite URL : " + markup); 45 StringBuffer resultMarkup = new StringBuffer (""); 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 baseURL, StringBuffer markup, String rewriteURL) throws WSRPException { 81 Map 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 createParameterMap(String rewriteURL) throws WSRPException { 92 Map params = (Map ) new HashMap (); 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 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 |