예제 #1
0
  /**
   * Generate http request entity from Spring Integration message.
   *
   * @param requestMessage
   * @param method
   * @return
   */
  private HttpEntity<?> generateRequest(Message<?> requestMessage, HttpMethod method) {
    HttpHeaders httpHeaders = new HttpHeaders();
    headerMapper.fromHeaders(requestMessage.getHeaders(), httpHeaders);

    Map<String, ?> messageHeaders = requestMessage.getHeaders();
    for (Entry<String, ?> header : messageHeaders.entrySet()) {
      if (!header.getKey().startsWith(CitrusMessageHeaders.PREFIX)
          && !MessageUtils.isSpringInternalHeader(header.getKey())
          && !httpHeaders.containsKey(header.getKey())) {
        httpHeaders.add(header.getKey(), header.getValue().toString());
      }
    }

    Object payload = requestMessage.getPayload();
    if (httpHeaders.getContentType() == null) {
      httpHeaders.setContentType(
          MediaType.parseMediaType(
              contentType.contains("charset") ? contentType : contentType + ";charset=" + charset));
    }

    if (HttpMethod.POST.equals(method) || HttpMethod.PUT.equals(method)) {
      return new HttpEntity<Object>(payload, httpHeaders);
    }

    return new HttpEntity<Object>(httpHeaders);
  }
예제 #2
0
  /**
   * Message headers consist of standard HTTP message headers and custom headers. This method
   * assumes that all header entries that were not initially mapped by header mapper implementations
   * are custom headers.
   *
   * @param httpHeaders all message headers in their pre nature.
   * @param mappedHeaders the previously mapped header entries (all standard headers).
   * @return
   */
  private Map<String, String> getCustomHeaders(
      HttpHeaders httpHeaders, Map<String, ?> mappedHeaders) {
    Map<String, String> customHeaders = new HashMap<String, String>();

    for (Entry<String, List<String>> header : httpHeaders.entrySet()) {
      if (!mappedHeaders.containsKey(header.getKey())) {
        customHeaders.put(
            header.getKey(), StringUtils.collectionToCommaDelimitedString(header.getValue()));
      }
    }

    return customHeaders;
  }
예제 #3
0
  /**
   * Perform validation on namespace qualified attribute values if present. This includes the
   * validation of namespace presence and equality.
   *
   * @param receivedElement
   * @param receivedAttribute
   * @param sourceElement
   * @param sourceAttribute
   */
  private void doNamespaceQualifiedAttributeValidation(
      Node receivedElement, Node receivedAttribute, Node sourceElement, Node sourceAttribute) {
    String receivedValue = receivedAttribute.getNodeValue();
    String sourceValue = sourceAttribute.getNodeValue();

    if (receivedValue.contains(":") && sourceValue.contains(":")) {
      // value has namespace prefix set, do special QName validation
      String receivedPrefix = receivedValue.substring(0, receivedValue.indexOf(':'));
      String sourcePrefix = sourceValue.substring(0, sourceValue.indexOf(':'));

      Map<String, String> receivedNamespaces =
          XMLUtils.lookupNamespaces(receivedAttribute.getOwnerDocument());
      receivedNamespaces.putAll(XMLUtils.lookupNamespaces(receivedElement));

      if (receivedNamespaces.containsKey(receivedPrefix)) {
        Map<String, String> sourceNamespaces =
            XMLUtils.lookupNamespaces(sourceAttribute.getOwnerDocument());
        sourceNamespaces.putAll(XMLUtils.lookupNamespaces(sourceElement));

        if (sourceNamespaces.containsKey(sourcePrefix)) {
          Assert.isTrue(
              sourceNamespaces.get(sourcePrefix).equals(receivedNamespaces.get(receivedPrefix)),
              ValidationUtils.buildValueMismatchErrorMessage(
                  "Values not equal for attribute value namespace '" + receivedValue + "'",
                  sourceNamespaces.get(sourcePrefix),
                  receivedNamespaces.get(receivedPrefix)));

          // remove namespace prefixes as they must not form equality
          receivedValue = receivedValue.substring((receivedPrefix + ":").length());
          sourceValue = sourceValue.substring((sourcePrefix + ":").length());
        } else {
          throw new ValidationException(
              "Received attribute value '"
                  + receivedAttribute.getLocalName()
                  + "' describes namespace qualified attribute value,"
                  + " control value '"
                  + sourceValue
                  + "' does not");
        }
      }
    }

    Assert.isTrue(
        receivedValue.equals(sourceValue),
        ValidationUtils.buildValueMismatchErrorMessage(
            "Values not equal for attribute '" + receivedAttribute.getLocalName() + "'",
            sourceValue,
            receivedValue));
  }
예제 #4
0
  /**
   * Validate namespaces in message. The method compares namespace declarations in the root element
   * of the received message to expected namespaces. Prefixes are important too, so differing
   * namespace prefixes will fail the validation.
   *
   * @param expectedNamespaces
   * @param receivedMessage
   */
  protected void validateNamespaces(
      Map<String, String> expectedNamespaces, Message receivedMessage) {
    if (CollectionUtils.isEmpty(expectedNamespaces)) {
      return;
    }

    if (receivedMessage.getPayload() == null
        || !StringUtils.hasText(receivedMessage.getPayload(String.class))) {
      throw new ValidationException(
          "Unable to validate message namespaces - receive message payload was empty");
    }

    log.info("Start XML namespace validation");

    Document received = XMLUtils.parseMessagePayload(receivedMessage.getPayload(String.class));

    Map<String, String> foundNamespaces =
        XMLUtils.lookupNamespaces(receivedMessage.getPayload(String.class));

    if (foundNamespaces.size() != expectedNamespaces.size()) {
      throw new ValidationException(
          "Number of namespace declarations not equal for node "
              + XMLUtils.getNodesPathName(received.getFirstChild())
              + " found "
              + foundNamespaces.size()
              + " expected "
              + expectedNamespaces.size());
    }

    for (Entry<String, String> entry : expectedNamespaces.entrySet()) {
      String namespace = entry.getKey();
      String url = entry.getValue();

      if (foundNamespaces.containsKey(namespace)) {
        if (!foundNamespaces.get(namespace).equals(url)) {
          throw new ValidationException(
              "Namespace '"
                  + namespace
                  + "' values not equal: found '"
                  + foundNamespaces.get(namespace)
                  + "' expected '"
                  + url
                  + "' in reference node "
                  + XMLUtils.getNodesPathName(received.getFirstChild()));
        } else {
          log.info(
              "Validating namespace " + namespace + " value as expected " + url + " - value OK");
        }
      } else {
        throw new ValidationException(
            "Missing namespace "
                + namespace
                + "("
                + url
                + ") in node "
                + XMLUtils.getNodesPathName(received.getFirstChild()));
      }
    }

    log.info("XML namespace validation finished successfully: All values OK");
  }