1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.resteasy.hypermedia.adapter;
17
18 import java.net.URI;
19 import java.net.URISyntaxException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.jboss.resteasy.links.RESTServiceDiscovery;
26
27 import com.google.common.collect.ArrayListMultimap;
28 import com.google.common.collect.Multimap;
29
30 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
31 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorAtom;
32 import de.smartics.resteasy.hypermedia.resources.Resources;
33 import de.smartics.resteasy.hypermedia.resources.ResourcesAdmin;
34
35
36
37
38 public class DiscoveredResourceRepository extends RESTServiceDiscovery
39 implements Resources, ResourcesAdmin
40 {
41
42
43
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50
51
52
53
54
55 private final Multimap<String, URI> links = ArrayListMultimap.create();
56
57
58
59
60
61 private boolean unencodeSlashInPath;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public boolean isUnencodeSlashInPath()
82 {
83 return unencodeSlashInPath;
84 }
85
86
87
88
89
90
91
92 public void setUnencodeSlashInPath(final boolean unencodeSlashInPath)
93 {
94 this.unencodeSlashInPath = unencodeSlashInPath;
95 }
96
97
98
99 @Override
100 public void addLink(final URI uri, final String rels)
101 {
102 final URI norm = normalizeUri(uri);
103
104 super.addLink(norm, rels);
105
106 for (final String rel : rels.split("\\s+"))
107 {
108 links.put(rel, norm);
109 }
110 }
111
112 private URI normalizeUri(final URI uri)
113 {
114 if (unencodeSlashInPath)
115 {
116 try
117 {
118 final String path = uri.toString();
119 final String norm = StringUtils.replace(path, "%2F", "/");
120 return new URI(norm);
121 }
122 catch (final URISyntaxException e)
123 {
124
125 }
126 }
127 return uri;
128 }
129
130
131
132
133
134
135
136 @Override
137 public List<LinkDescriptor> getLinksForRel(final String rel)
138 {
139 final Collection<URI> uris = links.get(rel);
140 final List<LinkDescriptor> links =
141 new ArrayList<LinkDescriptor>(uris.size());
142 for (final URI uri : uris)
143 {
144 final AtomLink atomLink = new AtomLink();
145 atomLink.setHref(uri.toString());
146 atomLink.setRel(rel);
147 final LinkDescriptor link = new LinkDescriptorAtom(atomLink);
148 links.add(link);
149 }
150 return links;
151 }
152
153 @Override
154 public List<LinkDescriptor> links()
155 {
156 final List<LinkDescriptor> links = new ArrayList<LinkDescriptor>();
157 for (final AtomLink atomLink : this)
158 {
159 final LinkDescriptorAtom link = new LinkDescriptorAtom(atomLink);
160 links.add(link);
161 }
162 return links;
163 }
164
165
166
167 }