Пример #1
0
 @SuppressWarnings("unchecked")
 private <T> T unserializeData(InputStream inputStream) {
   T object = null;
   try {
     if (dataFormat.equals(DataFormat.CBOR)) {
       object = (T) cborMapper.readValue(inputStream, TargetModel.class);
     } else if (dataFormat.equals(DataFormat.JSON)) {
       object = (T) jsonMapper.readValue(inputStream, TargetModelJson.class);
     } else if (dataFormat.equals(DataFormat.HTML)) {
       byte[] fileData = IOUtils.toByteArray(inputStream);
       object = (T) new String(fileData);
     }
   } catch (IOException e) {
     throw new RuntimeException("Failed to unserialize object.", e);
   }
   return object;
 }
Пример #2
0
  @SuppressWarnings("unchecked")
  protected void propagateHeadersFromCamelToCxf(
      Exchange camelExchange,
      Map<String, Object> camelHeaders,
      org.apache.cxf.message.Exchange cxfExchange,
      Map<String, Object> cxfContext) {

    // get cxf transport headers (if any) from camel exchange
    // use a treemap to keep ordering and ignore key case
    Map<String, List<String>> transportHeaders =
        new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
    if (camelExchange != null) {
      Map<String, List<String>> h =
          CastUtils.cast((Map<?, ?>) camelExchange.getProperty(Message.PROTOCOL_HEADERS));
      if (h != null) {
        transportHeaders.putAll(h);
      }
    }
    Map<String, List<String>> headers =
        CastUtils.cast((Map<?, ?>) camelHeaders.get(Message.PROTOCOL_HEADERS));
    if (headers != null) {
      transportHeaders.putAll(headers);
    }

    DataFormat dataFormat =
        camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class);

    for (Map.Entry<String, Object> entry : camelHeaders.entrySet()) {
      // put response code in request context so it will be copied to CXF message's property
      if (Message.RESPONSE_CODE.equals(entry.getKey())
          || Exchange.HTTP_RESPONSE_CODE.equals(entry.getKey())) {
        LOG.debug("Propagate to CXF header: {} value: {}", Message.RESPONSE_CODE, entry.getValue());
        cxfContext.put(Message.RESPONSE_CODE, entry.getValue());
        continue;
      }

      // We need to copy the content-type if the dataformat is RAW
      if (Message.CONTENT_TYPE.equalsIgnoreCase(entry.getKey())
          && dataFormat.equals(DataFormat.RAW)) {
        LOG.debug("Propagate to CXF header: {} value: {}", Message.CONTENT_TYPE, entry.getValue());
        cxfContext.put(Message.CONTENT_TYPE, entry.getValue().toString());
        continue;
      }

      // need to filter the User-Agent ignore the case, as CXF just check the header with
      // "User-Agent"
      if (entry.getKey().equalsIgnoreCase("User-Agent")) {
        List<String> listValue = new ArrayList<String>();
        listValue.add(entry.getValue().toString());
        transportHeaders.put("User-Agent", listValue);
      }

      // this header should be filtered, continue to the next header
      if (headerFilterStrategy.applyFilterToCamelHeaders(
          entry.getKey(), entry.getValue(), camelExchange)) {
        continue;
      }

      LOG.debug("Propagate to CXF header: {} value: {}", entry.getKey(), entry.getValue());

      // put SOAP/protocol header list in exchange
      if (Header.HEADER_LIST.equals(entry.getKey())) {
        List<Header> headerList = (List<Header>) entry.getValue();
        for (Header header : headerList) {
          header.setDirection(Header.Direction.DIRECTION_OUT);
          LOG.trace(
              "Propagate SOAP/protocol header: {} : {}", header.getName(), header.getObject());
        }

        // cxfExchange.put(Header.HEADER_LIST, headerList);
        cxfContext.put(entry.getKey(), headerList);
        continue;
      }

      // things that are not filtered and not specifically copied will be put in transport headers
      if (entry.getValue() instanceof List) {
        transportHeaders.put(entry.getKey(), (List<String>) entry.getValue());
      } else {
        List<String> listValue = new ArrayList<String>();
        listValue.add(entry.getValue().toString());
        transportHeaders.put(entry.getKey(), listValue);
      }
    }

    if (transportHeaders.size() > 0) {
      cxfContext.put(Message.PROTOCOL_HEADERS, transportHeaders);
    } else {
      // no propagated transport headers does really mean no headers, not the ones
      // from the previous request or response propagated with the invocation context
      cxfContext.remove(Message.PROTOCOL_HEADERS);
    }
  }
