2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.queryformats.utils;
24 import java.io.UnsupportedEncodingException;
26 import java.net.URISyntaxException;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.Version;
31 import org.onap.aai.serialization.db.DBSerializer;
32 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.onap.aai.util.AAIApiServerURLBase;
34 import org.onap.aai.util.AAIConstants;
35 import org.onap.aai.workarounds.LegacyURITransformer;
37 public class UrlBuilder {
39 private final DBSerializer serializer;
40 private final Version version;
41 private final String serverBase;
43 public UrlBuilder (Version version, DBSerializer serializer) throws AAIException {
44 this.serializer = serializer;
45 this.version = version;
46 this.serverBase = this.getServerBase(version);
49 public UrlBuilder (Version version, DBSerializer serializer, String serverBase) {
50 this.serializer = serializer;
51 this.version = version;
52 this.serverBase = serverBase;
55 public String pathed(Vertex v) throws AAIFormatVertexException {
58 final StringBuilder result = new StringBuilder();
59 final URI uri = this.serializer.getURIForVertex(v);
61 if (this.version.compareTo(Version.v11) >= 0) {
62 result.append(AAIConstants.AAI_APP_ROOT);
64 result.append(this.serverBase);
66 result.append(this.version);
69 return result.toString();
70 } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
71 throw new AAIFormatVertexException(e);
75 public String id(Vertex v) {
76 final StringBuilder result = new StringBuilder();
78 result.append("/resources/id/" + v.id());
79 result.insert(0, this.version);
80 if (this.version.compareTo(Version.v11) >= 0) {
81 result.insert(0, AAIConstants.AAI_APP_ROOT);
83 result.insert(0, this.serverBase);
86 return result.toString();
89 protected String getServerBase(Version v) throws AAIException {
90 return AAIApiServerURLBase.get(v);