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;
28 import java.nio.charset.StandardCharsets;
29 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.graphinventory.Format;
37 import org.onap.so.client.graphinventory.GraphInventoryObjectPlurals;
38 import org.onap.so.client.graphinventory.GraphInventoryObjectType;
39 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParser;
40 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParserSpringImpl;
41 import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys;
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));
59 validateValuesSize(this.getTemplate(type), values);
61 protected SimpleUri(GraphInventoryObjectType type, URI uri) {
63 this.pluralType = null;
64 this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
65 this.values = new Object[0];
67 protected SimpleUri(GraphInventoryObjectType type, UriBuilder builder, Object... values) {
68 this.internalURI = builder;
71 this.pluralType = null;
73 protected SimpleUri(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) {
74 this.internalURI = builder;
77 this.pluralType = type;
79 protected SimpleUri(GraphInventoryObjectPlurals type) {
81 this.pluralType = type;
82 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
83 this.values = new Object[0];
85 protected SimpleUri(GraphInventoryObjectPlurals type, Object... values) {
87 this.pluralType = type;
88 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
90 validateValuesSize(this.getTemplate(type), values);
92 protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectType childType, Object... childValues) {
93 this.type = childType;
94 this.pluralType = null;
95 this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri());
96 this.values = childValues;
97 validateValuesSize(childType.partialUri(), values);
100 protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectPlurals childType) {
102 this.pluralType = childType;
103 this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri());
104 this.values = new Object[0];
107 protected void setInternalURI(UriBuilder builder) {
108 this.internalURI = builder;
111 public SimpleUri relationshipAPI() {
112 this.internalURI = internalURI.path(relationshipAPI);
117 public SimpleUri relatedTo(GraphInventoryObjectPlurals plural) {
119 this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
123 public SimpleUri relatedTo(GraphInventoryObjectType type, String... values) {
124 this.internalURI = internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
129 public SimpleUri resourceVersion(String version) {
130 this.internalURI = internalURI.replaceQueryParam("resource-version", version);
135 public SimpleUri queryParam(String name, String... values) {
136 this.internalURI = internalURI.queryParam(name, values);
141 public SimpleUri replaceQueryParam(String name, String... values) {
142 this.internalURI = internalURI.replaceQueryParam(name, values);
147 public SimpleUri resultIndex(int index) {
148 this.internalURI = internalURI.replaceQueryParam("resultIndex", index);
153 public SimpleUri resultSize(int size) {
154 this.internalURI = internalURI.replaceQueryParam("resultSize", size);
159 public SimpleUri limit(int size) {
160 return this.resultIndex(0).resultSize(size);
165 return build(this.values);
168 protected URI build(Object... values) {
169 //This is a workaround because resteasy does not encode URIs correctly
170 final String[] encoded = new String[values.length];
171 for (int i = 0; i < values.length; i++) {
172 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
174 return internalURI.buildFromEncoded(encoded);
178 public Map<String, String> getURIKeys() {
179 return this.getURIKeys(this.build().toString());
182 protected Map<String, String> getURIKeys(String uri) {
184 if (this.type != null) {
185 if (!("".equals(this.getTemplate(type)))) {
186 parser = new UriParserSpringImpl(this.getTemplate(type));
188 return new HashMap<>();
191 parser = new UriParserSpringImpl(this.getTemplate(pluralType));
195 return parser.parse(uri);
199 public SimpleUri clone() {
200 if (this.type != null) {
201 return new SimpleUri(this.type, this.internalURI.clone(), values);
203 return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
208 public GraphInventoryObjectType getObjectType() {
213 public boolean equals(Object o) {
214 if (o instanceof GraphInventoryUri) {
215 return this.build().equals(((GraphInventoryUri)o).build());
221 public int hashCode() {
222 return new HashCodeBuilder().append(this.build()).toHashCode();
227 public SimpleUri depth(Depth depth) {
228 this.internalURI.replaceQueryParam("depth", depth.toString());
232 public SimpleUri nodesOnly(boolean nodesOnly) {
234 this.internalURI.replaceQueryParam("nodes-only", "");
240 public SimpleUri format(Format format) {
241 this.internalURI.replaceQueryParam("format", format);
245 public void validateValuesSize(String template, Object... values) {
246 UriParser parser = new UriParserSpringImpl(template);
247 Set<String> variables = parser.getVariables();
248 if (variables.size() != values.length) {
249 throw new IncorrectNumberOfUriKeys(String.format("Expected %s variables: %s", variables.size(), variables));
253 protected String getTemplate(GraphInventoryObjectType type) {
254 return type.uriTemplate();
257 protected String getTemplate(GraphInventoryObjectPlurals type) {
258 return type.uriTemplate();
261 private void writeObject(ObjectOutputStream oos) throws IOException {
262 oos.defaultWriteObject();
263 oos.writeUTF(this.internalURI.toTemplate());
266 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
267 ois.defaultReadObject();
268 String uri = ois.readUTF();
269 this.setInternalURI(UriBuilder.fromUri(uri));