1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.rest;
20
21 import java.util.Arrays;
22
23 import org.apache.commons.lang.ArrayUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.security.UserProvider;
28 import org.apache.hadoop.hbase.util.HttpServerUtil;
29 import org.apache.hadoop.util.StringUtils;
30 import org.mortbay.jetty.Server;
31 import org.mortbay.jetty.servlet.Context;
32 import org.mortbay.jetty.servlet.ServletHolder;
33
34 import com.sun.jersey.spi.container.servlet.ServletContainer;
35
36 public class HBaseRESTTestingUtility {
37
38 private static final Log LOG = LogFactory.getLog(HBaseRESTTestingUtility.class);
39
40 private int testServletPort;
41 private Server server;
42
43 public int getServletPort() {
44 return testServletPort;
45 }
46
47 public void startServletContainer(Configuration conf) throws Exception {
48 if (server != null) {
49 LOG.error("ServletContainer already running");
50 return;
51 }
52
53
54 RESTServlet.getInstance(conf, UserProvider.instantiate(conf));
55
56
57 ServletHolder sh = new ServletHolder(ServletContainer.class);
58 sh.setInitParameter(
59 "com.sun.jersey.config.property.resourceConfigClass",
60 ResourceConfig.class.getCanonicalName());
61 sh.setInitParameter("com.sun.jersey.config.property.packages",
62 "jetty");
63
64 LOG.info("configured " + ServletContainer.class.getName());
65
66
67 server = new Server(0);
68 server.setSendServerVersion(false);
69 server.setSendDateHeader(false);
70
71 Context context = new Context(server, "/", Context.SESSIONS);
72 context.addServlet(sh, "/*");
73
74 String[] filterClasses = conf.getStrings(Constants.FILTER_CLASSES,
75 ArrayUtils.EMPTY_STRING_ARRAY);
76 for (String filter : filterClasses) {
77 filter = filter.trim();
78 context.addFilter(Class.forName(filter), "/*", 0);
79 }
80 conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
81 RESTServer.addCSRFFilter(context, conf);
82 HttpServerUtil.constrainHttpMethods(context, false);
83 LOG.info("Loaded filter classes :" + Arrays.toString(filterClasses));
84
85 server.start();
86
87 testServletPort = server.getConnectors()[0].getLocalPort();
88
89 LOG.info("started " + server.getClass().getName() + " on port " +
90 testServletPort);
91 }
92
93 public void shutdownServletContainer() {
94 if (server != null) try {
95 server.stop();
96 server = null;
97 RESTServlet.stop();
98 } catch (Exception e) {
99 LOG.warn(StringUtils.stringifyException(e));
100 }
101 }
102 }