1 /*
2 * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10 package org.eclipse.jgit.transport.sshd;
11
12 import java.net.InetSocketAddress;
13 import java.net.Proxy;
14 import java.util.Arrays;
15
16 import org.eclipse.jgit.annotations.NonNull;
17
18 /**
19 * A DTO encapsulating the data needed to connect through a proxy server.
20 *
21 * @since 5.2
22 */
23 public class ProxyData {
24
25 private final @NonNull Proxy proxy;
26
27 private final String proxyUser;
28
29 private final char[] proxyPassword;
30
31 /**
32 * Creates a new {@link ProxyData} instance without user name or password.
33 *
34 * @param proxy
35 * to connect to; must not be {@link java.net.Proxy.Type#DIRECT}
36 * and must have an {@link InetSocketAddress}.
37 */
38 public ProxyData(@NonNull Proxy proxy) {
39 this(proxy, null, null);
40 }
41
42 /**
43 * Creates a new {@link ProxyData} instance.
44 *
45 * @param proxy
46 * to connect to; must not be {@link java.net.Proxy.Type#DIRECT}
47 * and must have an {@link InetSocketAddress}.
48 * @param proxyUser
49 * to use for log-in to the proxy, may be {@code null}
50 * @param proxyPassword
51 * to use for log-in to the proxy, may be {@code null}
52 */
53 public ProxyData(@NonNull Proxy proxy, String proxyUser,
54 char[] proxyPassword) {
55 this.proxy = proxy;
56 if (!(proxy.address() instanceof InetSocketAddress)) {
57 // Internal error not translated
58 throw new IllegalArgumentException(
59 "Proxy does not have an InetSocketAddress"); //$NON-NLS-1$
60 }
61 this.proxyUser = proxyUser;
62 this.proxyPassword = proxyPassword == null ? null
63 : proxyPassword.clone();
64 }
65
66 /**
67 * Obtains the remote {@link InetSocketAddress} of the proxy to connect to.
68 *
69 * @return the remote address of the proxy
70 */
71 @NonNull
72 public Proxy getProxy() {
73 return proxy;
74 }
75
76 /**
77 * Obtains the user to log in at the proxy with.
78 *
79 * @return the user name, or {@code null} if none
80 */
81 public String getUser() {
82 return proxyUser;
83 }
84
85 /**
86 * Obtains a copy of the internally stored password.
87 *
88 * @return the password or {@code null} if none
89 */
90 public char[] getPassword() {
91 return proxyPassword == null ? null : proxyPassword.clone();
92 }
93
94 /**
95 * Clears the stored password, if any.
96 */
97 public void clearPassword() {
98 if (proxyPassword != null) {
99 Arrays.fill(proxyPassword, '\000');
100 }
101 }
102
103 }