View Javadoc

1   /*
2    * @(#) URLResource.java Jun 17, 2004 Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628
3    * BX Delft, the Netherlands. All rights reserved. This software is proprietary information of Delft
4    * University of Technology The code is published under the Lesser General Public License
5    */
6   package nl.tudelft.simulation.language.io;
7   
8   import java.io.File;
9   import java.io.InputStream;
10  import java.net.Authenticator;
11  import java.net.PasswordAuthentication;
12  import java.net.URL;
13  
14  /***
15   * <p>
16   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
17   * Netherlands.
18   * <p>
19   * See for project information <a
20   * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
21   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
22   * </a>, no warranty
23   * 
24   * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
25   * @version $Revision: 1.10 $ $Date: 2005/08/04 12:08:57 $
26   * @since 1.5
27   */
28  public final class URLResource
29  {
30      /***
31       * constructs a new URLResource.
32       */
33      private URLResource()
34      {
35          super();
36          // unreachable code
37      }
38  
39      /***
40       * resolves a resource for name.
41       * 
42       * @param name
43       *            the name to search for
44       * @return the resolved URL
45       */
46      public static URL getResource(final String name)
47      {
48          try
49          {
50              if (name.startsWith("/"))
51              {
52                  URL url = URLResource.class.getResource(name);
53                  if (url != null)
54                  {
55                      return url;
56                  }
57                  url = Thread.currentThread().getContextClassLoader().getResource(name.substring(1));
58                  if (url != null)
59                  {
60                      return url;
61                  }
62                  File file = new File(name);
63                  if (file.exists())
64                  {
65                      return new URL("file:" + name);
66                  }
67              }
68              else
69              {
70                  if (name.indexOf("@") == -1)
71                  {
72                      return new URL(name);
73                  }
74                  // we need authentication
75                  String temp = name.substring(name.indexOf("//") + 2);
76                  String userName = temp.substring(0, temp.indexOf(":"));
77                  String password = temp.substring(temp.indexOf(":") + 1);
78                  password = password.substring(0, password.indexOf("@"));
79                  String url = name.substring(0, name.indexOf("//") + 2);
80                  url = url + name.substring(name.indexOf("@") + 1);
81                  Authenticator.setDefault(new PasswordAuthenticator(userName, password));
82                  return new URL(url);
83              }
84          }
85          catch (Exception exception)
86          {
87              exception = null;
88              // We neglect exceptions since we return null
89          }
90          return null;
91      }
92  
93      /***
94       * returns the reseource as stream.
95       * 
96       * @param name
97       *            the name of the resource
98       * @return the inputStream
99       */
100     public static InputStream getResourceAsStream(final String name)
101     {
102         try
103         {
104             URL url = URLResource.getResource(name);
105             if (url == null)
106             {
107                 return null;
108             }
109             return url.openStream();
110         }
111         catch (Exception exception)
112         {
113             return null;
114         }
115     }
116 
117     /***
118      * A Private password authenticator.
119      */
120     private static class PasswordAuthenticator extends Authenticator
121     {
122         /*** my username */
123         private String userName = null;
124 
125         /*** my password */
126         private String password = null;
127 
128         /***
129          * constructs a new PasswordAuthenticator.
130          * 
131          * @param userName
132          *            my userName
133          * @param password
134          *            my passWord
135          */
136         public PasswordAuthenticator(final String userName, final String password)
137         {
138             super();
139             this.userName = userName;
140             this.password = password;
141         }
142 
143         /***
144          * @see java.net.Authenticator#getPasswordAuthentication()
145          */
146         @Override
147         protected PasswordAuthentication getPasswordAuthentication()
148         {
149             return new PasswordAuthentication(this.userName, this.password.toCharArray());
150         }
151     }
152 }