Package org.glassfish.grizzly.http

Examples of org.glassfish.grizzly.http.HttpRequestPacket$Builder


    @Override
    public NextAction handleRead(final FilterChainContext ctx) throws IOException {
        final Connection connection = ctx.getConnection();
        final HttpContent httpContent = ctx.getMessage();

        final HttpRequestPacket request = (HttpRequestPacket) httpContent.getHttpHeader();
        final URI requestURI;
        try {
            final String uri = request.getQueryString() == null ?
                    request.getRequestURI() :
                    request.getRequestURI() + "?" + request.getQueryString();
            requestURI = new URI(uri);
        } catch (URISyntaxException ignored) {
            return ctx.getStopAction();
        }

        final boolean redirectToSecure;
        if (secure != null) { // if secure is set - we use it
            redirectToSecure = secure;
        } else // if secure is not set - use secure settings opposite to the current request
            final SSLEngine sslEngine = SSLUtils.getSSLEngine(connection);
            redirectToSecure = sslEngine == null;
        }


        final StringBuilder hostPort = new StringBuilder();

        String hostHeader = request.getHeader("host");
        if (hostHeader == null) {
            String hostRequestURI = requestURI.getHost();

            if (hostRequestURI == null) {
                hostPort.append(request.getLocalHost());
            } else {
                hostPort.append(hostRequestURI);
            }

            hostPort.append(':');

            if (redirectPort == null) {
                int port = requestURI.getPort();
                if (port == -1) {
                    hostPort.append(request.getLocalPort());
                } else {
                    hostPort.append(port);
                }
            } else {
                hostPort.append(redirectPort);
            }

        } else if (redirectPort != null) { // if port is specified - cut it from host header
            final int colonIdx = hostHeader.indexOf(':');
            if (colonIdx != -1) {
                hostHeader = hostHeader.substring(0, colonIdx);
            }
            hostPort.append(hostHeader)
                    .append(':')
                    .append(redirectPort);
        } else {
            hostPort.append(hostHeader);
        }

        if (hostPort.length() > 0) {
            String path = requestURI.toString();
           
            assert path != null;

            final StringBuilder sb = new StringBuilder();
            sb.append((redirectToSecure ? "https://" : "http://"))
                    .append(hostPort)
                    .append(path);

            request.setSkipRemainder(true);
            final HttpResponsePacket response = HttpResponsePacket.builder(request)
                    .status(302)
                    .header("Location", sb.toString())
                    .contentLength(0)
                    .build();
View Full Code Here


                final FilterChainContext ctx) {
           
            final boolean result = super.onHttpHeaderParsed(httpHeader,
                    buffer, ctx);
           
            final HttpRequestPacket request = (HttpRequestPacket) httpHeader;
            final HttpResponsePacket response = request.getResponse();
           
            // Set response "Server" header
            if (serverVersion != null && !serverVersion.isEmpty()) {
                response.addHeader(Header.Server, serverVersion);
            }
View Full Code Here

        ctx.write(HttpContent.builder(responsePacket).build());
    }

    private static UpgradeRequest createWebSocketRequest(final HttpContent requestContent) {

        final HttpRequestPacket requestPacket = (HttpRequestPacket) requestContent.getHttpHeader();

        final RequestContext requestContext = RequestContext.Builder.create()
                .requestURI(URI.create(requestPacket.getRequestURI()))
                .queryString(requestPacket.getQueryString())
                .secure(requestPacket.isSecure())
                .remoteAddr(requestPacket.getRemoteAddress())
                .build();

        for (String name : requestPacket.getHeaders().names()) {
            final List<String> values = requestContext.getHeaders().get(name);
            if (values == null) {
                requestContext.getHeaders().put(name, Utils.parseHeaderValue(requestPacket.getHeader(name).trim()));
            } else {
                values.addAll(Utils.parseHeaderValue(requestPacket.getHeader(name).trim()));
            }
        }

        return requestContext;
    }
