View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.localserver;
29  
30  import java.io.IOException;
31  import java.util.Locale;
32  
33  import org.apache.http.Consts;
34  import org.apache.http.HttpException;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.HttpStatus;
38  import org.apache.http.MethodNotSupportedException;
39  import org.apache.http.entity.ContentType;
40  import org.apache.http.nio.entity.NByteArrayEntity;
41  import org.apache.http.protocol.HttpContext;
42  import org.apache.http.protocol.HttpRequestHandler;
43  
44  /**
45   * A handler that generates random data.
46   */
47  public class RandomHandler implements HttpRequestHandler {
48  
49      private final static byte[] RANGE;
50      static {
51          RANGE = ("abcdefghijklmnopqrstuvwxyz" +
52                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"
53          ).getBytes(Consts.ASCII);
54      }
55  
56      /**
57       * Handles a request by generating random data.
58       * The length of the response can be specified in the request URI
59       * as a number after the last /. For example /random/whatever/20
60       * will generate 20 random bytes in the printable ASCII range.
61       * If the request URI ends with /, a random number of random bytes
62       * is generated, but at least one.
63       *
64       * @param request   the request
65       * @param response  the response
66       * @param context   the context
67       *
68       * @throws HttpException    in case of a problem
69       * @throws IOException      in case of an IO problem
70       */
71      @Override
72      public void handle(final HttpRequest request,
73                         final HttpResponse response,
74                         final HttpContext context)
75          throws HttpException, IOException {
76  
77          final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ROOT);
78          if (!"GET".equals(method) && !"HEAD".equals(method)) {
79              throw new MethodNotSupportedException
80                  (method + " not supported by " + getClass().getName());
81          }
82  
83          final String uri = request.getRequestLine().getUri();
84          final int  slash = uri.lastIndexOf('/');
85          int length = -1;
86          if (slash < uri.length()-1) {
87              try {
88                  // no more than Integer, 2 GB ought to be enough for anybody
89                  length = Integer.parseInt(uri.substring(slash+1));
90  
91                  if (length < 0) {
92                      response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
93                      response.setReasonPhrase("LENGTH " + length);
94                  }
95              } catch (final NumberFormatException nfx) {
96                  response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
97                  response.setReasonPhrase(nfx.toString());
98              }
99          } else {
100             // random length, but make sure at least something is sent
101             length = 1 + (int)(Math.random() * 79.0);
102         }
103 
104         if (length >= 0) {
105             final byte[] data = new byte[length];
106             for (int i = 0; i < length; i++) {
107                 double value = 0.0;
108                 // we get 5 random characters out of one random value
109                 if (i%5 == 0) {
110                     value = Math.random();
111                 }
112                 value = value * RANGE.length;
113                 final int d = (int) value;
114                 value = value - d;
115                 data[i] = RANGE[d];
116             }
117             final NByteArrayEntity bae = new NByteArrayEntity(data, ContentType.DEFAULT_TEXT);
118             response.setEntity(bae);
119         }
120     }
121 
122 }