001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018
019 package org.activemq.util;
020 import java.net.URI;
021 import java.net.URISyntaxException;
022 import java.util.HashMap;
023 import java.util.Map;
024 import java.util.StringTokenizer;
025
026 /**
027 * Some URI based helper methods.
028 *
029 * @version $Revision: 1.1.1.1 $
030 */
031 public class URIHelper {
032 /**
033 * Parsers the query string of the URI into a map of key-value pairs
034 */
035 public static Map parseQuery(URI uri) {
036 return parseQuery(getLastQuery(uri));
037 }
038
039 /**
040 * Parsers the query string of the URI into a map of key-value pairs
041 */
042 public static Map parseQuery(String query) {
043 Map answer = new HashMap();
044 if (query != null) {
045 if (query.startsWith("?")) {
046 query = query.substring(1);
047 }
048 StringTokenizer iter = new StringTokenizer(query, "&");
049 while (iter.hasMoreTokens()) {
050 String pair = iter.nextToken();
051 addKeyValuePair(answer, pair);
052 }
053 }
054 return answer;
055 }
056
057 /**
058 * Get the query str
059 * @param uri
060 * @return the last query - the URI could be a composite
061 */
062 public static String getLastQuery(URI uri) {
063 String result = null;
064 if (uri != null) {
065 String uriString = uri.toString();
066 if (uriString != null) {
067 int index = uri.toString().lastIndexOf('?');
068 if (index >= 0 && (index + 1) < uriString.length()) {
069 result = uriString.substring(index + 1);
070 }
071 }
072 }
073 return result;
074 }
075
076 /**
077 * Get the Path
078 * @param uri
079 * @return the last path - the URI could be a composite
080 */
081 public static String getLastPath(URI uri) {
082
083 String path = uri.getPath();
084 if(path==null) {
085 path = uri.getRawSchemeSpecificPart();
086 }
087
088 // Skip to the last composite uri in the path.
089 URIHelper helper = new URIHelper(path);
090 while( helper.hasNext() ) {
091 path = helper.getNext();
092 }
093
094 // Was the last thing in the composite a URI?
095 if( path.indexOf(":") >=0 ) {
096 try {
097 path = new URI(path).getPath();
098 } catch (URISyntaxException e) {
099 }
100 }
101
102 path = removeInitialSlash(path).trim();
103 if( path.length() == 0 )
104 return null;
105
106 return path;
107 }
108
109 /**
110 * @param path
111 * @return
112 */
113 private static String removeInitialSlash(String path) {
114 if( path.length()>0 && path.charAt(0)=='/' ) {
115 return path.substring(1);
116 }
117 return path;
118 }
119
120 protected static void addKeyValuePair(Map answer, String pair) {
121 int idx = pair.indexOf('=');
122 String name = null;
123 String value = null;
124 if (idx >= 0) {
125 name = pair.substring(0, idx);
126 if (++idx < pair.length()) {
127 value = pair.substring(idx);
128 }
129 }
130 else {
131 name = pair;
132 }
133 answer.put(name, value);
134 }
135 private StringTokenizer tokenizer;
136
137 /**
138 * Create URIHelper with a path
139 *
140 * @param path
141 */
142 public URIHelper(String path) {
143 if (path != null) {
144 this.tokenizer = new StringTokenizer(strip(path), ",");
145 }
146 }
147
148 public boolean hasNext() {
149 return tokenizer == null ? false : tokenizer.hasMoreTokens();
150 }
151
152 public String getNext() {
153 return tokenizer == null ? null : tokenizer.nextToken();
154 }
155
156 private String strip(String str) {
157 String result = null;
158 if (str != null) {
159 result = str.trim();
160 int index = result.indexOf('(');
161 if (index >= 0 && (index + 1) < result.length()) {
162 result = result.substring(index + 1);
163 index = result.lastIndexOf(')');
164 result = result.substring(0, index);
165 }
166 }
167 return result;
168 }
169 }