コード例 #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;
  }
コード例 #2
0
  public Object transform(MuleMessage msg, String outputEncoding) throws TransformerException {
    Object src = msg.getPayload();
    // Send back the exception payload if one has been set
    if (msg.getExceptionPayload() != null) {
      // src = context.getMessage().getExceptionPayload();
    }

    // Note this transformer excepts Null as we must always return a result
    // from the Http
    // connector if a response transformer is present
    if (src instanceof NullPayload) {
      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)) {
              throw new IOException(
                  "Chunked encoding not supported for HTTP version " + response.getHttpVersion());
            }
            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);
        }
      }

      if (!response.containsHeader(HttpConstants.HEADER_CONNECTION)) {
        // See if the the client explicitly handles connection persistence
        String connHeader = msg.getStringProperty(HttpConstants.HEADER_CONNECTION, null);
        if (connHeader != null) {
          if (connHeader.equalsIgnoreCase("keep-alive")) {
            Header header = new Header(HttpConstants.HEADER_CONNECTION, "keep-alive");
            response.addHeader(header);
            response.setKeepAlive(true);
          }
          if (connHeader.equalsIgnoreCase("close")) {
            Header header = new Header(HttpConstants.HEADER_CONNECTION, "close");
            response.addHeader(header);
            response.setKeepAlive(false);
          }
        } else {
          // Use protocol default connection policy
          if (response.getHttpVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
            response.setKeepAlive(true);
          } else {
            response.setKeepAlive(false);
          }
        }
      }
      if ("HEAD"
          .equalsIgnoreCase(msg.getStringProperty(HttpConnector.HTTP_METHOD_PROPERTY, null))) {
        // this is a head request, we don't want to send the actual content
        response.setBody((MuleMessage) null);
      }
      return response;
    } catch (IOException e) {
      throw new TransformerException(this, e);
    }
  }
コード例 #3
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;
  }