查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 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  package org.jboss.netty.handler.codec.socks;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.util.CharsetUtil;
20  
21  import java.nio.charset.CharsetEncoder;
22  
23  /**
24   * An socks auth request.
25   *
26   * @see {@link SocksAuthResponse}
27   * @see {@link SocksAuthRequestDecoder}
28   */
29  public final class SocksAuthRequest extends SocksRequest {
30      private static final CharsetEncoder asciiEncoder = CharsetUtil.getEncoder(CharsetUtil.US_ASCII);
31      private static final SubnegotiationVersion SUBNEGOTIATION_VERSION = SubnegotiationVersion.AUTH_PASSWORD;
32      private final String username;
33      private final String password;
34  
35      public SocksAuthRequest(String username, String password) {
36          super(SocksRequestType.AUTH);
37          if (username == null) {
38              throw new NullPointerException("username");
39          }
40          if (password == null) {
41              throw new NullPointerException("password");
42          }
43          if (!asciiEncoder.canEncode(username) || !asciiEncoder.canEncode(password)) {
44              throw new IllegalArgumentException(
45                      "username: " + username + " or password: **** values should be in pure ascii");
46          }
47          if (username.length() > 255) {
48              throw new IllegalArgumentException("username: " + username + " exceeds 255 char limit");
49          }
50          if (password.length() > 255) {
51              throw new IllegalArgumentException("password: **** exceeds 255 char limit");
52          }
53          this.username = username;
54          this.password = password;
55      }
56  
57      /**
58       * Returns username that needs to be authenticated
59       *
60       * @return username that needs to be authenticated
61       */
62      public String getUsername() {
63          return username;
64      }
65  
66      /**
67       * Returns password that needs to be validated
68       *
69       * @return password that needs to be validated
70       */
71      public String getPassword() {
72          return password;
73      }
74  
75      @Override
76      public void encodeAsByteBuf(ChannelBuffer channelBuffer) throws Exception {
77          channelBuffer.writeByte(SUBNEGOTIATION_VERSION.getByteValue());
78          channelBuffer.writeByte(username.length());
79          channelBuffer.writeBytes(username.getBytes("US-ASCII"));
80          channelBuffer.writeByte(password.length());
81          channelBuffer.writeBytes(password.getBytes("US-ASCII"));
82      }
83  }