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    * https://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.netty5.example.http2;
16  
17  import io.netty5.buffer.api.Buffer;
18  import io.netty5.handler.codec.http.QueryStringDecoder;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.List;
23  
24  import static io.netty5.buffer.api.DefaultBufferAllocators.preferredAllocator;
25  import static java.util.Objects.requireNonNull;
26  
27  /**
28   * Utility methods used by the example client and server.
29   */
30  public final class Http2ExampleUtil {
31  
32      /**
33       * Response header sent in response to the http->http2 cleartext upgrade request.
34       */
35      public static final String UPGRADE_RESPONSE_HEADER = "http-to-http2-upgrade";
36  
37      /**
38       * Size of the block to be read from the input stream.
39       */
40      private static final int BLOCK_SIZE = 1024;
41  
42      private Http2ExampleUtil() { }
43  
44      /**
45       * @param string the string to be converted to an integer.
46       * @param defaultValue the default value
47       * @return the integer value of a string or the default value, if the string is either null or empty.
48       */
49      public static int toInt(String string, int defaultValue) {
50          if (string != null && !string.isEmpty()) {
51              return Integer.parseInt(string);
52          }
53          return defaultValue;
54      }
55  
56      /**
57       * Reads an InputStream into a byte array.
58       * @param input the InputStream.
59       * @return a byte array representation of the InputStream.
60       * @throws IOException if an I/O exception of some sort happens while reading the InputStream.
61       */
62      public static Buffer toBuffer(InputStream input) throws IOException {
63          Buffer buf = preferredAllocator().allocate(BLOCK_SIZE);
64          byte[] array = new byte[BLOCK_SIZE];
65          for (;;) {
66              int n = input.read(array);
67              if (n < 0) {
68                  return buf;
69              }
70              buf.writeBytes(array, 0, n);
71          }
72      }
73  
74      /**
75       * @param query the decoder of query string
76       * @param key the key to lookup
77       * @return the first occurrence of that key in the string parameters
78       */
79      public static String firstValue(QueryStringDecoder query, String key) {
80          requireNonNull(query, "Query can't be null!");
81          List<String> values = query.parameters().get(key);
82          if (values == null || values.isEmpty()) {
83              return null;
84          }
85          return values.get(0);
86      }
87  }