Example #1
0
 public boolean applyFilterToExternalHeaders(
     String headerName, Object headerValue, org.apache.camel.Exchange exchange) {
   if (org.apache.cxf.headers.Header.HEADER_LIST.equals(headerName)) {
     return true;
   }
   return super.applyFilterToExternalHeaders(headerName, headerValue, exchange);
 }
Example #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);
    }
  }