ProxyData.java

  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. import java.net.InetSocketAddress;
  12. import java.net.Proxy;
  13. import java.util.Arrays;

  14. import org.eclipse.jgit.annotations.NonNull;

  15. /**
  16.  * A DTO encapsulating the data needed to connect through a proxy server.
  17.  *
  18.  * @since 5.2
  19.  */
  20. public class ProxyData {

  21.     private final @NonNull Proxy proxy;

  22.     private final String proxyUser;

  23.     private final char[] proxyPassword;

  24.     /**
  25.      * Creates a new {@link ProxyData} instance without user name or password.
  26.      *
  27.      * @param proxy
  28.      *            to connect to; must not be {@link java.net.Proxy.Type#DIRECT}
  29.      *            and must have an {@link InetSocketAddress}.
  30.      */
  31.     public ProxyData(@NonNull Proxy proxy) {
  32.         this(proxy, null, null);
  33.     }

  34.     /**
  35.      * Creates a new {@link ProxyData} instance.
  36.      *
  37.      * @param proxy
  38.      *            to connect to; must not be {@link java.net.Proxy.Type#DIRECT}
  39.      *            and must have an {@link InetSocketAddress}.
  40.      * @param proxyUser
  41.      *            to use for log-in to the proxy, may be {@code null}
  42.      * @param proxyPassword
  43.      *            to use for log-in to the proxy, may be {@code null}
  44.      */
  45.     public ProxyData(@NonNull Proxy proxy, String proxyUser,
  46.             char[] proxyPassword) {
  47.         this.proxy = proxy;
  48.         if (!(proxy.address() instanceof InetSocketAddress)) {
  49.             // Internal error not translated
  50.             throw new IllegalArgumentException(
  51.                     "Proxy does not have an InetSocketAddress"); //$NON-NLS-1$
  52.         }
  53.         this.proxyUser = proxyUser;
  54.         this.proxyPassword = proxyPassword == null ? null
  55.                 : proxyPassword.clone();
  56.     }

  57.     /**
  58.      * Obtains the remote {@link InetSocketAddress} of the proxy to connect to.
  59.      *
  60.      * @return the remote address of the proxy
  61.      */
  62.     @NonNull
  63.     public Proxy getProxy() {
  64.         return proxy;
  65.     }

  66.     /**
  67.      * Obtains the user to log in at the proxy with.
  68.      *
  69.      * @return the user name, or {@code null} if none
  70.      */
  71.     public String getUser() {
  72.         return proxyUser;
  73.     }

  74.     /**
  75.      * Obtains a copy of the internally stored password.
  76.      *
  77.      * @return the password or {@code null} if none
  78.      */
  79.     public char[] getPassword() {
  80.         return proxyPassword == null ? null : proxyPassword.clone();
  81.     }

  82.     /**
  83.      * Clears the stored password, if any.
  84.      */
  85.     public void clearPassword() {
  86.         if (proxyPassword != null) {
  87.             Arrays.fill(proxyPassword, '\000');
  88.         }
  89.     }

  90. }