SDK - Increase code coverage
[dcaegen2/services/sdk.git] / rest-services / http-client / src / main / java / org / onap / dcaegen2 / services / sdk / rest / services / adapters / http / URI.java
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2019-2022 NOKIA 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
22 package org.onap.dcaegen2.services.sdk.rest.services.adapters.http;
23
24 public final class URI {
25     private String scheme;
26     private String host;
27     private int port;
28     private String path;
29     private String fragment;
30     private String authority;
31     private String userInfo;
32     private String query;
33     private String schemeSpecificPart;
34     private String string;
35
36     private URI() {
37     }
38
39     public static final class URIBuilder {
40         private String scheme;
41         private String host;
42         private int port;
43         private String path;
44         private String fragment;
45         private String authority;
46         private String userInfo;
47         private String query;
48         private String schemeSpecificPart;
49
50         public URIBuilder scheme(String scheme) {
51             this.scheme = scheme;
52             return this;
53         }
54
55         public URIBuilder host(String host) {
56             this.host = host;
57             return this;
58         }
59
60         public URIBuilder port(int port) {
61             this.port = port;
62             return this;
63         }
64
65         public URIBuilder path(String path) {
66             this.path = path;
67             return this;
68         }
69
70         public URIBuilder fragment(String fragment) {
71             this.fragment = fragment;
72             return this;
73         }
74
75         public URIBuilder authority(String authority) {
76             this.authority = authority;
77             return this;
78         }
79
80         public URIBuilder userInfo(String userInfo) {
81             this.userInfo = userInfo;
82             return this;
83         }
84
85         public URIBuilder query(String query) {
86             this.query = query;
87             return this;
88         }
89
90         public URIBuilder schemeSpecificPart(String schemeSpecificPart) {
91             this.schemeSpecificPart = schemeSpecificPart;
92             return this;
93         }
94
95         public URI build() {
96             URI uri = new URI();
97             uri.scheme = this.scheme;
98             uri.host = this.host;
99             uri.port = this.port;
100             uri.path = this.path;
101             uri.fragment = this.fragment;
102             uri.authority = this.authority;
103             uri.userInfo = this.userInfo;
104             uri.query = this.query;
105             uri.schemeSpecificPart = this.schemeSpecificPart;
106             return uri;
107         }
108     }
109
110     @Override
111     public String toString() {
112         defineString();
113         return string;
114     }
115
116     private void defineString() {
117         if (string != null) return;
118
119         StringBuilder sb = new StringBuilder();
120         if (scheme != null) {
121             sb.append(scheme);
122             sb.append(':');
123         }
124         if (isOpaque()) {
125             sb.append(schemeSpecificPart);
126         } else {
127             if (host != null) {
128                 sb.append("//");
129                 if (userInfo != null) {
130                     sb.append(userInfo);
131                     sb.append('@');
132                 }
133                 boolean needBrackets = ((host.indexOf(':') >= 0)
134                     && !host.startsWith("[")
135                     && !host.endsWith("]"));
136                 if (needBrackets) sb.append('[');
137                 sb.append(host);
138                 if (needBrackets) sb.append(']');
139                 if (port != -1) {
140                     sb.append(':');
141                     sb.append(port);
142                 }
143             } else if (authority != null) {
144                 sb.append("//");
145                 sb.append(authority);
146             }
147             if (path != null)
148                 sb.append(path);
149             if (query != null) {
150                 sb.append('?');
151                 sb.append(query);
152             }
153         }
154         if (fragment != null) {
155             sb.append('#');
156             sb.append(fragment);
157         }
158         string = sb.toString();
159     }
160
161     private boolean isOpaque() {
162         return path == null;
163     }
164 }