Пример #3
0
  /**
   * @param cxfMessage
   * @param camelMessage
   * @param exchange provides context for filtering
   */
  protected void propagateHeadersFromCxfToCamel(
      Message cxfMessage, org.apache.camel.Message camelMessage, Exchange exchange) {
    Map<String, List<String>> cxfHeaders =
        CastUtils.cast((Map<?, ?>) cxfMessage.get(Message.PROTOCOL_HEADERS));
    Map<String, Object> camelHeaders = camelMessage.getHeaders();
    camelHeaders.put(CxfConstants.CAMEL_CXF_MESSAGE, cxfMessage);

    // Copy the http header to CAMEL as we do in camel-cxfrs
    CxfUtils.copyHttpHeadersFromCxfToCamel(cxfMessage, camelMessage);

    if (cxfHeaders != null) {
      for (Map.Entry<String, List<String>> entry : cxfHeaders.entrySet()) {
        if (!headerFilterStrategy.applyFilterToExternalHeaders(
            entry.getKey(), entry.getValue(), exchange)) {
          // We need to filter the content type with multi-part,
          // as the multi-part stream is already consumed by AttachmentInInterceptor,
          // it will cause some trouble when route this message to another CXF endpoint.

          if ("Content-Type".compareToIgnoreCase(entry.getKey()) == 0
              && entry.getValue().get(0) != null
              && entry.getValue().get(0).startsWith("multipart/related")) {
            // We need to keep the Content-Type if the data format is RAW message
            DataFormat dataFormat =
                exchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class);
            if (dataFormat.equals(DataFormat.RAW)) {
              camelHeaders.put(entry.getKey(), getContentTypeString(entry.getValue()));
            } else {
              String contentType = replaceMultiPartContentType(entry.getValue().get(0));
              LOG.trace("Find the multi-part Conent-Type, and replace it with {}", contentType);
              camelHeaders.put(entry.getKey(), contentType);
            }
          } else {
            LOG.trace(
                "Populate header from CXF header={} value={}", entry.getKey(), entry.getValue());
            List<String> values = entry.getValue();
            Object evalue;
            if (values.size() > 1) {
              if (exchange.getProperty(
                  CxfConstants.CAMEL_CXF_PROTOCOL_HEADERS_MERGED, Boolean.FALSE, Boolean.class)) {
                StringBuilder sb = new StringBuilder();
                for (Iterator<String> it = values.iterator(); it.hasNext(); ) {
                  sb.append(it.next());
                  if (it.hasNext()) {
                    sb.append(',').append(' ');
                  }
                }
                evalue = sb.toString();
              } else {
                evalue = values;
              }
            } else {
              evalue = values.get(0);
            }
            camelHeaders.put(entry.getKey(), evalue);
          }
        }
      }
    }

    // propagate SOAP/protocol header list
    String key = Header.HEADER_LIST;
    Object value = cxfMessage.get(key);
    if (value != null) {
      if (!headerFilterStrategy.applyFilterToExternalHeaders(key, value, exchange)) {
        camelHeaders.put(key, value);
        LOG.trace("Populate header from CXF header={} value={}", key, value);
      } else {
        ((List<?>) value).clear();
      }
    }

    // propagate the SOAPAction header
    String soapAction = (String) camelHeaders.get(SoapBindingConstants.SOAP_ACTION);
    // Remove SOAPAction from the protocol header, as it will not be overrided
    if (ObjectHelper.isEmpty(soapAction) || "\"\"".equals(soapAction)) {
      camelHeaders.remove(SoapBindingConstants.SOAP_ACTION);
    }
    soapAction = (String) cxfMessage.get(SoapBindingConstants.SOAP_ACTION);
    if (soapAction != null) {
      if (!headerFilterStrategy.applyFilterToExternalHeaders(
          SoapBindingConstants.SOAP_ACTION, soapAction, exchange)) {
        camelHeaders.put(SoapBindingConstants.SOAP_ACTION, soapAction);
        LOG.trace(
            "Populate header from CXF header={} value={}",
            SoapBindingConstants.SOAP_ACTION,
            soapAction);
      }
    }
  }