@Test
 public void testCreateSuccessResponse() throws IOException, SAXException {
   Document document =
       identificationReportingResponseProcessor.createSuccessResponse(
           "000001820140729014008340000", "Task ID");
   String documentString = OJBUtils.getStringFromDocument(document);
   System.out.println("Success doc: \n" + documentString);
   assertAsExpected(
       documentString,
       "src/test/resources/xmlInstances/"
           + "identificationReportingResponse/person_identification_report_success_response.xml");
 }
  @Override
  public String invokeIncidentSearchRequest(
      IncidentSearchRequest incidentSearchRequest, String federatedQueryID, Element samlToken)
      throws Exception {

    if (allowQueriesWithoutSAMLToken) {
      if (samlToken == null) {
        // Add SAML token to request call
        samlToken =
            SAMLTokenUtils.createStaticAssertionAsElement(
                "https://idp.ojbc-local.org:9443/idp/shibboleth",
                SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS,
                SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1,
                true,
                true,
                null);
      }
    }

    if (samlToken == null) {
      throw new Exception("No SAML token provided. Unable to perform query.");
    }

    // POJO to XML Request
    // When multiple operations are supported, we will call the appropriate POJO to XML Method
    Document incidentRequestPayload =
        RequestMessageBuilderUtilities.createIncidentSearchRequest(
            incidentSearchRequest, cityTownCodelistNamespace, cityTownCodelistElementName);

    incidentRequestPayload.normalizeDocument();

    log.debug(OJBUtils.getStringFromDocument(incidentRequestPayload));

    // Create exchange
    Exchange senderExchange = new DefaultExchange(camelContext, ExchangePattern.InOnly);

    // Set exchange body to XML Request message
    senderExchange.getIn().setBody(incidentRequestPayload);

    // Set reply to and WS-Addressing message ID
    senderExchange.getIn().setHeader("federatedQueryRequestGUID", federatedQueryID);
    senderExchange.getIn().setHeader("WSAddressingReplyTo", this.getReplyToAddress());

    // Set the token header so that CXF can retrieve this on the outbound call
    String tokenID = senderExchange.getExchangeId();
    senderExchange.getIn().setHeader("tokenID", tokenID);
    OJBSamlMap.putToken(tokenID, samlToken);

    incidentSearchMessageProcessor.sendResponseMessage(camelContext, senderExchange);

    // Put message ID and "noResponse" place holder.
    putRequestInMap(federatedQueryID);

    String response = pollMap(federatedQueryID);

    if (response.length() > 500) {
      log.debug("Here is the response (truncated): " + response.substring(0, 500));
    } else {
      log.debug("Here is the response: " + response);
    }

    // This is a defensive check in case the polling completes and the service has not yet returned
    // a response
    // In this case we send back an empty search result
    if (response.equals(NO_RESPONSE)) {
      log.debug("Endpoints timed out and no response recieved at web app, create error response");
      response = MergeNotificationErrorProcessor.returnMergeNotificationErrorMessage();
    }

    // return response here
    return response;
  }
