Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / util / Request.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.util;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URLEncoder;
27 import java.nio.charset.StandardCharsets;
28
29 import javax.ws.rs.core.UriBuilder;
30
31 import org.openecomp.aai.exceptions.AAIException;
32
33 public class Request<T> {
34
35         public static final String V2 = "v2";
36         public static final String V3 = "v3";
37         public static final String V4 = "v4";
38         public static final String V5 = "v5";
39         public final String fromAppId;
40         public final String transactionId;
41         public final String path;
42         public final RestObject<T> restObj;
43         public final boolean oldServer;
44         public final String apiVersion;
45         
46         
47         /**
48          * Instantiates a new request.
49          *
50          * @param builder the builder
51          */
52         public Request(RequestBuilder<T> builder) {
53                 
54                 fromAppId = builder.fromAppId;
55                 transactionId = builder.transactionId;
56                 restObj = builder.restObj;
57                 oldServer = builder.oldServer;
58                 apiVersion = builder.apiVersion;
59                 
60                 if (!oldServer) {
61                         path = apiVersion + "/" + builder.path;
62                 } else {
63                         path = builder.path;
64                 }
65                 
66                 
67         }
68         
69         public static class RequestBuilder<T> {
70                 private String fromAppId;
71                 private String transactionId;
72                 private String path;
73                 private RestObject<T> restObj;
74                 private boolean oldServer;
75                 private String apiVersion = Request.V4;
76                 
77
78                 /**
79                  * Sets the from app id.
80                  *
81                  * @param fromAppId the from app id
82                  * @return the request builder
83                  */
84                 public RequestBuilder<T> setFromAppId(String fromAppId) {
85                         this.fromAppId = fromAppId;
86                         return this;
87                 }
88                 
89                 /**
90                  * Sets the transaction id.
91                  *
92                  * @param transactionId the transaction id
93                  * @return the request builder
94                  */
95                 public RequestBuilder<T> setTransactionId(String transactionId) {
96                         this.transactionId = transactionId;
97                         return this;
98
99                 }
100                 
101                 /**
102                  * Sets the path.
103                  *
104                  * @param path the path
105                  * @return the request builder
106                  */
107                 public RequestBuilder<T> setPath(String path) {
108                         
109                         this.path = path;
110                         return this;
111
112                 }
113                 
114                 /**
115                  * Sets the restcore obj.
116                  *
117                  * @param restObj the restcore obj
118                  * @return the request builder
119                  */
120                 public RequestBuilder<T> setRestObj(RestObject<T> restObj) {
121                         this.restObj = restObj;
122                         return this;
123
124                 }
125                 
126                 /**
127                  * Sets the old server.
128                  *
129                  * @param oldServer the old server
130                  * @return the request builder
131                  */
132                 public RequestBuilder<T> setOldServer(boolean oldServer) {
133                         this.oldServer = oldServer;
134                         return this;
135
136                 }
137                 
138                 /**
139                  * Sets the api version.
140                  *
141                  * @param apiVersion the api version
142                  * @return the request builder
143                  */
144                 public RequestBuilder<T> setApiVersion(String apiVersion) {
145                         this.apiVersion = apiVersion;
146                         return this;
147                 }
148                 
149                 /**
150                  * Builds the.
151                  *
152                  * @return the request
153                  */
154                 public Request<T> build() {
155                         return new Request<T>(this);
156                 }
157                 
158         }
159         
160 }