View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.proxy.network;
21  
22  import org.waarp.openr66.protocol.exception.OpenR66ProtocolSystemException;
23  
24  import java.net.SocketAddress;
25  import java.util.HashMap;
26  
27  /**
28   * Proxy Entry
29   */
30  public class ProxyEntry {
31    public static final HashMap<String, ProxyEntry> proxyEntries =
32        new HashMap<String, ProxyEntry>();
33  
34    private final String name;
35    private final SocketAddress localSocketAddress;
36    private final boolean localIsSsl;
37    private final SocketAddress remoteSocketAddress;
38    private final boolean remoteIsSsl;
39  
40    public static void add(final SocketAddress localAddress,
41                           final boolean localIsSsl,
42                           final SocketAddress remoteAddress,
43                           final boolean remoteIsSsl)
44        throws OpenR66ProtocolSystemException {
45      final String sla = localAddress.toString();
46      if (proxyEntries.containsKey(sla)) {
47        throw new OpenR66ProtocolSystemException(
48            "Error in the configuration: Two entries with the same localAdress");
49      }
50      proxyEntries.put(sla, new ProxyEntry(sla, localAddress, localIsSsl,
51                                           remoteAddress, remoteIsSsl));
52    }
53  
54    public static ProxyEntry get(final String name) {
55      synchronized (proxyEntries) {
56        if (proxyEntries.containsKey(name)) {
57          return proxyEntries.get(name);
58        } else {
59          // error
60          return null;
61        }
62      }
63    }
64  
65    /**
66     * @param name
67     * @param localSocketAddress
68     * @param localIsSsl
69     * @param remoteSocketAddress
70     * @param remoteIsSsl
71     */
72    private ProxyEntry(final String name, final SocketAddress localSocketAddress,
73                       final boolean localIsSsl,
74                       final SocketAddress remoteSocketAddress,
75                       final boolean remoteIsSsl) {
76      this.name = name;
77      this.localSocketAddress = localSocketAddress;
78      this.localIsSsl = localIsSsl;
79      this.remoteSocketAddress = remoteSocketAddress;
80      this.remoteIsSsl = remoteIsSsl;
81    }
82  
83    /**
84     * @return the localSocketAddress
85     */
86    public final SocketAddress getLocalSocketAddress() {
87      return localSocketAddress;
88    }
89  
90    /**
91     * @return the localIsSsl
92     */
93    public final boolean isLocalSsl() {
94      return localIsSsl;
95    }
96  
97    /**
98     * @return the remoteSocketAddress
99     */
100   public final SocketAddress getRemoteSocketAddress() {
101     return remoteSocketAddress;
102   }
103 
104   /**
105    * @return the name
106    */
107   public final String getName() {
108     return name;
109   }
110 
111   /**
112    * @return the isSsl
113    */
114   public final boolean isRemoteSsl() {
115     return remoteIsSsl;
116   }
117 
118   @Override
119   public String toString() {
120     return "from: " + localSocketAddress + ':' + localIsSsl + " to: " +
121            remoteSocketAddress + ':' + remoteIsSsl;
122   }
123 }