Search in sources :

Example 81 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest in project activemq-artemis by apache.

the class NettyConnector method createConnection.

@Override
public Connection createConnection() {
    if (channelClazz == null) {
        return null;
    }
    // HORNETQ-907 - strip off IPv6 scope-id (if necessary)
    SocketAddress remoteDestination = new InetSocketAddress(host, port);
    InetAddress inetAddress = ((InetSocketAddress) remoteDestination).getAddress();
    if (inetAddress instanceof Inet6Address) {
        Inet6Address inet6Address = (Inet6Address) inetAddress;
        if (inet6Address.getScopeId() != 0) {
            try {
                remoteDestination = new InetSocketAddress(InetAddress.getByAddress(inet6Address.getAddress()), ((InetSocketAddress) remoteDestination).getPort());
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException(e.getMessage());
            }
        }
    }
    logger.debug("Remote destination: " + remoteDestination);
    ChannelFuture future;
    // port 0 does not work so only use local address if set
    if (localPort != 0) {
        SocketAddress localDestination;
        if (localAddress != null) {
            localDestination = new InetSocketAddress(localAddress, localPort);
        } else {
            localDestination = new InetSocketAddress(localPort);
        }
        future = bootstrap.connect(remoteDestination, localDestination);
    } else {
        future = bootstrap.connect(remoteDestination);
    }
    future.awaitUninterruptibly();
    if (future.isSuccess()) {
        final Channel ch = future.channel();
        SslHandler sslHandler = ch.pipeline().get(SslHandler.class);
        if (sslHandler != null) {
            Future<Channel> handshakeFuture = sslHandler.handshakeFuture();
            if (handshakeFuture.awaitUninterruptibly(30000)) {
                if (handshakeFuture.isSuccess()) {
                    ChannelPipeline channelPipeline = ch.pipeline();
                    ActiveMQChannelHandler channelHandler = channelPipeline.get(ActiveMQChannelHandler.class);
                    channelHandler.active = true;
                } else {
                    ch.close().awaitUninterruptibly();
                    ActiveMQClientLogger.LOGGER.errorCreatingNettyConnection(handshakeFuture.cause());
                    return null;
                }
            } else {
                // handshakeFuture.setFailure(new SSLException("Handshake was not completed in 30 seconds"));
                ch.close().awaitUninterruptibly();
                return null;
            }
        }
        if (httpUpgradeEnabled) {
            // Send a HTTP GET + Upgrade request that will be handled by the http-upgrade handler.
            try {
                // get this first incase it removes itself
                HttpUpgradeHandler httpUpgradeHandler = (HttpUpgradeHandler) ch.pipeline().get("http-upgrade");
                String scheme = "http";
                if (sslEnabled) {
                    scheme = "https";
                }
                String ipv6Host = IPV6Util.encloseHost(host);
                URI uri = new URI(scheme, null, ipv6Host, port, null, null, null);
                HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
                request.headers().set(HttpHeaderNames.HOST, ipv6Host);
                request.headers().set(HttpHeaderNames.UPGRADE, ACTIVEMQ_REMOTING);
                request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderNames.UPGRADE);
                final String serverName = ConfigurationHelper.getStringProperty(TransportConstants.ACTIVEMQ_SERVER_NAME, null, configuration);
                if (serverName != null) {
                    request.headers().set(TransportConstants.ACTIVEMQ_SERVER_NAME, serverName);
                }
                final String endpoint = ConfigurationHelper.getStringProperty(TransportConstants.HTTP_UPGRADE_ENDPOINT_PROP_NAME, null, configuration);
                if (endpoint != null) {
                    request.headers().set(TransportConstants.HTTP_UPGRADE_ENDPOINT_PROP_NAME, endpoint);
                }
                // Get 16 bit nonce and base 64 encode it
                byte[] nonce = randomBytes(16);
                String key = base64(nonce);
                request.headers().set(SEC_ACTIVEMQ_REMOTING_KEY, key);
                ch.attr(REMOTING_KEY).set(key);
                logger.debugf("Sending HTTP request %s", request);
                // Send the HTTP request.
                ch.writeAndFlush(request);
                if (!httpUpgradeHandler.awaitHandshake()) {
                    ch.close().awaitUninterruptibly();
                    return null;
                }
            } catch (URISyntaxException e) {
                ActiveMQClientLogger.LOGGER.errorCreatingNettyConnection(e);
                return null;
            }
        } else {
            ChannelPipeline channelPipeline = ch.pipeline();
            ActiveMQChannelHandler channelHandler = channelPipeline.get(ActiveMQChannelHandler.class);
            channelHandler.active = true;
        }
        // No acceptor on a client connection
        Listener connectionListener = new Listener();
        NettyConnection conn = new NettyConnection(configuration, ch, connectionListener, !httpEnabled && batchDelay > 0, false);
        connectionListener.connectionCreated(null, conn, protocolManager);
        return conn;
    } else {
        Throwable t = future.cause();
        if (t != null && !(t instanceof ConnectException)) {
            ActiveMQClientLogger.LOGGER.errorCreatingNettyConnection(future.cause());
        }
        return null;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) BaseConnectionLifeCycleListener(org.apache.activemq.artemis.spi.core.remoting.BaseConnectionLifeCycleListener) ClientConnectionLifeCycleListener(org.apache.activemq.artemis.spi.core.remoting.ClientConnectionLifeCycleListener) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) KQueueSocketChannel(io.netty.channel.kqueue.KQueueSocketChannel) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Inet6Address(java.net.Inet6Address) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) SslHandler(io.netty.handler.ssl.SslHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) ConnectException(java.net.ConnectException)

