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 private static final Log LOG = LogFactory.getLog(HBaseRESTTestingUtility.class);
38
39 private int testServletPort;
40 private Server server;
41
42 public int getServletPort() {
43 return testServletPort;
44 }
45
46 public void startServletContainer(Configuration conf) throws Exception {
47 if (server != null) {
48 LOG.error("ServletContainer already running");
49 return;
50 }
51
52
53 RESTServlet.getInstance(conf, UserProvider.instantiate(conf));
54
55
56 ServletHolder sh = new ServletHolder(ServletContainer.class);
57 sh.setInitParameter(
58 "com.sun.jersey.config.property.resourceConfigClass",
59 ResourceConfig.class.getCanonicalName());
60 sh.setInitParameter("com.sun.jersey.config.property.packages",
61 "jetty");
62
63 LOG.info("configured " + ServletContainer.class.getName());
64
65
66 server = new Server(0);
67 server.setSendServerVersion(false);
68 server.setSendDateHeader(false);
69
70 Context context = new Context(server, "/", Context.SESSIONS);
71 context.addServlet(sh, "/*");
72
73 String[] filterClasses = conf.getStrings(Constants.FILTER_CLASSES,
74 ArrayUtils.EMPTY_STRING_ARRAY);
75 for (String filter : filterClasses) {
76 filter = filter.trim();
77 context.addFilter(Class.forName(filter), "/*", 0);
78 }
79 conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
80 RESTServer.addCSRFFilter(context, conf);
81 HttpServerUtil.constrainHttpMethods(context, false);
82 LOG.info("Loaded filter classes :" + Arrays.toString(filterClasses));
83
84 server.start();
85
86 testServletPort = server.getConnectors()[0].getLocalPort();
87
88 LOG.info("started " + server.getClass().getName() + " on port " +
89 testServletPort);
90 }
91
92 public void shutdownServletContainer() {
93 if (server != null) {
94 try {
95 server.stop();
96 server = null;
97 RESTServlet.stop();
98 } catch (Exception e) {
99 LOG.warn(StringUtils.stringifyException(e));
100 }
101 }
102 }
103 }