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  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  @Deprecated
24  public final class URLResource
25  {
26      
27  
28  
29      private URLResource()
30      {
31          
32      }
33  
34      
35  
36  
37  
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("\\")) 
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                  
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              
95          }
96          return null;
97      }
98  
99      
100 
101 
102 
103 
104 
105 
106     @Deprecated
107     public static URL getResource(final String name, final String base)
108     {
109         URL url = null;
110 
111         
112         try
113         {
114             url = new URL(name);
115         }
116         catch (MalformedURLException ex)
117         {
118             
119         }
120 
121         
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             
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             
159             if (url == null && !name.equalsIgnoreCase(completeName))
160             {
161                 url = getResourceOrFile(name);
162             }
163 
164         }
165 
166         
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 
180 
181 
182 
183     private static URL getResourceOrFile(final String path)
184     {
185         URL url = null;
186 
187         
188         if (url == null)
189         {
190             url = URLResource.class.getResource(path);
191         }
192 
193         
194         if (url == null)
195         {
196             url = Thread.currentThread().getContextClassLoader().getResource(path.substring(1));
197         }
198 
199         
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                     
212                 }
213             }
214         }
215 
216         return url;
217     }
218 
219     
220 
221 
222 
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 
244 
245     private static class PasswordAuthenticator extends Authenticator
246     {
247         
248         private String userName = null;
249 
250         
251         private String password = null;
252 
253         
254 
255 
256 
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 }