View Javadoc

1   /*
2    * Copyright 2014 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.haproxy;
17  
18  /**
19   * The command of an HAProxy proxy protocol header
20   */
21  public enum HAProxyCommand {
22      /**
23       * The LOCAL command represents a connection that was established on purpose by the proxy
24       * without being relayed.
25       */
26      LOCAL(HAProxyConstants.COMMAND_LOCAL_BYTE),
27      /**
28       * The PROXY command represents a connection that was established on behalf of another node,
29       * and reflects the original connection endpoints.
30       */
31      PROXY(HAProxyConstants.COMMAND_PROXY_BYTE);
32  
33      /**
34       * The command is specified in the lowest 4 bits of the protocol version and command byte
35       */
36      private static final byte COMMAND_MASK = 0x0f;
37  
38      private final byte byteValue;
39  
40      /**
41       * Creates a new instance
42       */
43      HAProxyCommand(byte byteValue) {
44          this.byteValue = byteValue;
45      }
46  
47      /**
48       * Returns the {@link HAProxyCommand} represented by the lowest 4 bits of the specified byte.
49       *
50       * @param verCmdByte protocol version and command byte
51       */
52      public static HAProxyCommand valueOf(byte verCmdByte) {
53          int cmd = verCmdByte & COMMAND_MASK;
54          switch ((byte) cmd) {
55              case HAProxyConstants.COMMAND_PROXY_BYTE:
56                  return PROXY;
57              case HAProxyConstants.COMMAND_LOCAL_BYTE:
58                  return LOCAL;
59              default:
60                  throw new IllegalArgumentException("unknown command: " + cmd);
61          }
62      }
63  
64      /**
65       * Returns the byte value of this command.
66       */
67      public byte byteValue() {
68          return byteValue;
69      }
70  }