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