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.io.UnsupportedEncodingException;
32 import java.util.Locale;
33
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 byte[] range = null;
52 try {
53 range = ("abcdefghijklmnopqrstuvwxyz" +
54 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"
55 ).getBytes("US-ASCII");
56 } catch (final UnsupportedEncodingException uex) {
57 // never, US-ASCII is guaranteed
58 }
59 RANGE = range;
60 }
61
62 /**
63 * Handles a request by generating random data.
64 * The length of the response can be specified in the request URI
65 * as a number after the last /. For example /random/whatever/20
66 * will generate 20 random bytes in the printable ASCII range.
67 * If the request URI ends with /, a random number of random bytes
68 * is generated, but at least one.
69 *
70 * @param request the request
71 * @param response the response
72 * @param context the context
73 *
74 * @throws HttpException in case of a problem
75 * @throws IOException in case of an IO problem
76 */
77 public void handle(final HttpRequest request,
78 final HttpResponse response,
79 final HttpContext context)
80 throws HttpException, IOException {
81
82 final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
83 if (!"GET".equals(method) && !"HEAD".equals(method)) {
84 throw new MethodNotSupportedException
85 (method + " not supported by " + getClass().getName());
86 }
87
88 final String uri = request.getRequestLine().getUri();
89 final int slash = uri.lastIndexOf('/');
90 int length = -1;
91 if (slash < uri.length()-1) {
92 try {
93 // no more than Integer, 2 GB ought to be enough for anybody
94 length = Integer.parseInt(uri.substring(slash+1));
95
96 if (length < 0) {
97 response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
98 response.setReasonPhrase("LENGTH " + length);
99 }
100 } catch (final NumberFormatException nfx) {
101 response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
102 response.setReasonPhrase(nfx.toString());
103 }
104 } else {
105 // random length, but make sure at least something is sent
106 length = 1 + (int)(Math.random() * 79.0);
107 }
108
109 if (length >= 0) {
110 final byte[] data = new byte[length];
111 for (int i = 0; i < length; i++) {
112 double value = 0.0;
113 // we get 5 random characters out of one random value
114 if (i%5 == 0) {
115 value = Math.random();
116 }
117 value = value * RANGE.length;
118 final int d = (int) value;
119 value = value - d;
120 data[i] = RANGE[d];
121 }
122 final NByteArrayEntity bae = new NByteArrayEntity(data, ContentType.DEFAULT_TEXT);
123 response.setEntity(bae);
124 }
125 }
126
127 }