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++) {
162 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
164 return internalURI.buildFromEncoded(encoded);
168 public Map<String, String> getURIKeys() {
169 return this.getURIKeys(this.build().toString());
172 protected Map<String, String> getURIKeys(String uri) {
174 if (this.type != null) {
175 if (!("".equals(this.getTemplate(type)))) {
176 parser = new UriParserSpringImpl(this.getTemplate(type));
178 return new HashMap<>();
181 parser = new UriParserSpringImpl(this.getTemplate(pluralType));
185 return parser.parse(uri);
189 public SimpleUri clone() {
190 if (this.type != null) {
191 return new SimpleUri(this.type, this.internalURI.clone(), values);
193 return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
198 public GraphInventoryObjectType getObjectType() {
203 public boolean equals(Object o) {
204 if (o instanceof AAIUri) {
205 return this.build().equals(((AAIUri)o).build());
211 public int hashCode() {
212 return new HashCodeBuilder().append(this.build()).toHashCode();
217 public SimpleUri depth(Depth depth) {
218 this.internalURI.replaceQueryParam("depth", depth.toString());
222 public SimpleUri nodesOnly(boolean nodesOnly) {
224 this.internalURI.replaceQueryParam("nodes-only", "");
230 public SimpleUri format(Format format) {
231 this.internalURI.replaceQueryParam("format", format);
235 protected String getTemplate(GraphInventoryObjectType type) {
236 return type.uriTemplate();
239 protected String getTemplate(GraphInventoryObjectPlurals type) {
240 return type.uriTemplate();
243 private void writeObject(ObjectOutputStream oos) throws IOException {
244 oos.defaultWriteObject();
245 oos.writeUTF(this.internalURI.toTemplate());
248 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
249 ois.defaultReadObject();
250 String uri = ois.readUTF();
251 this.setInternalURI(UriBuilder.fromUri(uri));