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  
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      // Inject the conf for the test by being first to make singleton
54      RESTServlet.getInstance(conf, UserProvider.instantiate(conf));
55  
56      // set up the Jersey servlet container for Jetty
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      // set up Jetty and run the embedded server
67      server = new Server(0);
68      server.setSendServerVersion(false);
69      server.setSendDateHeader(false);
70        // set up context
71      Context context = new Context(server, "/", Context.SESSIONS);
72      context.addServlet(sh, "/*");
73      // Load filters specified from configuration.
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        // start the server
85      server.start();
86        // get the port
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 }