001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: package org.apache.servicemix.jbi.util;
018:
019: import java.io.UnsupportedEncodingException;
020: import java.net.URI;
021: import java.net.URISyntaxException;
022: import java.net.URLDecoder;
023: import java.net.URLEncoder;
024: import java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: /**
032: * @version $Revision$
033: */
034: public class URISupport {
035:
036: public static class CompositeData {
037: String scheme;
038:
039: String path;
040:
041: URI components[];
042:
043: Map parameters;
044:
045: String fragment;
046:
047: String host;
048:
049: public URI[] getComponents() {
050: return components;
051: }
052:
053: public String getFragment() {
054: return fragment;
055: }
056:
057: public Map getParameters() {
058: return parameters;
059: }
060:
061: public String getScheme() {
062: return scheme;
063: }
064:
065: public String getPath() {
066: return path;
067: }
068:
069: public String getHost() {
070: return host;
071: }
072:
073: public URI toURI() throws URISyntaxException {
074: StringBuffer sb = new StringBuffer();
075: if (scheme != null) {
076: sb.append(scheme);
077: sb.append(':');
078: }
079:
080: if (host != null && host.length() != 0) {
081: sb.append(host);
082: } else {
083: sb.append('(');
084: for (int i = 0; i < components.length; i++) {
085: if (i != 0) {
086: sb.append(',');
087: }
088: sb.append(components[i].toString());
089: }
090: sb.append(')');
091: }
092:
093: if (path != null) {
094: sb.append('/');
095: sb.append(path);
096: }
097: if (!parameters.isEmpty()) {
098: sb.append("?");
099: sb.append(createQueryString(parameters));
100: }
101: if (fragment != null) {
102: sb.append("#");
103: sb.append(fragment);
104: }
105: return new URI(sb.toString());
106: }
107: }
108:
109: public static Map parseQuery(String uri) throws URISyntaxException {
110: try {
111: Map rc = new HashMap();
112: if (uri != null) {
113: String[] parameters = uri.split("&");
114: for (int i = 0; i < parameters.length; i++) {
115: int p = parameters[i].indexOf("=");
116: if (p >= 0) {
117: String name = URLDecoder.decode(parameters[i]
118: .substring(0, p), "UTF-8");
119: String value = URLDecoder.decode(parameters[i]
120: .substring(p + 1), "UTF-8");
121: rc.put(name, value);
122: } else {
123: rc.put(parameters[i], null);
124: }
125: }
126: }
127: return rc;
128: } catch (UnsupportedEncodingException e) {
129: throw (URISyntaxException) new URISyntaxException(e
130: .toString(), "Invalid encoding").initCause(e);
131: }
132: }
133:
134: public static Map parseParamters(URI uri) throws URISyntaxException {
135: return uri.getQuery() == null ? Collections.EMPTY_MAP
136: : parseQuery(stripPrefix(uri.getQuery(), "?"));
137: }
138:
139: /**
140: * Removes any URI query from the given uri
141: */
142: public static URI removeQuery(URI uri) throws URISyntaxException {
143: return createURIWithQuery(uri, null);
144: }
145:
146: /**
147: * Creates a URI with the given query
148: */
149: public static URI createURIWithQuery(URI uri, String query)
150: throws URISyntaxException {
151: return new URI(uri.getScheme(), uri.getUserInfo(), uri
152: .getHost(), uri.getPort(), uri.getPath(), query, uri
153: .getFragment());
154: }
155:
156: public static CompositeData parseComposite(URI uri)
157: throws URISyntaxException {
158:
159: CompositeData rc = new CompositeData();
160: rc.scheme = uri.getScheme();
161: String ssp = stripPrefix(uri.getSchemeSpecificPart().trim(),
162: "//").trim();
163:
164: parseComposite(uri, rc, ssp);
165:
166: rc.fragment = uri.getFragment();
167: return rc;
168: }
169:
170: /**
171: * @param uri
172: * @param rc
173: * @param ssp
174: * @param p
175: * @throws URISyntaxException
176: */
177: private static void parseComposite(URI uri, CompositeData rc,
178: String ssp) throws URISyntaxException {
179: String componentString;
180: String params;
181:
182: if (!checkParenthesis(ssp)) {
183: throw new URISyntaxException(uri.toString(),
184: "Not a matching number of '(' and ')' parenthesis");
185: }
186:
187: int p;
188: int intialParen = ssp.indexOf("(");
189: if (intialParen == 0) {
190: rc.host = ssp.substring(0, intialParen);
191: p = rc.host.indexOf("/");
192: if (p >= 0) {
193: rc.path = rc.host.substring(p);
194: rc.host = rc.host.substring(0, p);
195: }
196: p = ssp.lastIndexOf(")");
197: componentString = ssp.substring(intialParen + 1, p);
198: params = ssp.substring(p + 1).trim();
199:
200: } else {
201: componentString = ssp;
202: params = "";
203: }
204:
205: String components[] = splitComponents(componentString);
206: rc.components = new URI[components.length];
207: for (int i = 0; i < components.length; i++) {
208: rc.components[i] = new URI(components[i].trim());
209: }
210:
211: p = params.indexOf("?");
212: if (p >= 0) {
213: if (p > 0) {
214: rc.path = stripPrefix(params.substring(0, p), "/");
215: }
216: rc.parameters = parseQuery(params.substring(p + 1));
217: } else {
218: if (params.length() > 0) {
219: rc.path = stripPrefix(params, "/");
220: }
221: rc.parameters = Collections.EMPTY_MAP;
222: }
223: }
224:
225: /**
226: * @param componentString
227: * @return
228: */
229: private static String[] splitComponents(String str) {
230: List<String> l = new ArrayList<String>();
231:
232: int last = 0;
233: int depth = 0;
234: char chars[] = str.toCharArray();
235: for (int i = 0; i < chars.length; i++) {
236: switch (chars[i]) {
237: case '(':
238: depth++;
239: break;
240: case ')':
241: depth--;
242: break;
243: case ',':
244: if (depth == 0) {
245: String s = str.substring(last, i);
246: l.add(s);
247: last = i + 1;
248: }
249: break;
250: default:
251: break;
252: }
253: }
254:
255: String s = str.substring(last);
256: if (s.length() != 0) {
257: l.add(s);
258: }
259:
260: String rc[] = new String[l.size()];
261: l.toArray(rc);
262: return rc;
263: }
264:
265: public static String stripPrefix(String value, String prefix) {
266: if (value.startsWith(prefix)) {
267: return value.substring(prefix.length());
268: }
269: return value;
270: }
271:
272: public static URI stripScheme(URI uri) throws URISyntaxException {
273: return new URI(stripPrefix(uri.getSchemeSpecificPart().trim(),
274: "//"));
275: }
276:
277: public static String createQueryString(Map options)
278: throws URISyntaxException {
279: try {
280: if (options.size() > 0) {
281: StringBuffer rc = new StringBuffer();
282: boolean first = true;
283: for (Iterator iter = options.keySet().iterator(); iter
284: .hasNext();) {
285: if (first) {
286: first = false;
287: } else {
288: rc.append("&");
289: }
290: String key = (String) iter.next();
291: String value = (String) options.get(key);
292: rc.append(URLEncoder.encode(key, "UTF-8"));
293: rc.append("=");
294: rc.append(URLEncoder.encode(value, "UTF-8"));
295: }
296: return rc.toString();
297: } else {
298: return "";
299: }
300: } catch (UnsupportedEncodingException e) {
301: throw (URISyntaxException) new URISyntaxException(e
302: .toString(), "Invalid encoding").initCause(e);
303: }
304: }
305:
306: /**
307: * Creates a URI from the original URI and the remaining paramaters
308: *
309: * @throws URISyntaxException
310: */
311: public static URI createRemainingURI(URI originalURI, Map params)
312: throws URISyntaxException {
313: String s = createQueryString(params);
314: if (s.length() == 0) {
315: s = null;
316: }
317: return createURIWithQuery(originalURI, s);
318: }
319:
320: public static URI changeScheme(URI bindAddr, String scheme)
321: throws URISyntaxException {
322: return new URI(scheme, bindAddr.getUserInfo(), bindAddr
323: .getHost(), bindAddr.getPort(), bindAddr.getPath(),
324: bindAddr.getQuery(), bindAddr.getFragment());
325: }
326:
327: public static boolean checkParenthesis(String str) {
328: boolean result = true;
329: if (str != null) {
330: int open = 0;
331: int closed = 0;
332:
333: int i = str.indexOf('(');
334: while (i >= 0) {
335: open++;
336: i = str.indexOf('(', i + 1);
337: }
338: i = str.indexOf(')');
339: while (i >= 0) {
340: closed++;
341: i = str.indexOf(')', i + 1);
342: }
343: result = open == closed;
344: }
345: return result;
346: }
347:
348: public int indexOfParenthesisMatch(String str) {
349: int result = -1;
350:
351: return result;
352: }
353:
354: }
|