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;
32 import javax.ws.rs.core.UriBuilder;
33 import org.apache.commons.lang3.builder.HashCodeBuilder;
34 import org.onap.so.client.graphinventory.Format;
35 import org.onap.so.client.graphinventory.GraphInventoryObjectPlurals;
36 import org.onap.so.client.graphinventory.GraphInventoryObjectType;
37 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParser;
38 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParserSpringImpl;
39 import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys;
40 import org.springframework.web.util.UriUtils;
42 public class SimpleUri implements GraphInventoryResourceUri, Serializable {
44 private static final long serialVersionUID = -337701171277616439L;
46 protected transient UriBuilder internalURI;
47 protected final static String relationshipAPI = "/relationship-list/relationship";
48 protected final static String relatedTo = "/related-to";
49 protected final Object[] values;
50 protected final GraphInventoryObjectType type;
51 protected final GraphInventoryObjectPlurals pluralType;
53 protected SimpleUri(GraphInventoryObjectType type, Object... values) {
55 this.pluralType = null;
56 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
58 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];
68 protected SimpleUri(GraphInventoryObjectType type, UriBuilder builder, Object... values) {
69 this.internalURI = builder;
72 this.pluralType = null;
75 protected SimpleUri(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) {
76 this.internalURI = builder;
79 this.pluralType = type;
82 protected SimpleUri(GraphInventoryObjectPlurals type) {
84 this.pluralType = type;
85 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
86 this.values = new Object[0];
89 protected SimpleUri(GraphInventoryObjectPlurals type, Object... values) {
91 this.pluralType = type;
92 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
94 validateValuesSize(this.getTemplate(type), values);
97 protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectType childType,
98 Object... childValues) {
99 this.type = childType;
100 this.pluralType = null;
101 this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri());
102 this.values = childValues;
103 validateValuesSize(childType.partialUri(), values);
106 protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectPlurals childType) {
108 this.pluralType = childType;
109 this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri());
110 this.values = new Object[0];
113 protected void setInternalURI(UriBuilder builder) {
114 this.internalURI = builder;
118 public SimpleUri relationshipAPI() {
119 this.internalURI = internalURI.path(relationshipAPI);
124 public SimpleUri relatedTo(GraphInventoryObjectPlurals plural) {
126 this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
131 public SimpleUri relatedTo(GraphInventoryObjectType type, String... values) {
133 internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
138 public SimpleUri resourceVersion(String version) {
139 this.internalURI = internalURI.replaceQueryParam("resource-version", version);
144 public SimpleUri queryParam(String name, String... values) {
145 this.internalURI = internalURI.queryParam(name, values);
150 public SimpleUri replaceQueryParam(String name, String... values) {
151 this.internalURI = internalURI.replaceQueryParam(name, values);
156 public SimpleUri resultIndex(int index) {
157 this.internalURI = internalURI.replaceQueryParam("resultIndex", index);
162 public SimpleUri resultSize(int size) {
163 this.internalURI = internalURI.replaceQueryParam("resultSize", size);
168 public SimpleUri limit(int size) {
169 return this.resultIndex(0).resultSize(size);
174 return build(this.values);
177 protected URI build(Object... values) {
178 // This is a workaround because resteasy does not encode URIs correctly
179 final String[] encoded = new String[values.length];
180 for (int i = 0; i < values.length; i++) {
181 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
183 return internalURI.buildFromEncoded(encoded);
187 public Map<String, String> getURIKeys() {
188 return this.getURIKeys(this.build().toString());
191 protected Map<String, String> getURIKeys(String uri) {
193 if (this.type != null) {
194 if (!("".equals(this.getTemplate(type)))) {
195 parser = new UriParserSpringImpl(this.getTemplate(type));
197 return new HashMap<>();
200 parser = new UriParserSpringImpl(this.getTemplate(pluralType));
204 return parser.parse(uri);
208 public SimpleUri clone() {
209 if (this.type != null) {
210 return new SimpleUri(this.type, this.internalURI.clone(), values);
212 return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
217 public GraphInventoryObjectType getObjectType() {
222 public boolean equals(Object o) {
223 if (o instanceof GraphInventoryUri) {
224 return this.build().equals(((GraphInventoryUri) o).build());
230 public int hashCode() {
231 return new HashCodeBuilder().append(this.build()).toHashCode();
236 public SimpleUri depth(Depth depth) {
237 this.internalURI.replaceQueryParam("depth", depth.toString());
242 public SimpleUri nodesOnly(boolean nodesOnly) {
244 this.internalURI.replaceQueryParam("nodes-only", "");
250 public SimpleUri format(Format format) {
251 this.internalURI.replaceQueryParam("format", format);
255 public void validateValuesSize(String template, Object... values) {
256 UriParser parser = new UriParserSpringImpl(template);
257 Set<String> variables = parser.getVariables();
258 if (variables.size() != values.length) {
259 throw new IncorrectNumberOfUriKeys(String.format("Expected %s variables: %s", variables.size(), variables));
263 protected String getTemplate(GraphInventoryObjectType type) {
264 return type.uriTemplate();
267 protected String getTemplate(GraphInventoryObjectPlurals type) {
268 return type.uriTemplate();
271 private void writeObject(ObjectOutputStream oos) throws IOException {
272 oos.defaultWriteObject();
273 oos.writeUTF(this.internalURI.toTemplate());
276 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
277 ois.defaultReadObject();
278 String uri = ois.readUTF();
279 this.setInternalURI(UriBuilder.fromUri(uri));