/
ControlHandler.java
149 lines (116 loc) · 4.53 KB
/
ControlHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2013 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.couchbase.mock.http;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.*;
import java.net.URLDecoder;
import org.couchbase.mock.control.CommandNotFoundException;
import org.couchbase.mock.control.MockCommandDispatcher;
import org.couchbase.mock.control.handlers.MockHelpCommandHandler;
import org.couchbase.mock.control.MockCommand;
/**
*
* @author Mark Nunberg <mnunberg@haskalah.org>
*/
public class ControlHandler implements HttpHandler {
private final MockCommandDispatcher dispatcher;
private static class InvalidQueryException extends Exception { }
private class WantHelpException extends Exception { }
public ControlHandler(MockCommandDispatcher dispatcher) {
this.dispatcher = dispatcher;
}
private static JsonObject parseQueryParams(HttpExchange exchange) throws InvalidQueryException {
String query = exchange.getRequestURI().getRawQuery();
JsonObject payload = new JsonObject();
JsonParser parser = new JsonParser();
if (query == null) {
throw new InvalidQueryException();
}
for (String kv : query.split("&")) {
String[] parts = kv.split("=");
if (parts.length != 2) {
throw new InvalidQueryException();
}
String optName = parts[0];
JsonElement optVal;
try {
optVal = parser.parse(URLDecoder.decode(parts[1], "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new InvalidQueryException();
}
payload.add(optName, optVal);
}
return payload;
}
/**
* Sends a help text with the provided code
* @param exchange The exchange to send the text to
* @param code The HTTP status code to be used in the response
*/
private static void sendHelpText(HttpExchange exchange, int code) throws IOException {
byte[] ret = MockHelpCommandHandler.getIndentedHelp().getBytes();
exchange.sendResponseHeaders(code, ret.length);
exchange.getResponseBody().write(ret);
}
@Override
public void handle(HttpExchange exchange) throws IOException {
try {
_handle(exchange);
} catch (IOException e) {
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
}
private void _handle(HttpExchange exchange) throws IOException {
String path = exchange.getRequestURI().getPath();
path = path.replaceFirst("^/+", "");
String[] components = path.split("/");
OutputStream body = exchange.getResponseBody();
exchange.getResponseHeaders().set("Content-Type", "application/json");
try {
if (components.length != 2) {
throw new CommandNotFoundException("/mock/<COMMAND>", null);
}
if (components[1].equals("help")) {
throw new WantHelpException();
}
JsonObject payload = parseQueryParams(exchange);
String cmdStr = URLDecoder.decode(components[1], "UTF-8");
MockCommand cmd = dispatcher.getCommand(cmdStr, payload);
byte[] response = cmd.getResponse().getBytes();
exchange.sendResponseHeaders(200, response.length);
body.write(response);
} catch (WantHelpException e) {
sendHelpText(exchange, 200);
} catch (CommandNotFoundException e) {
sendHelpText(exchange, 404);
} catch (InvalidQueryException e) {
sendHelpText(exchange, 400);
} catch (RuntimeException e) {
exchange.sendResponseHeaders(500, -1);
e.printStackTrace(new PrintWriter(body));
throw e;
} finally {
body.close();
}
}
}