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.aai.parsers.uri;
23 import java.io.UnsupportedEncodingException;
27 import javax.ws.rs.core.MultivaluedHashMap;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.UriBuilder;
31 import org.openecomp.aai.exceptions.AAIException;
32 import org.openecomp.aai.introspection.Introspector;
33 import org.openecomp.aai.introspection.Loader;
34 import org.openecomp.aai.introspection.LoaderFactory;
35 import org.openecomp.aai.introspection.Version;
36 import org.openecomp.aai.logging.ErrorLogHelper;
37 import org.openecomp.aai.parsers.exceptions.DoesNotStartWithValidNamespaceException;
38 import org.openecomp.aai.rest.RestTokens;
39 import org.openecomp.aai.schema.enums.ObjectMetadata;
40 import org.openecomp.aai.serialization.db.EdgeType;
41 import org.openecomp.aai.util.AAIConfig;
42 import org.springframework.web.util.UriUtils;
46 * The Class URIParser.
48 public class URIParser {
50 private URI uri = null;
52 protected Loader loader = null;
54 protected Loader originalLoader = null;
56 private URI originalURI = null;
58 private MultivaluedMap<String, String> queryParams = null;
62 * Instantiates a new URI parser.
64 * @param loader the loader
67 public URIParser(Loader loader, URI uri) {
70 String currentVersion = "v7";
71 this.originalLoader = loader;
73 currentVersion = AAIConfig.get("aai.default.api.version");
74 } catch (AAIException e) {
75 ErrorLogHelper.logException(e);
78 //Load the latest version because we need it for cloud region
84 * Instantiates a new URI parser.
86 * @param loader the loader
88 * @param queryParams the query params
90 public URIParser(Loader loader, URI uri, MultivaluedMap<String, String> queryParams) {
92 this.queryParams = queryParams;
95 public Loader getLoader() {
102 * Gets the original URI.
104 * @return the original URI
106 public URI getOriginalURI() {
107 return this.originalURI;
114 * @throws UnsupportedEncodingException the unsupported encoding exception
115 * @throws AAIException the AAI exception
117 public void parse(Parsable p) throws UnsupportedEncodingException, AAIException {
119 boolean isRelative = false;
120 uri = this.trimURI(uri);
121 uri = handleCloudRegion(p.getCloudRegionTransform(), uri);
122 if (p.useOriginalLoader()) {
123 this.loader = this.originalLoader;
125 this.originalURI = UriBuilder.fromPath(uri.getRawPath()).build();
126 if (uri.getRawPath().startsWith("./")) {
127 uri = new URI(uri.getRawPath().replaceFirst("\\./", ""));
130 String[] parts = uri.getRawPath().split("/");
131 Introspector validNamespaces = loader.introspectorFromName("inventory");
132 Set<String> keys = null;
134 Introspector previousObj = null;
135 EdgeType type = EdgeType.TREE;
136 for (int i = 0; i < parts.length;) {
138 Introspector introspector = null;
139 if (part.equals(RestTokens.COUSIN.toString())) {
140 if (i == parts.length-1) {
141 throw new AAIException("AAI_3000", uri + " not a valid path. Cannot end in " + RestTokens.COUSIN);
143 introspector = loader.introspectorFromName(parts[i+1]);
144 if (previousObj.isContainer() && introspector.isContainer()) {
145 throw new AAIException("AAI_3000", uri + " not a valid path. Cannot chain plurals together");
147 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
148 if (i == parts.length-2 && queryParams != null) {
149 Set<String> queryKeys = queryParams.keySet();
150 for (String key : queryKeys) {
151 uriKeys.put(key, queryParams.get(key));
155 if (introspector.isContainer()) {
156 boolean isFinalContainer = i == parts.length-2;
157 p.processContainer(introspector, EdgeType.COUSIN, uriKeys, isFinalContainer);
159 previousObj = introspector;
160 type = EdgeType.COUSIN;
164 introspector = loader.introspectorFromName(part);
165 if (introspector != null) {
167 //previous has current as property
168 if (previousObj != null && !previousObj.hasChild(introspector) && !previousObj.getDbName().equals("nodes")) {
169 throw new AAIException("AAI_3001", uri + " not a valid path. " + part + " not valid");
170 } else if (previousObj == null) {
171 String abstractType = introspector.getMetadata(ObjectMetadata.ABSTRACT);
172 if (abstractType == null) {
175 //first time through, make sure it starts from a namespace
176 //ignore abstract types
177 if (!isRelative && !abstractType.equals("true") && !validNamespaces.hasChild(introspector)) {
178 throw new DoesNotStartWithValidNamespaceException( uri + " not a valid path. It does not start from a valid namespace");
182 keys = introspector.getKeys();
183 if (keys.size() > 0) {
184 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
186 if (i == parts.length && queryParams != null) {
187 Set<String> queryKeys = queryParams.keySet();
188 for (String key : queryKeys) {
189 uriKeys.put(key, queryParams.get(key));
192 for (String key : keys) {
193 part = UriUtils.decode(parts[i], "UTF-8");
195 introspector.setValue(key, part);
197 //skip this for further processing
202 p.processObject(introspector, type, uriKeys);
203 type = EdgeType.TREE;
204 } else if (introspector.isContainer()) {
205 boolean isFinalContainer = i == parts.length-1;
206 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
208 if (isFinalContainer && queryParams != null) {
209 Set<String> queryKeys = queryParams.keySet();
210 for (String key : queryKeys) {
211 uriKeys.put(key, queryParams.get(key));
215 p.processContainer(introspector, type, uriKeys, isFinalContainer);
219 p.processNamespace(introspector);
223 previousObj = introspector;
225 //invalid item found should log
226 //original said bad path
227 throw new AAIException("AAI_3001", "invalid item found in path: " + part);
230 } catch (AAIException e) {
232 } catch (Exception e) {
233 throw new AAIException("AAI_3001", e);
237 public boolean validate() throws UnsupportedEncodingException, AAIException {
238 this.parse(new URIValidate());
242 * Handle cloud region.
244 * @param action the action
248 protected URI handleCloudRegion(String action, URI uri) {
260 protected URI trimURI(URI uri) {
262 String result = uri.getRawPath();
263 if (result.startsWith("/")) {
264 result = result.substring(1, result.length());
267 if (result.endsWith("/")) {
268 result = result.substring(0, result.length() - 1);
271 result = result.replaceFirst("aai/v\\d+/", "");
273 return UriBuilder.fromPath(result).build();