Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / graphinventory / entities / uri / SimpleUri.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.graphinventory.entities.uri;
22
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.Serializable;
27 import java.net.URI;
28 import java.nio.charset.StandardCharsets;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Set;
32 import javax.ws.rs.core.UriBuilder;
33 import org.apache.commons.lang3.builder.HashCodeBuilder;
34 import org.onap.so.client.graphinventory.Format;
35 import org.onap.so.client.graphinventory.GraphInventoryObjectPlurals;
36 import org.onap.so.client.graphinventory.GraphInventoryObjectType;
37 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParser;
38 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParserSpringImpl;
39 import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys;
40 import org.springframework.web.util.UriUtils;
41
42 public class SimpleUri implements GraphInventoryResourceUri, Serializable {
43
44     private static final long serialVersionUID = -337701171277616439L;
45
46     protected transient UriBuilder internalURI;
47     protected final static String relationshipAPI = "/relationship-list/relationship";
48     protected final static String relatedTo = "/related-to";
49     protected final Object[] values;
50     protected final GraphInventoryObjectType type;
51     protected final GraphInventoryObjectPlurals pluralType;
52
53     protected SimpleUri(GraphInventoryObjectType type, Object... values) {
54         this.type = type;
55         this.pluralType = null;
56         this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
57         this.values = values;
58         validateValuesSize(this.getTemplate(type), values);
59     }
60
61     protected SimpleUri(GraphInventoryObjectType type, URI uri) {
62         this.type = type;
63         this.pluralType = null;
64         this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
65         this.values = new Object[0];
66     }
67
68     protected SimpleUri(GraphInventoryObjectType type, UriBuilder builder, Object... values) {
69         this.internalURI = builder;
70         this.values = values;
71         this.type = type;
72         this.pluralType = null;
73     }
74
75     protected SimpleUri(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) {
76         this.internalURI = builder;
77         this.values = values;
78         this.type = null;
79         this.pluralType = type;
80     }
81
82     protected SimpleUri(GraphInventoryObjectPlurals type) {
83         this.type = null;
84         this.pluralType = type;
85         this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
86         this.values = new Object[0];
87     }
88
89     protected SimpleUri(GraphInventoryObjectPlurals type, Object... values) {
90         this.type = null;
91         this.pluralType = type;
92         this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
93         this.values = values;
94         validateValuesSize(this.getTemplate(type), values);
95     }
96
97     protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectType childType,
98             Object... childValues) {
99         this.type = childType;
100         this.pluralType = null;
101         this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri());
102         this.values = childValues;
103         validateValuesSize(childType.partialUri(), values);
104     }
105
106     protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectPlurals childType) {
107         this.type = null;
108         this.pluralType = childType;
109         this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri());
110         this.values = new Object[0];
111     }
112
113     protected void setInternalURI(UriBuilder builder) {
114         this.internalURI = builder;
115     }
116
117     @Override
118     public SimpleUri relationshipAPI() {
119         this.internalURI = internalURI.path(relationshipAPI);
120         return this;
121     }
122
123     @Override
124     public SimpleUri relatedTo(GraphInventoryObjectPlurals plural) {
125
126         this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
127         return this;
128     }
129
130     @Override
131     public SimpleUri relatedTo(GraphInventoryObjectType type, String... values) {
132         this.internalURI =
133                 internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
134         return this;
135     }
136
137     @Override
138     public SimpleUri resourceVersion(String version) {
139         this.internalURI = internalURI.replaceQueryParam("resource-version", version);
140         return this;
141     }
142
143     @Override
144     public SimpleUri queryParam(String name, String... values) {
145         this.internalURI = internalURI.queryParam(name, values);
146         return this;
147     }
148
149     @Override
150     public SimpleUri replaceQueryParam(String name, String... values) {
151         this.internalURI = internalURI.replaceQueryParam(name, values);
152         return this;
153     }
154
155     @Override
156     public SimpleUri resultIndex(int index) {
157         this.internalURI = internalURI.replaceQueryParam("resultIndex", index);
158         return this;
159     }
160
161     @Override
162     public SimpleUri resultSize(int size) {
163         this.internalURI = internalURI.replaceQueryParam("resultSize", size);
164         return this;
165     }
166
167     @Override
168     public SimpleUri limit(int size) {
169         return this.resultIndex(0).resultSize(size);
170     }
171
172     @Override
173     public URI build() {
174         return build(this.values);
175     }
176
177     protected URI build(Object... values) {
178         // This is a workaround because resteasy does not encode URIs correctly
179         final String[] encoded = new String[values.length];
180         for (int i = 0; i < values.length; i++) {
181             encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
182         }
183         return internalURI.buildFromEncoded(encoded);
184     }
185
186     @Override
187     public Map<String, String> getURIKeys() {
188         return this.getURIKeys(this.build().toString());
189     }
190
191     protected Map<String, String> getURIKeys(String uri) {
192         UriParser parser;
193         if (this.type != null) {
194             if (!("".equals(this.getTemplate(type)))) {
195                 parser = new UriParserSpringImpl(this.getTemplate(type));
196             } else {
197                 return new HashMap<>();
198             }
199         } else {
200             parser = new UriParserSpringImpl(this.getTemplate(pluralType));
201         }
202
203
204         return parser.parse(uri);
205     }
206
207     @Override
208     public SimpleUri clone() {
209         if (this.type != null) {
210             return new SimpleUri(this.type, this.internalURI.clone(), values);
211         } else {
212             return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
213         }
214     }
215
216     @Override
217     public GraphInventoryObjectType getObjectType() {
218         return this.type;
219     }
220
221     @Override
222     public boolean equals(Object o) {
223         if (o instanceof GraphInventoryUri) {
224             return this.build().equals(((GraphInventoryUri) o).build());
225         }
226         return false;
227     }
228
229     @Override
230     public int hashCode() {
231         return new HashCodeBuilder().append(this.build()).toHashCode();
232     }
233
234
235     @Override
236     public SimpleUri depth(Depth depth) {
237         this.internalURI.replaceQueryParam("depth", depth.toString());
238         return this;
239     }
240
241     @Override
242     public SimpleUri nodesOnly(boolean nodesOnly) {
243         if (nodesOnly) {
244             this.internalURI.replaceQueryParam("nodes-only", "");
245         }
246         return this;
247     }
248
249     @Override
250     public SimpleUri format(Format format) {
251         this.internalURI.replaceQueryParam("format", format);
252         return this;
253     }
254
255     public void validateValuesSize(String template, Object... values) {
256         UriParser parser = new UriParserSpringImpl(template);
257         Set<String> variables = parser.getVariables();
258         if (variables.size() != values.length) {
259             throw new IncorrectNumberOfUriKeys(String.format("Expected %s variables: %s", variables.size(), variables));
260         }
261     }
262
263     protected String getTemplate(GraphInventoryObjectType type) {
264         return type.uriTemplate();
265     }
266
267     protected String getTemplate(GraphInventoryObjectPlurals type) {
268         return type.uriTemplate();
269     }
270
271     private void writeObject(ObjectOutputStream oos) throws IOException {
272         oos.defaultWriteObject();
273         oos.writeUTF(this.internalURI.toTemplate());
274     }
275
276     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
277         ois.defaultReadObject();
278         String uri = ois.readUTF();
279         this.setInternalURI(UriBuilder.fromUri(uri));
280     }
281 }