Package org.mule.transport.http

Examples of org.mule.transport.http.HttpResponse


    @Override
    protected void doSetUp() throws Exception
    {
        super.doSetUp();

        _resp = new HttpResponse();
        _resp.setStatusLine(new HttpVersion(1, 1), 200);
        _resp.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.DEFAULT_CONTENT_TYPE));
        _resp.setBody(new DefaultMuleMessage(_body, muleContext));
    }
View Full Code Here


    @Override
    protected Object doTransform(Object src, String encoding) throws TransformerException
    {
        try
        {
            HttpResponse response = (HttpResponse)src;
            ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
            OutputStream outstream = bos;
            ResponseWriter writer = new ResponseWriter(outstream, encoding);
            writer.println(response.getStatusLine());
            Iterator item = response.getHeaderIterator();
            while (item.hasNext())
            {
                Header header = (Header)item.next();
                writer.print(header.toExternalForm());
            }
            writer.println();
            writer.flush();

            if (response.hasBody())
            {
                OutputHandler handler = response.getBody();
                Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
                if (transferenc != null)
                {
                    response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH);
                    if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
                    {
                        outstream = new ChunkedOutputStream(outstream);
                    }
                }
View Full Code Here

                servletResponse.getWriter().write("Action was processed successfully. There was no result");
            }
        }
        else
        {
            HttpResponse httpResponse;

            if (message.getPayload() instanceof HttpResponse)
            {
                httpResponse = (HttpResponse) message.getPayload();

            }
            else
            {
                httpResponse = (HttpResponse) responseTransformer.transform(message);
            }

            // Map the HttpResponse to the ServletResponse
            Header contentTypeHeader = httpResponse.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
            String contentType = defaultContentType;
            if (contentTypeHeader != null && contentTypeHeader.getValue() != null)
            {
                if (logger.isDebugEnabled())
                {
                    logger.debug("Using Content-Type from message header = " + contentTypeHeader.getValue());
                }
                contentType = contentTypeHeader.getValue();
            }

            servletResponse.setContentType(contentType);

            servletResponse = setHttpHeadersOnServletResponse(httpResponse, servletResponse);

            if (!servletResponse.isCommitted())
            {
                servletResponse.setStatus(httpResponse.getStatusCode());
            }

            if (httpResponse.hasBody())
            {
                OutputHandler outputHandler = httpResponse.getBody();
                outputHandler.write(RequestContext.getEvent(), servletResponse.getOutputStream());
            }
        }
        servletResponse.flushBuffer();
    }
