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