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   * @version $Rev: 685703 $, $Date: 2008-08-14 00:14:47 +0200 (Thu, 14 Aug 2008) $
31   * @since MINA 2.0.0-M3
32   */
33  public class SocksProxyRequest extends ProxyRequest {
34  
35      private byte protocolVersion;
36  
37      private byte commandCode;
38  
39      private String userName;
40  
41      private String password;
42  
43      private String host;
44  
45      private int port;
46  
47      private String serviceKerberosName;
48  
49      /**
50       * Constructor used when making a SOCKS4/SOCKS5 request.
51       */
52      public SocksProxyRequest(byte protocolVersion, byte commandCode,
53              InetSocketAddress endpointAddress, String userName) {
54          super(endpointAddress);
55          this.protocolVersion = protocolVersion;
56          this.commandCode = commandCode;
57          this.userName = userName;
58      }
59  
60      /**
61       * Constructor used when making a SOCKS4a request.
62       */
63      public SocksProxyRequest(byte commandCode, String host, int port,
64              String userName) {
65          this.protocolVersion = SocksProxyConstants.SOCKS_VERSION_4;
66          this.commandCode = commandCode;
67          this.userName = userName;
68          this.host = host;
69          this.port = port;
70      }
71  
72      public byte[] getIpAddress() {
73          if (getEndpointAddress() == null)
74              return SocksProxyConstants.FAKE_IP;
75          else
76              return getEndpointAddress().getAddress().getAddress();
77      }
78  
79      public byte[] getPort() {
80          byte[] port = new byte[2];
81          int p = (int) (getEndpointAddress() == null ? this.port
82                  : getEndpointAddress().getPort());
83          port[1] = (byte) p;
84          port[0] = (byte) (p >> 8);
85          return port;
86      }
87  
88      public byte getCommandCode() {
89          return commandCode;
90      }
91  
92      public byte getProtocolVersion() {
93          return protocolVersion;
94      }
95  
96      public String getUserName() {
97          return userName;
98      }
99  
100     public synchronized final String getHost() {
101         if (host == null) {
102             if (getEndpointAddress() != null) {
103                 host = getEndpointAddress().getHostName();
104             }
105         }
106 
107         return host;
108     }
109 
110     public String getPassword() {
111         return password;
112     }
113 
114     public void setPassword(String password) {
115         this.password = password;
116     }
117 
118     public String getServiceKerberosName() {
119         return serviceKerberosName;
120     }
121 
122     public void setServiceKerberosName(String serviceKerberosName) {
123         this.serviceKerberosName = serviceKerberosName;
124     }
125 }