001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.wsdlextensions.jms.validator;
021:
022: import java.util.Properties;
023: import java.util.StringTokenizer;
024: import java.net.URLDecoder;
025: import java.io.UnsupportedEncodingException;
026:
027: /**
028: * A generic parser for the following pattern:
029: * protocol://host[:port][/service][/path]
030: * service :=
031: * path := file?query | ?query | file
032: */
033: public class SunOneConnectionUrl extends ConnectionUrl {
034: private String mUrl;
035: private boolean mParsed;
036: private String mProtocol;
037: private int mPort;
038: private String mHost;
039: private String mService;
040: private String mPath;
041: private String mFile;
042: private String mQuery;
043:
044: /**
045: * Constructor
046: *
047: * @param url String
048: */
049: public SunOneConnectionUrl(String url) {
050: mUrl = url;
051: }
052:
053: private void parse() throws ValidationException {
054: if (mParsed) {
055: return;
056: }
057:
058: String r = mUrl;
059: mParsed = true;
060:
061: // Protocol
062: int i = r.indexOf("://");
063: if (i < 0) {
064: throw new ValidationException("Invalid URL [" + mUrl
065: + "]: no protocol specified");
066: }
067: mProtocol = r.substring(0, i);
068: r = r.substring(i + "://".length());
069:
070: // host[:port]
071: i = r.indexOf('/');
072: if (i < 0) {
073: i = r.indexOf('?');
074: }
075: String server = i >= 0 ? r.substring(0, i) : r;
076: r = i >= 0 ? r.substring(i) : "";
077:
078: i = server.indexOf(':');
079: if (i >= 0) {
080: mHost = server.substring(0, i);
081: String port = server.substring(i + 1);
082: if (port.length() > 0) {
083: mPort = Integer.parseInt(port);
084: } else {
085: mPort = -1;
086: }
087: } else {
088: mHost = server;
089: mPort = -1;
090: }
091:
092: // service
093: int tunnel = r.indexOf("/tunnel");
094: if (r.length() > 0 && tunnel != -1) {
095: mService = r.substring(0, tunnel + "/tunnel".length());
096: r = r.substring(tunnel + "/tunnel".length());
097: } else if (r.length() > 0 && r.startsWith("/ssljms")) {
098: mService = "ssljms";
099: r = r.substring("/ssljms".length());
100: } else if (r.length() > 0 && r.startsWith("/jms")) {
101: mService = "jms";
102: r = r.substring("jms".length());
103: } else {
104: mService = "";
105: }
106:
107: // path
108: if (r.length() > 0) {
109: mFile = r.substring(0);
110: } else {
111: mFile = "";
112: }
113:
114: // file
115: if (!r.startsWith("/")) {
116: mPath = "";
117: } else {
118: i = r.indexOf('?');
119: if (i >= 0) {
120: mPath = r.substring(0, i);
121: r = r.substring(i);
122: } else {
123: mPath = r;
124: r = "";
125: }
126: }
127:
128: // query
129: if (r.startsWith("?")) {
130: mQuery = r.substring(1);
131: }
132: }
133:
134: /**
135: * Changes the port
136: *
137: * @param port int
138: */
139: public void setPort(int port) throws ValidationException {
140: parse();
141: mPort = port;
142: mUrl = null;
143: }
144:
145: /**
146: * Changes the server portion
147: *
148: * @param host String
149: */
150: public void setHost(String host) throws ValidationException {
151: parse();
152: mHost = host;
153: mUrl = null;
154: }
155:
156: /**
157: * Changes the server connection service
158: *
159: * @param host String
160: */
161: public void setService(String service) throws ValidationException {
162: parse();
163: mService = service;
164: mUrl = null;
165: }
166:
167: /**
168: * Returns the URL in full string form
169: *
170: * @return String
171: */
172: public String toString() {
173: if (mUrl == null) {
174: StringBuffer url = new StringBuffer();
175: url.append(mProtocol).append("://").append(mHost);
176: if (mPort != -1) {
177: url.append(":").append(mPort);
178: }
179: if (!"".equals(mService)) {
180: url.append("/").append(mService);
181: }
182: url.append(mFile);
183: mUrl = url.toString();
184: }
185: return mUrl;
186: }
187:
188: /**
189: * Gets the protocol name of this <code>URL</code>.
190: *
191: * @return the protocol of this <code>URL</code>.
192: */
193: public String getProtocol() throws ValidationException {
194: parse();
195: return mProtocol;
196: }
197:
198: /**
199: * Gets the host name of this <code>URL</code>
200: *
201: * @return the host name of this <code>URL</code>.
202: */
203: public String getHost() throws ValidationException {
204: parse();
205: return mHost;
206: }
207:
208: /**
209: * Gets the port number of this <code>URL</code>.
210: *
211: * @return the port number, or -1 if the port is not set
212: */
213: public int getPort() throws ValidationException {
214: parse();
215: return mPort;
216: }
217:
218: /**
219: * Gets the connection service of this <code>URL</code>.
220: *
221: * @return the connection service name
222: */
223: public String getService() throws ValidationException {
224: parse();
225: return mService;
226: }
227:
228: /**
229: * Gets the file name of this <code>URL</code>.
230: * The returned file portion will be
231: * the same as <CODE>getPath()</CODE>, plus the concatenation of
232: * the value of <CODE>getQuery()</CODE>, if any. If there is
233: * no query portion, this method and <CODE>getPath()</CODE> will
234: * return identical results.
235: *
236: * @return the file name of this <code>URL</code>,
237: * or an empty string if one does not exist
238: */
239: public String getFile() throws ValidationException {
240: parse();
241: return mFile;
242: }
243:
244: /**
245: * Gets the path part of this <code>URL</code>.
246: *
247: * @return the path part of this <code>URL</code>, or an
248: * empty string if one does not exist
249: */
250: public String getPath() throws ValidationException {
251: parse();
252: return mPath;
253: }
254:
255: /**
256: * Gets the query part of this <code>URL</code>.
257: *
258: * @return the query part of this <code>URL</code>,
259: * or <CODE>null</CODE> if one does not exist
260: */
261: public String getQuery() throws ValidationException {
262: parse();
263: return mQuery;
264: }
265:
266: /**
267: * Extracts the key value pairs from the query string
268: *
269: * @param toAddTo Properties key-value pairs will be added to this properties set
270: */
271: public void getQueryProperties(Properties toAddTo)
272: throws ValidationException {
273: if (mUrl == null) {
274: return;
275: }
276: String q = getQuery();
277: getQueryProperties(q, toAddTo);
278: }
279:
280: /**
281: * Tool function: queries a query string and adds the key/value pairs to the specified
282: * properties map
283: *
284: * @param q String
285: * @param toAddTo Properties
286: */
287: public static void getQueryProperties(String q, Properties toAddTo)
288: throws ValidationException {
289: if (q == null || q.length() == 0) {
290: return;
291: }
292: for (StringTokenizer iter = new StringTokenizer(q, "&"); iter
293: .hasMoreElements();/*-*/) {
294: String pair = (String) iter.nextToken();
295: int split = pair.indexOf('=');
296: if (split <= 0) {
297: throw new ValidationException("Invalid pair [" + pair
298: + "] in query string [" + q + "]");
299: } else {
300: String key = pair.substring(0, split);
301: String value = pair.substring(split + 1);
302: try {
303: key = URLDecoder.decode(key, "UTF-8");
304: value = URLDecoder.decode(value, "UTF-8");
305: } catch (UnsupportedEncodingException e) {
306: throw new ValidationException(
307: "Invalid encoding in [" + pair
308: + "] in query string [" + q + "]",
309: e);
310: }
311: toAddTo.setProperty(key, value);
312: }
313: }
314: }
315: }
|