View Javadoc
1   /*
2    * Copyright 2013 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  
17  package io.netty.handler.codec.socksx.v5;
18  
19  /**
20   * The authentication method of SOCKS5.
21   */
22  public class Socks5AuthMethod implements Comparable<Socks5AuthMethod> {
23  
24      public static final Socks5AuthMethod NO_AUTH = new Socks5AuthMethod(0x00, "NO_AUTH");
25      public static final Socks5AuthMethod GSSAPI = new Socks5AuthMethod(0x01, "GSSAPI");
26      public static final Socks5AuthMethod PASSWORD = new Socks5AuthMethod(0x02, "PASSWORD");
27  
28      /**
29       * Indicates that the server does not accept any authentication methods the client proposed.
30       */
31      public static final Socks5AuthMethod UNACCEPTED = new Socks5AuthMethod(0xff, "UNACCEPTED");
32  
33      public static Socks5AuthMethod valueOf(byte b) {
34          switch (b) {
35          case 0x00:
36              return NO_AUTH;
37          case 0x01:
38              return GSSAPI;
39          case 0x02:
40              return PASSWORD;
41          case (byte) 0xFF:
42              return UNACCEPTED;
43          }
44  
45          return new Socks5AuthMethod(b);
46      }
47  
48      private final byte byteValue;
49      private final String name;
50      private String text;
51  
52      public Socks5AuthMethod(int byteValue) {
53          this(byteValue, "UNKNOWN");
54      }
55  
56      public Socks5AuthMethod(int byteValue, String name) {
57          if (name == null) {
58              throw new NullPointerException("name");
59          }
60  
61          this.byteValue = (byte) byteValue;
62          this.name = name;
63      }
64  
65      public byte byteValue() {
66          return byteValue;
67      }
68  
69      @Override
70      public int hashCode() {
71          return byteValue;
72      }
73  
74      @Override
75      public boolean equals(Object obj) {
76          if (!(obj instanceof Socks5AuthMethod)) {
77              return false;
78          }
79  
80          return byteValue == ((Socks5AuthMethod) obj).byteValue;
81      }
82  
83      @Override
84      public int compareTo(Socks5AuthMethod o) {
85          return byteValue - o.byteValue;
86      }
87  
88      @Override
89      public String toString() {
90          String text = this.text;
91          if (text == null) {
92              this.text = text = name + '(' + (byteValue & 0xFF) + ')';
93          }
94          return text;
95      }
96  }