Search in sources :

Example 6 with NettyHttpService

use of co.cask.http.NettyHttpService in project cdap by caskdata.

the class HttpHandlerGeneratorTest method testHttpHandlerGenerator.

@Test
public void testHttpHandlerGenerator() throws Exception {
    MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
    HttpHandlerFactory factory = new HttpHandlerFactory("/prefix", noOpsMetricsContext);
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(MyHttpHandler.class), new AbstractDelegatorContext<MyHttpHandler>() {

        @Override
        protected MyHttpHandler createHandler() {
            return new MyHttpHandler();
        }
    });
    HttpHandler httpHandlerWithoutAnnotation = factory.createHttpHandler(TypeToken.of(NoAnnotationHandler.class), new AbstractDelegatorContext<NoAnnotationHandler>() {

        @Override
        protected NoAnnotationHandler createHandler() {
            return new NoAnnotationHandler();
        }
    });
    NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler, httpHandlerWithoutAnnotation)).build();
    service.startAndWait();
    try {
        InetSocketAddress bindAddress = service.getBindAddress();
        // Make a GET call
        URLConnection urlConn = new URL(String.format("http://%s:%d/prefix/p2/handle", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        urlConn.setReadTimeout(2000);
        Assert.assertEquals("Hello World", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
        // Make a POST call
        urlConn = new URL(String.format("http://%s:%d/prefix/p2/echo/test", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        urlConn.setReadTimeout(2000);
        urlConn.setDoOutput(true);
        ByteStreams.copy(ByteStreams.newInputStreamSupplier("Hello".getBytes(Charsets.UTF_8)), urlConn.getOutputStream());
        Assert.assertEquals("Hello test", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
        // Ensure that even though the handler did not have a class-level annotation, we still prefix the path that it
        // handles by "/prefix"
        urlConn = new URL(String.format("http://%s:%d/prefix/ping", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        urlConn.setReadTimeout(2000);
        Assert.assertEquals("OK", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
    } finally {
        service.stopAndWait();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL) NettyHttpService(co.cask.http.NettyHttpService) Test(org.junit.Test)

Example 7 with NettyHttpService

use of co.cask.http.NettyHttpService in project cdap by caskdata.

the class HttpHandlerGeneratorTest method testContentConsumer.

@Test
public void testContentConsumer() throws Exception {
    MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
    HttpHandlerFactory factory = new HttpHandlerFactory("/content", noOpsMetricsContext);
    // Create the file upload handler and starts a netty server with it
    final File outputDir = TEMP_FOLDER.newFolder();
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {

        @Override
        protected FileHandler createHandler() {
            return new FileHandler(outputDir);
        }
    });
    // Creates a Netty http server with 1K request buffer
    NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler)).setHttpChunkLimit(1024).build();
    service.startAndWait();
    try {
        InetSocketAddress bindAddress = service.getBindAddress();
        testUpload(outputDir, bindAddress, "");
        testUpload(outputDir, bindAddress, "-no-tx");
    } finally {
        service.stopAndWait();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NettyHttpService(co.cask.http.NettyHttpService) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) File(java.io.File) Test(org.junit.Test)

Example 8 with NettyHttpService

use of co.cask.http.NettyHttpService in project cdap by caskdata.

the class AuthorizationHandlerTest method testDisabled.

private void testDisabled(CConfiguration cConf, FeatureDisabledException.Feature feature, String configSetting) throws Exception {
    final InMemoryAuthorizer authorizer = new InMemoryAuthorizer();
    NettyHttpService service = new CommonNettyHttpServiceBuilder(cConf, getClass().getSimpleName()).addHttpHandlers(ImmutableList.of(new AuthorizationHandler(authorizer, new AuthorizerInstantiator(cConf, FACTORY) {

        @Override
        public Authorizer get() {
            return authorizer;
        }
    }, cConf, authorizer, new MasterAuthenticationContext(), entityExistenceVerifier))).build();
    service.startAndWait();
    try {
        final AuthorizationClient client = new AuthorizationClient(ClientConfig.builder().setConnectionConfig(ConnectionConfig.builder().setHostname(service.getBindAddress().getHostName()).setPort(service.getBindAddress().getPort()).setSSLEnabled(false).build()).build());
        final NamespaceId ns1 = Ids.namespace("ns1");
        final Role admins = new Role("admins");
        // Test that the right exception is thrown when any Authorization REST API is called with authorization disabled
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.grant(ns1, admin, ImmutableSet.of(Action.READ));
            }
        }, feature, configSetting);
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.revoke(ns1, admin, ImmutableSet.of(Action.READ));
            }
        }, feature, configSetting);
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.revoke(ns1);
            }
        }, feature, configSetting);
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.listPrivileges(admin);
            }
        }, feature, configSetting);
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.addRoleToPrincipal(admins, admin);
            }
        }, feature, configSetting);
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.removeRoleFromPrincipal(admins, admin);
            }
        }, feature, configSetting);
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.createRole(admins);
            }
        }, feature, configSetting);
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.dropRole(admins);
            }
        }, feature, configSetting);
        verifyFeatureDisabled(new DisabledFeatureCaller() {

            @Override
            public void call() throws Exception {
                client.listAllRoles();
            }
        }, feature, configSetting);
    } finally {
        service.stopAndWait();
    }
}
Also used : Role(co.cask.cdap.proto.security.Role) MasterAuthenticationContext(co.cask.cdap.security.auth.context.MasterAuthenticationContext) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) InMemoryAuthorizer(co.cask.cdap.security.authorization.InMemoryAuthorizer) AuthorizerInstantiator(co.cask.cdap.security.authorization.AuthorizerInstantiator) NettyHttpService(co.cask.http.NettyHttpService) AuthorizationClient(co.cask.cdap.client.AuthorizationClient) NamespaceId(co.cask.cdap.proto.id.NamespaceId) FeatureDisabledException(co.cask.cdap.common.FeatureDisabledException) IOException(java.io.IOException) RoleNotFoundException(co.cask.cdap.security.spi.authorization.RoleNotFoundException) RoleAlreadyExistsException(co.cask.cdap.security.spi.authorization.RoleAlreadyExistsException) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) NotFoundException(co.cask.cdap.common.NotFoundException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException)

Aggregations

NettyHttpService (co.cask.http.NettyHttpService)8 HttpHandler (co.cask.http.HttpHandler)5 InetSocketAddress (java.net.InetSocketAddress)5 Test (org.junit.Test)5 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)4 NoOpMetricsCollectionService (co.cask.cdap.common.metrics.NoOpMetricsCollectionService)4 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 CommonNettyHttpServiceBuilder (co.cask.cdap.common.http.CommonNettyHttpServiceBuilder)2 File (java.io.File)2 Discoverable (org.apache.twill.discovery.Discoverable)2 AuthorizationClient (co.cask.cdap.client.AuthorizationClient)1 FeatureDisabledException (co.cask.cdap.common.FeatureDisabledException)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)1 HttpHandlerFactory (co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory)1 ProgramType (co.cask.cdap.proto.ProgramType)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 Role (co.cask.cdap.proto.security.Role)1