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 public final class URLResource
24 {
25
26
27
28 private URLResource()
29 {
30
31 }
32
33
34
35
36
37
38 public static URL getResource(final String name)
39 {
40 try
41 {
42 File file = new File(name);
43
44 if (name.startsWith("/"))
45 {
46 URL url = URLResource.class.getResource(name);
47 if (url != null)
48 {
49 return url;
50 }
51 url = Thread.currentThread().getContextClassLoader().getResource(name.substring(1));
52 if (url != null)
53 {
54 return url;
55 }
56 if (file.exists())
57 {
58 return new URL("file:" + name);
59 }
60 }
61 else if (name.startsWith("\\") || name.contains("\\"))
62 {
63 if (file.exists())
64 {
65 return new URL("file:" + name);
66 }
67 }
68 else if (file.exists())
69 {
70 return new URL("file:" + name);
71 }
72 else
73 {
74 if (name.indexOf("@") == -1)
75 {
76 return new URL(name);
77 }
78
79 String temp = name.substring(name.indexOf("//") + 2);
80 String userName = temp.substring(0, temp.indexOf(":"));
81 String password = temp.substring(temp.indexOf(":") + 1);
82 password = password.substring(0, password.indexOf("@"));
83 String url = name.substring(0, name.indexOf("//") + 2);
84 url = url + name.substring(name.indexOf("@") + 1);
85 Authenticator.setDefault(new PasswordAuthenticator(userName, password));
86 return new URL(url);
87 }
88 }
89 catch (Exception exception)
90 {
91 exception = null;
92
93 }
94 return null;
95 }
96
97
98
99
100
101
102
103
104 public static URL getResource(final String name, final String base)
105 {
106 URL url = null;
107
108
109 try
110 {
111 url = new URL(name);
112 }
113 catch (MalformedURLException ex)
114 {
115
116 }
117
118
119 if (url == null)
120 {
121 String completeName = name;
122 if (!name.startsWith(File.separator) && !name.startsWith("/") && base != null)
123 {
124 String baseDir = "";
125 int i = base.lastIndexOf(File.separator);
126 if (i == -1 && !File.separator.equals("/"))
127 {
128 i = base.lastIndexOf("/");
129 }
130 if (i != -1)
131 {
132 baseDir = base.substring(0, i + 1);
133 }
134 completeName = baseDir + name;
135 }
136
137
138 try
139 {
140 url = new URL(completeName);
141 if (url.getProtocol().equalsIgnoreCase("file"))
142 {
143 File file = new File(url.getPath());
144 if (!file.exists())
145 {
146 url = null;
147 }
148 }
149 }
150 catch (MalformedURLException ex)
151 {
152 url = getResourceOrFile(completeName);
153 }
154
155
156 if (url == null && !name.equalsIgnoreCase(completeName))
157 {
158 url = getResourceOrFile(name);
159 }
160
161 }
162
163
164 if (url != null && url.getUserInfo() != null)
165 {
166 String ui = url.getUserInfo();
167 String userName = ui.substring(0, ui.indexOf(":"));
168 String password = ui.substring(ui.indexOf(":") + 1);
169 Authenticator.setDefault(new PasswordAuthenticator(userName, password));
170 }
171
172 return url;
173 }
174
175
176
177
178
179
180 private static URL getResourceOrFile(final String path)
181 {
182 URL url = null;
183
184
185 if (url == null)
186 {
187 url = URLResource.class.getResource(path);
188 }
189
190
191 if (url == null)
192 {
193 url = Thread.currentThread().getContextClassLoader().getResource(path.substring(1));
194 }
195
196
197 if (url == null)
198 {
199 File file = new File(path);
200 if (file.exists())
201 {
202 try
203 {
204 url = new URL("file:" + file.getCanonicalPath());
205 }
206 catch (IOException ex)
207 {
208
209 }
210 }
211 }
212
213 return url;
214 }
215
216
217
218
219
220
221 public static InputStream getResourceAsStream(final String name)
222 {
223 try
224 {
225 URL url = URLResource.getResource(name);
226 if (url == null)
227 {
228 return null;
229 }
230 return url.openStream();
231 }
232 catch (Exception exception)
233 {
234 return null;
235 }
236 }
237
238
239
240
241 private static class PasswordAuthenticator extends Authenticator
242 {
243
244 private String userName = null;
245
246
247 private String password = null;
248
249
250
251
252
253
254 PasswordAuthenticator(final String userName, final String password)
255 {
256 this.userName = userName;
257 this.password = password;
258 }
259
260 @Override
261 protected PasswordAuthentication getPasswordAuthentication()
262 {
263 return new PasswordAuthentication(this.userName, this.password.toCharArray());
264 }
265 }
266
267 }