View Javadoc
1   package org.djutils.io;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.net.Authenticator;
7   import java.net.MalformedURLException;
8   import java.net.PasswordAuthentication;
9   import java.net.URL;
10  
11  /**
12   * The URLResource class helps to resolve a file location in a project, JAR, or folder. The static methods return a URL of the
13   * file location that was found, or null in case it was not found.
14   * <p>
15   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
17   * distributed under a three-clause BSD-style license, which can be found at
18   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
19   * </p>
20   * @author Peter Jacobs
21   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   */
23  @Deprecated
24  public final class URLResource
25  {
26      /**
27       * Utility class; do not instantiate.
28       */
29      private URLResource()
30      {
31          // Do not instantiate
32      }
33  
34      /**
35       * Resolves a resource for name.
36       * @param name the name to search for
37       * @return the resolved URL
38       */
39      @Deprecated
40      public static URL getResource(final String name)
41      {
42          try
43          {
44              File file = new File(name);
45  
46              if (name.startsWith("/"))
47              {
48                  URL url = URLResource.class.getResource(name);
49                  if (url != null)
50                  {
51                      return url;
52                  }
53                  url = Thread.currentThread().getContextClassLoader().getResource(name.substring(1));
54                  if (url != null)
55                  {
56                      return url;
57                  }
58                  if (file.exists())
59                  {
60                      return new URL("file:" + name);
61                  }
62              }
63              else if (name.startsWith("\\") || name.contains("\\")) // added the second part
64              {
65                  if (file.exists())
66                  {
67                      return new URL("file:" + name);
68                  }
69              }
70              else if (file.exists())
71              {
72                  return new URL("file:" + name);
73              }
74              else
75              {
76                  if (name.indexOf("@") == -1)
77                  {
78                      return new URL(name);
79                  }
80                  // we need authentication
81                  String temp = name.substring(name.indexOf("//") + 2);
82                  String userName = temp.substring(0, temp.indexOf(":"));
83                  String password = temp.substring(temp.indexOf(":") + 1);
84                  password = password.substring(0, password.indexOf("@"));
85                  String url = name.substring(0, name.indexOf("//") + 2);
86                  url = url + name.substring(name.indexOf("@") + 1);
87                  Authenticator.setDefault(new PasswordAuthenticator(userName, password));
88                  return new URL(url);
89              }
90          }
91          catch (Exception exception)
92          {
93              exception = null;
94              // We neglect exceptions since we return null
95          }
96          return null;
97      }
98  
99      /**
100      * Resolves a resource for name. For relative names, base is used to resolve to an absolute name. If name is absolute, base
101      * is ignored.
102      * @param name the name to search for
103      * @param base the base for relative paths
104      * @return the resolved URL
105      */
106     @Deprecated
107     public static URL getResource(final String name, final String base)
108     {
109         URL url = null;
110 
111         // case complete URL
112         try
113         {
114             url = new URL(name);
115         }
116         catch (MalformedURLException ex)
117         {
118             // neglect exception -- just trying
119         }
120 
121         // absolute or relative case
122         if (url == null)
123         {
124             String completeName = name;
125             if (!name.startsWith(File.separator) && !name.startsWith("/") && base != null)
126             {
127                 String baseDir = "";
128                 int i = base.lastIndexOf(File.separator);
129                 if (i == -1 && !File.separator.equals("/"))
130                 {
131                     i = base.lastIndexOf("/");
132                 }
133                 if (i != -1)
134                 {
135                     baseDir = base.substring(0, i + 1);
136                 }
137                 completeName = baseDir + name;
138             }
139 
140             // case base = URL
141             try
142             {
143                 url = new URL(completeName);
144                 if (url.getProtocol().equalsIgnoreCase("file"))
145                 {
146                     File file = new File(url.getPath());
147                     if (!file.exists())
148                     {
149                         url = null;
150                     }
151                 }
152             }
153             catch (MalformedURLException ex)
154             {
155                 url = getResourceOrFile(completeName);
156             }
157 
158             // just try plain name if that's still another option
159             if (url == null && !name.equalsIgnoreCase(completeName))
160             {
161                 url = getResourceOrFile(name);
162             }
163 
164         }
165 
166         // handle authentication
167         if (url != null && url.getUserInfo() != null)
168         {
169             String ui = url.getUserInfo();
170             String userName = ui.substring(0, ui.indexOf(":"));
171             String password = ui.substring(ui.indexOf(":") + 1);
172             Authenticator.setDefault(new PasswordAuthenticator(userName, password));
173         }
174 
175         return url;
176     }
177 
178     /**
179      * Resolves a resource for a path.
180      * @param path the path to search for
181      * @return the resolved URL to the path
182      */
183     private static URL getResourceOrFile(final String path)
184     {
185         URL url = null;
186 
187         // resource
188         if (url == null)
189         {
190             url = URLResource.class.getResource(path);
191         }
192 
193         // thread context resource
194         if (url == null)
195         {
196             url = Thread.currentThread().getContextClassLoader().getResource(path.substring(1));
197         }
198 
199         // file
200         if (url == null)
201         {
202             File file = new File(path);
203             if (file.exists())
204             {
205                 try
206                 {
207                     url = new URL("file:" + file.getCanonicalPath());
208                 }
209                 catch (IOException ex)
210                 {
211                     // ignore -- if not found, we return null
212                 }
213             }
214         }
215 
216         return url;
217     }
218 
219     /**
220      * returns the resource as stream.
221      * @param name the name of the resource
222      * @return the inputStream
223      */
224     @Deprecated
225     public static InputStream getResourceAsStream(final String name)
226     {
227         try
228         {
229             URL url = URLResource.getResource(name);
230             if (url == null)
231             {
232                 return null;
233             }
234             return url.openStream();
235         }
236         catch (Exception exception)
237         {
238             return null;
239         }
240     }
241 
242     /**
243      * A Private password authenticator.
244      */
245     private static class PasswordAuthenticator extends Authenticator
246     {
247         /** my user name. */
248         private String userName = null;
249 
250         /** my password. */
251         private String password = null;
252 
253         /**
254          * constructs a new PasswordAuthenticator.
255          * @param userName my userName
256          * @param password my passWord
257          */
258         PasswordAuthenticator(final String userName, final String password)
259         {
260             this.userName = userName;
261             this.password = password;
262         }
263 
264         @Override
265         protected PasswordAuthentication getPasswordAuthentication()
266         {
267             return new PasswordAuthentication(this.userName, this.password.toCharArray());
268         }
269     }
270 
271 }