Update license files, sonar plugin and fix tests
[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 java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28
29 import org.openecomp.aai.db.props.AAIProperties;
30 import org.openecomp.aai.exceptions.AAIException;
31 import org.openecomp.aai.introspection.Version;
32 import org.openecomp.aai.serialization.db.DBSerializer;
33 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
34 import org.openecomp.aai.util.AAIApiServerURLBase;
35 import org.openecomp.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 = AAIApiServerURLBase.get(AAIProperties.LATEST);
47         }
48         
49         public String pathed(Vertex v) throws AAIFormatVertexException {
50
51                 try {
52                         final StringBuilder result = new StringBuilder();
53                         final URI uri = this.serializer.getURIForVertex(v);
54         
55                         result.append(this.serverBase);
56                         result.append(this.version);
57                         result.append(uri.getRawPath());
58         
59                         return result.toString();
60                 } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) {
61                         throw new AAIFormatVertexException(e);
62                 }
63         }
64         
65         public String id(Vertex v) {
66                 final StringBuilder result = new StringBuilder();
67
68                 result.append("/resources/id/" + v.id());
69                 result.insert(0, this.version);
70                 result.insert(0, this.serverBase);
71
72                 return result.toString();
73         }
74 }