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.springframework.web.util.UriUtils;
33 import org.openecomp.aai.exceptions.AAIException;
34 import org.openecomp.aai.introspection.Introspector;
35 import org.openecomp.aai.introspection.Loader;
36 import org.openecomp.aai.introspection.LoaderFactory;
37 import org.openecomp.aai.introspection.Version;
38 import org.openecomp.aai.logging.ErrorLogHelper;
39 import org.openecomp.aai.schema.enums.ObjectMetadata;
40 import org.openecomp.aai.util.AAIConfig;
44 * The Class URIParser.
46 public class URIParser {
48 private URI uri = null;
50 protected Loader loader = null;
52 protected Loader originalLoader = null;
54 private URI originalURI = null;
56 private MultivaluedMap<String, String> queryParams = null;
60 * Instantiates a new URI parser.
62 * @param loader the loader
65 public URIParser(Loader loader, URI uri) {
68 String currentVersion = "v7";
69 this.originalLoader = loader;
71 currentVersion = AAIConfig.get("aai.default.api.version");
72 } catch (AAIException e) {
73 ErrorLogHelper.logException(e);
76 //Load the latest version because we need it for cloud region
82 * Instantiates a new URI parser.
84 * @param loader the loader
86 * @param queryParams the query params
88 public URIParser(Loader loader, URI uri, MultivaluedMap<String, String> queryParams) {
90 this.queryParams = queryParams;
93 public Loader getLoader() {
100 * Gets the original URI.
102 * @return the original URI
104 public URI getOriginalURI() {
105 return this.originalURI;
112 * @throws UnsupportedEncodingException the unsupported encoding exception
113 * @throws AAIException the AAI exception
115 public void parse(Parsable p) throws UnsupportedEncodingException, AAIException {
117 boolean isRelative = false;
118 uri = this.trimURI(uri);
119 uri = handleCloudRegion(p.getCloudRegionTransform(), uri);
120 if (p.useOriginalLoader()) {
121 this.loader = this.originalLoader;
123 this.originalURI = UriBuilder.fromPath(uri.getRawPath()).build();
124 if (uri.getRawPath().startsWith("./")) {
125 uri = new URI(uri.getRawPath().replaceFirst("\\./", ""));
128 String[] parts = uri.getRawPath().split("/");
129 Introspector validNamespaces = loader.introspectorFromName("inventory");
130 Set<String> keys = null;
132 Introspector previousObj = null;
134 for (int i = 0; i < parts.length;) {
136 Introspector introspector = null;
137 introspector = loader.introspectorFromName(part);
138 if (introspector != null) {
140 //previous has current as property
141 if (previousObj != null && !previousObj.hasChild(introspector) && !previousObj.getDbName().equals("nodes")) {
142 throw new AAIException("AAI_3001", uri + " not a valid path. " + part + " not valid");
143 } else if (previousObj == null) {
144 String abstractType = introspector.getMetadata(ObjectMetadata.ABSTRACT);
145 if (abstractType == null) {
148 //first time through, make sure it starts from a namespace
149 //ignore abstract types
150 if (!isRelative && !abstractType.equals("true") && !validNamespaces.hasChild(introspector)) {
151 throw new AAIException("AAI_3000", uri + " not a valid path. It does not start from a valid namespace");
155 keys = introspector.getKeys();
156 if (keys.size() > 0) {
157 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
159 if (i == parts.length && queryParams != null) {
160 Set<String> queryKeys = queryParams.keySet();
161 for (String key : queryKeys) {
162 uriKeys.put(key, queryParams.get(key));
165 for (String key : keys) {
166 part = UriUtils.decode(parts[i], "UTF-8");
168 introspector.setValue(key, part);
170 //skip this for further processing
175 p.processObject(introspector, uriKeys);
177 } else if (introspector.isContainer()) {
178 boolean isFinalContainer = i == parts.length-1;
179 MultivaluedMap<String, String> uriKeys = new MultivaluedHashMap<>();
181 if (isFinalContainer && queryParams != null) {
182 Set<String> queryKeys = queryParams.keySet();
183 for (String key : queryKeys) {
184 uriKeys.put(key, queryParams.get(key));
188 p.processContainer(introspector, uriKeys, isFinalContainer);
192 p.processNamespace(introspector);
196 previousObj = introspector;
198 //invalid item found should log
199 //original said bad path
200 throw new AAIException("AAI_3001", "invalid item found in path: " + part);
203 } catch (AAIException e) {
205 } catch (Exception e) {
206 throw new AAIException("AAI_3001", e);
210 public boolean validate() throws UnsupportedEncodingException, AAIException {
211 this.parse(new URIValidate());
215 * Handle cloud region.
217 * @param action the action
221 protected URI handleCloudRegion(String action, URI uri) {
233 protected URI trimURI(URI uri) {
235 String result = uri.getRawPath();
236 if (result.startsWith("/")) {
237 result = result.substring(1, result.length());
240 if (result.endsWith("/")) {
241 result = result.substring(0, result.length() - 1);
244 result = result.replaceFirst("aai/v\\d+/", "");
246 return UriBuilder.fromPath(result).build();