Source Code Cross Referenced for UriResolver.java in  » J2EE » openejb3 » org » apache » openejb » core » webservices » 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 » J2EE » openejb3 » org.apache.openejb.core.webservices 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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