Springboot 2.0 upgrade
[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                                 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());                  
163                 }
164                 return internalURI.buildFromEncoded(encoded);
165         }
166         
167         @Override
168         public Map<String, String> getURIKeys() {
169                 return this.getURIKeys(this.build().toString());
170         }
171         
172         protected Map<String, String> getURIKeys(String uri) {
173                 UriParser parser;
174                 if (this.type != null) {
175                         if (!("".equals(this.getTemplate(type)))) {
176                                 parser = new UriParserSpringImpl(this.getTemplate(type));
177                         } else {
178                                 return new HashMap<>();
179                         }
180                 } else {
181                         parser = new UriParserSpringImpl(this.getTemplate(pluralType));
182                 }
183                 
184                 
185                 return parser.parse(uri);
186         }
187         
188         @Override
189         public SimpleUri clone() {
190                 if (this.type != null) {
191                         return new SimpleUri(this.type, this.internalURI.clone(), values);
192                 } else {
193                         return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
194                 }
195         }
196         
197         @Override
198         public GraphInventoryObjectType getObjectType() {
199                 return this.type;
200         }
201         
202         @Override
203         public boolean equals(Object o) {
204                 if (o instanceof AAIUri) {
205                         return this.build().equals(((AAIUri)o).build());
206                 }
207                 return false;
208         }
209         
210         @Override
211         public int hashCode() {         
212                 return new HashCodeBuilder().append(this.build()).toHashCode();
213         }
214         
215         
216         @Override
217         public SimpleUri depth(Depth depth) {
218                 this.internalURI.replaceQueryParam("depth", depth.toString());
219                 return this;
220         }
221         @Override
222         public SimpleUri nodesOnly(boolean nodesOnly) {
223                 if (nodesOnly) {
224                         this.internalURI.replaceQueryParam("nodes-only", "");
225                 }
226                 return this;
227         }
228         
229         @Override
230         public SimpleUri format(Format format) {
231                 this.internalURI.replaceQueryParam("format", format);
232                 return this;
233         }
234         
235         protected String getTemplate(GraphInventoryObjectType type) {
236                 return type.uriTemplate();
237         }
238         
239         protected String getTemplate(GraphInventoryObjectPlurals type) {
240                 return type.uriTemplate();
241         }
242         
243         private void writeObject(ObjectOutputStream oos) throws IOException {
244                 oos.defaultWriteObject();
245                 oos.writeUTF(this.internalURI.toTemplate());
246         }
247         
248         private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
249                  ois.defaultReadObject();
250                  String uri = ois.readUTF();
251                  this.setInternalURI(UriBuilder.fromUri(uri));
252         }
253 }