Example usage for io.netty.handler.codec.http HttpObject getDecoderResult

List of usage examples for io.netty.handler.codec.http HttpObject getDecoderResult

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpObject getDecoderResult.

Prototype

@Deprecated
DecoderResult getDecoderResult();

Source Link

Usage

From source file:com.bala.learning.learning.netty.HttpServerHandler.java

License:Apache License

private static void appendDecoderResult(StringBuilder buf, HttpObject o) {
    DecoderResult result = o.getDecoderResult();
    if (result.isSuccess()) {
        return;// w ww  .  j  ava  2 s  . c  o m
    }

    buf.append(".. WITH DECODER FAILURE: ");
    buf.append(result.cause());
    buf.append("\r\n");
}

From source file:com.bala.learning.learning.netty.HttpServerHandler.java

License:Apache License

private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = HttpHeaders.isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
            currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }/*from   w  w w  .  java 2  s. c om*/

    // Encode the cookie.
    String cookieString = request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = CookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie : cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}

From source file:com.crm.provisioning.thread.osa.CallbackServerHandler.java

License:Apache License

private static void appendDecoderResult(StringBuilder buf, HttpObject o) {
    DecoderResult result = o.getDecoderResult();

    if (result.isSuccess()) {
        return;//from   w  w w.j a  v  a2  s. c o m
    }
}

From source file:com.crm.provisioning.thread.osa.CallbackServerHandler.java

License:Apache License

private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx, String actionType) {
    StringBuilder content = new StringBuilder();

    content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    content.append("<SOAP-ENV:Envelope");
    content.append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"");
    content.append(" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"");
    content.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    content.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"");
    content.append(" xmlns:osaxsd=\"http://www.csapi.org/osa/schema\"");
    content.append(" xmlns:osa=\"http://www.csapi.org/osa/wsdl\"");
    content.append(" xmlns:csxsd=\"http://www.csapi.org/cs/schema\"");
    content.append(" xmlns:cs=\"http://www.csapi.org/cs/wsdl\">");
    content.append("<SOAP-ENV:Body");
    content.append(" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
    content.append("<cs:");
    content.append(actionType);/*ww  w . ja  v  a2  s.c  o m*/
    content.append("Response></cs:");
    content.append(actionType);
    content.append("Response>");
    content.append("</SOAP-ENV:Body>");
    content.append("</SOAP-ENV:Envelope>");
    content.append("\r\n");

    // Decide whether to close the connection or not.
    boolean keepAlive = isKeepAlive(request);

    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_0,
            currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(content.toString(), CharsetUtil.UTF_8));

    dfGMT.setTimeZone(tz);

    response.headers().set(DATE, dfGMT.format(new Date()));
    response.headers().set(CONTENT_TYPE, "text/htlm");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    response.headers().set(CONNECTION, keepAlive ? HttpHeaders.Values.KEEP_ALIVE : HttpHeaders.Values.CLOSE);

    // Write the response.
    ctx.write(response);

    return keepAlive;
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject obj) throws Exception {
    if (obj != null && obj instanceof HttpRequest) {
        if (obj.getDecoderResult().isSuccess()) {
            handleRequest((HttpRequest) obj);
        } else {//  w w  w . ja v  a2 s.c o m
            throw new RestServiceException("Malformed request received - " + obj,
                    RestServiceErrorCode.MalformedRequest);
        }
    } else if (obj != null && obj instanceof HttpContent) {
        handleContent((HttpContent) obj);
    } else {
        throw new RestServiceException("HttpObject received is null or not of a known type",
                RestServiceErrorCode.UnknownHttpObject);
    }
}

From source file:com.sohail.alam.http.server.HttpClientHandler.java

License:Apache License

/**
 * Message received./*  ww  w . j a v a 2  s  .co m*/
 *
 * @param ctx the ctx
 * @param msg the msg
 *
 * @throws Exception the exception
 */
@Override
protected void messageReceived(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    // If decoder failed
    if (!msg.getDecoderResult().isSuccess()) {
        LOGGER.debug("Could not decode received request");
        send400BadRequest(ctx, null, "Could not decode received request".getBytes(), true);
        return;
    }
    // If the msg is a HttpRequest then process it
    if (msg instanceof HttpRequest) {
        request = (HttpRequest) msg;
        processHttpRequest();
    }
}

From source file:com.topsec.bdc.platform.api.test.http.snoop.HttpSnoopServerHandler.java

License:Apache License

private static void appendDecoderResult(StringBuilder buf, HttpObject o) {

    DecoderResult result = o.getDecoderResult();
    if (result.isSuccess()) {
        return;//from w  w w.  ja v a  2  s  .c  om
    }

    buf.append(".. WITH DECODER FAILURE: ");
    buf.append(result.cause());
    buf.append("\r\n");
}

From source file:com.topsec.bdc.platform.api.test.http.snoop.HttpSnoopServerHandler.java

License:Apache License

private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {

    // Decide whether to close the connection or not.
    boolean keepAlive = HttpHeaders.isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
            currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }//w  ww  . j a v a  2 s  .co m

    // Encode the cookie.
    String cookieString = request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = CookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie : cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}

From source file:com.zhuowenfeng.devtool.hs_server.handler.HttpServiceHandler.java

License:Apache License

private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx, JSONObject result) {

    boolean keepAlive = isKeepAlive(request);
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
            currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(result.toString(), CharsetUtil.UTF_8));
    response.headers().set(CONTENT_TYPE, "application/json; charset=UTF-8");
    if (keepAlive) {
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }//from   ww  w  .  j  a  va 2 s  .  co m
    String cookieString = request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = CookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            for (Cookie cookie : cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
            }
        }
    } else {
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
    }
    ctx.write(response);
    return keepAlive;
}

From source file:metricsdb.core.http.HttpSnoopServerHandler.java

License:Apache License

private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = isKeepAlive(httpRequest);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
            currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }/*from   w  w  w.ja  v a 2 s  . c o  m*/

    // Encode the cookie.
    String cookieString = httpRequest.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = CookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie : cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}