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