summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/util/URIUtil.java
blob: 5af40d591a6142c78af93f020d6ae59b9fada3a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package de.danoeh.antennapod.util;

import android.util.Log;
import de.danoeh.antennapod.BuildConfig;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Utility methods for dealing with URL encoding.
 */
public class URIUtil {
    private static final String TAG = "URIUtil";

    private URIUtil() {}

    public static URI getURIFromRequestUrl(String source) {
        // try without encoding the URI
        try {
            return new URI(source);
        } catch (URISyntaxException e) {
            if (BuildConfig.DEBUG) Log.d(TAG, "Source is not encoded, encoding now");
        }
        try {
            URL url = new URL(source);
            return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException(e);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }
}