2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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=========================================================
20 package org.onap.aai.serialization.queryformats.utils;
22 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.onap.aai.exceptions.AAIException;
28 import org.onap.aai.introspection.Version;
29 import org.onap.aai.serialization.db.DBSerializer;
30 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
31 import org.onap.aai.util.AAIApiServerURLBase;
32 import org.onap.aai.util.AAIConstants;
33 import org.onap.aai.workarounds.LegacyURITransformer;
35 public class UrlBuilder {
37 private final DBSerializer serializer;
38 private final Version version;
39 private final String serverBase;
41 public UrlBuilder (Version version, DBSerializer serializer) throws AAIException {
42 this.serializer = serializer;
43 this.version = version;
44 this.serverBase = this.getServerBase(version);
47 public UrlBuilder (Version version, DBSerializer serializer, String serverBase) {
48 this.serializer = serializer;
49 this.version = version;
50 this.serverBase = serverBase;
53 public String pathed(Vertex v) throws AAIFormatVertexException {
56 final StringBuilder result = new StringBuilder();
57 final URI uri = this.serializer.getURIForVertex(v);
59 if (this.version.compareTo(Version.v11) >= 0) {
60 result.append(AAIConstants.AAI_APP_ROOT);
62 result.append(this.serverBase);
64 result.append(this.version);
67 return result.toString();
68 } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
69 throw new AAIFormatVertexException(e);
73 public String id(Vertex v) {
74 final StringBuilder result = new StringBuilder();
76 result.append("/resources/id/" + v.id());
77 result.insert(0, this.version);
78 if (this.version.compareTo(Version.v11) >= 0) {
79 result.insert(0, AAIConstants.AAI_APP_ROOT);
81 result.insert(0, this.serverBase);
84 return result.toString();
87 protected String getServerBase(Version v) throws AAIException {
88 return AAIApiServerURLBase.get(v);