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
|
public URI(String scheme, String authority, String path, String query, String fragment) throws URISyntaxException{
|
83
|
javaUri = new java.net.URI(scheme, authority, path, query, fragment);
|
84
|
}
|
85
|
|
86
|
//************************************ GETTER ***********************************/
|
87
|
|
88
|
public java.net.URI getJavaUri(){
|
89
|
return javaUri;
|
90
|
}
|
91
|
|
92
|
//************************************ METHODS ************************************/
|
93
|
|
94
|
private java.net.URI parseUriString(String uriString) throws URISyntaxException {
|
95
|
java.net.URI javaUri = null;
|
96
|
if(uriString != null){
|
97
|
try{
|
98
|
javaUri = new java.net.URI(uriString);
|
99
|
}catch(Exception e){
|
100
|
try {
|
101
|
String encodedUri = uriString;
|
102
|
URL url = new URL(encodedUri);
|
103
|
String[] pathElements = url.getPath().split("/");
|
104
|
for (String element: pathElements){
|
105
|
if(element.contains("\\")){
|
106
|
//TODO needs discussion if backslash should be converted to slash instead, for now we keep it more strict
|
107
|
throw new URISyntaxException(uriString, "URI path must not contain backslash ('\')");
|
108
|
}
|
109
|
String replacement = UrlUtf8Coder.encode(element);
|
110
|
encodedUri = encodedUri.replace(element, replacement);
|
111
|
}
|
112
|
if (url.getQuery() != null){
|
113
|
encodedUri = encodedUri.replace(url.getQuery(), UrlUtf8Coder.encode(url.getQuery()));
|
114
|
}
|
115
|
url = new URL(encodedUri);
|
116
|
|
117
|
javaUri = url.toURI();
|
118
|
} catch (MalformedURLException e1) {
|
119
|
throw new URISyntaxException(uriString, e1.getMessage());
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
return javaUri;
|
124
|
}
|
125
|
|
126
|
public File toFile(){
|
127
|
return new File(javaUri);
|
128
|
}
|
129
|
|
130
|
//******************************** Wrapper methods *********************/
|
131
|
|
132
|
public URL toURL() throws MalformedURLException{
|
133
|
return javaUri.toURL();
|
134
|
}
|
135
|
|
136
|
public String getHost() {
|
137
|
return javaUri.getHost();
|
138
|
}
|
139
|
|
140
|
public int getPort() {
|
141
|
return javaUri.getPort();
|
142
|
}
|
143
|
|
144
|
public String getScheme() {
|
145
|
return javaUri.getScheme();
|
146
|
}
|
147
|
|
148
|
public boolean isAbsolute() {
|
149
|
return javaUri.isAbsolute();
|
150
|
}
|
151
|
|
152
|
public String getPath() {
|
153
|
return javaUri.getPath();
|
154
|
}
|
155
|
|
156
|
public String getFragment() {
|
157
|
return javaUri.getFragment();
|
158
|
}
|
159
|
|
160
|
public Object getQuery() {
|
161
|
return javaUri.getQuery();
|
162
|
}
|
163
|
|
164
|
|
165
|
//****************************** equals *****************************/
|
166
|
|
167
|
@Override
|
168
|
public int hashCode() {
|
169
|
return javaUri.hashCode();
|
170
|
}
|
171
|
|
172
|
@Override
|
173
|
public boolean equals(Object obj) {
|
174
|
if (obj instanceof URI){
|
175
|
return javaUri.equals(((URI)obj).javaUri);
|
176
|
}
|
177
|
return false;
|
178
|
}
|
179
|
|
180
|
@Override
|
181
|
public int compareTo(URI that) {
|
182
|
return this.javaUri.compareTo(that.javaUri);
|
183
|
}
|
184
|
|
185
|
//********************** clone ***********************************/
|
186
|
|
187
|
@Override
|
188
|
protected URI clone() throws CloneNotSupportedException {
|
189
|
return new URI(this.javaUri);
|
190
|
}
|
191
|
|
192
|
//******************************** toString ****************************/
|
193
|
|
194
|
@Override
|
195
|
public String toString() {
|
196
|
return javaUri.toString();
|
197
|
}
|
198
|
|
199
|
|
200
|
}
|