1 | /** |
2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
3 | */ |
4 | package org.sourceforge.xradar.util; |
5 | |
6 | import java.io.IOException; |
7 | import java.io.InputStream; |
8 | import java.net.MalformedURLException; |
9 | import java.net.URL; |
10 | import java.net.URLConnection; |
11 | |
12 | import org.sourceforge.xradar.XRadarException; |
13 | |
14 | /** |
15 | * @author Romain PELISSE <belaran@gmail.com> |
16 | * |
17 | */ |
18 | public final class URLUtils { |
19 | |
20 | /** |
21 | * <p>Handy method to create an {@link URLConnection} instance that |
22 | * wraps any exception in an {@link XRadarException}.</p> |
23 | * |
24 | * @param urlAsString |
25 | * @return the {@link URLConnection} instance or <code>null</code>. |
26 | * @throws XRadarException |
27 | */ |
28 | public static URLConnection openConnection(String urlAsString) throws XRadarException { |
29 | try { |
30 | return URLUtils.createURL(urlAsString).openConnection(); |
31 | } catch (IOException e) { |
32 | throw new XRadarException(e); |
33 | } |
34 | } |
35 | |
36 | /** |
37 | * <p>Handy method to create an {@link URL} that wraps any exception |
38 | * in an {@link XRadarException}. |
39 | * |
40 | * @param url, a {@link String} that describs the URL. |
41 | * @return the {@link URL} instance or <code>null</code>. |
42 | * @throws XRadarException |
43 | */ |
44 | public static URL createURL(String url) throws XRadarException { |
45 | try |
46 | { |
47 | return new URL(url); |
48 | } |
49 | catch (MalformedURLException e) |
50 | { |
51 | throw new XRadarException(e); |
52 | } |
53 | } |
54 | |
55 | /** |
56 | * <p>An handy method that create an {@link InputStream} and that wraps |
57 | * any exception in an {@link XRadarException} instance.</p> |
58 | * |
59 | * @param url, a {@link String} that describs the URL. |
60 | * @return the {@link InputStream} instance of <code>null</code>. |
61 | * @throws XRadarException |
62 | */ |
63 | public static InputStream openStream(String url) throws XRadarException { |
64 | try { |
65 | return URLUtils.createURL(url).openStream(); |
66 | } catch (IOException e) { |
67 | throw new XRadarException(e); |
68 | } |
69 | } |
70 | } |