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