View Full Code Here

        @Override
        public void onErrorEvent(Connection connection, HttpPacket packet, Throwable error) {
            if (accessLoggingEnabled) {
                if (packet instanceof HttpRequestPacket) {

                    final HttpRequestPacket requestPacket = (HttpRequestPacket) packet;
                    final HttpResponsePacket responsePacket = requestPacket.getResponse();

                    // 400 should be hardcoded since the response status isn't available for bad requests
                    responsePacket.setStatus(HttpStatus.BAD_REQUEST_400);

                    org.glassfish.grizzly.http.server.Request request = org.glassfish.grizzly.http.server.Request.create();
View Full Code Here

    @Override
    public NextAction handleRead(final FilterChainContext ctx) throws IOException {

        final HttpContent requestContent = ctx.getMessage();
        final HttpRequestPacket request = (HttpRequestPacket) requestContent.getHttpHeader();

        if (fileCache.isEnabled() && Method.GET.equals(request.getMethod())) {
            final FileCacheEntry cacheEntry = fileCache.get(request);
            if (cacheEntry != null) {
                final HttpResponsePacket response = request.getResponse();
                prepareResponse(cacheEntry, response);
               
                if (response.getStatus() != 200) {
                    // The cache hit - return empty response
                    ctx.write(HttpContent.builder(response)
                            .content(Buffers.EMPTY_BUFFER)
                            .last(true)
                            .build());
                   
                    return flush(ctx);
                }

                // check if we can send plain or compressed data back.
                // depends on client request headers and file cache entry
                final boolean isServeCompressed =
                        cacheEntry.canServeCompressed(request);
               
                // The client doesn't have this resource cached, so
                // we have to send entire payload
                prepareResponseWithPayload(cacheEntry, response,
                        isServeCompressed);

                if (cacheEntry.type != CacheType.FILE) {
                    // the payload is available in a ByteBuffer
                    final Buffer buffer = Buffers.wrap(ctx.getMemoryManager(),
                            cacheEntry.getByteBuffer(isServeCompressed));

                    ctx.write(HttpContent.builder(response)
                            .content(buffer)
                            .last(true)
                            .build());

                    return flush(ctx);
                }
               
                if (fileCache.isFileSendEnabled() && !request.isSecure()) {
                    return sendFileZeroCopy(ctx, response, cacheEntry,
                            isServeCompressed);
                } else {
                    return sendFileUsingBuffers(ctx, response, cacheEntry,
                            isServeCompressed);
View Full Code Here

                return true;
            }
        }

        try {
            final HttpRequestPacket httpRequestPacket = request.getRequest();
            final RequestURIRef requestURIRef = httpRequestPacket.getRequestURIRef();
            requestURIRef.setDefaultURIEncoding(requestURIEncoding);

            if (decodeURL) {
                // URI decoding
                try {
View Full Code Here

                    .getProcessingState().getHttpContext();
            Request handlerRequest = httpRequestInProgress.get(context);

            if (handlerRequest == null) {
                // It's a new HTTP request
                final HttpRequestPacket request = (HttpRequestPacket) httpContent.getHttpHeader();
                final HttpResponsePacket response = request.getResponse();
               
                handlerRequest = Request.create();
                handlerRequest.parameters.setLimit(config.getMaxRequestParameters());
                httpRequestInProgress.set(context, handlerRequest);
                final Response handlerResponse = handlerRequest.getResponse();

                handlerRequest.initialize(request, ctx, this);
                handlerResponse.initialize(handlerRequest, response,
                        ctx, suspendedResponseQueue, this);

                if (config.isGracefulShutdownSupported()) {
                    activeRequestsCounter.incrementAndGet();
                    handlerRequest.addAfterServiceListener(flushResponseHandler);
                }
               
                HttpServerProbeNotifier.notifyRequestReceive(this, connection,
                        handlerRequest);

                boolean wasSuspended = false;
               
                try {
                    ctx.setMessage(handlerResponse);

                    if (isShuttingDown) { // if we're in the shutting down phase - serve shutdown page and exit
                        handlerResponse.getResponse().getProcessingState().setError(true);
                        HtmlHelper.setErrorAndSendErrorPage(
                                handlerRequest, handlerResponse,
                                config.getDefaultErrorPageGenerator(),
                                503, HttpStatus.SERVICE_UNAVAILABLE_503.getReasonPhrase(),
                                "The server is being shutting down...", null);
                    } else if (!config.isPassTraceRequest()
                            && request.getMethod() == Method.TRACE) {
                        onTraceRequest(handlerRequest, handlerResponse);
                    } else if (!checkMaxPostSize(request.getContentLength())) {
                        handlerResponse.getResponse().getProcessingState().setError(true);
                        HtmlHelper.setErrorAndSendErrorPage(
                                handlerRequest, handlerResponse,
                                config.getDefaultErrorPageGenerator(),
                                400, HttpStatus.BAD_REQUEST_400.getReasonPhrase(),
                                "The request payload size exceeds the max post size limitation", null);
                    } else {
                        final HttpHandler httpHandlerLocal = httpHandler;
                        if (httpHandlerLocal != null) {
                            wasSuspended = !httpHandlerLocal.doHandle(
                                    handlerRequest, handlerResponse);
                        }
                    }
                } catch (Exception t) {
                    LOGGER.log(Level.WARNING,
                            LogMessages.WARNING_GRIZZLY_HTTP_SERVER_FILTER_HTTPHANDLER_INVOCATION_ERROR(), t);
                   
                    request.getProcessingState().setError(true);
                   
                    if (!response.isCommitted()) {
                            HtmlHelper.setErrorAndSendErrorPage(
                                    handlerRequest, handlerResponse,
                                    config.getDefaultErrorPageGenerator(),
View Full Code Here

        response.finish();
        request.onAfterService();
       
        HttpServerProbeNotifier.notifyRequestComplete(this, connection, response);
       
        final HttpRequestPacket httpRequest = request.getRequest();
        final boolean isBroken = httpRequest.isContentBroken();
       
        // Suspend state is cancelled - it means normal processing might have
        // been broken. We don't want to reuse Request and Response in this state,
        // cause there still might be threads referencing them.
        if (response.suspendState != Response.SuspendState.CANCELLED) {
View Full Code Here

        ctx.write(HttpContent.builder(responsePacket).build());
    }

    private static UpgradeRequest createWebSocketRequest(final HttpContent requestContent) {

        final HttpRequestPacket requestPacket = (HttpRequestPacket) requestContent.getHttpHeader();

        final RequestContext requestContext = RequestContext.Builder.create()
                .requestURI(URI.create(requestPacket.getRequestURI()))
                .queryString(requestPacket.getQueryString())
                .secure(requestPacket.isSecure())
                .build();

        for (String name : requestPacket.getHeaders().names()) {
            final List<String> values = requestContext.getHeaders().get(name);
            if (values == null) {
                requestContext.getHeaders().put(name, Utils.parseHeaderValue(requestPacket.getHeader(name).trim()));
            } else {
                values.addAll(Utils.parseHeaderValue(requestPacket.getHeader(name).trim()));
            }
        }

        return requestContext;
    }
View Full Code Here

                final FilterChainContext ctx) {
           
            final boolean result = super.onHttpHeaderParsed(httpHeader,
                    buffer, ctx);
           
            final HttpRequestPacket request = (HttpRequestPacket) httpHeader;
            final HttpResponsePacket response = request.getResponse();
           
            // Set response "Server" header
            if (serverVersion != null && !serverVersion.isEmpty()) {
                response.addHeader(Header.Server, serverVersion);
            }
View Full Code Here

TOP

Related Classes of org.glassfish.grizzly.http.HttpRequestPacket$Builder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.