Skip to content

Commit b415f91

Browse files
committed
Add implementation of a Link class
1 parent ca85eec commit b415f91

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.trellisldp.camel;
15+
16+
import static java.util.Collections.unmodifiableMap;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
/**
22+
* @author acoburn
23+
*/
24+
public class Link {
25+
26+
private final String uri;
27+
28+
private final Map<String, String> params = new HashMap<>();
29+
30+
/**
31+
* Create a Link object from an HTTP header
32+
* @param value the value of the link header
33+
*/
34+
public Link(final String value) {
35+
final String[] parts = value.split(";");
36+
if (parts.length >= 1) {
37+
final String val = parts[0].trim();
38+
this.uri = val.startsWith("<") && val.endsWith(">") ? val.substring(1, val.length() - 1) : null;
39+
for (int i = 1; i < parts.length; i++) {
40+
final String[] p = parts[1].trim().split("=");
41+
if (p.length == 2) {
42+
if (!params.containsKey(p[0])) {
43+
params.put(p[0], p[1]);
44+
}
45+
}
46+
}
47+
} else {
48+
this.uri = null;
49+
}
50+
}
51+
52+
/**
53+
* Get the URI portion of the link header
54+
* @return the URI
55+
*/
56+
public String getUri() {
57+
return uri;
58+
}
59+
60+
/**
61+
* Get the type portion of the link header, if one exists
62+
* @return the type or null if one doesn not exist
63+
*/
64+
public String getType() {
65+
return params.get("type");
66+
}
67+
68+
/**
69+
* Get the title portion of the link header, if one exists
70+
* @return the title or null if one doesn not exist
71+
*/
72+
public String getTitle() {
73+
return params.get("title");
74+
}
75+
76+
/**
77+
* Get the rel portion of the link header, if one exists
78+
* @return the rel or null if one doesn not exist
79+
*/
80+
public String getRel() {
81+
return params.get("rel");
82+
}
83+
84+
/**
85+
* Get all defined parameters from the link header, including type, rel and title if they exist.
86+
* @return the header parameters
87+
*/
88+
public Map<String, String> getParams() {
89+
return unmodifiableMap(params);
90+
}
91+
}

0 commit comments

Comments
 (0)