874b06e192b689a7b5b11d1056059301af1b92f4
[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.io.UnsupportedEncodingException;
28 import java.net.URI;
29 import java.nio.charset.StandardCharsets;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import javax.ws.rs.core.UriBuilder;
34
35 import org.apache.commons.lang3.builder.HashCodeBuilder;
36 import org.onap.so.client.aai.entities.uri.AAIUri;
37 import org.onap.so.client.graphinventory.Format;
38 import org.onap.so.client.graphinventory.GraphInventoryObjectPlurals;
39 import org.onap.so.client.graphinventory.GraphInventoryObjectType;
40 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParser;
41 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParserSpringImpl;
42 import org.springframework.web.util.UriUtils;
43
44 public class SimpleUri implements GraphInventoryResourceUri, Serializable {
45
46         private static final long serialVersionUID = -337701171277616439L;
47         
48         protected transient UriBuilder internalURI;
49         protected final static String relationshipAPI = "/relationship-list/relationship";
50         protected final static String relatedTo = "/related-to";
51         protected final Object[] values;
52         protected final GraphInventoryObjectType type;
53         protected final GraphInventoryObjectPlurals pluralType;
54         protected SimpleUri(GraphInventoryObjectType type, Object... values) {
55                 this.type = type;
56                 this.pluralType = null;
57                 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
58                 this.values = values;
59         }
60         protected SimpleUri(GraphInventoryObjectType type, URI uri) {
61                 this.type = type;
62                 this.pluralType = null;
63                 this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
64                 this.values = new Object[0];
65         }
66         protected SimpleUri(GraphInventoryObjectType type, UriBuilder builder, Object... values) {
67                 this.internalURI = builder;
68                 this.values = values;
69                 this.type = type;
70                 this.pluralType = null;
71         }
72         protected SimpleUri(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) {
73                 this.internalURI = builder;
74                 this.values = values;
75                 this.type = null;
76                 this.pluralType = type;
77         }
78         protected SimpleUri(GraphInventoryObjectPlurals type) {
79                 this.type = null;
80                 this.pluralType = type;
81                 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
82                 this.values = new Object[0];
83         }
84         protected SimpleUri(GraphInventoryObjectPlurals type, Object... values) {
85                 this.type = null;
86                 this.pluralType = type;
87                 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
88                 this.values = values;
89         }
90         protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectType childType, Object... childValues) {
91                 this.type = childType;
92                 this.pluralType = null;
93                 this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri());
94                 this.values = childValues;
95         }
96         
97         protected void setInternalURI(UriBuilder builder) {
98                 this.internalURI = builder;
99         }
100         @Override
101         public SimpleUri relationshipAPI() {
102                 this.internalURI = internalURI.path(relationshipAPI);
103                 return this;
104         }
105         
106         @Override
107         public SimpleUri relatedTo(GraphInventoryObjectPlurals plural) {
108                 
109                 this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
110                 return this;
111         }
112         @Override
113         public SimpleUri relatedTo(GraphInventoryObjectType type, String... values) {
114                 this.internalURI = internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
115                 return this;
116         }
117         
118         @Override
119         public SimpleUri resourceVersion(String version) {
120                 this.internalURI = internalURI.replaceQueryParam("resource-version", version);
121                 return this;
122         }
123         
124         @Override
125         public SimpleUri queryParam(String name, String... values) {
126                 this.internalURI = internalURI.queryParam(name, values);
127                 return this;
128         }
129         
130         @Override
131         public SimpleUri replaceQueryParam(String name, String... values) {
132                 this.internalURI = internalURI.replaceQueryParam(name, values);
133                 return this;
134         }
135         
136         @Override
137         public SimpleUri resultIndex(int index) {
138                 this.internalURI = internalURI.replaceQueryParam("resultIndex", index);
139                 return this;
140         }
141         
142         @Override
143         public SimpleUri resultSize(int size) {
144                 this.internalURI = internalURI.replaceQueryParam("resultSize", size);
145                 return this;
146         }
147         
148         @Override
149         public SimpleUri limit(int size) {
150                 return this.resultIndex(0).resultSize(size);
151         }
152         
153         @Override
154         public URI build() {
155                 return build(this.values);
156         }
157         
158         protected URI build(Object... values) {
159                 //This is a workaround because resteasy does not encode URIs correctly
160                 final String[] encoded = new String[values.length];
161                 for (int i = 0; i < values.length; i++) {
162                         try {
163                                 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
164                         } catch (UnsupportedEncodingException e) {
165                                 encoded[i] = values[i].toString();
166                         }
167                 }
168                 return internalURI.buildFromEncoded(encoded);
169         }
170         
171         @Override
172         public Map<String, String> getURIKeys() {
173                 return this.getURIKeys(this.build().toString());
174         }
175         
176         protected Map<String, String> getURIKeys(String uri) {
177                 UriParser parser;
178                 if (this.type != null) {
179                         if (!("".equals(this.getTemplate(type)))) {
180                                 parser = new UriParserSpringImpl(this.getTemplate(type));
181                         } else {
182                                 return new HashMap<>();
183                         }
184                 } else {
185                         parser = new UriParserSpringImpl(this.getTemplate(pluralType));
186                 }
187                 
188                 
189                 return parser.parse(uri);
190         }
191         
192         @Override
193         public SimpleUri clone() {
194                 if (this.type != null) {
195                         return new SimpleUri(this.type, this.internalURI.clone(), values);
196                 } else {
197                         return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
198                 }
199         }
200         
201         @Override
202         public GraphInventoryObjectType getObjectType() {
203                 return this.type;
204         }
205         
206         @Override
207         public boolean equals(Object o) {
208                 if (o instanceof AAIUri) {
209                         return this.build().equals(((AAIUri)o).build());
210                 }
211                 return false;
212         }
213         
214         @Override
215         public int hashCode() {         
216                 return new HashCodeBuilder().append(this.build()).toHashCode();
217         }
218         
219         
220         @Override
221         public SimpleUri depth(Depth depth) {
222                 this.internalURI.replaceQueryParam("depth", depth.toString());
223                 return this;
224         }
225         @Override
226         public SimpleUri nodesOnly(boolean nodesOnly) {
227                 if (nodesOnly) {
228                         this.internalURI.replaceQueryParam("nodes-only", "");
229                 }
230                 return this;
231         }
232         
233         @Override
234         public SimpleUri format(Format format) {
235                 this.internalURI.replaceQueryParam("format", format);
236                 return this;
237         }
238         
239         protected String getTemplate(GraphInventoryObjectType type) {
240                 return type.uriTemplate();
241         }
242         
243         protected String getTemplate(GraphInventoryObjectPlurals type) {
244                 return type.uriTemplate();
245         }
246         
247         private void writeObject(ObjectOutputStream oos) throws IOException {
248                 oos.defaultWriteObject();
249                 oos.writeUTF(this.internalURI.toTemplate());
250         }
251         
252         private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
253                  ois.defaultReadObject();
254                  String uri = ois.readUTF();
255                  this.setInternalURI(UriBuilder.fromUri(uri));
256         }
257 }