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=========================================================
21 package org.onap.aai.serialization.queryformats.utils;
23 import java.io.UnsupportedEncodingException;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.onap.aai.exceptions.AAIException;
28 import org.onap.aai.serialization.db.DBSerializer;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
30 import org.onap.aai.setup.SchemaVersion;
31 import org.onap.aai.setup.SchemaVersions;
32 import org.onap.aai.util.AAIConfig;
33 import org.onap.aai.util.AAIConstants;
35 public class UrlBuilder {
37 private final DBSerializer serializer;
38 private final SchemaVersion version;
39 private final String serverBase;
40 private final SchemaVersions schemaVersions;
41 private final String basePath;
43 public UrlBuilder(SchemaVersion version, DBSerializer serializer, SchemaVersions schemaVersions, String basePath)
45 this.serializer = serializer;
46 this.version = version;
47 this.serverBase = this.getServerBase();
48 this.schemaVersions = schemaVersions;
49 if (!basePath.endsWith("/")) {
50 this.basePath = basePath + "/";
52 this.basePath = basePath;
56 public UrlBuilder(SchemaVersion version, DBSerializer serializer, String serverBase, SchemaVersions schemaVersions,
58 this.serializer = serializer;
59 this.version = version;
60 this.serverBase = serverBase;
61 this.schemaVersions = schemaVersions;
62 if (!basePath.endsWith("/")) {
63 this.basePath = basePath + "/";
65 this.basePath = basePath;
69 public String pathed(Vertex v) throws AAIFormatVertexException {
72 final StringBuilder result = new StringBuilder();
73 final URI uri = this.serializer.getURIForVertex(v);
75 if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) {
76 result.append(basePath);
78 result.append(this.serverBase);
80 result.append(this.version);
81 result.append(uri.getRawPath());
83 return result.toString();
84 } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
85 throw new AAIFormatVertexException(e);
89 public String id(Vertex v) {
90 final StringBuilder result = new StringBuilder();
92 result.append("/resources/id/" + v.id());
93 result.insert(0, this.version);
94 if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) {
95 result.insert(0, basePath);
97 result.insert(0, this.serverBase);
100 return result.toString();
103 protected String getServerBase() throws AAIException {
104 return AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE);