View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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      // Inject the conf for the test by being first to make singleton
53      RESTServlet.getInstance(conf, UserProvider.instantiate(conf));
54  
55      // set up the Jersey servlet container for Jetty
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      // set up Jetty and run the embedded server
66      server = new Server(0);
67      server.setSendServerVersion(false);
68      server.setSendDateHeader(false);
69        // set up context
70      Context context = new Context(server, "/", Context.SESSIONS);
71      context.addServlet(sh, "/*");
72      // Load filters specified from configuration.
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        // start the server
84      server.start();
85        // get the port
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 }