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

    int status = msg.getIntProperty(HttpConnector.HTTP_STATUS_PROPERTY, HttpConstants.SC_OK);
    String version =
        msg.getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, HttpConstants.HTTP11);
    String etag = msg.getStringProperty(HttpConstants.HEADER_ETAG, null);

    String date;
    synchronized (format) {
      date = format.format(new Date());
    }

    String contentType =
        msg.getStringProperty(
            HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.DEFAULT_CONTENT_TYPE);

    response.setStatusLine(HttpVersion.parse(version), status);
    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.getProperty(HttpConstants.HEADER_EXPIRES) == null) {
      response.setHeader(new Header(HttpConstants.HEADER_EXPIRES, date));
    }
    if (etag != null) {
      response.setHeader(new Header(HttpConstants.HEADER_ETAG, etag));
    }
    response.setFallbackCharset(encoding);

    Collection headerNames = HttpConstants.RESPONSE_HEADER_NAMES.values();
    String headerName, value;
    for (Iterator iterator = headerNames.iterator(); iterator.hasNext(); ) {
      headerName = (String) iterator.next();
      value = msg.getStringProperty(headerName, null);
      if (value != null) {
        response.setHeader(new Header(headerName, value));
      }
    }

    // Custom responseHeaderNames
    Map customHeaders = (Map) msg.getProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY);
    if (customHeaders != null) {
      Map.Entry entry;
      for (Iterator iterator = customHeaders.entrySet().iterator(); iterator.hasNext(); ) {
        entry = (Map.Entry) iterator.next();
        if (entry.getValue() != null) {
          response.setHeader(new Header(entry.getKey().toString(), entry.getValue().toString()));
        }
      }
    }

    // Mule properties
    String user = msg.getStringProperty(MuleProperties.MULE_USER_PROPERTY, null);
    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()));
    }

    response.setBody(msg);

    return response;
  }
Beispiel #2
0
  protected HttpResponse createResponse(Object src, String encoding, MuleMessage msg)
      throws IOException, TransformerException {
    HttpResponse response = new HttpResponse();

    Object tmp = msg.getProperty(HttpConnector.HTTP_STATUS_PROPERTY, PropertyScope.OUTBOUND);
    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 =
        (String) msg.getProperty(HttpConnector.HTTP_VERSION_PROPERTY, PropertyScope.OUTBOUND);
    if (version == null) {
      version = HttpConstants.HTTP11;
    }
    String date;
    synchronized (format) {
      date = format.format(new Date());
    }

    String contentType =
        (String) msg.getProperty(HttpConstants.HEADER_CONTENT_TYPE, PropertyScope.OUTBOUND);
    if (contentType == null) {
      contentType =
          (String) msg.getProperty(HttpConstants.HEADER_CONTENT_TYPE, PropertyScope.INVOCATION);
    }

    // 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.getProperty(HttpConstants.HEADER_EXPIRES) == null) {
      response.setHeader(new Header(HttpConstants.HEADER_EXPIRES, date));
    }

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

    Collection headerNames = HttpConstants.RESPONSE_HEADER_NAMES.values();
    String headerName, value;
    for (Iterator iterator = headerNames.iterator(); iterator.hasNext(); ) {
      headerName = (String) iterator.next();
      value = msg.getStringProperty(headerName, null);
      if (value != null) {
        response.setHeader(new Header(headerName, value));
      }
    }

    // TODO: This is the legacy way of setting custom headers and can be removed in 3.0
    // Custom responseHeaderNames
    Map customHeaders = (Map) msg.getProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY);
    if (customHeaders != null) {
      Map.Entry entry;
      for (Iterator iterator = customHeaders.entrySet().iterator(); iterator.hasNext(); ) {
        entry = (Map.Entry) iterator.next();
        if (entry.getValue() != null) {
          response.setHeader(new Header(entry.getKey().toString(), entry.getValue().toString()));
        }
      }
    }

    // attach the outbound prorperties to the message
    Object v;
    for (Iterator iterator = msg.getPropertyNames(PropertyScope.OUTBOUND).iterator();
        iterator.hasNext(); ) {
      headerName = (String) iterator.next();

      v = msg.getProperty(headerName, PropertyScope.OUTBOUND);
      if (v != null) {
        response.setHeader(new Header(headerName, v.toString()));
      }
    }

    // Mule properties
    String user = msg.getStringProperty(MuleProperties.MULE_USER_PROPERTY, null);
    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()));
    }

    response.setBody(msg);

    return response;
  }