9da82fe0dcf09f464f0d229068a01b3761477108
[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) {
58         this.serializer = serializer;
59         this.version = version;
60         this.serverBase = serverBase;
61         this.schemaVersions = schemaVersions;
62         if (!basePath.endsWith("/")) {
63             this.basePath = basePath + "/";
64         } else {
65             this.basePath = basePath;
66         }
67     }
68
69     public String pathed(Vertex v) throws AAIFormatVertexException {
70
71         try {
72             final StringBuilder result = new StringBuilder();
73             final URI uri = this.serializer.getURIForVertex(v);
74
75             if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) {
76                 result.append(basePath);
77             } else {
78                 result.append(this.serverBase);
79             }
80             result.append(this.version);
81             result.append(uri.getRawPath());
82
83             return result.toString();
84         } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
85             throw new AAIFormatVertexException(e);
86         }
87     }
88
89     public String id(Vertex v) {
90         final StringBuilder result = new StringBuilder();
91
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);
96         } else {
97             result.insert(0, this.serverBase);
98         }
99
100         return result.toString();
101     }
102
103     protected String getServerBase() throws AAIException {
104         return AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE);
105     }
106 }