Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » webservices » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.core.webservices;
   19   
   20   import org.apache.openejb.util.Base64;
   21   
   22   import java.io.File;
   23   import java.io.FileInputStream;
   24   import java.io.FileNotFoundException;
   25   import java.io.IOException;
   26   import java.io.InputStream;
   27   import java.net.HttpURLConnection;
   28   import java.net.MalformedURLException;
   29   import java.net.URI;
   30   import java.net.URISyntaxException;
   31   import java.net.URL;
   32   
   33   /**
   34    * Resolves a File, classpath resource, or URL according to the follow rules:
   35    * <ul>
   36    * <li>Check to see if a file exists, relative to the base URI.</li>
   37    * <li>If the file doesn't exist, check the classpath</li>
   38    * <li>If the classpath doesn't exist, try to create URL from the URI.</li>
   39    * </ul>
   40    */
   41   // Imported from CXF
   42   public class UriResolver {
   43       private File file;
   44       private URI uri;
   45       private URL url;
   46       private InputStream is;
   47       private Class calling;
   48   
   49       public UriResolver() {
   50       }
   51   
   52       public UriResolver(String path) throws IOException {
   53           this("", path);
   54       }
   55   
   56       public UriResolver(String baseUriStr, String uriStr) throws IOException {
   57           this(baseUriStr, uriStr, null);
   58       }
   59   
   60       public UriResolver(String baseUriStr, String uriStr, Class calling) throws IOException {
   61           this.calling = (calling != null) ? calling : getClass();
   62           if (uriStr.startsWith("classpath:")) {
   63               tryClasspath(uriStr);
   64           } else if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
   65               tryJar(baseUriStr, uriStr);
   66           } else if (uriStr.startsWith("jar:")) {
   67               tryJar(uriStr);
   68           } else {
   69               tryFileSystem(baseUriStr, uriStr);
   70           }
   71       }
   72   
   73   
   74       public void resolve(String baseUriStr, String uriStr, Class callingCls) throws IOException {
   75           this.calling = (callingCls != null) ? callingCls : getClass();
   76           this.file = null;
   77           this.uri = null;
   78   
   79           this.is = null;
   80   
   81           if (uriStr.startsWith("classpath:")) {
   82               tryClasspath(uriStr);
   83           } else if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
   84               tryJar(baseUriStr, uriStr);
   85           } else if (uriStr.startsWith("jar:")) {
   86               tryJar(uriStr);
   87           } else {
   88               tryFileSystem(baseUriStr, uriStr);
   89           }
   90       }
   91   
   92   
   93   
   94       private void tryFileSystem(String baseUriStr, String uriStr) throws IOException, MalformedURLException {
   95           try {
   96               URI relative;
   97               File uriFile = new File(uriStr);
   98               uriFile = new File(uriFile.getAbsolutePath());
   99   
  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.openConnection();
  112   
  113                       String host = System.getProperty("http.proxyHost");
  114                       if (host != null) {
  115                           //comment out unused port to pass pmd check
  116                           /*String ports = System.getProperty("http.proxyPort");
  117                           int port = 80;
  118                           if (ports != null) {
  119                               port = Integer.parseInt(ports);
  120                           }*/
  121   
  122                           String username = System.getProperty("http.proxy.user");
  123                           String password = System.getProperty("http.proxy.password");
  124   
  125                           if (username != null && password != null) {
  126                               String encoded = new String(Base64.encodeBase64((username + ":" + password).getBytes()));
  127                               huc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
  128                           }
  129                       }
  130                       is =  huc.getInputStream();
  131                   } catch (ClassCastException ex) {
  132                       is = url.openStream();
  133                   }
  134               } else if (baseUriStr != null) {
  135                   URI base;
  136                   File baseFile = new File(baseUriStr);
  137   
  138                   if (!baseFile.exists() && baseUriStr.startsWith("file:/")) {
  139                       baseFile = new File(baseUriStr.substring(6));
  140                   }
  141   
  142                   if (baseFile.exists()) {
  143                       base = baseFile.toURI();
  144                   } else {
  145                       base = new URI(baseUriStr);
  146                   }
  147   
  148                   base = base.resolve(relative);
  149                   if (base.isAbsolute()) {
  150                       try {
  151                           baseFile = new File(base);
  152                           if (baseFile.exists()) {
  153                               is = base.toURL().openStream();
  154                               uri = base;
  155                           } else {
  156                               tryClasspath(base.toString().startsWith("file:")
  157                                            ? base.toString().substring(5) : base.toString());
  158                           }
  159                       } catch (Throwable th) {
  160                           tryClasspath(base.toString().startsWith("file:")
  161                                        ? base.toString().substring(5) : base.toString());
  162                       }
  163                   }
  164               }
  165           } catch (URISyntaxException e) {
  166               // do nothing
  167           }
  168   
  169           if (uri != null && "file".equals(uri.getScheme())) {
  170               try {
  171                   file = new File(uri);
  172               } catch (IllegalArgumentException iae) {
  173                   file = org.apache.openejb.util.URLs.toFile(uri.toURL());
  174                   if (!file.exists()) {
  175                       file = null;
  176                   }
  177               }
  178           }
  179   
  180           if (is == null && file != null && file.exists()) {
  181               uri = file.toURI();
  182               try {
  183                   is = new FileInputStream(file);
  184               } catch (FileNotFoundException e) {
  185                   throw new RuntimeException("File was deleted! " + uriStr, e);
  186               }
  187               url = file.toURI().toURL();
  188           } else if (is == null) {
  189               tryClasspath(uriStr);
  190           }
  191       }
  192   
  193       private void tryJar(String baseStr, String uriStr) throws IOException {
  194           int i = baseStr.indexOf('!');
  195           if (i == -1) {
  196               tryFileSystem(baseStr, uriStr);
  197           }
  198   
  199           String jarBase = baseStr.substring(0, i + 1);
  200           String jarEntry = baseStr.substring(i + 1);
  201           try {
  202               URI u = new URI(jarEntry).resolve(uriStr);
  203   
  204               tryJar(jarBase + u.toString());
  205   
  206               if (is != null) {
  207                   if (u.isAbsolute()) {
  208                       url = u.toURL();
  209                   }
  210                   return;
  211               }
  212           } catch (URISyntaxException e) {
  213               // do nothing
  214           }
  215   
  216           tryFileSystem("", uriStr);
  217       }
  218   
  219       private void tryJar(String uriStr) throws IOException {
  220           int i = uriStr.indexOf('!');
  221           if (i == -1) {
  222               return;
  223           }
  224   
  225           url = new URL(uriStr);
  226           try {
  227               is = url.openStream();
  228               try {
  229                   uri = url.toURI();
  230               } catch (URISyntaxException ex) {
  231                   // ignore
  232               }
  233           } catch (IOException e) {
  234               uriStr = uriStr.substring(i + 1);
  235               tryClasspath(uriStr);
  236           }
  237       }
  238   
  239       private void tryClasspath(String uriStr) throws IOException {
  240           if (uriStr.startsWith("classpath:")) {
  241               uriStr = uriStr.substring(10);
  242           }
  243           url = getResource(uriStr, calling);
  244           if (url == null) {
  245               tryRemote(uriStr);
  246           } else {
  247               try {
  248                   uri = url.toURI();
  249               } catch (URISyntaxException e) {
  250                   // processing the jar:file:/ type value
  251                   String urlStr = url.toString();
  252                   if (urlStr.startsWith("jar:")) {
  253                       int pos = urlStr.indexOf('!');
  254                       if (pos != -1) {
  255                           try {
  256                               uri = new URI("classpath:" + urlStr.substring(pos + 1));
  257                           } catch (URISyntaxException ue) {
  258                               // ignore
  259                           }
  260                       }
  261                   }
  262   
  263               }
  264               is = url.openStream();
  265           }
  266       }
  267   
  268       private void tryRemote(String uriStr) throws IOException {
  269           try {
  270               url = new URL(uriStr);
  271               uri = new URI(url.toString());
  272               is = url.openStream();
  273           } catch (MalformedURLException e) {
  274               // do nothing
  275           } catch (URISyntaxException e) {
  276               // do nothing
  277           }
  278       }
  279   
  280       public URI getURI() {
  281           return uri;
  282       }
  283   
  284       public URL getURL() {
  285           return url;
  286       }
  287   
  288       public InputStream getInputStream() {
  289           return is;
  290       }
  291   
  292       public boolean isFile() {
  293           return file != null && file.exists();
  294       }
  295   
  296       public File getFile() {
  297           return file;
  298       }
  299   
  300       public boolean isResolved() {
  301           return is != null;
  302       }
  303   
  304       /**
  305        * Load a given resource. <p/> This method will try to load the resource
  306        * using the following methods (in order):
  307        * <ul>
  308        * <li>From Thread.currentThread().getContextClassLoader()
  309        * <li>From ClassLoaderUtil.class.getClassLoader()
  310        * <li>callingClass.getClassLoader()
  311        * </ul>
  312        *
  313        * @param resourceName The name of the resource to load
  314        * @param callingClass The Class object of the calling object
  315        */
  316       public static URL getResource(String resourceName, Class callingClass) {
  317           URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
  318   
  319           if (url == null) {
  320               url = UriResolver.class.getClassLoader().getResource(resourceName);
  321           }
  322   
  323           if (url == null) {
  324               ClassLoader cl = callingClass.getClassLoader();
  325   
  326               if (cl != null) {
  327                   url = cl.getResource(resourceName);
  328               }
  329           }
  330   
  331           if (url == null) {
  332               url = callingClass.getResource(resourceName);
  333           }
  334   
  335           if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) {
  336               return getResource('/' + resourceName, callingClass);
  337           }
  338   
  339           return url;
  340       }
  341   }

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » webservices » [javadoc | source]