1 package fr.in2p3.jsaga.impl.url;
2
3 import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
4 import fr.in2p3.jsaga.helpers.URLEncoder;
5
6 import org.ogf.saga.SagaObject;
7 import org.ogf.saga.error.BadParameterException;
8 import org.ogf.saga.error.NoSuccessException;
9 import org.ogf.saga.url.URL;
10
11 import java.net.URI;
12 import java.net.URISyntaxException;
13 import java.util.regex.Pattern;
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class AbsoluteURLImpl extends AbstractURLImpl implements URL {
28 protected URI u;
29 public final static String ABSOLUTE_URL_REGEXP = "^[^/\\\\]{2,}\\:/.*";
30
31
32 AbsoluteURLImpl(String url) throws BadParameterException {
33 this.setString(url);
34 }
35
36 AbsoluteURLImpl(FileAttributes cache) throws BadParameterException {
37 this(cache.getRelativePath());
38 this.setCache(cache);
39 }
40
41
42 protected AbsoluteURLImpl(URI u) throws BadParameterException {
43 this.u = u;
44 }
45
46
47 public SagaObject clone() throws CloneNotSupportedException {
48 AbsoluteURLImpl clone = (AbsoluteURLImpl) super.clone();
49 clone.u = u;
50 clone.m_cache = m_cache;
51 clone.m_cache_creation_time = m_cache_creation_time;
52 return clone;
53 }
54
55
56 public void setString(String url) throws BadParameterException {
57 if (url == null) {
58 url = "";
59 }
60
61 if (url != "" && ! Pattern.matches(ABSOLUTE_URL_REGEXP, url)) {
62 throw new BadParameterException("URL must be absolute");
63 }
64
65
66
67 String encodedUrl = (URLHelper.startsWithLocalScheme(url))
68 ? URLEncoder.encodeUrlNoQueryNoFragment(url)
69 : URLEncoder.encode(url);
70 try {
71 u = new URI(encodedUrl);
72
73 setPath(getPath());
74 } catch(URISyntaxException e) {
75 throw new BadParameterException("syntax error in url", e);
76 }
77 }
78
79
80 public String getString() {
81 StringBuffer buffer = new StringBuffer();
82 if (getScheme() != null) {
83 buffer.append(getScheme());
84 buffer.append(":");
85 }
86 if (getHost() != null) {
87 if (getScheme() != null) buffer.append("//");
88 if (getUserInfo() != null) {
89 buffer.append(getUserInfo());
90 buffer.append('@');
91 }
92 buffer.append(getHost());
93 if (getPort() != -1) {
94 buffer.append(':');
95 buffer.append(getPort());
96 }
97 }
98 if (getPath() != null) {
99 buffer.append(getPath());
100 }
101 if (getQuery() != null) {
102 buffer.append('?');
103 buffer.append(getQuery());
104 }
105 if (getFragment() != null) {
106 buffer.append('#');
107 buffer.append(getFragment());
108 }
109 return buffer.toString();
110 }
111
112
113 public String getEscaped() {
114 return u.toString();
115 }
116
117 public String toString() {
118 return u.toString();
119 }
120
121 public String getFragment() {
122 return u.getFragment();
123 }
124
125 public void setFragment(String fragment) throws BadParameterException {
126 try {
127 u = new URI(u.getScheme(), u.getUserInfo(), u.getHost(),
128 u.getPort(), u.getPath(), u.getQuery(), fragment);
129 } catch(URISyntaxException e) {
130 throw new BadParameterException("syntax error in fragment", e);
131 }
132 }
133
134 public String getHost() {
135 if (u.getHost() == null) {
136
137 return this.getSchemeSpecificPart().getHost();
138 }
139 return u.getHost();
140 }
141
142 public void setHost(String host) throws BadParameterException {
143 try {
144 u = new URI(u.getScheme(), u.getUserInfo(), host,
145 u.getPort(), u.getPath(), u.getQuery(), u.getFragment());
146 } catch(URISyntaxException e) {
147 throw new BadParameterException("syntax error in host", e);
148 }
149 }
150
151 public String getPath() {
152 if (u.getPath() == null) {
153 return this.getSchemeSpecificPart().getPath();
154 }
155 return u.getPath();
156 }
157
158 public void setPath(String path) throws BadParameterException {
159 if (path == null) {
160 path = "";
161 }
162
163 if (System.getProperty("file.separator") != "/")
164 path = path.replace(System.getProperty("file.separator"), "/");
165 try {
166
167 int i;for(i=0; i<path.length() && path.charAt(i)=='/'; i++);
168 if(i>1)path="/"+path.substring(i);
169
170 if (Pattern.matches("^[^/\\\\]{1}\\:.*", path))
171 path = "/"+path;
172 if (path == "" && u.getRawAuthority() == null)
173 throw new BadParameterException("Path cannot by empty if authority is empty");
174 u = new URI(u.getScheme(), u.getUserInfo(), u.getHost(),
175 u.getPort(), path, u.getQuery(), u.getFragment());
176 } catch(URISyntaxException e) {
177 throw new BadParameterException("syntax error in path", e);
178 }
179 }
180
181 public int getPort() {
182 if (u.getPort() == -1) {
183 return this.getSchemeSpecificPart().getPort();
184 }
185 return u.getPort();
186 }
187
188 public void setPort(int port) throws BadParameterException {
189 try {
190 u = new URI(u.getScheme(), u.getUserInfo(), u.getHost(),
191 port, u.getPath(), u.getQuery(), u.getFragment());
192 } catch(URISyntaxException e) {
193 throw new BadParameterException("syntax error in port", e);
194 }
195 }
196
197 public String getQuery() {
198 if (u.getQuery() == null) {
199 return this.getSchemeSpecificPart().getQuery();
200 }
201 return u.getQuery();
202 }
203
204 public void setQuery(String query) throws BadParameterException {
205 try {
206 u = new URI(u.getScheme(), u.getUserInfo(), u.getHost(),
207 u.getPort(), u.getPath(), query, u.getFragment());
208 } catch(URISyntaxException e) {
209 throw new BadParameterException("syntax error in query", e);
210 }
211 }
212
213 public String getScheme() {
214 return u.getScheme();
215 }
216
217 public void setScheme(String scheme) throws BadParameterException {
218 try {
219 u = new URI(scheme, u.getUserInfo(), u.getHost(),
220 u.getPort(), u.getPath(), u.getQuery(), u.getFragment());
221 } catch(URISyntaxException e) {
222 throw new BadParameterException("syntax error in scheme", e);
223 }
224 }
225
226 public String getUserInfo() {
227 if (u.getUserInfo() == null) {
228 return this.getSchemeSpecificPart().getUserInfo();
229 }
230 return u.getUserInfo();
231 }
232
233 public void setUserInfo(String userInfo) throws BadParameterException {
234 try {
235 u = new URI(u.getScheme(), userInfo, u.getHost(),
236 u.getPort(), u.getPath(), u.getQuery(), u.getFragment());
237 } catch(URISyntaxException e) {
238 throw new BadParameterException("syntax error in query", e);
239 }
240 }
241
242 public URL translate(String scheme) throws BadParameterException, NoSuccessException {
243 try {
244 URI url = new URI(scheme, u.getUserInfo(), u.getHost(),
245 u.getPort(), u.getPath(), u.getQuery(), u.getFragment());
246
247
248
249 return new AbsoluteURLImpl(url);
250 } catch(URISyntaxException e) {
251 throw new BadParameterException("syntax error in scheme", e);
252 }
253 }
254
255 public URL resolve(URL url) throws NoSuccessException {
256 URI uri;
257 if (url instanceof AbsoluteURLImpl) {
258 AbsoluteURLImpl urlimpl = (AbsoluteURLImpl) url;
259 uri = u.resolve(urlimpl.u);
260 if (uri == urlimpl.u) {
261 return url;
262 }
263 } else if (url instanceof RelativeURLImpl) {
264
265 String encodedUrl = ((RelativeURLImpl)url).getEncodedPathOnly();
266 if (System.getProperty("os.name").startsWith("Windows") &&
267 encodedUrl.length()>1 && encodedUrl.charAt(1)==':') {
268 encodedUrl = "/"+encodedUrl;
269 }
270 uri = u.resolve(encodedUrl);
271 } else {
272 throw new NoSuccessException("Unknown class: " + url.getClass().getName());
273 }
274 try {
275 return new AbsoluteURLImpl(uri);
276 } catch (BadParameterException e) {
277 throw new NoSuccessException(e);
278 }
279 }
280
281 public boolean isAbsolute() {
282 return true;
283 }
284
285 public URL normalize() {
286 URI uri = u.normalize();
287 if (uri == u) {
288 return this;
289 }
290 try {
291 return new AbsoluteURLImpl(uri);
292 } catch (BadParameterException e) {
293 return this;
294 }
295 }
296
297
298
299 public int hashCode() {
300 return u.hashCode();
301 }
302
303 public boolean equals(Object o) {
304 if (o == null) {
305 return false;
306 }
307 if (! (o instanceof AbsoluteURLImpl)) {
308 return false;
309 }
310 AbsoluteURLImpl other = (AbsoluteURLImpl) o;
311 return u.equals(other.u);
312 }
313
314 private URI getSchemeSpecificPart() {
315 try {
316 return new URI(u.getRawSchemeSpecificPart());
317 } catch (URISyntaxException e) {
318 return u;
319 }
320 }
321
322 }