|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * Copyright (c) 2019, Google and/or its affiliates. All rights reserved. |
|
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
5 * |
|
6 * This code is free software; you can redistribute it and/or modify it |
|
7 * under the terms of the GNU General Public License version 2 only, as |
|
8 * published by the Free Software Foundation. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 2 along with this work; if not, write to the Free Software Foundation, |
|
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 * |
|
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
21 * or visit www.oracle.com if you need additional information or have any |
|
22 * questions. |
|
23 */ |
|
24 |
|
25 package jdk.test.lib.net; |
|
26 |
|
27 import java.net.InetAddress; |
|
28 import java.net.MalformedURLException; |
|
29 import java.net.URI; |
|
30 import java.net.URISyntaxException; |
|
31 import java.net.URL; |
|
32 |
|
33 public class URIBuilder { |
|
34 |
|
35 public static URIBuilder newBuilder() { |
|
36 return new URIBuilder(); |
|
37 } |
|
38 |
|
39 private String scheme; |
|
40 private String userInfo; |
|
41 private String host; |
|
42 private int port; |
|
43 private String path; |
|
44 private String query; |
|
45 private String fragment; |
|
46 |
|
47 private URIBuilder() {} |
|
48 |
|
49 public URIBuilder scheme(String scheme) { |
|
50 this.scheme = scheme; |
|
51 return this; |
|
52 } |
|
53 |
|
54 public URIBuilder userInfo(String userInfo) { |
|
55 this.userInfo = userInfo; |
|
56 return this; |
|
57 } |
|
58 |
|
59 public URIBuilder host(String host) { |
|
60 this.host = host; |
|
61 return this; |
|
62 } |
|
63 |
|
64 public URIBuilder loopback() { |
|
65 return host(InetAddress.getLoopbackAddress().getHostAddress()); |
|
66 } |
|
67 |
|
68 public URIBuilder port(int port) { |
|
69 this.port = port; |
|
70 return this; |
|
71 } |
|
72 |
|
73 public URIBuilder path(String path) { |
|
74 this.path = path; |
|
75 return this; |
|
76 } |
|
77 |
|
78 public URIBuilder query(String query) { |
|
79 this.query = query; |
|
80 return this; |
|
81 } |
|
82 |
|
83 public URIBuilder fragment(String fragment) { |
|
84 this.fragment = fragment; |
|
85 return this; |
|
86 } |
|
87 |
|
88 public URI build() throws URISyntaxException { |
|
89 return new URI(scheme, userInfo, host, port, path, query, fragment); |
|
90 } |
|
91 |
|
92 public URI buildUnchecked() { |
|
93 try { |
|
94 return build(); |
|
95 } catch (URISyntaxException e) { |
|
96 throw new IllegalArgumentException(e); |
|
97 } |
|
98 } |
|
99 |
|
100 public URL toURL() throws URISyntaxException, MalformedURLException { |
|
101 return build().toURL(); |
|
102 } |
|
103 |
|
104 public URL toURLUnchecked() { |
|
105 try { |
|
106 return toURL(); |
|
107 } catch (URISyntaxException | MalformedURLException e) { |
|
108 throw new IllegalArgumentException(e); |
|
109 } |
|
110 } |
|
111 } |