|
1 /* |
|
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.util; |
|
27 |
|
28 import java.util.UUID; |
|
29 import java.util.regex.Pattern; |
|
30 import java.net.URL; |
|
31 import java.net.MalformedURLException; |
|
32 import java.net.URI; |
|
33 import java.net.URISyntaxException; |
|
34 import java.io.File; |
|
35 import java.io.IOException; |
|
36 |
|
37 import javax.xml.namespace.QName; |
|
38 |
|
39 /** |
|
40 * @author Vivek Pandey |
|
41 * |
|
42 * Wrapper utility class to be used from the generated code or run time. |
|
43 */ |
|
44 public final class JAXWSUtils { |
|
45 public static String getUUID(){ |
|
46 return UUID.randomUUID().toString(); |
|
47 } |
|
48 |
|
49 |
|
50 |
|
51 public static String getFileOrURLName(String fileOrURL) { |
|
52 try{ |
|
53 try { |
|
54 return escapeSpace(new URL(fileOrURL).toExternalForm()); |
|
55 } catch (MalformedURLException e) { |
|
56 return new File(fileOrURL).getCanonicalFile().toURL().toExternalForm(); |
|
57 } |
|
58 } catch (Exception e) { |
|
59 // try it as an URL |
|
60 return fileOrURL; |
|
61 } |
|
62 } |
|
63 |
|
64 public static URL getFileOrURL(String fileOrURL) throws IOException { |
|
65 try { |
|
66 URL url = new URL(fileOrURL); |
|
67 String scheme = String.valueOf(url.getProtocol()).toLowerCase(); |
|
68 if (scheme.equals("http") || scheme.equals("https")) |
|
69 return new URL(url.toURI().toASCIIString()); |
|
70 return url; |
|
71 } catch (URISyntaxException e) { |
|
72 return new File(fileOrURL).toURL(); |
|
73 } catch (MalformedURLException e) { |
|
74 return new File(fileOrURL).toURL(); |
|
75 } |
|
76 } |
|
77 |
|
78 public static URL getEncodedURL(String urlStr) throws MalformedURLException { |
|
79 URL url = new URL(urlStr); |
|
80 String scheme = String.valueOf(url.getProtocol()).toLowerCase(); |
|
81 if (scheme.equals("http") || scheme.equals("https")) { |
|
82 try { |
|
83 return new URL(url.toURI().toASCIIString()); |
|
84 } catch (URISyntaxException e) { |
|
85 MalformedURLException malformedURLException = new MalformedURLException(e.getMessage()); |
|
86 malformedURLException.initCause(e); |
|
87 throw malformedURLException; |
|
88 } |
|
89 } |
|
90 return url; |
|
91 } |
|
92 |
|
93 private static String escapeSpace( String url ) { |
|
94 // URLEncoder didn't work. |
|
95 StringBuilder buf = new StringBuilder(); |
|
96 for (int i = 0; i < url.length(); i++) { |
|
97 // TODO: not sure if this is the only character that needs to be escaped. |
|
98 if (url.charAt(i) == ' ') |
|
99 buf.append("%20"); |
|
100 else |
|
101 buf.append(url.charAt(i)); |
|
102 } |
|
103 return buf.toString(); |
|
104 } |
|
105 |
|
106 public static String absolutize(String name) { |
|
107 // absolutize all the system IDs in the input, |
|
108 // so that we can map system IDs to DOM trees. |
|
109 try { |
|
110 URL baseURL = new File(".").getCanonicalFile().toURL(); |
|
111 return new URL(baseURL, name).toExternalForm(); |
|
112 } catch( IOException e) { |
|
113 //ignore |
|
114 } |
|
115 return name; |
|
116 } |
|
117 |
|
118 /** |
|
119 * Checks if the system ID is absolute. |
|
120 */ |
|
121 @SuppressWarnings("ResultOfObjectAllocationIgnored") |
|
122 public static void checkAbsoluteness(String systemId) { |
|
123 // we need to be able to handle system IDs like "urn:foo", which java.net.URL can't process, |
|
124 // but OTOH we also need to be able to process system IDs like "file://a b c/def.xsd", |
|
125 // which java.net.URI can't process. So for now, let's fail only if both of them fail. |
|
126 // eventually we need a proper URI class that works for us. |
|
127 try { |
|
128 new URL(systemId); |
|
129 } catch( MalformedURLException mue) { |
|
130 try { |
|
131 new URI(systemId); |
|
132 } catch (URISyntaxException e) { |
|
133 throw new IllegalArgumentException("system ID '"+systemId+"' isn't absolute",e); |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 /* |
|
139 * To match, both QNames must have the same namespace and the local |
|
140 * part of the target must match the local part of the 'pattern' |
|
141 * QName, which may contain wildcard characters. |
|
142 */ |
|
143 public static boolean matchQNames(QName target, QName pattern) { |
|
144 if ((target == null) || (pattern == null)) { |
|
145 // if no service or port is in descriptor |
|
146 return false; |
|
147 } |
|
148 if (pattern.getNamespaceURI().equals(target.getNamespaceURI())) { |
|
149 String regex = pattern.getLocalPart().replaceAll("\\*", ".*"); |
|
150 return Pattern.matches(regex, target.getLocalPart()); |
|
151 } |
|
152 return false; |
|
153 } |
|
154 |
|
155 } |