コード例 #1
0
ファイル: LoopProcessor.java プロジェクト: nkukhar/camel
 /**
  * Prepares the exchange for the next iteration
  *
  * @param exchange the exchange
  * @param index the index of the next iteration
  * @return the exchange to use
  */
 protected Exchange prepareExchange(Exchange exchange, int index, Exchange original) {
   if (copy) {
     // use a copy but let it reuse the same exchange id so it appear as one exchange
     // use the original exchange rather than the looping exchange (esp. with the async routing
     // engine)
     return ExchangeHelper.createCopy(original, true);
   } else {
     ExchangeHelper.prepareOutToIn(exchange);
     return exchange;
   }
 }
コード例 #2
0
    @Override
    public void onAfterRoute(Route route, Exchange exchange) {
      // we use the onAfterRoute callback, to ensure the data has been marshalled before
      // the consumer writes the response back

      // only trigger when it was the 1st route that was done
      if (!routeId.equals(route.getId())) {
        return;
      }

      // only marshal if there was no exception
      if (exchange.getException() != null) {
        return;
      }

      if (skipBindingOnErrorCode) {
        Integer code =
            exchange.hasOut()
                ? exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class)
                : exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
        // if there is a custom http error code then skip binding
        if (code != null && code >= 300) {
          return;
        }
      }

      boolean isXml = false;
      boolean isJson = false;

      // accept takes precedence
      if (accept != null) {
        isXml = accept.toLowerCase(Locale.ENGLISH).contains("xml");
        isJson = accept.toLowerCase(Locale.ENGLISH).contains("json");
      }
      // fallback to content type if still undecided
      if (!isXml && !isJson) {
        String contentType = ExchangeHelper.getContentType(exchange);
        if (contentType != null) {
          isXml = contentType.toLowerCase(Locale.ENGLISH).contains("xml");
          isJson = contentType.toLowerCase(Locale.ENGLISH).contains("json");
        }
      }
      // if content type could not tell us if it was json or xml, then fallback to if the binding
      // was configured with
      // that information in the consumes
      if (!isXml && !isJson) {
        isXml = produces != null && produces.toLowerCase(Locale.ENGLISH).contains("xml");
        isJson = produces != null && produces.toLowerCase(Locale.ENGLISH).contains("json");
      }

      // only allow xml/json if the binding mode allows that (when off we still want to know if its
      // xml or json)
      if (bindingMode != null) {
        isXml &=
            bindingMode.equals("off") || bindingMode.equals("auto") || bindingMode.contains("xml");
        isJson &=
            bindingMode.equals("off") || bindingMode.equals("auto") || bindingMode.contains("json");

        // if we do not yet know if its xml or json, then use the binding mode to know the mode
        if (!isJson && !isXml) {
          isXml = bindingMode.equals("auto") || bindingMode.contains("xml");
          isJson = bindingMode.equals("auto") || bindingMode.contains("json");
        }
      }

      // in case we have not yet been able to determine if xml or json, then use the same as in the
      // unmarshaller
      if (isXml && isJson) {
        isXml = wasXml;
        isJson = !wasXml;
      }

      // need to prepare exchange first
      ExchangeHelper.prepareOutToIn(exchange);

      // ensure there is a content type header (even if binding is off)
      ensureHeaderContentType(produces, isXml, isJson, exchange);

      if (bindingMode == null || "off".equals(bindingMode)) {
        // binding is off, so no message body binding
        return;
      }

      // is there any marshaller at all
      if (jsonMarshal == null && xmlMarshal == null) {
        return;
      }

      // is the body empty
      if ((exchange.hasOut() && exchange.getOut().getBody() == null)
          || (!exchange.hasOut() && exchange.getIn().getBody() == null)) {
        return;
      }

      try {
        // favor json over xml
        if (isJson && jsonMarshal != null) {
          jsonMarshal.process(exchange);
        } else if (isXml && xmlMarshal != null) {
          xmlMarshal.process(exchange);
        } else {
          // we could not bind
          if (bindingMode.equals("auto")) {
            // okay for auto we do not mind if we could not bind
          } else {
            if (bindingMode.contains("xml")) {
              exchange.setException(
                  new BindingException(
                      "Cannot bind to xml as message body is not xml compatible", exchange));
            } else {
              exchange.setException(
                  new BindingException(
                      "Cannot bind to json as message body is not json compatible", exchange));
            }
          }
        }
      } catch (Throwable e) {
        exchange.setException(e);
      }
    }