View Full Code Here

            src = StringUtils.EMPTY;
        }

        try
        {
            HttpResponse response;
            if (src instanceof HttpResponse)
            {
                response = (HttpResponse) src;
            }
            else
            {
                response = createResponse(src, outputEncoding, msg);
            }

            // Ensure there's a content type header
            if (!response.containsHeader(HttpConstants.HEADER_CONTENT_TYPE))
            {
                response.addHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE,
                        HttpConstants.DEFAULT_CONTENT_TYPE));
            }

            // Ensure there's a content length or transfer encoding header
            if (!response.containsHeader(HttpConstants.HEADER_CONTENT_LENGTH)
                    && !response.containsHeader(HttpConstants.HEADER_TRANSFER_ENCODING))
            {
                if (response.hasBody())
                {
                    long len = response.getContentLength();
                    if (len < 0)
                    {
                        if (response.getHttpVersion().lessEquals(HttpVersion.HTTP_1_0))
                        {
                            // Ensure that we convert the payload to an in memory representation
                            // so we don't end up with a chunked response
                            len = msg.getPayloadAsBytes().length;

                            response.setBody(msg);

                            Header header = new Header(HttpConstants.HEADER_CONTENT_LENGTH, Long.toString(len));
                            response.setHeader(header);
                        }
                        else
                        {
                            Header header = new Header(HttpConstants.HEADER_TRANSFER_ENCODING, "chunked");
                            response.addHeader(header);
                        }
                    }
                    else
                    {
                        Header header = new Header(HttpConstants.HEADER_CONTENT_LENGTH, Long.toString(len));
                        response.setHeader(header);
                    }
                }
                else
                {
                    Header header = new Header(HttpConstants.HEADER_CONTENT_LENGTH, "0");
                    response.addHeader(header);
                }
            }

            // See if the the client explicitly handles connection persistence
            String connHeader = msg.getOutboundProperty(HttpConstants.HEADER_CONNECTION);
            if (connHeader != null)
            {
                if (connHeader.equalsIgnoreCase("keep-alive"))
                {
                    response.setKeepAlive(true);
                }
                if (connHeader.equalsIgnoreCase("close"))
                {
                    response.setKeepAlive(false);
                }
            }

            final String method = msg.getOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY);
            if ("HEAD".equalsIgnoreCase(method))
            {
                // this is a head request, we don't want to send the actual content
                response.setBody((MuleMessage) null);
            }
            return response;
        }
        catch (Exception e)
        {
View Full Code Here

    }

    protected HttpResponse createResponse(Object src, String encoding, MuleMessage msg)
            throws IOException, TransformerException
    {
        HttpResponse response = new HttpResponse();

        Object tmp = msg.getOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY);
        int status = HttpConstants.SC_OK;

        if (tmp != null)
        {
            status = Integer.valueOf(tmp.toString());
        }
        else if (msg.getExceptionPayload() != null)
        {
            status = HttpConstants.SC_INTERNAL_SERVER_ERROR;
        }

        String version = msg.getInboundProperty(HttpConnector.HTTP_VERSION_PROPERTY);
        if (version == null)
        {
            version = HttpConstants.HTTP11;
        }
        String date = new SimpleDateFormat(HttpConstants.DATE_FORMAT, Locale.US).format(new Date());

        String contentType = msg.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE);
        if (contentType == null)
        {
            contentType = msg.getInvocationProperty(HttpConstants.HEADER_CONTENT_TYPE);
        }

        // MULE-4047 Don't explicitly set the content-type to a default value here,
        // otherwise any settings on the servlet/transport will be happily ignored.
        //if (contentType == null)
        //{
        //    contentType = HttpConstants.DEFAULT_CONTENT_TYPE;
        //
        //    if (encoding != null)
        //    {
        //        contentType += "; charset=" + encoding;
        //    }
        //    logger.warn("Content-Type was not set, defaulting to: " + contentType);
        //}

        response.setStatusLine(HttpVersion.parse(version), status);
        if (contentType != null)
        {
            response.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, contentType));
        }
        response.setHeader(new Header(HttpConstants.HEADER_DATE, date));
        response.setHeader(new Header(HttpConstants.HEADER_SERVER, server));
        if (msg.getOutboundProperty(HttpConstants.HEADER_EXPIRES) == null)
        {
            response.setHeader(new Header(HttpConstants.HEADER_EXPIRES, date));
        }

        String etag = msg.getOutboundProperty(HttpConstants.HEADER_ETAG);
        if (etag != null)
        {
            response.setHeader(new Header(HttpConstants.HEADER_ETAG, etag));
        }
        response.setFallbackCharset(encoding);

        Collection<String> headerNames = HttpConstants.RESPONSE_HEADER_NAMES.values();

        for (String headerName : headerNames)
        {
            if (HttpConstants.HEADER_COOKIE_SET.equals(headerName))
            {
                // TODO This have to be improved. We shouldn't have to look in all
                // scopes
                Object cookiesObject = msg.getInvocationProperty(headerName);
                if (cookiesObject == null)
                {
                    cookiesObject = msg.getOutboundProperty(headerName);
                }
                if (cookiesObject == null)
                {
                    cookiesObject = msg.getInboundProperty(headerName);
                }
                if (cookiesObject == null)
                {
                    continue;
                }
               
                if (!(cookiesObject instanceof Cookie[]))
                {
                    response.addHeader(new Header(headerName, cookiesObject.toString()));
                }
                else
                {
                    Cookie[] arrayOfCookies = CookieHelper.asArrayOfCookies(cookiesObject);
                    for (Cookie cookie : arrayOfCookies)
                    {
                        response.addHeader(new Header(headerName,
                            CookieHelper.formatCookieForASetCookieHeader(cookie)));
                    }
                }
            }
            else
            {
                String value = msg.getInvocationProperty(headerName);
                if (value == null)
                {
                    value = msg.getOutboundProperty(headerName);
                }
                if (value != null)
                {
                    response.setHeader(new Header(headerName, value));
                }
            }
        }

        Map customHeaders = msg.getOutboundProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY);
        if (customHeaders != null)
        {
            throw new TransformerException(HttpMessages.customHeaderMapDeprecated(), this);
        }

        //attach the outbound properties to the message
        for (String headerName : msg.getOutboundPropertyNames())
        {
            Object v = msg.getOutboundProperty(headerName);
            if (v != null)
            {
                response.setHeader(new Header(headerName, v.toString()));
            }
        }

        // Mule properties
        String user = msg.getOutboundProperty(MuleProperties.MULE_USER_PROPERTY);
        if (user != null)
        {
            response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_USER_PROPERTY, user));
        }
        if (msg.getCorrelationId() != null)
        {
            response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_CORRELATION_ID_PROPERTY,
                    msg.getCorrelationId()));
            response.setHeader(new Header(CUSTOM_HEADER_PREFIX
                    + MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY,
                    String.valueOf(msg.getCorrelationGroupSize())));
            response.setHeader(new Header(CUSTOM_HEADER_PREFIX
                    + MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY,
                    String.valueOf(msg.getCorrelationSequence())));
        }
        if (msg.getReplyTo() != null)
        {
            response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_REPLY_TO_PROPERTY,
                    msg.getReplyTo().toString()));
        }

        try
        {
            response.setBody(msg);
        }
        catch (Exception e)
        {
            throw new TransformerException(this, e);
        }