Example 82 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest in project cdap by caskdata.

the class AppFabricClient method getProgramSchedules.

public List<ScheduleDetail> getProgramSchedules(String namespace, String app, String workflow) throws NotFoundException {
    MockResponder responder = new MockResponder();
    String uri = String.format("%s/apps/%s/workflows/%s/schedules", getNamespacePath(namespace), app, workflow);
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    try {
        workflowHttpHandler.getWorkflowSchedules(request, responder, namespace, app, workflow, null, null, null);
    } catch (Exception e) {
        // cannot happen
        throw Throwables.propagate(e);
    }
    List<ScheduleDetail> schedules = responder.decodeResponseContent(SCHEDULE_DETAILS_TYPE, GSON);
    verifyResponse(HttpResponseStatus.OK, responder.getStatus(), "Getting workflow schedules failed");
    return schedules;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail) BadRequestException(co.cask.cdap.common.BadRequestException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) NotFoundException(co.cask.cdap.common.NotFoundException)

Example 83 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest in project cdap by caskdata.

the class AppFabricClient method getVersionedInfo.

public ApplicationDetail getVersionedInfo(ApplicationId appId) throws Exception {
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, String.format("%s/apps/%s/versions/%s", getNamespacePath(appId.getNamespace()), appId.getApplication(), appId.getVersion()));
    request.headers().set(Constants.Gateway.API_KEY, "api-key-example");
    MockResponder mockResponder = new MockResponder();
    appLifecycleHttpHandler.getAppVersionInfo(request, mockResponder, appId.getNamespace(), appId.getApplication(), appId.getVersion());
    verifyResponse(HttpResponseStatus.OK, mockResponder.getStatus(), "Getting app version info failed");
    return mockResponder.decodeResponseContent(new TypeToken<ApplicationDetail>() {
    }.getType(), GSON);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) TypeToken(com.google.gson.reflect.TypeToken)

Example 84 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest in project cdap by caskdata.

the class AppFabricClient method deployApplication.

public void deployApplication(ApplicationId appId, AppRequest appRequest) throws Exception {
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, String.format("%s/apps/%s/versions/%s/create", getNamespacePath(appId.getNamespace()), appId.getApplication(), appId.getVersion()));
    request.headers().set(Constants.Gateway.API_KEY, "api-key-example");
    HttpUtil.setTransferEncodingChunked(request, true);
    MockResponder mockResponder = new MockResponder();
    BodyConsumer bodyConsumer = appLifecycleHttpHandler.createAppVersion(request, mockResponder, appId.getNamespace(), appId.getApplication(), appId.getVersion());
    Preconditions.checkNotNull(bodyConsumer, "BodyConsumer from deploy call should not be null");
    bodyConsumer.chunk(Unpooled.copiedBuffer(GSON.toJson(appRequest), StandardCharsets.UTF_8), mockResponder);
    bodyConsumer.finished(mockResponder);
    verifyResponse(HttpResponseStatus.OK, mockResponder.getStatus(), "Failed to deploy app");
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) BodyConsumer(co.cask.http.BodyConsumer)

Example 85 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest in project cdap by caskdata.

the class AppFabricClient method getPlugins.

public List<PluginInstanceDetail> getPlugins(ApplicationId application) throws Exception {
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, String.format("%s/apps/%s", getNamespacePath(application.getNamespace()), application.getApplication()));
    request.headers().set(Constants.Gateway.API_KEY, "api-key-example");
    MockResponder mockResponder = new MockResponder();
    appLifecycleHttpHandler.getPluginsInfo(request, mockResponder, application.getNamespace(), application.getApplication());
    verifyResponse(HttpResponseStatus.OK, mockResponder.getStatus(), "Getting app info failed");
    return mockResponder.decodeResponseContent(new TypeToken<List<PluginInstanceDetail>>() {
    }.getType(), GSON);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) TypeToken(com.google.gson.reflect.TypeToken) PluginInstanceDetail(co.cask.cdap.proto.PluginInstanceDetail)

Aggregations

HttpRequest (io.netty.handler.codec.http.HttpRequest)292 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)105 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)95 Test (org.junit.Test)86 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)67 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)53 HttpResponse (io.netty.handler.codec.http.HttpResponse)50 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)38 ByteBuf (io.netty.buffer.ByteBuf)35 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)35 Test (org.junit.jupiter.api.Test)34 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)31 HttpContent (io.netty.handler.codec.http.HttpContent)30 Channel (io.netty.channel.Channel)29 URI (java.net.URI)27 HttpMethod (io.netty.handler.codec.http.HttpMethod)26 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)25 IOException (java.io.IOException)23 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)19 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)19