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  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  /**
26   * The default {@link Socks5InitialRequest}.
27   */
28  public class DefaultSocks5InitialRequest extends AbstractSocks5Message implements Socks5InitialRequest {
29  
30      private final List<Socks5AuthMethod> authMethods;
31  
32      public DefaultSocks5InitialRequest(Socks5AuthMethod... authMethods) {
33          if (authMethods == null) {
34              throw new NullPointerException("authMethods");
35          }
36  
37          List<Socks5AuthMethod> list = new ArrayList<Socks5AuthMethod>(authMethods.length);
38          for (Socks5AuthMethod m: authMethods) {
39              if (m == null) {
40                  break;
41              }
42              list.add(m);
43          }
44  
45          if (list.isEmpty()) {
46              throw new IllegalArgumentException("authMethods is empty");
47          }
48  
49          this.authMethods = Collections.unmodifiableList(list);
50      }
51  
52      public DefaultSocks5InitialRequest(Iterable<Socks5AuthMethod> authMethods) {
53          if (authMethods == null) {
54              throw new NullPointerException("authSchemes");
55          }
56  
57          List<Socks5AuthMethod> list = new ArrayList<Socks5AuthMethod>();
58          for (Socks5AuthMethod m: authMethods) {
59              if (m == null) {
60                  break;
61              }
62              list.add(m);
63          }
64  
65          if (list.isEmpty()) {
66              throw new IllegalArgumentException("authMethods is empty");
67          }
68  
69          this.authMethods = Collections.unmodifiableList(list);
70      }
71  
72      @Override
73      public List<Socks5AuthMethod> authMethods() {
74          return authMethods;
75      }
76  
77      @Override
78      public String toString() {
79          StringBuilder buf = new StringBuilder(StringUtil.simpleClassName(this));
80  
81          DecoderResult decoderResult = decoderResult();
82          if (!decoderResult.isSuccess()) {
83              buf.append("(decoderResult: ");
84              buf.append(decoderResult);
85              buf.append(", authMethods: ");
86          } else {
87              buf.append("(authMethods: ");
88          }
89          buf.append(authMethods());
90          buf.append(')');
91  
92          return buf.toString();
93      }
94  }