Example usage for io.netty.handler.codec.http HttpResponseStatus codeAsText

List of usage examples for io.netty.handler.codec.http HttpResponseStatus codeAsText

Introduction

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

Prototype

AsciiString codeAsText

To view the source code for io.netty.handler.codec.http HttpResponseStatus codeAsText.

Click Source Link

Usage

From source file:com.linecorp.armeria.server.http.Http2RequestDecoder.java

License:Apache License

private void writeErrorResponse(ChannelHandlerContext ctx, int streamId, HttpResponseStatus status) {
    final byte[] content = status.toString().getBytes(StandardCharsets.UTF_8);

    writer.writeHeaders(ctx, streamId,/*from w w  w  .ja  v  a2  s.  co m*/
            new DefaultHttp2Headers(false).status(status.codeAsText())
                    .set(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString())
                    .setInt(HttpHeaderNames.CONTENT_LENGTH, content.length),
            0, false, ctx.voidPromise());

    writer.writeData(ctx, streamId, Unpooled.wrappedBuffer(content), 0, true, ctx.voidPromise());
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.http2.Http2ResponseWriter.java

License:Open Source License

/**
 * writing a response on wire/*from w w  w  .j av a  2s  .c  o m*/
 *
 * @param synCtx
 * @throws AxisFault
 */
public void writeNormalResponse(MessageContext synCtx) throws AxisFault {
    org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) synCtx)
            .getAxis2MessageContext();
    Http2Headers transportHeaders = new DefaultHttp2Headers();
    InboundResponseSender responseSender = synCtx
            .getProperty(InboundEndpointConstants.INBOUND_ENDPOINT_RESPONSE_WORKER) == null ? null
                    : (InboundHttp2ResponseSender) synCtx
                            .getProperty(InboundEndpointConstants.INBOUND_ENDPOINT_RESPONSE_WORKER);
    try {
        sourceConfiguration = PassThroughInboundEndpointHandler.getPassThroughSourceConfiguration();
    } catch (Exception e) {
        throw new AxisFault("Error while building sourceConfiguration " + e);
    }

    //status
    try {
        int statusCode = PassThroughTransportUtils.determineHttpStatusCode(msgContext);
        if (statusCode > 0) {
            HttpResponseStatus status1 = HttpResponseStatus.valueOf(statusCode);
            transportHeaders.status(status1.codeAsText());
        }
    } catch (Exception e) {
        throw new AxisFault("Error occured while parsing response status", e);
    }
    //content_type
    Boolean noEntityBody = msgContext.getProperty(NhttpConstants.NO_ENTITY_BODY) != null
            ? (boolean) msgContext.getProperty(NhttpConstants.NO_ENTITY_BODY)
            : null;
    if (noEntityBody == null || Boolean.FALSE == noEntityBody) {
        Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
        if (pipe == null) {
            pipe = new Pipe(sourceConfiguration.getBufferFactory().getBuffer(), "Test", sourceConfiguration);
            msgContext.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe);
            msgContext.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
        }

        OMOutputFormat format = NhttpUtil.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = MessageFormatterDecoratorFactory
                .createMessageFormatterDecorator(msgContext);
        if (msgContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE) == null) {
            transportHeaders.add(HttpHeaderNames.CONTENT_TYPE,
                    messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction()));
        }
    }

    if (transportHeaders != null
            && msgContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE) != null) {
        if (msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE) != null
                && msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE).toString()
                        .contains(PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED)) {
            transportHeaders.add(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE,
                    PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED);
        } else {
            Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
            if (pipe != null
                    && !Boolean.TRUE
                            .equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))
                    && msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE) != null) {
                transportHeaders.add(HttpHeaderNames.CONTENT_TYPE, msgContext
                        .getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE).toString());
            }
        }
    }

    //Excess headers
    String excessProp = NhttpConstants.EXCESS_TRANSPORT_HEADERS;
    Map excessHeaders = msgContext.getProperty(excessProp) == null ? null
            : (Map) msgContext.getProperty(excessProp);
    if (excessHeaders != null) {
        for (Iterator iterator = excessHeaders.keySet().iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();
            for (String excessVal : (Collection<String>) excessHeaders.get(key)) {
                transportHeaders.add(key.toLowerCase(), (String) excessVal);
            }
        }
    }

    boolean hasBody = false;
    if (!transportHeaders.contains(HttpHeaderNames.CONTENT_TYPE)) {
        String contentType = null;
        try {
            contentType = new InboundMessageHandler(responseSender, config).getContentType(msgContext);
        } catch (Exception e) {
            throw new AxisFault("Error while parsing content type", e);
        }
        if (contentType != null) {
            transportHeaders.add(HttpHeaderNames.CONTENT_TYPE, contentType);
            hasBody = true;
        }
    } else
        hasBody = true;

    int streamId = (int) synCtx.getProperty("stream-id");
    ChannelHandlerContext c = (ChannelHandlerContext) synCtx.getProperty("stream-channel");
    if (c == null) {
        c = chContext;
    }
    ChannelPromise promise = c.newPromise();
    if (hasBody) {
        Pipe pipe = msgContext.getProperty("pass-through.pipe") == null ? null
                : (Pipe) msgContext.getProperty("pass-through.pipe");
        encoder.writeHeaders(c, streamId, transportHeaders, 0, false, promise);
        http2Encoder pipeEncoder = new http2Encoder(c, streamId, encoder, c.newPromise());
        if (pipe != null) {
            pipe.attachConsumer(new Http2CosumerIoControl());
            try {
                if (Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    MessageFormatter formatter = MessageProcessorSelector.getMessageFormatter(msgContext);
                    OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext);
                    formatter.writeTo(msgContext, format, out, false);
                    OutputStream _out = pipe.getOutputStream();
                    IOUtils.write(out.toByteArray(), _out);
                }
                int t = pipe.consume(pipeEncoder);
                if (t < 1)
                    throw new AxisFault("Pipe consuming failed");
            } catch (Exception e) {
                throw new AxisFault("Error while writing built message back to pipe", e);
            }
        }
        c.flush();
    } else {
        encoder.writeHeaders(c, streamId, transportHeaders, 0, true, promise);
        c.flush();
    }
}