2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.graphinventory.entities.uri;
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;
29 import java.nio.charset.StandardCharsets;
30 import java.util.HashMap;
33 import javax.ws.rs.core.UriBuilder;
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;
44 public class SimpleUri implements GraphInventoryResourceUri, Serializable {
46 private static final long serialVersionUID = -337701171277616439L;
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) {
56 this.pluralType = null;
57 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
60 protected SimpleUri(GraphInventoryObjectType type, URI uri) {
62 this.pluralType = null;
63 this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
64 this.values = new Object[0];
66 protected SimpleUri(GraphInventoryObjectType type, UriBuilder builder, Object... values) {
67 this.internalURI = builder;
70 this.pluralType = null;
72 protected SimpleUri(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) {
73 this.internalURI = builder;
76 this.pluralType = type;
78 protected SimpleUri(GraphInventoryObjectPlurals type) {
80 this.pluralType = type;
81 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
82 this.values = new Object[0];
84 protected SimpleUri(GraphInventoryObjectPlurals type, Object... values) {
86 this.pluralType = type;
87 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
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;
97 protected void setInternalURI(UriBuilder builder) {
98 this.internalURI = builder;
101 public SimpleUri relationshipAPI() {
102 this.internalURI = internalURI.path(relationshipAPI);
107 public SimpleUri relatedTo(GraphInventoryObjectPlurals plural) {
109 this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
113 public SimpleUri relatedTo(GraphInventoryObjectType type, String... values) {
114 this.internalURI = internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
119 public SimpleUri resourceVersion(String version) {
120 this.internalURI = internalURI.replaceQueryParam("resource-version", version);
125 public SimpleUri queryParam(String name, String... values) {
126 this.internalURI = internalURI.queryParam(name, values);
131 public SimpleUri replaceQueryParam(String name, String... values) {
132 this.internalURI = internalURI.replaceQueryParam(name, values);
137 public SimpleUri resultIndex(int index) {
138 this.internalURI = internalURI.replaceQueryParam("resultIndex", index);
143 public SimpleUri resultSize(int size) {
144 this.internalURI = internalURI.replaceQueryParam("resultSize", size);
149 public SimpleUri limit(int size) {
150 return this.resultIndex(0).resultSize(size);
155 return build(this.values);
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++) {
163 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
164 } catch (UnsupportedEncodingException e) {
165 encoded[i] = values[i].toString();
168 return internalURI.buildFromEncoded(encoded);
172 public Map<String, String> getURIKeys() {
173 return this.getURIKeys(this.build().toString());
176 protected Map<String, String> getURIKeys(String uri) {
178 if (this.type != null) {
179 if (!("".equals(this.getTemplate(type)))) {
180 parser = new UriParserSpringImpl(this.getTemplate(type));
182 return new HashMap<>();
185 parser = new UriParserSpringImpl(this.getTemplate(pluralType));
189 return parser.parse(uri);
193 public SimpleUri clone() {
194 if (this.type != null) {
195 return new SimpleUri(this.type, this.internalURI.clone(), values);
197 return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
202 public GraphInventoryObjectType getObjectType() {
207 public boolean equals(Object o) {
208 if (o instanceof AAIUri) {
209 return this.build().equals(((AAIUri)o).build());
215 public int hashCode() {
216 return new HashCodeBuilder().append(this.build()).toHashCode();
221 public SimpleUri depth(Depth depth) {
222 this.internalURI.replaceQueryParam("depth", depth.toString());
226 public SimpleUri nodesOnly(boolean nodesOnly) {
228 this.internalURI.replaceQueryParam("nodes-only", "");
234 public SimpleUri format(Format format) {
235 this.internalURI.replaceQueryParam("format", format);
239 protected String getTemplate(GraphInventoryObjectType type) {
240 return type.uriTemplate();
243 protected String getTemplate(GraphInventoryObjectPlurals type) {
244 return type.uriTemplate();
247 private void writeObject(ObjectOutputStream oos) throws IOException {
248 oos.defaultWriteObject();
249 oos.writeUTF(this.internalURI.toTemplate());
252 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
253 ois.defaultReadObject();
254 String uri = ois.readUTF();
255 this.setInternalURI(UriBuilder.fromUri(uri));