View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Adapter to add additional features to handle breadcumbs.
37   */
38  public class DiscoveredResourceRepository extends RESTServiceDiscovery
39      implements Resources, ResourcesAdmin
40  {
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44  
45    /**
46     * The class version identifier.
47     */
48    private static final long serialVersionUID = 1L;
49  
50    // --- members --------------------------------------------------------------
51  
52    /**
53     * Maps relation values to links.
54     */
55    private final Multimap<String, URI> links = ArrayListMultimap.create();
56  
57    /**
58     * Controls whether (<code>true</code>) or not (<code>false</code>) encoded
59     * slashes in the URL paths should be unencoded.
60     */
61    private boolean unencodeSlashInPath;
62  
63    // ****************************** Initializer *******************************
64  
65    // ****************************** Constructors ******************************
66  
67    // ****************************** Inner Classes *****************************
68  
69    // ********************************* Methods ********************************
70  
71    // --- init -----------------------------------------------------------------
72  
73    // --- get&set --------------------------------------------------------------
74  
75    /**
76     * Returns the flag that controls whether (<code>true</code>) or not (
77     * <code>false</code>) encoded slashes in the URL paths should be unencoded.
78     *
79     * @return the value of the flag.
80     */
81    public boolean isUnencodeSlashInPath()
82    {
83      return unencodeSlashInPath;
84    }
85  
86    /**
87     * Sets the flag that controls whether (<code>true</code>) or not (
88     * <code>false</code>) encoded slashes in the URL paths should be unencoded.
89     *
90     * @param unencodeSlashInPath the value of the flag.
91     */
92    public void setUnencodeSlashInPath(final boolean unencodeSlashInPath)
93    {
94      this.unencodeSlashInPath = unencodeSlashInPath;
95    }
96  
97    // --- business -------------------------------------------------------------
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         // Return uri at the end...
125       }
126     }
127     return uri;
128   }
129 
130   /**
131    * Returns links associated with a given relation.
132    *
133    * @param rel a single relation value.
134    * @return the associated links, may be empty.
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   // --- object basics --------------------------------------------------------
166 
167 }