View Javadoc
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 io.netty.handler.codec.socksx.v5;
17  
18  import io.netty.handler.codec.DecoderResult;
19  import io.netty.util.internal.StringUtil;
20  
21  /**
22   * The default {@link Socks5PasswordAuthRequest}.
23   */
24  public class DefaultSocks5PasswordAuthRequest extends AbstractSocks5Message implements Socks5PasswordAuthRequest {
25  
26      private final String username;
27      private final String password;
28  
29      public DefaultSocks5PasswordAuthRequest(String username, String password) {
30          if (username == null) {
31              throw new NullPointerException("username");
32          }
33          if (password == null) {
34              throw new NullPointerException("password");
35          }
36  
37          if (username.length() > 255) {
38              throw new IllegalArgumentException("username: **** (expected: less than 256 chars)");
39          }
40          if (password.length() > 255) {
41              throw new IllegalArgumentException("password: **** (expected: less than 256 chars)");
42          }
43  
44          this.username = username;
45          this.password = password;
46      }
47  
48      @Override
49      public String username() {
50          return username;
51      }
52  
53      @Override
54      public String password() {
55          return password;
56      }
57  
58      @Override
59      public String toString() {
60          StringBuilder buf = new StringBuilder(StringUtil.simpleClassName(this));
61  
62          DecoderResult decoderResult = decoderResult();
63          if (!decoderResult.isSuccess()) {
64              buf.append("(decoderResult: ");
65              buf.append(decoderResult);
66              buf.append(", username: ");
67          } else {
68              buf.append("(username: ");
69          }
70          buf.append(username());
71          buf.append(", password: ****)");
72  
73          return buf.toString();
74      }
75  }