Exemple #3
0
  @Test
  public void testNotificationBroker() throws Exception {

    notificationBrokerMockEndpoint.expectedMessageCount(1);
    loggingEndpoint.expectedMessageCount(1);

    // Create a new exchange
    Exchange senderExchange = new DefaultExchange(context);

    // Set the WS-Address Message ID
    Map<String, Object> requestContext = OJBUtils.setWSAddressingMessageID("123456789");

    // Set the operation name and operation namespace for the CXF exchange
    senderExchange.getIn().setHeader(Client.REQUEST_CONTEXT, requestContext);

    Document doc = createDocument();
    List<SoapHeader> soapHeaders = new ArrayList<SoapHeader>();
    soapHeaders.add(
        makeSoapHeader(doc, "http://www.w3.org/2005/08/addressing", "MessageID", "12345"));
    senderExchange.getIn().setHeader(Header.HEADER_LIST, soapHeaders);

    org.apache.cxf.message.Message message = new MessageImpl();

    senderExchange.getIn().setHeader(CxfConstants.CAMEL_CXF_MESSAGE, message);
    senderExchange
        .getIn()
        .setHeader(CxfConstants.OPERATION_NAME, CXF_OPERATION_NAME_CRIMINAL_HISTORY);
    senderExchange
        .getIn()
        .setHeader(CxfConstants.OPERATION_NAMESPACE, CXF_OPERATION_NAMESPACE_CRIMINAL_HISTORY);

    // Read the firearm search request file from the file system
    File inputFile =
        new File(
            "src/test/resources/xmlInstances/cycleTrackingIdentifierAssignmentReport/Cycle-Tracking-Identifier-Assignment-Report.xml");
    String inputStr = FileUtils.readFileToString(inputFile);

    assertNotNull(inputStr);

    log.debug(inputStr);

    // Set it as the message message body
    senderExchange.getIn().setBody(inputStr);

    // Send the one-way exchange.  Using template.send will send an one way message
    Exchange returnExchange =
        template.send("direct:criminalHistoryUpdatedReportingService", senderExchange);

    // Use getException to see if we received an exception
    if (returnExchange.getException() != null) {
      throw new Exception(returnExchange.getException());
    }

    // Sleep while a response is generated
    Thread.sleep(3000);

    // Assert that the mock endpoint expectations are satisfied
    notificationBrokerMockEndpoint.assertIsSatisfied();
    loggingEndpoint.assertIsSatisfied();

    // Get the first exchange (the only one)
    Exchange ex = notificationBrokerMockEndpoint.getExchanges().get(0);

    String opName = (String) ex.getIn().getHeader("operationName");
    assertEquals("Notify", opName);

    String opNamespace = (String) ex.getIn().getHeader("operationNamespace");
    assertEquals("http://docs.oasis-open.org/wsn/brw-2", opNamespace);

    Document returnDocumentNotificationBroker = ex.getIn().getBody(Document.class);

    // Do some very basic assertions to assure the message is transformed.
    // The XSLT test does a more complete examination of the transformation.
    Node notifyNode = XmlUtils.xPathNodeSearch(returnDocumentNotificationBroker, "/b-2:Notify");
    Node notifyMesssageNode = XmlUtils.xPathNodeSearch(notifyNode, "b-2:NotificationMessage");

    Node messageNode = XmlUtils.xPathNodeSearch(notifyMesssageNode, "b-2:Message");
    assertNotNull(messageNode);

    // Get the first exchange (the only one) to the logger
    // This is what would be sent to the implementation specific route
    Exchange exImplementationSpecific = loggingEndpoint.getExchanges().get(0);

    Document returnDocumentImplementationSpecific =
        exImplementationSpecific.getIn().getBody(Document.class);

    // Make sure the root node here is the message to the original exchange
    Node rootNode =
        XmlUtils.xPathNodeSearch(
            returnDocumentImplementationSpecific,
            "/crimhistory-update-exch:CycleTrackingIdentifierAssignmentReport");
    assertNotNull(rootNode);

    // XmlUtils.printNode(returnDocumentImplementationSpecific);
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public void aggregateMergedMessageWithErrorResponses(Exchange groupedExchange) throws Exception {
    // Get the grouped exchanged consisting of the aggregated search results and merged results
    List<Exchange> grouped = groupedExchange.getProperty(Exchange.GROUPED_EXCHANGE, List.class);

    boolean responseHasErrors = false;
    Document erResponseBodyDocument = null;
    Document psResultsBeforeER = null;

    for (Exchange exchange : grouped) {
      // This is the original exchange, it contains the aggregated response message before the
      // Person Search to ER XSLT
      if (grouped.indexOf(exchange) == 0) {
        String messageID = (String) exchange.getIn().getHeader("federatedQueryRequestGUID");

        // The new grouped exchange does not get the message headers from the original exchange so
        // we manually copy the message ID
        groupedExchange.getIn().setHeader("federatedQueryRequestGUID", messageID);

        // Get header to see if we have error nodes.  This is the exchange before calling ER so it
        // has these headers, subsequent exchanges do not.
        String errorResponseNodeCountString =
            (String) exchange.getIn().getHeader("errorResponseNodeCount");

        Integer errorResponseNodeCount = null;

        if (errorResponseNodeCountString != null) {
          errorResponseNodeCount = Integer.valueOf(errorResponseNodeCountString);
        } else {
          errorResponseNodeCount = 0;
        }

        if (errorResponseNodeCount > 0) {
          responseHasErrors = true;
          String aggregatedResponse = (String) exchange.getIn().getBody();

          // Load up the aggregated results into a document
          psResultsBeforeER = OJBUtils.loadXMLFromString(aggregatedResponse);
        }

      }

      // This is the actual response from the ER service, it will always be exchange indexed at
      // position 1
      else {
        // Uncomment the line below to see the individual aggregated message
        // log.debug("This is the body of the exchange in the exchange group: " +
        // exchange.getIn().getBody());

        CxfPayload cxfPayload = (CxfPayload) exchange.getIn().getBody();
        List<Element> elementList = cxfPayload.getBody();

        erResponseBodyDocument = elementList.get(0).getOwnerDocument();
      }
    }

    // The ER service did not return with an actual response, it is down or has timed out.  Set a
    // static error response and return.
    if (erResponseBodyDocument == null) {
      String returnMessage =
          MergeNotificationErrorProcessor.returnMergeNotificationErrorMessageEntityResolution();
      groupedExchange.getIn().setBody(returnMessage);
      return;
    }

    // If we have errors, splice them into the response
    if (responseHasErrors) {
      log.debug("Response has errors, splice them in here");

      NodeList list =
          psResultsBeforeER.getElementsByTagNameNS(
              "http://ojbc.org/IEPD/Extensions/SearchResultsMetadata/1.0", "SearchResultsMetadata");
      Element searchResultsMetadataElement =
          (Element) erResponseBodyDocument.importNode(list.item(0), true);

      Element searchResultsMetadataCollectionElement =
          erResponseBodyDocument.createElementNS(
              "http://nij.gov/IEPD/Exchange/EntityMergeResultMessage/1.0",
              "SearchResultsMetadataCollection");
      searchResultsMetadataCollectionElement.appendChild(searchResultsMetadataElement);

      erResponseBodyDocument.getFirstChild().appendChild(searchResultsMetadataCollectionElement);
    }

    // Set the response
    groupedExchange.getIn().setBody(erResponseBodyDocument);
  }