View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.proxy.handlers.socks;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.proxy.handlers.ProxyRequest;
25  
26  /**
27   * SocksProxyRequest.java - Wrapper class for SOCKS requests.
28   * 
29   * @author The Apache MINA Project (dev@mina.apache.org)
30   * @since MINA 2.0.0-M3
31   */
32  public class SocksProxyRequest extends ProxyRequest {
33  
34      /**
35       * The SOCKS protocol version.
36       */
37      private byte protocolVersion;
38  
39      /**
40       * The command code.
41       */
42      private byte commandCode;
43  
44      /**
45       * The user name used when authenticating to the proxy server. 
46       */
47      private String userName;
48  
49      /**
50       * The user's password used when authenticating to the proxy server.
51       */
52      private String password;
53  
54      /**
55       * The SOCKS server host name.
56       */
57      private String host;
58  
59      /**
60       * The SOCKS server port.
61       */
62      private int port;
63  
64      /**
65       * The Kerberos service name used in GSSAPI authentication mode.
66       */
67      private String serviceKerberosName;
68  
69      /**
70       * Constructor used when building a SOCKS4 request.
71       * 
72       * @param protocolVersion the protocol version
73       * @param commandCode the command code
74       * @param endpointAddress the endpoint address
75       * @param userName the user name
76       */
77      public SocksProxyRequest(byte protocolVersion, byte commandCode,
78              InetSocketAddress endpointAddress, String userName) {
79          super(endpointAddress);
80          this.protocolVersion = protocolVersion;
81          this.commandCode = commandCode;
82          this.userName = userName;
83      }
84  
85      /**
86       * Constructor used when building a SOCKS4a request.
87       * 
88       * @param commandCode the command code
89       * @param host the server host name
90       * @param port the server port
91       * @param userName the user name
92       */
93      public SocksProxyRequest(byte commandCode, String host, int port,
94              String userName) {
95          this.protocolVersion = SocksProxyConstants.SOCKS_VERSION_4;
96          this.commandCode = commandCode;
97          this.userName = userName;
98          this.host = host;
99          this.port = port;
100     }
101 
102     /**
103      * Returns the endpoint address resulting from the {@link #getEndpointAddress()}. 
104      * If not set, it will return the {@link SocksProxyConstants#FAKE_IP} constant 
105      * value which will be ignored in a SOCKS v4 request.
106      *   
107      * @return the endpoint address
108      */
109     public byte[] getIpAddress() {
110         if (getEndpointAddress() == null) {
111             return SocksProxyConstants.FAKE_IP;
112         }
113         
114         return getEndpointAddress().getAddress().getAddress();
115     }
116 
117     /**
118      * Return the server port as a byte array.
119      * 
120      * @return the server port
121      */
122     public byte[] getPort() {
123         byte[] port = new byte[2];
124         int p = (getEndpointAddress() == null ? this.port
125                 : getEndpointAddress().getPort());
126         port[1] = (byte) p;
127         port[0] = (byte) (p >> 8);
128         return port;
129     }
130 
131     /**
132      * Return the command code.
133      * 
134      * @return the command code
135      */
136     public byte getCommandCode() {
137         return commandCode;
138     }
139 
140     /**
141      * Return the protocol version.
142      * 
143      * @return the protocol version
144      */
145     public byte getProtocolVersion() {
146         return protocolVersion;
147     }
148 
149     /**
150      * Return the user name.
151      * 
152      * @return the user name
153      */
154     public String getUserName() {
155         return userName;
156     }
157 
158     /**
159      * Return the server host name.
160      * 
161      * @return the server host name
162      */
163     public synchronized final String getHost() {
164         if (host == null) {
165             InetSocketAddress adr = getEndpointAddress();
166             
167             if ( adr != null && !adr.isUnresolved()) {
168                 host = getEndpointAddress().getHostName();
169             }
170         }
171 
172         return host;
173     }
174 
175     /**
176      * Return the user password.
177      * 
178      * @return the user password
179      */
180     public String getPassword() {
181         return password;
182     }
183 
184     /**
185      * Set the user password
186      * 
187      * @param password the user password value
188      */
189     public void setPassword(String password) {
190         this.password = password;
191     }
192 
193     /**
194      * Return the Kerberos service name.
195      * 
196      * @return the Kerberos service name
197      */
198     public String getServiceKerberosName() {
199         return serviceKerberosName;
200     }
201 
202     /**
203      * Set the Kerberos service name.
204      * 
205      * @param serviceKerberosName the Kerberos service name
206      */
207     public void setServiceKerberosName(String serviceKerberosName) {
208         this.serviceKerberosName = serviceKerberosName;
209     }
210 }