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.UnsupportedEncodingException;
25 import java.nio.charset.StandardCharsets;
26 import java.util.HashMap;
29 import javax.ws.rs.core.UriBuilder;
31 import org.apache.commons.lang3.builder.HashCodeBuilder;
32 import org.onap.so.client.graphinventory.Format;
33 import org.onap.so.client.aai.entities.uri.AAIUri;
34 import org.onap.so.client.graphinventory.entities.uri.Depth;
35 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParser;
36 import org.onap.so.client.graphinventory.entities.uri.parsers.UriParserSpringImpl;
37 import org.onap.so.client.graphinventory.GraphInventoryObjectPlurals;
38 import org.onap.so.client.graphinventory.GraphInventoryObjectType;
39 import org.springframework.web.util.UriUtils;
41 public class SimpleUri implements GraphInventoryResourceUri {
43 protected UriBuilder internalURI;
44 protected final static String relationshipAPI = "/relationship-list/relationship";
45 protected final static String relatedTo = "/related-to";
46 protected final Object[] values;
47 protected final GraphInventoryObjectType type;
48 protected final GraphInventoryObjectPlurals pluralType;
49 protected SimpleUri(GraphInventoryObjectType type, Object... values) {
51 this.pluralType = null;
52 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
55 protected SimpleUri(GraphInventoryObjectType type, URI uri) {
57 this.pluralType = null;
58 this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
59 this.values = new Object[0];
61 protected SimpleUri(GraphInventoryObjectType type, UriBuilder builder, Object... values) {
62 this.internalURI = builder;
65 this.pluralType = null;
67 protected SimpleUri(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) {
68 this.internalURI = builder;
71 this.pluralType = type;
73 protected SimpleUri(GraphInventoryObjectPlurals type) {
75 this.pluralType = type;
76 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
77 this.values = new Object[0];
79 protected SimpleUri(GraphInventoryObjectPlurals type, Object... values) {
81 this.pluralType = type;
82 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
87 public SimpleUri relationshipAPI() {
88 this.internalURI = internalURI.path(relationshipAPI);
93 public SimpleUri relatedTo(GraphInventoryObjectPlurals plural) {
95 this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
99 public SimpleUri relatedTo(GraphInventoryObjectType type, String... values) {
100 this.internalURI = internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
105 public SimpleUri resourceVersion(String version) {
106 this.internalURI = internalURI.replaceQueryParam("resource-version", version);
111 public SimpleUri queryParam(String name, String... values) {
112 this.internalURI = internalURI.queryParam(name, values);
117 public SimpleUri replaceQueryParam(String name, String... values) {
118 this.internalURI = internalURI.replaceQueryParam(name, values);
124 return build(this.values);
127 protected URI build(Object... values) {
128 //This is a workaround because resteasy does not encode URIs correctly
129 final String[] encoded = new String[values.length];
130 for (int i = 0; i < values.length; i++) {
132 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
133 } catch (UnsupportedEncodingException e) {
134 encoded[i] = values[i].toString();
137 return internalURI.buildFromEncoded(encoded);
141 public Map<String, String> getURIKeys() {
142 return this.getURIKeys(this.build().toString());
145 protected Map<String, String> getURIKeys(String uri) {
147 if (this.type != null) {
148 if (!("".equals(this.getTemplate(type)))) {
149 parser = new UriParserSpringImpl(this.getTemplate(type));
151 return new HashMap<>();
154 parser = new UriParserSpringImpl(this.getTemplate(pluralType));
158 return parser.parse(uri);
162 public SimpleUri clone() {
163 if (this.type != null) {
164 return new SimpleUri(this.type, this.internalURI.clone(), values);
166 return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
171 public GraphInventoryObjectType getObjectType() {
176 public boolean equals(Object o) {
177 if (o instanceof AAIUri) {
178 return this.build().equals(((AAIUri)o).build());
184 public int hashCode() {
185 return new HashCodeBuilder().append(this.build()).toHashCode();
190 public SimpleUri depth(Depth depth) {
191 this.internalURI.replaceQueryParam("depth", depth.toString());
195 public SimpleUri nodesOnly(boolean nodesOnly) {
197 this.internalURI.replaceQueryParam("nodes-only", "");
203 public SimpleUri format(Format format) {
204 this.internalURI.replaceQueryParam("format", format);
208 protected String getTemplate(GraphInventoryObjectType type) {
209 return type.uriTemplate();
212 protected String getTemplate(GraphInventoryObjectPlurals type) {
213 return type.uriTemplate();