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