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.v4;
17  
18  import io.netty.handler.codec.DecoderResult;
19  import io.netty.util.internal.StringUtil;
20  
21  import java.net.IDN;
22  
23  /**
24   * The default {@link Socks4CommandRequest}.
25   */
26  public class DefaultSocks4CommandRequest extends AbstractSocks4Message implements Socks4CommandRequest {
27  
28      private final Socks4CommandType type;
29      private final String dstAddr;
30      private final int dstPort;
31      private final String userId;
32  
33      /**
34       * Creates a new instance.
35       *
36       * @param type the type of the request
37       * @param dstAddr the {@code DSTIP} field of the request
38       * @param dstPort the {@code DSTPORT} field of the request
39       */
40      public DefaultSocks4CommandRequest(Socks4CommandType type, String dstAddr, int dstPort) {
41          this(type, dstAddr, dstPort, "");
42      }
43  
44      /**
45       * Creates a new instance.
46       *
47       * @param type the type of the request
48       * @param dstAddr the {@code DSTIP} field of the request
49       * @param dstPort the {@code DSTPORT} field of the request
50       * @param userId the {@code USERID} field of the request
51       */
52      public DefaultSocks4CommandRequest(Socks4CommandType type, String dstAddr, int dstPort, String userId) {
53          if (type == null) {
54              throw new NullPointerException("type");
55          }
56          if (dstAddr == null) {
57              throw new NullPointerException("dstAddr");
58          }
59          if (dstPort <= 0 || dstPort >= 65536) {
60              throw new IllegalArgumentException("dstPort: " + dstPort + " (expected: 1~65535)");
61          }
62          if (userId == null) {
63              throw new NullPointerException("userId");
64          }
65  
66          this.userId = userId;
67          this.type = type;
68          this.dstAddr = IDN.toASCII(dstAddr);
69          this.dstPort = dstPort;
70      }
71  
72      @Override
73      public Socks4CommandType type() {
74          return type;
75      }
76  
77      @Override
78      public String dstAddr() {
79          return dstAddr;
80      }
81  
82      @Override
83      public int dstPort() {
84          return dstPort;
85      }
86  
87      @Override
88      public String userId() {
89          return userId;
90      }
91  
92      @Override
93      public String toString() {
94          StringBuilder buf = new StringBuilder(128);
95          buf.append(StringUtil.simpleClassName(this));
96  
97          DecoderResult decoderResult = decoderResult();
98          if (!decoderResult.isSuccess()) {
99              buf.append("(decoderResult: ");
100             buf.append(decoderResult);
101             buf.append(", type: ");
102         } else {
103             buf.append("(type: ");
104         }
105         buf.append(type());
106         buf.append(", dstAddr: ");
107         buf.append(dstAddr());
108         buf.append(", dstPort: ");
109         buf.append(dstPort());
110         buf.append(", userId: ");
111         buf.append(userId());
112         buf.append(')');
113 
114         return buf.toString();
115     }
116 }