blob: f0aa5702e8c7b83cd08cec1ea277e2494abe4f34 (
plain) (
blame)
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.restapi;
import java.net.URI;
import java.net.URISyntaxException;
/**
* A Uri which provides convenience methods for creating various manipulated copies.
* This is immutable.
*
* @author bratseth
*/
public class UriBuilder {
/** The URI instance wrapped by this */
private final URI uri;
public UriBuilder(URI uri) {
this.uri = uri;
}
public UriBuilder(String uri) {
try {
this.uri = new URI(uri);
}
catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid URI", e);
}
}
/** Returns a uri with the given path appended and all parameters removed */
public UriBuilder append(String pathElement) {
return new UriBuilder(withoutParameters().withTrailingSlash() + pathElement);
}
public UriBuilder withoutParameters() {
int parameterStart = uri.toString().indexOf("?");
if (parameterStart < 0)
return new UriBuilder(uri.toString());
else
return new UriBuilder(uri.toString().substring(0, parameterStart));
}
public UriBuilder withPath(String path) {
try {
return new UriBuilder(new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(),
uri.getPort(), path, uri.getQuery(), uri.getFragment()));
}
catch (URISyntaxException e) {
throw new IllegalArgumentException("Could not add path '" + path + "' to " + this);
}
}
public UriBuilder withTrailingSlash() {
if (toString().endsWith("/")) return this;
return new UriBuilder(toString() + "/");
}
public URI toURI() { return uri; }
@Override
public String toString() { return uri.toString(); }
}
|