use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project voldemort by voldemort.
the class CoordinatorAdminRequestHandler method handlePut.
private HttpResponse handlePut(Map<String, Properties> configsToPut) {
logger.info("Handling a Http POST Admin request");
String response = storeClientConfigs.putConfigs(configsToPut);
HttpResponseStatus responseStatus;
if (response.contains(StoreClientConfigService.ERROR_MESSAGE_PARAM_KEY)) {
responseStatus = BAD_REQUEST;
} else {
responseStatus = OK;
}
return sendResponse(responseStatus, response);
}
use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project crate by crate.
the class HttpBlobHandler method writeToFile.
protected void writeToFile(ChannelBuffer input, boolean last, final boolean continueExpected) throws IOException {
if (digestBlob == null) {
throw new IllegalStateException("digestBlob is null in writeToFile");
}
RemoteDigestBlob.Status status = digestBlob.addContent(input, last);
HttpResponseStatus exitStatus = null;
switch(status) {
case FULL:
exitStatus = HttpResponseStatus.CREATED;
break;
case PARTIAL:
// tell the client to continue
if (continueExpected) {
write(ctx, succeededFuture(ctx.getChannel()), CONTINUE.duplicate());
}
return;
case MISMATCH:
exitStatus = HttpResponseStatus.BAD_REQUEST;
break;
case EXISTS:
exitStatus = HttpResponseStatus.CONFLICT;
break;
case FAILED:
exitStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;
break;
}
assert exitStatus != null : "exitStatus should not be null";
LOGGER.trace("writeToFile exit status http:{} blob: {}", exitStatus, status);
simpleResponse(exitStatus);
}
use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project crate by crate.
the class HttpBlobHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
Throwable ex = e.getCause();
if (ex instanceof ClosedChannelException) {
LOGGER.trace("channel closed: {}", ex.toString());
return;
} else if (ex instanceof IOException) {
String message = ex.getMessage();
if (message != null && message.contains("Connection reset by peer")) {
LOGGER.debug(message);
} else {
LOGGER.warn(message, e);
}
return;
}
HttpResponseStatus status;
String body = null;
if (ex instanceof DigestMismatchException || ex instanceof BlobsDisabledException || ex instanceof IllegalArgumentException) {
status = HttpResponseStatus.BAD_REQUEST;
body = String.format(Locale.ENGLISH, "Invalid request sent: %s", ex.getMessage());
} else if (ex instanceof DigestNotFoundException || ex instanceof IndexNotFoundException) {
status = HttpResponseStatus.NOT_FOUND;
} else if (ex instanceof EsRejectedExecutionException) {
status = TOO_MANY_REQUESTS;
body = String.format(Locale.ENGLISH, "Rejected execution: %s", ex.getMessage());
} else {
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
body = String.format(Locale.ENGLISH, "Unhandled exception: %s", ex);
}
if (body != null) {
LOGGER.debug(body);
}
simpleResponse(status, body);
}
use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project socket.io-netty by ibdknox.
the class WebSocketServerHandler method handleHttpRequest.
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
String reqURI = req.getUri();
if (reqURI.contains(POLLING_PATH)) {
String[] parts = reqURI.split("/");
String ID = parts.length > 3 ? parts[3] : "";
PollingIOClient client = (PollingIOClient) this.pollingClients.get(ID);
if (client == null) {
//new client
client = connectPoller(ctx);
client.Reconnect(ctx, req);
return;
}
if (req.getMethod() == GET) {
client.heartbeat();
client.Reconnect(ctx, req);
} else {
//we got a message
QueryStringDecoder decoder = new QueryStringDecoder("/?" + req.getContent().toString(CharsetUtil.UTF_8));
String message = decoder.getParameters().get("data").get(0);
handleMessage(client, message);
//make sure the connection is closed once we send a response
setKeepAlive(req, false);
//send a response that allows for cross domain access
HttpResponse resp = new DefaultHttpResponse(HTTP_1_1, OK);
resp.addHeader("Access-Control-Allow-Origin", "*");
sendHttpResponse(ctx, req, resp);
}
return;
}
// Serve the WebSocket handshake request.
String location = "";
if (reqURI.equals(WEBSOCKET_PATH)) {
location = getWebSocketLocation(req);
} else if (reqURI.equals(FLASHSOCKET_PATH)) {
location = getFlashSocketLocation(req);
}
if (location != "" && Values.UPGRADE.equalsIgnoreCase(req.getHeader(CONNECTION)) && WEBSOCKET.equalsIgnoreCase(req.getHeader(Names.UPGRADE))) {
// Create the WebSocket handshake response.
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
res.addHeader(Names.UPGRADE, WEBSOCKET);
res.addHeader(CONNECTION, Values.UPGRADE);
// Fill in the headers and contents depending on handshake method.
if (req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2)) {
// New handshake method with a challenge:
res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req));
String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL);
if (protocol != null) {
res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol);
}
// Calculate the answer of the challenge.
String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length());
int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length());
long c = req.getContent().readLong();
ChannelBuffer input = ChannelBuffers.buffer(16);
input.writeInt(a);
input.writeInt(b);
input.writeLong(c);
ChannelBuffer output = ChannelBuffers.wrappedBuffer(MessageDigest.getInstance("MD5").digest(input.array()));
res.setContent(output);
} else {
// Old handshake method with no challenge:
res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req));
String protocol = req.getHeader(WEBSOCKET_PROTOCOL);
if (protocol != null) {
res.addHeader(WEBSOCKET_PROTOCOL, protocol);
}
}
// Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline();
p.remove("aggregator");
p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder());
ctx.getChannel().write(res);
p.replace("encoder", "wsencoder", new WebSocketFrameEncoder());
connectSocket(ctx);
return;
}
// Send an error page otherwise.
sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
}
use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project bagheera by mozilla-metrics.
the class SubmissionHandler method handlePost.
private void handlePost(MessageEvent e, BagheeraHttpRequest request) {
HttpResponseStatus status = BAD_REQUEST;
ChannelBuffer content = request.getContent();
String remoteIpAddress = HttpUtil.getRemoteAddr(request, ((InetSocketAddress) e.getChannel().getRemoteAddress()).getAddress().getHostAddress());
if (content.readable() && content.readableBytes() > 0) {
BagheeraMessage.Builder templateBuilder = BagheeraMessage.newBuilder();
setMessageFields(request, e, templateBuilder, System.currentTimeMillis(), false);
BagheeraMessage template = templateBuilder.buildPartial();
BagheeraMessage.Builder storeBuilder = BagheeraMessage.newBuilder(template);
storeBuilder.setPayload(ByteString.copyFrom(content.toByteBuffer()));
storeBuilder.setId(request.getId());
producer.send(storeBuilder.build());
if (request.containsHeader(HEADER_OBSOLETE_DOCUMENT)) {
handleObsoleteDocuments(request, remoteIpAddress, request.getHeaders(HEADER_OBSOLETE_DOCUMENT), template);
} else {
LOG.info("IP " + remoteIpAddress + " " + request.getNamespace() + " HTTP_PUT " + request.getId());
}
status = CREATED;
}
updateRequestMetrics(request.getNamespace(), request.getMethod().getName(), content.readableBytes());
writeResponse(status, e, request.getNamespace(), URI.create(request.getId()).toString());
}
Aggregations