2 * Copyright 2008-2009 LinkedIn, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
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
;
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
;
58 public void init() throws ServletException
{
60 // if we don't already have a stores map, attempt to initialize from the
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();
71 protected void doPost(HttpServletRequest request
, HttpServletResponse response
)
72 throws ServletException
, IOException
{
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());