001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.resource;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.net.HttpURLConnection;
026: import java.net.MalformedURLException;
027: import java.net.URI;
028: import java.net.URISyntaxException;
029: import java.net.URL;
030:
031: import org.apache.cxf.common.classloader.ClassLoaderUtils;
032: import org.apache.cxf.common.util.Base64Utility;
033:
034: /**
035: * Resolves a File, classpath resource, or URL according to the follow rules:
036: * <ul>
037: * <li>Check to see if a file exists, relative to the base URI.</li>
038: * <li>If the file doesn't exist, check the classpath</li>
039: * <li>If the classpath doesn't exist, try to create URL from the URI.</li>
040: * </ul>
041: *
042: * @author Dan Diephouse
043: */
044: public class URIResolver {
045: private File file;
046: private URI uri;
047: private URL url;
048: private InputStream is;
049: private Class calling;
050:
051: public URIResolver() {
052: }
053:
054: public URIResolver(String path) throws IOException {
055: this ("", path);
056: }
057:
058: public URIResolver(String baseUriStr, String uriStr)
059: throws IOException {
060: this (baseUriStr, uriStr, null);
061: }
062:
063: public URIResolver(String baseUriStr, String uriStr, Class calling)
064: throws IOException {
065: this .calling = (calling != null) ? calling : getClass();
066: if (uriStr.startsWith("classpath:")) {
067: tryClasspath(uriStr);
068: } else if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
069: tryJar(baseUriStr, uriStr);
070: } else if (uriStr.startsWith("jar:")) {
071: tryJar(uriStr);
072: } else {
073: tryFileSystem(baseUriStr, uriStr);
074: }
075: }
076:
077: public void resolve(String baseUriStr, String uriStr,
078: Class callingCls) throws IOException {
079: this .calling = (callingCls != null) ? callingCls : getClass();
080: this .file = null;
081: this .uri = null;
082:
083: this .is = null;
084:
085: if (uriStr.startsWith("classpath:")) {
086: tryClasspath(uriStr);
087: } else if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
088: tryJar(baseUriStr, uriStr);
089: } else if (uriStr.startsWith("jar:")) {
090: tryJar(uriStr);
091: } else {
092: tryFileSystem(baseUriStr, uriStr);
093: }
094: }
095:
096: private void tryFileSystem(String baseUriStr, String uriStr)
097: throws IOException, MalformedURLException {
098: try {
099: URI relative;
100: File uriFile = new File(uriStr);
101: uriFile = new File(uriFile.getAbsolutePath());
102:
103: if (uriFile.exists()) {
104: relative = uriFile.toURI();
105: } else {
106: relative = new URI(uriStr.replaceAll(" ", "%20"));
107: }
108:
109: if (relative.isAbsolute()) {
110: uri = relative;
111: url = relative.toURL();
112:
113: try {
114: HttpURLConnection huc = (HttpURLConnection) url
115: .openConnection();
116:
117: String host = System.getProperty("http.proxyHost");
118: if (host != null) {
119: //comment out unused port to pass pmd check
120: /*String ports = System.getProperty("http.proxyPort");
121: int port = 80;
122: if (ports != null) {
123: port = Integer.parseInt(ports);
124: }*/
125:
126: String username = System
127: .getProperty("http.proxy.user");
128: String password = System
129: .getProperty("http.proxy.password");
130:
131: if (username != null && password != null) {
132: String encoded = Base64Utility
133: .encode((username + ":" + password)
134: .getBytes());
135: huc.setRequestProperty(
136: "Proxy-Authorization", "Basic "
137: + encoded);
138: }
139: }
140: is = huc.getInputStream();
141: } catch (ClassCastException ex) {
142: is = url.openStream();
143: }
144: } else if (baseUriStr != null) {
145: URI base;
146: File baseFile = new File(baseUriStr);
147:
148: if (!baseFile.exists()
149: && baseUriStr.startsWith("file:/")) {
150: baseFile = new File(baseUriStr.substring(6));
151: }
152:
153: if (baseFile.exists()) {
154: base = baseFile.toURI();
155: } else {
156: base = new URI(baseUriStr);
157: }
158:
159: base = base.resolve(relative);
160: if (base.isAbsolute()) {
161: try {
162: baseFile = new File(base);
163: if (baseFile.exists()) {
164: is = base.toURL().openStream();
165: uri = base;
166: } else {
167: tryClasspath(base.toString().startsWith(
168: "file:") ? base.toString()
169: .substring(5) : base.toString());
170: }
171: } catch (Throwable th) {
172: tryClasspath(base.toString()
173: .startsWith("file:") ? base.toString()
174: .substring(5) : base.toString());
175: }
176: }
177: }
178: } catch (URISyntaxException e) {
179: // do nothing
180: }
181:
182: if (uri != null && "file".equals(uri.getScheme())) {
183: try {
184: file = new File(uri);
185: } catch (IllegalArgumentException iae) {
186: file = new File(uri.toURL().getPath());
187: if (!file.exists()) {
188: file = null;
189: }
190: }
191: }
192:
193: if (is == null && file != null && file.exists()) {
194: uri = file.toURI();
195: try {
196: is = new FileInputStream(file);
197: } catch (FileNotFoundException e) {
198: throw new RuntimeException("File was deleted! "
199: + uriStr, e);
200: }
201: url = file.toURI().toURL();
202: } else if (is == null) {
203: tryClasspath(uriStr);
204: }
205: }
206:
207: private void tryJar(String baseStr, String uriStr)
208: throws IOException {
209: int i = baseStr.indexOf('!');
210: if (i == -1) {
211: tryFileSystem(baseStr, uriStr);
212: }
213:
214: String jarBase = baseStr.substring(0, i + 1);
215: String jarEntry = baseStr.substring(i + 1);
216: try {
217: URI u = new URI(jarEntry).resolve(uriStr);
218:
219: tryJar(jarBase + u.toString());
220:
221: if (is != null) {
222: if (u.isAbsolute()) {
223: url = u.toURL();
224: }
225: return;
226: }
227: } catch (URISyntaxException e) {
228: // do nothing
229: }
230:
231: tryFileSystem("", uriStr);
232: }
233:
234: private void tryJar(String uriStr) throws IOException {
235: int i = uriStr.indexOf('!');
236: if (i == -1) {
237: return;
238: }
239:
240: url = new URL(uriStr);
241: try {
242: is = url.openStream();
243: try {
244: uri = url.toURI();
245: } catch (URISyntaxException ex) {
246: // ignore
247: }
248: } catch (IOException e) {
249: uriStr = uriStr.substring(i + 1);
250: tryClasspath(uriStr);
251: }
252: }
253:
254: private void tryClasspath(String uriStr) throws IOException {
255: if (uriStr.startsWith("classpath:")) {
256: uriStr = uriStr.substring(10);
257: }
258: url = ClassLoaderUtils.getResource(uriStr, calling);
259: if (url == null) {
260: tryRemote(uriStr);
261: } else {
262: try {
263: uri = url.toURI();
264: } catch (URISyntaxException e) {
265: // processing the jar:file:/ type value
266: String urlStr = url.toString();
267: if (urlStr.startsWith("jar:")) {
268: int pos = urlStr.indexOf('!');
269: if (pos != -1) {
270: try {
271: uri = new URI("classpath:"
272: + urlStr.substring(pos + 1));
273: } catch (URISyntaxException ue) {
274: // ignore
275: }
276: }
277: }
278:
279: }
280: is = url.openStream();
281: }
282: }
283:
284: private void tryRemote(String uriStr) throws IOException {
285: try {
286: url = new URL(uriStr);
287: uri = new URI(url.toString());
288: is = url.openStream();
289: } catch (MalformedURLException e) {
290: // do nothing
291: } catch (URISyntaxException e) {
292: // do nothing
293: }
294: }
295:
296: public URI getURI() {
297: return uri;
298: }
299:
300: public URL getURL() {
301: return url;
302: }
303:
304: public InputStream getInputStream() {
305: return is;
306: }
307:
308: public boolean isFile() {
309: if (file != null) {
310: return file.exists();
311: }
312: return false;
313: }
314:
315: public File getFile() {
316: return file;
317: }
318:
319: public boolean isResolved() {
320: return is != null;
321: }
322: }
|