View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty.example.http2;
16  
17  import static io.netty.util.internal.ObjectUtil.checkNotNull;
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.handler.codec.http.QueryStringDecoder;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.List;
25  
26  /**
27   * Utility methods used by the example client and server.
28   */
29  public final class Http2ExampleUtil {
30  
31      /**
32       * Response header sent in response to the http->http2 cleartext upgrade request.
33       */
34      public static final String UPGRADE_RESPONSE_HEADER = "http-to-http2-upgrade";
35  
36      /**
37       * Size of the block to be read from the input stream.
38       */
39      private static final int BLOCK_SIZE = 1024;
40  
41      private Http2ExampleUtil() { }
42  
43      /**
44       * @param string the string to be converted to an integer.
45       * @param defaultValue the default value
46       * @return the integer value of a string or the default value, if the string is either null or empty.
47       */
48      public static int toInt(String string, int defaultValue) {
49          if (string != null && !string.isEmpty()) {
50              return Integer.parseInt(string);
51          }
52          return defaultValue;
53      }
54  
55      /**
56       * Reads an InputStream into a byte array.
57       * @param input the InputStream.
58       * @return a byte array representation of the InputStream.
59       * @throws IOException if an I/O exception of some sort happens while reading the InputStream.
60       */
61      public static ByteBuf toByteBuf(InputStream input) throws IOException {
62          ByteBuf buf = Unpooled.buffer();
63          int n = 0;
64          do {
65              n = buf.writeBytes(input, BLOCK_SIZE);
66          } while (n > 0);
67          return buf;
68      }
69  
70      /**
71       * @param query the decoder of query string
72       * @param key the key to lookup
73       * @return the first occurrence of that key in the string parameters
74       */
75      public static String firstValue(QueryStringDecoder query, String key) {
76          checkNotNull(query, "Query can't be null!");
77          List<String> values = query.parameters().get(key);
78          if (values == null || values.isEmpty()) {
79              return null;
80          }
81          return values.get(0);
82      }
83  }