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.openecomp.mso.client.aai.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.openecomp.mso.client.aai.AAIObjectPlurals;
32 import org.openecomp.mso.client.aai.AAIObjectType;
33 import org.openecomp.mso.client.aai.entities.uri.parsers.UriParser;
34 import org.openecomp.mso.client.aai.entities.uri.parsers.UriParserSpringImpl;
35 import org.springframework.web.util.UriUtils;
37 public class SimpleUri implements AAIResourceUri {
39 protected UriBuilder internalURI;
40 protected final static String relationshipAPI = "/relationship-list/relationship";
41 protected final static String relatedTo = "/related-to";
42 protected final Object[] values;
43 protected final AAIObjectType type;
44 protected final AAIObjectPlurals pluralType;
45 protected SimpleUri(AAIObjectType type, Object... values) {
47 this.pluralType = null;
48 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
51 protected SimpleUri(AAIObjectType type, URI uri) {
53 this.pluralType = null;
54 this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
55 this.values = new Object[0];
57 protected SimpleUri(AAIObjectType type, UriBuilder builder, Object... values) {
58 this.internalURI = builder;
61 this.pluralType = null;
63 protected SimpleUri(AAIObjectPlurals type, UriBuilder builder, Object... values) {
64 this.internalURI = builder;
67 this.pluralType = type;
69 protected SimpleUri(AAIObjectPlurals type) {
71 this.pluralType = type;
72 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
73 this.values = new Object[0];
77 public SimpleUri relationshipAPI() {
78 this.internalURI = internalURI.path(relationshipAPI);
83 public SimpleUri relatedTo(AAIObjectPlurals plural) {
85 this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
89 public SimpleUri relatedTo(AAIObjectType type, String... values) {
90 this.internalURI = internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
95 public SimpleUri resourceVersion(String version) {
96 this.internalURI = internalURI.queryParam("resource-version", version);
101 public SimpleUri queryParam(String name, String... values) {
102 this.internalURI = internalURI.queryParam(name, values);
108 return build(this.values);
111 protected URI build(Object... values) {
112 //This is a workaround because resteasy does not encode URIs correctly
113 final String[] encoded = new String[values.length];
114 for (int i = 0; i < values.length; i++) {
116 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
117 } catch (UnsupportedEncodingException e) {
118 encoded[i] = values[i].toString();
121 return internalURI.buildFromEncoded(encoded);
125 public Map<String, String> getURIKeys() {
126 return this.getURIKeys(this.build().toString());
129 protected Map<String, String> getURIKeys(String uri) {
131 if (this.type != null) {
132 if (!("".equals(this.getTemplate(type)))) {
133 parser = new UriParserSpringImpl(this.getTemplate(type));
135 return new HashMap<>();
138 parser = new UriParserSpringImpl(this.getTemplate(pluralType));
142 return parser.parse(uri);
146 public SimpleUri clone() {
147 if (this.type != null) {
148 return new SimpleUri(this.type, this.internalURI.clone(), values);
150 return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
155 public AAIObjectType getObjectType() {
160 public boolean equals(Object o) {
161 if (o instanceof AAIUri) {
162 return this.build().equals(((AAIUri)o).build());
167 public SimpleUri depth(Depth depth) {
168 this.internalURI.queryParam("depth", depth.toString());
172 public SimpleUri nodesOnly(boolean nodesOnly) {
174 this.internalURI.queryParam("nodes-only", "");
179 protected String getTemplate(AAIObjectType type) {
180 return type.uriTemplate();
183 protected String getTemplate(AAIObjectPlurals type) {
184 return type.uriTemplate();