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.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.apache.commons.cli.Options;
29 import org.apache.commons.cli.ParseException;
30 import org.apache.commons.cli.PosixParser;
31 import org.apache.commons.lang.ArrayUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.hbase.HBaseConfiguration;
37 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
38 import org.apache.hadoop.hbase.http.InfoServer;
39 import org.apache.hadoop.hbase.rest.filter.AuthFilter;
40 import org.apache.hadoop.hbase.security.UserProvider;
41 import org.apache.hadoop.hbase.util.DNS;
42 import org.apache.hadoop.hbase.util.HttpServerUtil;
43 import org.apache.hadoop.hbase.util.Strings;
44 import org.apache.hadoop.hbase.util.VersionInfo;
45 import org.mortbay.jetty.Connector;
46 import org.mortbay.jetty.Server;
47 import org.mortbay.jetty.nio.SelectChannelConnector;
48 import org.mortbay.jetty.security.SslSelectChannelConnector;
49 import org.mortbay.jetty.servlet.Context;
50 import org.mortbay.jetty.servlet.FilterHolder;
51 import org.mortbay.jetty.servlet.ServletHolder;
52 import org.mortbay.thread.QueuedThreadPool;
53
54 import com.google.common.base.Preconditions;
55 import com.sun.jersey.api.json.JSONConfiguration;
56 import com.sun.jersey.spi.container.servlet.ServletContainer;
57
58
59
60
61
62
63
64
65
66
67 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
68 public class RESTServer implements Constants {
69
70 private static void printUsageAndExit(Options options, int exitCode) {
71 HelpFormatter formatter = new HelpFormatter();
72 formatter.printHelp("bin/hbase rest start", "", options,
73 "\nTo run the REST server as a daemon, execute " +
74 "bin/hbase-daemon.sh start|stop rest [--infoport <port>] [-p <port>] [-ro]\n", true);
75 System.exit(exitCode);
76 }
77
78
79
80
81
82
83 public static void main(String[] args) throws Exception {
84 Log LOG = LogFactory.getLog("RESTServer");
85
86 VersionInfo.logVersion();
87 FilterHolder authFilter = null;
88 Configuration conf = HBaseConfiguration.create();
89 Class<? extends ServletContainer> containerClass = ServletContainer.class;
90 UserProvider userProvider = UserProvider.instantiate(conf);
91
92 if (userProvider.isHadoopSecurityEnabled() && userProvider.isHBaseSecurityEnabled()) {
93 String machineName = Strings.domainNamePointerToHostName(
94 DNS.getDefaultHost(conf.get(REST_DNS_INTERFACE, "default"),
95 conf.get(REST_DNS_NAMESERVER, "default")));
96 String keytabFilename = conf.get(REST_KEYTAB_FILE);
97 Preconditions.checkArgument(keytabFilename != null && !keytabFilename.isEmpty(),
98 REST_KEYTAB_FILE + " should be set if security is enabled");
99 String principalConfig = conf.get(REST_KERBEROS_PRINCIPAL);
100 Preconditions.checkArgument(principalConfig != null && !principalConfig.isEmpty(),
101 REST_KERBEROS_PRINCIPAL + " should be set if security is enabled");
102 userProvider.login(REST_KEYTAB_FILE, REST_KERBEROS_PRINCIPAL, machineName);
103 if (conf.get(REST_AUTHENTICATION_TYPE) != null) {
104 containerClass = RESTServletContainer.class;
105 authFilter = new FilterHolder();
106 authFilter.setClassName(AuthFilter.class.getName());
107 authFilter.setName("AuthenticationFilter");
108 }
109 }
110
111 RESTServlet servlet = RESTServlet.getInstance(conf, userProvider);
112
113 Options options = new Options();
114 options.addOption("p", "port", true, "Port to bind to [default: 8080]");
115 options.addOption("ro", "readonly", false, "Respond only to GET HTTP " +
116 "method requests [default: false]");
117 options.addOption(null, "infoport", true, "Port for web UI");
118
119 CommandLine commandLine = null;
120 try {
121 commandLine = new PosixParser().parse(options, args);
122 } catch (ParseException e) {
123 LOG.error("Could not parse: ", e);
124 printUsageAndExit(options, -1);
125 }
126
127
128 if (commandLine != null && commandLine.hasOption("port")) {
129 String val = commandLine.getOptionValue("port");
130 servlet.getConfiguration()
131 .setInt("hbase.rest.port", Integer.valueOf(val));
132 LOG.debug("port set to " + val);
133 }
134
135
136 if (commandLine != null && commandLine.hasOption("readonly")) {
137 servlet.getConfiguration().setBoolean("hbase.rest.readonly", true);
138 LOG.debug("readonly set to true");
139 }
140
141
142 if (commandLine != null && commandLine.hasOption("infoport")) {
143 String val = commandLine.getOptionValue("infoport");
144 servlet.getConfiguration()
145 .setInt("hbase.rest.info.port", Integer.valueOf(val));
146 LOG.debug("Web UI port set to " + val);
147 }
148
149 @SuppressWarnings("unchecked")
150 List<String> remainingArgs = commandLine != null ?
151 commandLine.getArgList() : new ArrayList<String>();
152 if (remainingArgs.size() != 1) {
153 printUsageAndExit(options, 1);
154 }
155
156 String command = remainingArgs.get(0);
157 if ("start".equals(command)) {
158
159 } else if ("stop".equals(command)) {
160 System.exit(1);
161 } else {
162 printUsageAndExit(options, 1);
163 }
164
165
166 ServletHolder sh = new ServletHolder(containerClass);
167 sh.setInitParameter(
168 "com.sun.jersey.config.property.resourceConfigClass",
169 ResourceConfig.class.getCanonicalName());
170 sh.setInitParameter("com.sun.jersey.config.property.packages",
171 "jetty");
172
173
174
175
176
177
178
179 ServletHolder shPojoMap = new ServletHolder(containerClass);
180 @SuppressWarnings("unchecked")
181 Map<String, String> shInitMap = sh.getInitParameters();
182 for (Entry<String, String> e : shInitMap.entrySet()) {
183 shPojoMap.setInitParameter(e.getKey(), e.getValue());
184 }
185 shPojoMap.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
186
187
188
189 Server server = new Server();
190
191 Connector connector = new SelectChannelConnector();
192 if(conf.getBoolean(REST_SSL_ENABLED, false)) {
193 SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
194 String keystore = conf.get(REST_SSL_KEYSTORE_STORE);
195 String password = HBaseConfiguration.getPassword(conf,
196 REST_SSL_KEYSTORE_PASSWORD, null);
197 String keyPassword = HBaseConfiguration.getPassword(conf,
198 REST_SSL_KEYSTORE_KEYPASSWORD, password);
199 sslConnector.setKeystore(keystore);
200 sslConnector.setPassword(password);
201 sslConnector.setKeyPassword(keyPassword);
202 connector = sslConnector;
203 }
204 connector.setPort(servlet.getConfiguration().getInt("hbase.rest.port", 8080));
205 connector.setHost(servlet.getConfiguration().get("hbase.rest.host", "0.0.0.0"));
206 connector.setHeaderBufferSize(65536);
207
208 server.addConnector(connector);
209
210
211
212
213
214
215 int maxThreads = servlet.getConfiguration().getInt("hbase.rest.threads.max", 100);
216 int minThreads = servlet.getConfiguration().getInt("hbase.rest.threads.min", 2);
217 QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads);
218 threadPool.setMinThreads(minThreads);
219 server.setThreadPool(threadPool);
220
221 server.setSendServerVersion(false);
222 server.setSendDateHeader(false);
223 server.setStopAtShutdown(true);
224
225 Context context = new Context(server, "/", Context.SESSIONS);
226 context.addServlet(shPojoMap, "/status/cluster");
227 context.addServlet(sh, "/*");
228 if (authFilter != null) {
229 context.addFilter(authFilter, "/*", 1);
230 }
231
232
233 String[] filterClasses = servlet.getConfiguration().getStrings(FILTER_CLASSES,
234 ArrayUtils.EMPTY_STRING_ARRAY);
235 for (String filter : filterClasses) {
236 filter = filter.trim();
237 context.addFilter(Class.forName(filter), "/*", 0);
238 }
239 HttpServerUtil.constrainHttpMethods(context);
240
241
242 int port = conf.getInt("hbase.rest.info.port", 8085);
243 if (port >= 0) {
244 conf.setLong("startcode", System.currentTimeMillis());
245 String a = conf.get("hbase.rest.info.bindAddress", "0.0.0.0");
246 InfoServer infoServer = new InfoServer("rest", a, port, false, conf);
247 infoServer.setAttribute("hbase.conf", conf);
248 infoServer.start();
249 }
250
251
252 server.start();
253 server.join();
254 }
255 }