protected void doWriteDirectResponse()

in components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpBinding.java [199:296]


    protected void doWriteDirectResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
        String contentType = (String)message.getHeader("Content-Type", String.class);
        if ("application/x-java-serialized-object".equals(contentType)) {
            if (!isAllowJavaSerializedObject() && !this.isTransferException()) {
                throw new RuntimeCamelException("Content-type application/x-java-serialized-object is not allowed");
            } else {
                try {
                    Object object = message.getMandatoryBody(Serializable.class);
                    org.apache.camel.http.common.HttpHelper.writeObjectToServletResponse(response, object);
                } catch (InvalidPayloadException var19) {
                    InvalidPayloadException e = var19;
                    throw new IOException(e);
                }
            }
        } else {
            InputStream is = null;
            if (this.checkChunked(message, exchange)) {
                is = message.getBody(InputStream.class);
            } else if (!this.isText(contentType)) {
                is = exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, message.getBody());
            }

            int len;
            if (is != null) {
                ServletOutputStream os = response.getOutputStream();
                if (!this.checkChunked(message, exchange)) {
                    CachedOutputStream stream = new CachedOutputStream(exchange);

                    try {
                        len = this.copyStream(is, stream, response.getBufferSize());
                        response.setContentLength(len);
                        OutputStream current = stream.getCurrentStream();
                        if (current instanceof ByteArrayOutputStream) {
                            ByteArrayOutputStream bos = (ByteArrayOutputStream)current;
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Streaming (direct) response in non-chunked mode with content-length {}", len);
                            }

                            bos.writeTo(os);
                        } else {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Streaming response in non-chunked mode with content-length {} and buffer size: {}", len, len);
                            }

                            this.copyStream(stream.getInputStream(), os, len);
                        }
                    } finally {
                        IOHelper.close(new Closeable[]{is, os});
                        stream.close();
                    }
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Streaming response in chunked mode with buffer size {}", response.getBufferSize());
                    }

                    this.copyStream(is, os, response.getBufferSize());
                }
            } else {
                Object body = message.getBody();
                if (body instanceof String) {
                    String data = message.getBody(String.class);

                    if (data != null) {
                        String charset = ExchangeHelper.getCharsetName(exchange, true);
                        len = data.getBytes(charset).length;
                        response.setCharacterEncoding(charset);
                        response.setContentLength(len);
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Writing response in non-chunked mode as plain text with content-length {} and buffer size: {}", len, response.getBufferSize());
                        }

                        try {
                            response.getWriter().print(data);
                        } finally {
                            response.getWriter().flush();
                        }
                    }
                } else if (body instanceof InputStream) {
                    InputStream bodyIS = message.getBody(InputStream.class);
                    bodyIS.transferTo(response.getOutputStream());
                } else {
                    final TypeConverter tc = exchange.getContext().getTypeConverter();
                    // Try to convert to ByteBuffer for performance reason
                    final ByteBuffer bb = tc.tryConvertTo(ByteBuffer.class, exchange, body);
                    if (bb != null) {
                        response.getOutputStream().write(bb.array());
                    } else {
                        try {
                            final InputStream bodyIS = tc.mandatoryConvertTo(InputStream.class, exchange, body);
                            bodyIS.transferTo(response.getOutputStream());
                        } catch (NoTypeConversionAvailableException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }