Source Code Cross Referenced for URIResolver.java in  » Web-Services-apache-cxf-2.0.1 » common » org » apache » cxf » resource » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services apache cxf 2.0.1 » common » org.apache.cxf.resource 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.