Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / utils / UrlBuilder.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats.utils;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25
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;
34
35 public class UrlBuilder {
36
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;
42
43     public UrlBuilder(SchemaVersion version, DBSerializer serializer, SchemaVersions schemaVersions, String basePath)
44             throws AAIException {
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 + "/";
51         } else {
52             this.basePath = basePath;
53         }
54     }
55
56     public UrlBuilder(SchemaVersion version, DBSerializer serializer, String serverBase, SchemaVersions schemaVersions,
57             String basePath) throws AAIException {
58         this.serializer = serializer;
59         this.version = version;
60         if (serverBase == null) {
61             this.serverBase = getServerBase();
62         } else {
63             this.serverBase = serverBase;
64         }
65         this.schemaVersions = schemaVersions;
66         if (!basePath.endsWith("/")) {
67             this.basePath = basePath + "/";
68         } else {
69             this.basePath = basePath;
70         }
71     }
72
73     public String pathed(Vertex v) throws AAIFormatVertexException {
74
75         try {
76             final StringBuilder result = new StringBuilder();
77             final URI uri = this.serializer.getURIForVertex(v);
78
79             if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) {
80                 result.append(basePath);
81             } else {
82                 result.append(this.serverBase);
83             }
84             result.append(this.version);
85             result.append(uri.getRawPath());
86
87             return result.toString();
88         } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
89             throw new AAIFormatVertexException(e);
90         }
91     }
92
93     public String id(Vertex v) {
94         final StringBuilder result = new StringBuilder();
95
96         result.append("/resources/id/" + v.id());
97         result.insert(0, this.version);
98         if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) {
99             result.insert(0, basePath);
100         } else {
101             result.insert(0, this.serverBase);
102         }
103
104         return result.toString();
105     }
106
107     protected String getServerBase() throws AAIException {
108         return AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE);
109     }
110 }