forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUriPrefixer.java
More file actions
76 lines (66 loc) · 2.26 KB
/
Copy pathUriPrefixer.java
File metadata and controls
76 lines (66 loc) · 2.26 KB
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
/**
* Copyright (C) 2004-2016, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import static com.gooddata.util.Validate.notEmpty;
import static com.gooddata.util.Validate.notNull;
import static org.springframework.util.StringUtils.trimLeadingCharacter;
/**
* Used internally by GoodData SDK to hold and set URI prefix (hostname and port) of all requests.
*/
public class UriPrefixer {
private final URI uriPrefix;
/**
* Construct URI prefixer using given URI prefix (just hostname and port is used)
*
* @param uriPrefix the URI prefix
*/
public UriPrefixer(URI uriPrefix) {
this.uriPrefix = notNull(uriPrefix, "uriPrefix");
}
/**
* Construct URI prefixer using given URI prefix (just hostname and port is used)
*
* @param uriPrefix the URI prefix string
*/
public UriPrefixer(String uriPrefix) {
this(URI.create(uriPrefix));
}
/**
* Get the URI prefix
*
* @return the URI prefix
*/
public URI getUriPrefix() {
return uriPrefix;
}
/**
* Return merged URI prefix (hostname and port) with the given URI (path, query, and fragment URI parts)
*
* @param uri the URI its parts (path, query, and fragment) will be merged with URI prefix
* @return the merged URI
*/
public URI mergeUris(URI uri) {
notNull(uri, "uri");
final String path = trimLeadingCharacter(uri.getRawPath(), '/');
return UriComponentsBuilder.fromUri(uriPrefix)
.pathSegment(path)
.query(uri.getRawQuery())
.fragment(uri.getRawFragment())
.build().toUri();
}
/**
* Return merged URI prefix (hostname and port) with the given URI string (path, query, and fragment URI parts)
*
* @param uri the URI string its parts (path, query, and fragment) will be merged with URI prefix
* @return the merged URI
*/
public URI mergeUris(String uri) {
notEmpty(uri, "uri");
return mergeUris(URI.create(uri));
}
}