Merge "Reorder modifiers"
[so.git] / common / src / main / java / org / openecomp / mso / client / aai / entities / uri / SimpleUri.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.mso.client.aai.entities.uri;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.nio.charset.StandardCharsets;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.ws.rs.core.UriBuilder;
30
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;
36
37 public class SimpleUri implements AAIResourceUri {
38
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) {
46                 this.type = type;
47                 this.pluralType = null;
48                 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
49                 this.values = values;
50         }
51         protected SimpleUri(AAIObjectType type, URI uri) {
52                 this.type = type;
53                 this.pluralType = null;
54                 this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
55                 this.values = new Object[0];
56         }
57         protected SimpleUri(AAIObjectType type, UriBuilder builder, Object... values) {
58                 this.internalURI = builder;
59                 this.values = values;
60                 this.type = type;
61                 this.pluralType = null;
62         }
63         protected SimpleUri(AAIObjectPlurals type, UriBuilder builder, Object... values) {
64                 this.internalURI = builder;
65                 this.values = values;
66                 this.type = null;
67                 this.pluralType = type;
68         }
69         protected SimpleUri(AAIObjectPlurals type) {
70                 this.type = null;
71                 this.pluralType = type;
72                 this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
73                 this.values = new Object[0];
74         }
75         
76         @Override
77         public SimpleUri relationshipAPI() {
78                 this.internalURI = internalURI.path(relationshipAPI);
79                 return this;
80         }
81         
82         @Override
83         public SimpleUri relatedTo(AAIObjectPlurals plural) {
84                 
85                 this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
86                 return this;
87         }
88         @Override
89         public SimpleUri relatedTo(AAIObjectType type, String... values) {
90                 this.internalURI = internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString());
91                 return this;
92         }
93         
94         @Override
95         public SimpleUri resourceVersion(String version) {
96                 this.internalURI = internalURI.queryParam("resource-version", version);
97                 return this;
98         }
99         
100         @Override
101         public SimpleUri queryParam(String name, String... values) {
102                 this.internalURI = internalURI.queryParam(name, values);
103                 return this;
104         }
105         
106         @Override
107         public URI build() {
108                 return build(this.values);
109         }
110         
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++) {
115                         try {
116                                 encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
117                         } catch (UnsupportedEncodingException e) {
118                                 encoded[i] = values[i].toString();
119                         }
120                 }
121                 return internalURI.buildFromEncoded(encoded);
122         }
123         
124         @Override
125         public Map<String, String> getURIKeys() {
126                 return this.getURIKeys(this.build().toString());
127         }
128         
129         protected Map<String, String> getURIKeys(String uri) {
130                 UriParser parser;
131                 if (this.type != null) {
132                         if (!("".equals(this.getTemplate(type)))) {
133                                 parser = new UriParserSpringImpl(this.getTemplate(type));
134                         } else {
135                                 return new HashMap<>();
136                         }
137                 } else {
138                         parser = new UriParserSpringImpl(this.getTemplate(pluralType));
139                 }
140                 
141                 
142                 return parser.parse(uri);
143         }
144         
145         @Override
146         public SimpleUri clone() {
147                 if (this.type != null) {
148                         return new SimpleUri(this.type, this.internalURI.clone(), values);
149                 } else {
150                         return new SimpleUri(this.pluralType, this.internalURI.clone(), values);
151                 }
152         }
153         
154         @Override
155         public AAIObjectType getObjectType() {
156                 return this.type;
157         }
158         
159         @Override
160         public boolean equals(Object o) {
161                 if (o instanceof AAIUri) {
162                         return this.build().equals(((AAIUri)o).build());
163                 }
164                 return false;
165         }
166         @Override
167         public SimpleUri depth(Depth depth) {
168                 this.internalURI.queryParam("depth", depth.toString());
169                 return this;
170         }
171         @Override
172         public SimpleUri nodesOnly(boolean nodesOnly) {
173                 if (nodesOnly) {
174                         this.internalURI.queryParam("nodes-only", "");
175                 }
176                 return this;
177         }
178         
179         protected String getTemplate(AAIObjectType type) {
180                 return type.uriTemplate();
181         }
182         
183         protected String getTemplate(AAIObjectPlurals type) {
184                 return type.uriTemplate();
185         }
186         
187 }