View Full Code Here

    @Override
    protected void doSetUp() throws Exception
    {
        super.doSetUp();

        _resp = new HttpResponse();
        _resp.setStatusLine(new HttpVersion(1, 1), 200);
        _resp.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.DEFAULT_CONTENT_TYPE));
        _resp.setBody(new DefaultMuleMessage(_body, muleContext));
    }
View Full Code Here

        when(msg.getOutboundProperty("Set-Cookie")).thenReturn(cookiesOutbound);
        Set props = new HashSet();
        props.add("Set-Cookie");
        when(msg.getOutboundPropertyNames()).thenReturn(props);

        HttpResponse response = transformer.createResponse(null, "UTF-8", msg);
        Header[] headers = response.getHeaders();
        int cookiesSet = 0;
        for(Header header : headers)
        {
            if ("Set-Cookie".equals(header.getName()))
            {
View Full Code Here

    public void testSetDateOnOutbound() throws Exception
    {
        MuleMessageToHttpResponse transformer = getMuleMessageToHttpResponse();
        MuleMessage msg = mock(MuleMessage.class);

        HttpResponse response = transformer.createResponse(null, "UTF-8", msg);
        Header[] headers = response.getHeaders();

        boolean hasDateHeader = false;
        for (Header header : headers)
        {
            if (HttpConstants.HEADER_DATE.equals(header.getName()))
View Full Code Here

        msg.setInvocationProperty(HttpConstants.HEADER_CONTENT_TYPE, wrongContentType);
        msg.setProperty(HttpConstants.HEADER_CONTENT_TYPE, wrongContentType, PropertyScope.INBOUND);
       
        msg.setOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE, contentType);

        HttpResponse response = transformer.createResponse(null, "UTF-8", msg);
        Header[] headers = response.getHeaders();

        boolean hasContentTypeHeader = false;
        for (Header header : headers)
        {
            if (HttpConstants.HEADER_CONTENT_TYPE.equals(header.getName()))
View Full Code Here

    public void testHttpResponseEmptyCacheControl() throws Exception
    {
        HttpResponseBuilder httpResponseBuilder = createHttpResponseBuilder();
        httpResponseBuilder.setCacheControl(new CacheControlHeader());

        HttpResponse response = new HttpResponse();
        httpResponseBuilder.setCacheControl(response, mockMuleMessage);
        assertNull(response.getFirstHeader(HttpConstants.HEADER_CACHE_CONTROL));
    }
View Full Code Here

TOP

Related Classes of org.mule.transport.http.HttpResponse

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.