304593640e1eaef99dbf45cd0440ba742dd0f63d
[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 package org.onap.aai.serialization.queryformats.utils;
21
22 import java.io.UnsupportedEncodingException;
23 import java.net.URI;
24
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;
33
34 public class UrlBuilder {
35
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;
41
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 + "/";
49                 } else {
50                         this.basePath = basePath;
51                 }
52         }
53         
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 + "/";
61                 } else {
62                         this.basePath = basePath;
63                 }
64         }
65         
66         public String pathed(Vertex v) throws AAIFormatVertexException {
67
68                 try {
69                         final StringBuilder result = new StringBuilder();
70                         final URI uri = this.serializer.getURIForVertex(v);
71
72                         if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) {
73                                 result.append(basePath);
74                         } else {
75                                 result.append(this.serverBase);
76                         }
77                         result.append(this.version);
78                         result.append(uri.getRawPath());
79                         
80                         return result.toString();
81             } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
82                         throw new AAIFormatVertexException(e);
83                 }
84         }
85         
86         public String id(Vertex v) {
87                 final StringBuilder result = new StringBuilder();
88
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);
93                 } else {
94                         result.insert(0, this.serverBase);
95                 }
96
97                 return result.toString();
98         }
99         
100         protected String getServerBase() throws AAIException {
101                 return AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE);
102         }
103 }