/
MetadataLinkInfoImpl.java
172 lines (140 loc) · 4.76 KB
/
MetadataLinkInfoImpl.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.catalog.impl;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import org.geoserver.catalog.MetadataLinkInfo;
public class MetadataLinkInfoImpl implements MetadataLinkInfo {
String id;
String type;
String about;
String metadataType;
String content;
public MetadataLinkInfoImpl() {
// nothing to do
}
public MetadataLinkInfoImpl(MetadataLinkInfoImpl other) {
this.id = other.id;
this.type = other.type;
this.about = other.about;
this.metadataType = other.metadataType;
this.content = other.content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public String getMetadataType() {
return metadataType;
}
public void setMetadataType(String metadataType) {
this.metadataType = metadataType;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
static final List<String> protocols = Arrays.asList("http", "https", "ftp");
/**
* @throws IllegalArgumentException if the url is invalid for use as a Metadata Link
* @param url
*/
public static void validate(String url) {
if (url==null) return;
URL dummy;
try {
dummy = new URL("http://dummy/");
} catch (MalformedURLException ex) {
throw new IllegalStateException("Could not parse dummy context URL", ex);
}
try {
// Doing this with exceptions isn't ideal but it works, and we're throwing an
// exception anyway
// The dummy context will allow it to parse relative URLs, which should be allowed.
URL parsed = new URL(dummy, url);
String protocol = parsed.getProtocol();
// Converting to URI forces validation
parsed.toURI();
if(!protocols.contains(protocol)){
throw new IllegalArgumentException("Protocol "+protocol+" is not supported in url "+url);
}
} catch (MalformedURLException | URISyntaxException ex) {
throw new IllegalArgumentException("Not a valid URL: "+url, ex);
}
}
public void setContent(String content) {
validate(content);
this.content = content;
}
private Object readResolve() {
validate(content);
return this;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((about == null) ? 0 : about.hashCode());
result = PRIME * result + ((content == null) ? 0 : content.hashCode());
result = PRIME * result + ((metadataType == null) ? 0 : metadataType.hashCode());
result = PRIME * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof MetadataLinkInfo)) {
return false;
}
final MetadataLinkInfo other = (MetadataLinkInfo) obj;
if (about == null) {
if (other.getAbout() != null)
return false;
} else if (!about.equals(other.getAbout()))
return false;
if (content == null) {
if (other.getContent() != null)
return false;
} else if (!content.equals(other.getContent()))
return false;
if (metadataType == null) {
if (other.getMetadataType() != null)
return false;
} else if (!metadataType.equals(other.getMetadataType()))
return false;
if (type == null) {
if (other.getType() != null)
return false;
} else if (!type.equals(other.getType()))
return false;
return true;
}
@Override
public String toString() {
return new StringBuilder(getClass().getSimpleName()).append("[type:").append(type).append(
", metadataType:").append(metadataType).append(", content:").append(content)
.append(']').toString();
}
}