Coverage Report - org.apache.camel.util.URISupport
 
Classes in this File Line Coverage Branch Coverage Complexity
URISupport
14% 
19% 
3.048
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.util;
 18  
 
 19  
 import java.io.UnsupportedEncodingException;
 20  
 import java.net.URI;
 21  
 import java.net.URISyntaxException;
 22  
 import java.net.URLDecoder;
 23  
 import java.net.URLEncoder;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.HashMap;
 27  
 import java.util.Iterator;
 28  
 import java.util.Map;
 29  
 
 30  
 /**
 31  
  * @version $Revision$
 32  
  */
 33  0
 public class URISupport {
 34  0
     public static class CompositeData {
 35  
         public String host;
 36  
 
 37  
         String scheme;
 38  
         String path;
 39  
         URI components[];
 40  
         Map parameters;
 41  
         String fragment;
 42  
 
 43  
         public URI[] getComponents() {
 44  0
             return components;
 45  
         }
 46  
 
 47  
         public String getFragment() {
 48  0
             return fragment;
 49  
         }
 50  
 
 51  
         public Map getParameters() {
 52  0
             return parameters;
 53  
         }
 54  
 
 55  
         public String getScheme() {
 56  0
             return scheme;
 57  
         }
 58  
 
 59  
         public String getPath() {
 60  0
             return path;
 61  
         }
 62  
 
 63  
         public String getHost() {
 64  0
             return host;
 65  
         }
 66  
 
 67  
         public URI toURI() throws URISyntaxException {
 68  0
             StringBuffer sb = new StringBuffer();
 69  0
             if (scheme != null) {
 70  0
                 sb.append(scheme);
 71  0
                 sb.append(':');
 72  
             }
 73  
 
 74  0
             if (host != null && host.length() != 0) {
 75  0
                 sb.append(host);
 76  0
             } else {
 77  0
                 sb.append('(');
 78  0
                 for (int i = 0; i < components.length; i++) {
 79  0
                     if (i != 0) {
 80  0
                         sb.append(',');
 81  
                     }
 82  0
                     sb.append(components[i].toString());
 83  
                 }
 84  0
                 sb.append(')');
 85  
             }
 86  
 
 87  0
             if (path != null) {
 88  0
                 sb.append('/');
 89  0
                 sb.append(path);
 90  
             }
 91  0
             if (!parameters.isEmpty()) {
 92  0
                 sb.append("?");
 93  0
                 sb.append(createQueryString(parameters));
 94  
             }
 95  0
             if (fragment != null) {
 96  0
                 sb.append("#");
 97  0
                 sb.append(fragment);
 98  
             }
 99  0
             return new URI(sb.toString());
 100  
         }
 101  
     }
 102  
 
 103  
     public static Map parseQuery(String uri) throws URISyntaxException {
 104  
         try {
 105  30
             Map rc = new HashMap();
 106  30
             if (uri != null) {
 107  30
                 String[] parameters = uri.split("&");
 108  66
                 for (int i = 0; i < parameters.length; i++) {
 109  36
                     int p = parameters[i].indexOf("=");
 110  36
                     if (p >= 0) {
 111  36
                         String name = URLDecoder.decode(parameters[i].substring(0, p), "UTF-8");
 112  36
                         String value = URLDecoder.decode(parameters[i].substring(p + 1), "UTF-8");
 113  36
                         rc.put(name, value);
 114  36
                     } else {
 115  0
                         rc.put(parameters[i], null);
 116  
                     }
 117  
                 }
 118  
             }
 119  30
             return rc;
 120  0
         } catch (UnsupportedEncodingException e) {
 121  0
             throw (URISyntaxException)new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
 122  
         }
 123  
     }
 124  
 
 125  
     public static Map parseParamters(URI uri) throws URISyntaxException {
 126  585
         String query = uri.getQuery();
 127  585
         if (query == null) {
 128  579
             String schemeSpecificPart = uri.getSchemeSpecificPart();
 129  579
             int idx = schemeSpecificPart.lastIndexOf('?');
 130  579
             if (idx < 0) {
 131  555
                 return Collections.EMPTY_MAP;
 132  
             } else {
 133  24
                 query = schemeSpecificPart.substring(idx + 1);
 134  
             }
 135  24
         } else {
 136  6
             query = stripPrefix(query, "?");
 137  
         }
 138  30
         return parseQuery(query);
 139  
     }
 140  
 
 141  
     /**
 142  
      * Removes any URI query from the given uri
 143  
      */
 144  
     public static URI removeQuery(URI uri) throws URISyntaxException {
 145  0
         return createURIWithQuery(uri, null);
 146  
     }
 147  
 
 148  
     /**
 149  
      * Creates a URI with the given query
 150  
      */
 151  
     public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException {
 152  0
         return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(),
 153  
                        query, uri.getFragment());
 154  
     }
 155  
 
 156  
     public static CompositeData parseComposite(URI uri) throws URISyntaxException {
 157  
 
 158  0
         CompositeData rc = new CompositeData();
 159  0
         rc.scheme = uri.getScheme();
 160  0
         String ssp = stripPrefix(uri.getSchemeSpecificPart().trim(), "//").trim();
 161  
 
 162  0
         parseComposite(uri, rc, ssp);
 163  
 
 164  0
         rc.fragment = uri.getFragment();
 165  0
         return rc;
 166  
     }
 167  
 
 168  
     /**
 169  
      * @param uri
 170  
      * @param rc
 171  
      * @param ssp
 172  
      * @throws URISyntaxException
 173  
      */
 174  
     private static void parseComposite(URI uri, CompositeData rc, String ssp) throws URISyntaxException {
 175  
         String componentString;
 176  
         String params;
 177  
 
 178  0
         if (!checkParenthesis(ssp)) {
 179  0
             throw new URISyntaxException(uri.toString(), "Not a matching number of '(' and ')' parenthesis");
 180  
         }
 181  
 
 182  
         int p;
 183  0
         int intialParen = ssp.indexOf("(");
 184  0
         if (intialParen == 0) {
 185  0
             rc.host = ssp.substring(0, intialParen);
 186  0
             p = rc.host.indexOf("/");
 187  0
             if (p >= 0) {
 188  0
                 rc.path = rc.host.substring(p);
 189  0
                 rc.host = rc.host.substring(0, p);
 190  
             }
 191  0
             p = ssp.lastIndexOf(")");
 192  0
             componentString = ssp.substring(intialParen + 1, p);
 193  0
             params = ssp.substring(p + 1).trim();
 194  0
         } else {
 195  0
             componentString = ssp;
 196  0
             params = "";
 197  
         }
 198  
 
 199  0
         String components[] = splitComponents(componentString);
 200  0
         rc.components = new URI[components.length];
 201  0
         for (int i = 0; i < components.length; i++) {
 202  0
             rc.components[i] = new URI(components[i].trim());
 203  
         }
 204  
 
 205  0
         p = params.indexOf("?");
 206  0
         if (p >= 0) {
 207  0
             if (p > 0) {
 208  0
                 rc.path = stripPrefix(params.substring(0, p), "/");
 209  
             }
 210  0
             rc.parameters = parseQuery(params.substring(p + 1));
 211  0
         } else {
 212  0
             if (params.length() > 0) {
 213  0
                 rc.path = stripPrefix(params, "/");
 214  
             }
 215  0
             rc.parameters = Collections.EMPTY_MAP;
 216  
         }
 217  0
     }
 218  
 
 219  
     /**
 220  
      * @param str
 221  
      * @return
 222  
      */
 223  
     private static String[] splitComponents(String str) {
 224  0
         ArrayList l = new ArrayList();
 225  
 
 226  0
         int last = 0;
 227  0
         int depth = 0;
 228  0
         char chars[] = str.toCharArray();
 229  0
         for (int i = 0; i < chars.length; i++) {
 230  0
             switch (chars[i]) {
 231  
             case '(':
 232  0
                 depth++;
 233  0
                 break;
 234  
             case ')':
 235  0
                 depth--;
 236  0
                 break;
 237  
             case ',':
 238  0
                 if (depth == 0) {
 239  0
                     String s = str.substring(last, i);
 240  0
                     l.add(s);
 241  0
                     last = i + 1;
 242  0
                 }
 243  
                 break;
 244  
             default:
 245  
             }
 246  
         }
 247  
 
 248  0
         String s = str.substring(last);
 249  0
         if (s.length() != 0) {
 250  0
             l.add(s);
 251  
         }
 252  
 
 253  0
         String rc[] = new String[l.size()];
 254  0
         l.toArray(rc);
 255  0
         return rc;
 256  
     }
 257  
 
 258  
     public static String stripPrefix(String value, String prefix) {
 259  6
         if (value.startsWith(prefix)) {
 260  0
             return value.substring(prefix.length());
 261  
         }
 262  6
         return value;
 263  
     }
 264  
 
 265  
     public static URI stripScheme(URI uri) throws URISyntaxException {
 266  0
         return new URI(stripPrefix(uri.getSchemeSpecificPart().trim(), "//"));
 267  
     }
 268  
 
 269  
     public static String createQueryString(Map options) throws URISyntaxException {
 270  
         try {
 271  0
             if (options.size() > 0) {
 272  0
                 StringBuffer rc = new StringBuffer();
 273  0
                 boolean first = true;
 274  0
                 for (Iterator iter = options.keySet().iterator(); iter.hasNext();) {
 275  0
                     if (first) {
 276  0
                         first = false;
 277  0
                     } else {
 278  0
                         rc.append("&");
 279  
                     }
 280  
 
 281  0
                     String key = (String)iter.next();
 282  0
                     String value = (String)options.get(key);
 283  0
                     rc.append(URLEncoder.encode(key, "UTF-8"));
 284  0
                     rc.append("=");
 285  0
                     rc.append(URLEncoder.encode(value, "UTF-8"));
 286  0
                 }
 287  0
                 return rc.toString();
 288  
             } else {
 289  0
                 return "";
 290  
             }
 291  0
         } catch (UnsupportedEncodingException e) {
 292  0
             throw (URISyntaxException)new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
 293  
         }
 294  
     }
 295  
 
 296  
     /**
 297  
      * Creates a URI from the original URI and the remaining paramaters
 298  
      * 
 299  
      * @throws URISyntaxException
 300  
      */
 301  
     public static URI createRemainingURI(URI originalURI, Map params) throws URISyntaxException {
 302  0
         String s = createQueryString(params);
 303  0
         if (s.length() == 0) {
 304  0
             s = null;
 305  
         }
 306  0
         return createURIWithQuery(originalURI, s);
 307  
     }
 308  
 
 309  
     public static URI changeScheme(URI bindAddr, String scheme) throws URISyntaxException {
 310  0
         return new URI(scheme, bindAddr.getUserInfo(), bindAddr.getHost(), bindAddr.getPort(), bindAddr
 311  
             .getPath(), bindAddr.getQuery(), bindAddr.getFragment());
 312  
     }
 313  
 
 314  
     public static boolean checkParenthesis(String str) {
 315  0
         boolean result = true;
 316  0
         if (str != null) {
 317  0
             int open = 0;
 318  0
             int closed = 0;
 319  
 
 320  0
             int i = 0;
 321  0
             while ((i = str.indexOf('(', i)) >= 0) {
 322  0
                 i++;
 323  0
                 open++;
 324  0
             }
 325  0
             i = 0;
 326  0
             while ((i = str.indexOf(')', i)) >= 0) {
 327  0
                 i++;
 328  0
                 closed++;
 329  0
             }
 330  0
             result = open == closed;
 331  
         }
 332  0
         return result;
 333  
     }
 334  
 
 335  
     public int indexOfParenthesisMatch(String str) {
 336  0
         int result = -1;
 337  
 
 338  0
         return result;
 339  
     }
 340  
 }