View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.handler.proxy;
18  
19  import io.netty.util.internal.StringUtil;
20  
21  import java.net.SocketAddress;
22  
23  public final class ProxyConnectionEvent {
24  
25      private final String protocol;
26      private final String authScheme;
27      private final SocketAddress proxyAddress;
28      private final SocketAddress destinationAddress;
29      private String strVal;
30  
31      /**
32       * Creates a new event that indicates a successful connection attempt to the destination address.
33       */
34      public ProxyConnectionEvent(
35              String protocol, String authScheme, SocketAddress proxyAddress, SocketAddress destinationAddress) {
36          if (protocol == null) {
37              throw new NullPointerException("protocol");
38          }
39          if (authScheme == null) {
40              throw new NullPointerException("authScheme");
41          }
42          if (proxyAddress == null) {
43              throw new NullPointerException("proxyAddress");
44          }
45          if (destinationAddress == null) {
46              throw new NullPointerException("destinationAddress");
47          }
48  
49          this.protocol = protocol;
50          this.authScheme = authScheme;
51          this.proxyAddress = proxyAddress;
52          this.destinationAddress = destinationAddress;
53      }
54  
55      /**
56       * Returns the name of the proxy protocol in use.
57       */
58      public String protocol() {
59          return protocol;
60      }
61  
62      /**
63       * Returns the name of the authentication scheme in use.
64       */
65      public String authScheme() {
66          return authScheme;
67      }
68  
69      /**
70       * Returns the address of the proxy server.
71       */
72      @SuppressWarnings("unchecked")
73      public <T extends SocketAddress> T proxyAddress() {
74          return (T) proxyAddress;
75      }
76  
77      /**
78       * Returns the address of the destination.
79       */
80      @SuppressWarnings("unchecked")
81      public <T extends SocketAddress> T destinationAddress() {
82          return (T) destinationAddress;
83      }
84  
85      @Override
86      public String toString() {
87          if (strVal != null) {
88              return strVal;
89          }
90  
91          StringBuilder buf = new StringBuilder(128)
92              .append(StringUtil.simpleClassName(this))
93              .append('(')
94              .append(protocol)
95              .append(", ")
96              .append(authScheme)
97              .append(", ")
98              .append(proxyAddress)
99              .append(" => ")
100             .append(destinationAddress)
101             .append(')');
102 
103         return strVal = buf.toString();
104     }
105 }