Project

General

Profile

Download (5.45 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.common;
10

    
11
import java.io.File;
12
import java.io.Serializable;
13
import java.net.MalformedURLException;
14
import java.net.URISyntaxException;
15
import java.net.URL;
16

    
17
/**
18
 * See https://dev.e-taxonomy.eu/redmine/issues/9114
19
 *
20
 * @author a.mueller
21
 * @since 05.01.2021
22
 */
23
public class URI
24
        implements Comparable<URI>, Serializable {
25

    
26
    private static final long serialVersionUID = -8002215586516542076L;
27

    
28
    private final java.net.URI javaUri;
29

    
30
 // ***************************** FACTORY METHODS ***************************************/
31

    
32
    public static URI fromString(String uri) throws URISyntaxException {
33
        return new URI(uri);
34
    }
35

    
36
    /**
37
     * Factory method.
38
     *
39
     * @see java.net.URI#create(String)
40
     */
41
    public static URI create(String uri) {
42
        try {
43
            return new URI(uri);
44
        } catch (URISyntaxException e) {
45
            throw new IllegalArgumentException(e);
46
        }
47
    }
48

    
49
    public static URI fromUrl(URL url) throws URISyntaxException {
50
        return new URI(url);
51
    }
52

    
53
    public static URI fromFile(File file) {
54
        return new URI(file.toURI());
55
    }
56

    
57
 // ******************************* CONSTRUCTOR ************************************/
58

    
59
    @SuppressWarnings("unused")
60
    private URI(){javaUri = null;} //empty constructor required for JAXB (not tested but copied from DOI)
61

    
62
    public URI(String uriString) throws URISyntaxException {
63
        javaUri = parseUriString(uriString);
64
    }
65

    
66
    public URI(java.net.URI javaUri) {
67
        this.javaUri = javaUri;
68
    }
69

    
70
    //TODO maybe we can do encoding in between
71
    public URI(URL url) throws URISyntaxException {
72
        this.javaUri = url.toURI();
73
    }
74

    
75
    /**
76
     * @see java.net.URI#URI(String, String, String, int, String, String, String)
77
     */
78
    public URI(String protocol, String userInfo, String host, int port, String path, String query, String ref) throws URISyntaxException {
79
        javaUri = new java.net.URI(protocol, userInfo, host, port, path, query, ref);
80
    }
81

    
82
//************************************ GETTER ***********************************/
83

    
84
    public java.net.URI getJavaUri(){
85
        return javaUri;
86
    }
87

    
88
//************************************ METHODS ************************************/
89

    
90
    private java.net.URI parseUriString(String uriString) throws URISyntaxException {
91
        java.net.URI javaUri = null;
92
        if(uriString != null){
93
            try{
94
                javaUri = new java.net.URI(uriString);
95
            }catch(Exception e){
96
                try {
97
                    String encodedUri = uriString;
98
                    URL url = new URL(encodedUri);
99
                    String[] pathElements =  url.getPath().split("/");
100
                    for (String element: pathElements){
101
                        if(element.contains("\\")){
102
                            //TODO needs discussion if backslash should be converted to slash instead, for now we keep it more strict
103
                            throw new URISyntaxException(uriString, "URI path must not contain backslash ('\')");
104
                        }
105
                        String replacement = UrlUtf8Coder.encode(element);
106
                        encodedUri = encodedUri.replace(element, replacement);
107
                    }
108
                    if (url.getQuery() != null){
109
                        encodedUri = encodedUri.replace(url.getQuery(), UrlUtf8Coder.encode(url.getQuery()));
110
                    }
111
                    url = new URL(encodedUri);
112

    
113
                    javaUri = url.toURI();
114
                } catch (MalformedURLException e1) {
115
                    throw new URISyntaxException(uriString, e1.getMessage());
116
                }
117
            }
118
        }
119
        return javaUri;
120
    }
121

    
122
    public File toFile(){
123
        return new File(javaUri);
124
    }
125

    
126
//******************************** Wrapper methods *********************/
127

    
128
    public URL toURL() throws MalformedURLException{
129
        return javaUri.toURL();
130
    }
131

    
132
    public String getHost() {
133
        return javaUri.getHost();
134
    }
135

    
136
    public int getPort() {
137
        return javaUri.getPort();
138
    }
139

    
140
    public String getScheme() {
141
        return javaUri.getScheme();
142
    }
143

    
144
    public boolean isAbsolute() {
145
        return javaUri.isAbsolute();
146
    }
147

    
148
    public String getPath() {
149
        return javaUri.getPath();
150
    }
151

    
152
    public String getFragment() {
153
        return javaUri.getFragment();
154
    }
155

    
156
    public Object getQuery() {
157
        return javaUri.getQuery();
158
    }
159

    
160

    
161
//****************************** equals *****************************/
162

    
163
    @Override
164
    public int hashCode() {
165
        return javaUri.hashCode();
166
    }
167

    
168
    @Override
169
    public boolean equals(Object obj) {
170
        if (obj instanceof URI){
171
            return javaUri.equals(((URI)obj).javaUri);
172
        }
173
        return false;
174
    }
175

    
176
    @Override
177
    public int compareTo(URI that) {
178
        return this.javaUri.compareTo(that.javaUri);
179
    }
180

    
181
//********************** clone ***********************************/
182

    
183
    @Override
184
    protected URI clone() throws CloneNotSupportedException {
185
        return new URI(this.javaUri);
186
    }
187

    
188
//******************************** toString ****************************/
189

    
190
    @Override
191
    public String toString() {
192
        return javaUri.toString();
193
    }
194

    
195

    
196
}
(19-19/23)