Search in sources :

Example 6 with Service

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()));
}
Also used : Service(org.apache.calcite.avatica.remote.Service) Test(org.junit.Test)

Example 7 with Service

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;
}
Also used : Meta(org.apache.calcite.avatica.Meta) LocalService(org.apache.calcite.avatica.remote.LocalService) Service(org.apache.calcite.avatica.remote.Service) LocalService(org.apache.calcite.avatica.remote.LocalService)

Example 8 with Service

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();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) Service(org.apache.calcite.avatica.remote.Service) LocalService(org.apache.calcite.avatica.remote.LocalService) LocalService(org.apache.calcite.avatica.remote.LocalService) Test(org.junit.Test)

Example 9 with Service

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);
}
Also used : SqlQuery(org.apache.druid.sql.http.SqlQuery) Server(org.apache.druid.client.selector.Server) Query(org.apache.druid.query.Query) SqlQuery(org.apache.druid.sql.http.SqlQuery) Service(org.apache.calcite.avatica.remote.Service) IOException(java.io.IOException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServletException(javax.servlet.ServletException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 10 with Service

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()));
}
Also used : Service(org.apache.calcite.avatica.remote.Service) Test(org.junit.Test)

Aggregations

Service (org.apache.calcite.avatica.remote.Service)14 Test (org.junit.Test)11 LocalService (org.apache.calcite.avatica.remote.LocalService)4 ArrayList (java.util.ArrayList)2 Meta (org.apache.calcite.avatica.Meta)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 JsonHandler (org.apache.calcite.avatica.remote.JsonHandler)1 JsonService (org.apache.calcite.avatica.remote.JsonService)1 LocalJsonService (org.apache.calcite.avatica.remote.LocalJsonService)1 TypedValue (org.apache.calcite.avatica.remote.TypedValue)1 HttpServer (org.apache.calcite.avatica.server.HttpServer)1 Server (org.apache.druid.client.selector.Server)1