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;
25 import org.apache.tinkerpop.gremlin.structure.Vertex;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.setup.SchemaVersion;
28 import org.onap.aai.serialization.db.DBSerializer;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
30 import org.onap.aai.setup.SchemaVersions;
31 import org.onap.aai.util.AAIConfig;
32 import org.onap.aai.util.AAIConstants;
34 public class UrlBuilder {
36 private final DBSerializer serializer;
37 private final SchemaVersion version;
38 private final String serverBase;
39 private final SchemaVersions schemaVersions;
40 private final String basePath;
42 public UrlBuilder (SchemaVersion version, DBSerializer serializer, SchemaVersions schemaVersions, String basePath) throws AAIException {
43 this.serializer = serializer;
44 this.version = version;
45 this.serverBase = this.getServerBase();
46 this.schemaVersions = schemaVersions;
47 if(!basePath.endsWith("/")){
48 this.basePath = basePath + "/";
50 this.basePath = basePath;
54 public UrlBuilder (SchemaVersion version, DBSerializer serializer, String serverBase, SchemaVersions schemaVersions, String basePath) {
55 this.serializer = serializer;
56 this.version = version;
57 this.serverBase = serverBase;
58 this.schemaVersions = schemaVersions;
59 if(!basePath.endsWith("/")){
60 this.basePath = basePath + "/";
62 this.basePath = basePath;
66 public String pathed(Vertex v) throws AAIFormatVertexException {
69 final StringBuilder result = new StringBuilder();
70 final URI uri = this.serializer.getURIForVertex(v);
72 if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) {
73 result.append(basePath);
75 result.append(this.serverBase);
77 result.append(this.version);
78 result.append(uri.getRawPath());
80 return result.toString();
81 } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
82 throw new AAIFormatVertexException(e);
86 public String id(Vertex v) {
87 final StringBuilder result = new StringBuilder();
89 result.append("/resources/id/" + v.id());
90 result.insert(0, this.version);
91 if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) {
92 result.insert(0, basePath);
94 result.insert(0, this.serverBase);
97 return result.toString();
100 protected String getServerBase() throws AAIException {
101 return AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE);