bind failures while running tests
[voldemort/jeffpc.git] / src / java / voldemort / server / http / StoreServlet.java
blob0e561ef6b39b81e0b00d3d951b3ef1669f663b12
1 /*
2 * Copyright 2008-2009 LinkedIn, Inc
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
17 package voldemort.server.http;
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import java.net.HttpURLConnection;
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.apache.log4j.Logger;
32 import voldemort.common.service.ServiceType;
33 import voldemort.server.VoldemortServer;
34 import voldemort.server.protocol.RequestHandler;
35 import voldemort.utils.Utils;
37 /**
38 * Handles requests from HttpStores and multiplexes them to the appropriate
39 * sub-store for actual storage
43 public class StoreServlet extends HttpServlet {
45 private static final Logger logger = Logger.getLogger(StoreServlet.class);
46 private static final long serialVersionUID = 1;
48 private RequestHandler requestHandler;
50 /* For use by servlet container */
51 public StoreServlet() {}
53 public StoreServlet(RequestHandler handler) {
54 this.requestHandler = handler;
57 @Override
58 public void init() throws ServletException {
59 super.init();
60 // if we don't already have a stores map, attempt to initialize from the
61 // servlet context
62 if(this.requestHandler == null) {
63 ServletContext context = this.getServletContext();
64 VoldemortServer server = (VoldemortServer) Utils.notNull(context.getAttribute(VoldemortServletContextListener.SERVER_KEY));
65 HttpService httpService = (HttpService) server.getService(ServiceType.HTTP);
66 this.requestHandler = httpService.getRequestHandler();
70 @Override
71 protected void doPost(HttpServletRequest request, HttpServletResponse response)
72 throws ServletException, IOException {
73 try {
74 requestHandler.handleRequest(new DataInputStream(request.getInputStream()),
75 new DataOutputStream(response.getOutputStream()));
77 } catch(Exception e) {
78 logger.error("Uncaught exception in store servlet:", e);
79 response.sendError(HttpURLConnection.HTTP_UNAVAILABLE, e.getMessage());