8387285d67e4eb5580b11f1ae2803ccbcfc31a98
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.queryformats.utils;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.Version;
31 import org.onap.aai.serialization.db.DBSerializer;
32 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.onap.aai.util.AAIApiServerURLBase;
34 import org.onap.aai.util.AAIConstants;
35 import org.onap.aai.workarounds.LegacyURITransformer;
36
37 public class UrlBuilder {
38
39         private final DBSerializer serializer;
40         private final Version version;
41         private final String serverBase;
42         
43         public UrlBuilder (Version version, DBSerializer serializer) throws AAIException {
44                 this.serializer = serializer;
45                 this.version = version;
46                 this.serverBase = this.getServerBase(version);
47         }
48         
49         public UrlBuilder (Version version, DBSerializer serializer, String serverBase) {
50                 this.serializer = serializer;
51                 this.version = version;
52                 this.serverBase = serverBase;
53         }
54         
55         public String pathed(Vertex v) throws AAIFormatVertexException {
56
57                 try {
58                         final StringBuilder result = new StringBuilder();
59                         final URI uri = this.serializer.getURIForVertex(v);
60                         
61                         if (this.version.compareTo(Version.v11) >= 0) {
62                                 result.append(AAIConstants.AAI_APP_ROOT);
63                         } else {
64                                 result.append(this.serverBase);
65                         }
66                         result.append(this.version);
67                         result.append(uri);
68
69                                 return result.toString();
70                 } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
71                         throw new AAIFormatVertexException(e);
72                 }
73         }
74         
75         public String id(Vertex v) {
76                 final StringBuilder result = new StringBuilder();
77
78                 result.append("/resources/id/" + v.id());
79                 result.insert(0, this.version);
80                 if (this.version.compareTo(Version.v11) >= 0) {
81                         result.insert(0, AAIConstants.AAI_APP_ROOT);
82                 } else {
83                         result.insert(0, this.serverBase);
84                 }
85
86                 return result.toString();
87         }
88         
89         protected String getServerBase(Version v) throws AAIException {
90                 return AAIApiServerURLBase.get(v);
91         }
92 }