use of org.apache.calcite.avatica.remote.Service in project calcite-avatica by apache.
the class HttpServerBuilderTest method emptyExtraRolesWithExplitRealm.
@Test
public void emptyExtraRolesWithExplitRealm() {
final String[] extraRoles = new String[0];
final Service mockService = Mockito.mock(Service.class);
HttpServer server = new HttpServer.Builder().withSpnego("HTTP/localhost.localdomain@EXAMPLE.COM", "EXAMPLE.COM", extraRoles).withHandler(mockService, Serialization.JSON).build();
assertArrayEquals(extraRoles, server.getConfig().getAllowedRoles());
assertArrayEquals(new String[] { "EXAMPLE.COM" }, server.getAllowedRealms("EXAMPLE.COM", server.getConfig()));
}
use of org.apache.calcite.avatica.remote.Service in project calcite-avatica by apache.
the class Main method start.
/**
* Creates and starts an {@link HttpServer} using the given factory to create the Handler.
*
* <p>Arguments are as follows:
* <ul>
* <li>args[0]: the {@link org.apache.calcite.avatica.Meta.Factory} class
* name
* <li>args[1+]: arguments passed along to
* {@link org.apache.calcite.avatica.Meta.Factory#create(java.util.List)}
* </ul>
*
* @param args Command-line arguments
* @param port Server port to bind
* @param handlerFactory Factory to create the handler used by the server
*/
public static HttpServer start(String[] args, int port, HandlerFactory handlerFactory) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
String factoryClassName = args[0];
@SuppressWarnings("unchecked") Class<Meta.Factory> factoryClass = (Class<Meta.Factory>) Class.forName(factoryClassName);
Meta.Factory factory = factoryClass.getConstructor().newInstance();
Meta meta = factory.create(Arrays.asList(args).subList(1, args.length));
Service service = new LocalService(meta);
HttpServer server = new HttpServer(port, HttpServer.wrapJettyHandler(handlerFactory.createHandler(service)));
server.start();
return server;
}
use of org.apache.calcite.avatica.remote.Service in project calcite-avatica by apache.
the class HttpServerCustomizerTest method serverCustomizersInvoked.
// needed for the mocked customizers, not the builder
@SuppressWarnings("unchecked")
@Test
public void serverCustomizersInvoked() {
ServerCustomizer<Server> mockCustomizer1 = (ServerCustomizer<Server>) mock(ServerCustomizer.class);
ServerCustomizer<Server> mockCustomizer2 = (ServerCustomizer<Server>) mock(ServerCustomizer.class);
Service service = new LocalService(mockMeta);
HttpServer server = HttpServer.Builder.<Server>newBuilder().withHandler(service, Driver.Serialization.PROTOBUF).withServerCustomizers(Arrays.asList(mockCustomizer1, mockCustomizer2), Server.class).withPort(0).build();
try {
server.start();
verify(mockCustomizer2).customize(any(Server.class));
verify(mockCustomizer1).customize(any(Server.class));
} finally {
server.stop();
}
}
use of org.apache.calcite.avatica.remote.Service in project druid by druid-io.
the class AsyncQueryForwardingServlet method service.
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(request.getContentType()) || APPLICATION_SMILE.equals(request.getContentType());
final ObjectMapper objectMapper = isSmile ? smileMapper : jsonMapper;
request.setAttribute(OBJECTMAPPER_ATTRIBUTE, objectMapper);
final String requestURI = request.getRequestURI();
final String method = request.getMethod();
final Server targetServer;
// The Router does not have the ability to look inside SQL queries and route them intelligently, so just treat
// them as a generic request.
final boolean isNativeQueryEndpoint = requestURI.startsWith("/druid/v2") && !requestURI.startsWith("/druid/v2/sql");
final boolean isSqlQueryEndpoint = requestURI.startsWith("/druid/v2/sql");
final boolean isAvaticaJson = requestURI.startsWith("/druid/v2/sql/avatica");
final boolean isAvaticaPb = requestURI.startsWith("/druid/v2/sql/avatica-protobuf");
if (isAvaticaPb) {
byte[] requestBytes = IOUtils.toByteArray(request.getInputStream());
Service.Request protobufRequest = this.protobufTranslation.parseRequest(requestBytes);
String connectionId = getAvaticaProtobufConnectionId(protobufRequest);
targetServer = hostFinder.findServerAvatica(connectionId);
request.setAttribute(AVATICA_QUERY_ATTRIBUTE, requestBytes);
LOG.debug("Forwarding protobuf JDBC connection [%s] to broker [%s]", connectionId, targetServer);
} else if (isAvaticaJson) {
Map<String, Object> requestMap = objectMapper.readValue(request.getInputStream(), JacksonUtils.TYPE_REFERENCE_MAP_STRING_OBJECT);
String connectionId = getAvaticaConnectionId(requestMap);
targetServer = hostFinder.findServerAvatica(connectionId);
byte[] requestBytes = objectMapper.writeValueAsBytes(requestMap);
request.setAttribute(AVATICA_QUERY_ATTRIBUTE, requestBytes);
LOG.debug("Forwarding JDBC connection [%s] to broker [%s]", connectionId, targetServer.getHost());
} else if (HttpMethod.DELETE.is(method)) {
// query cancellation request
targetServer = hostFinder.pickDefaultServer();
broadcastQueryCancelRequest(request, targetServer);
LOG.debug("Broadcasting cancellation request to all brokers");
} else if (isNativeQueryEndpoint && HttpMethod.POST.is(method)) {
// query request
try {
Query inputQuery = objectMapper.readValue(request.getInputStream(), Query.class);
if (inputQuery != null) {
targetServer = hostFinder.pickServer(inputQuery);
if (inputQuery.getId() == null) {
inputQuery = inputQuery.withId(UUID.randomUUID().toString());
}
LOG.debug("Forwarding JSON query [%s] to broker [%s]", inputQuery.getId(), targetServer.getHost());
} else {
targetServer = hostFinder.pickDefaultServer();
LOG.debug("Forwarding JSON request to broker [%s]", targetServer.getHost());
}
request.setAttribute(QUERY_ATTRIBUTE, inputQuery);
} catch (IOException e) {
handleQueryParseException(request, response, objectMapper, e, true);
return;
} catch (Exception e) {
handleException(response, objectMapper, e);
return;
}
} else if (routeSqlByStrategy && isSqlQueryEndpoint && HttpMethod.POST.is(method)) {
try {
SqlQuery inputSqlQuery = objectMapper.readValue(request.getInputStream(), SqlQuery.class);
request.setAttribute(SQL_QUERY_ATTRIBUTE, inputSqlQuery);
targetServer = hostFinder.findServerSql(inputSqlQuery);
LOG.debug("Forwarding SQL query to broker [%s]", targetServer.getHost());
} catch (IOException e) {
handleQueryParseException(request, response, objectMapper, e, false);
return;
} catch (Exception e) {
handleException(response, objectMapper, e);
return;
}
} else {
targetServer = hostFinder.pickDefaultServer();
LOG.debug("Forwarding query to broker [%s]", targetServer.getHost());
}
request.setAttribute(HOST_ATTRIBUTE, targetServer.getHost());
request.setAttribute(SCHEME_ATTRIBUTE, targetServer.getScheme());
doService(request, response);
}
use of org.apache.calcite.avatica.remote.Service in project calcite-avatica by apache.
the class HttpServerBuilderTest method extraAllowedRolesConfiguredWithExplitRealm.
@Test
public void extraAllowedRolesConfiguredWithExplitRealm() {
final String[] extraRoles = new String[] { "BAR.COM" };
final Service mockService = Mockito.mock(Service.class);
HttpServer server = new HttpServer.Builder().withSpnego("HTTP/localhost.localdomain@EXAMPLE.COM", "EXAMPLE.COM", extraRoles).withHandler(mockService, Serialization.JSON).build();
assertArrayEquals(extraRoles, server.getConfig().getAllowedRoles());
assertArrayEquals(new String[] { "EXAMPLE.COM", "BAR.COM" }, server.getAllowedRealms("EXAMPLE.COM", server.getConfig()));
}
Aggregations