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