@SuppressWarnings("unchecked")
    public void process(Exchange exchange) throws Exception {
      CxfPayload<SoapHeader> in = exchange.getIn().getBody(CxfPayload.class);

      // verify request
      assertEquals(1, in.getBody().size());

      Map<String, String> ns = new HashMap<String, String>();
      ns.put("ns", MtomTestHelper.SERVICE_TYPES_NS);
      ns.put("xop", MtomTestHelper.XOP_NS);

      XPathUtils xu = new XPathUtils(ns);
      Element body = new XmlConverter().toDOMElement(in.getBody().get(0));
      Element ele =
          (Element) xu.getValue("//ns:Detail/ns:photo/xop:Include", body, XPathConstants.NODE);
      String photoId = ele.getAttribute("href").substring(4); // skip "cid:"
      assertEquals(MtomTestHelper.REQ_PHOTO_CID, photoId);

      ele = (Element) xu.getValue("//ns:Detail/ns:image/xop:Include", body, XPathConstants.NODE);
      String imageId = ele.getAttribute("href").substring(4); // skip "cid:"
      assertEquals(MtomTestHelper.REQ_IMAGE_CID, imageId);

      DataHandler dr = exchange.getIn().getAttachment(photoId);
      assertEquals("application/octet-stream", dr.getContentType());
      MtomTestHelper.assertEquals(
          MtomTestHelper.REQ_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));

      dr = exchange.getIn().getAttachment(imageId);
      assertEquals("image/jpeg", dr.getContentType());
      MtomTestHelper.assertEquals(
          MtomTestHelper.requestJpeg, IOUtils.readBytesFromStream(dr.getInputStream()));

      // create response
      List<Source> elements = new ArrayList<Source>();
      elements.add(
          new DOMSource(
              DOMUtils.readXml(new StringReader(MtomTestHelper.RESP_MESSAGE))
                  .getDocumentElement()));
      CxfPayload<SoapHeader> sbody =
          new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null);
      exchange.getOut().setBody(sbody);
      exchange
          .getOut()
          .addAttachment(
              MtomTestHelper.RESP_PHOTO_CID,
              new DataHandler(
                  new ByteArrayDataSource(
                      MtomTestHelper.RESP_PHOTO_DATA, "application/octet-stream")));

      exchange
          .getOut()
          .addAttachment(
              MtomTestHelper.RESP_IMAGE_CID,
              new DataHandler(new ByteArrayDataSource(MtomTestHelper.responseJpeg, "image/jpeg")));
    }
Ejemplo n.º 2
0
 public CachedCxfPayload(CxfPayload<T> orig, Exchange exchange, XmlConverter xml) {
   super(orig.getHeaders(), new ArrayList<Source>(orig.getBodySources()), orig.getNsMap());
   ListIterator<Source> li = getBodySources().listIterator();
   this.xml = xml;
   while (li.hasNext()) {
     Source source = li.next();
     XMLStreamReader reader = null;
     // namespace definitions that are on the SOAP envelope can get lost, if this is
     // not a DOM (there is special coding on the CXFPayload.getBody().get() method for
     // this, that only works on DOM nodes.
     // We have to do some delegation on the XMLStreamReader for StAXSource and StaxSource
     // that re-injects the missing namespaces into the XMLStreamReader.
     // Replace all other Sources that are not DOMSources with DOMSources.
     if (source instanceof StaxSource) {
       reader = ((StaxSource) source).getXMLStreamReader();
     } else if (source instanceof StAXSource) {
       reader = ((StAXSource) source).getXMLStreamReader();
     }
     if (reader != null) {
       Map<String, String> nsmap = getNsMap();
       if (nsmap == null) {
         nsmap = Collections.emptyMap();
       }
       source = new StAXSource(new DelegatingXMLStreamReader(reader, nsmap));
       StreamSource streamSource =
           exchange
               .getContext()
               .getTypeConverter()
               .convertTo(StreamSource.class, exchange, source);
       if (streamSource != null) {
         try {
           li.set(new StreamSourceCache(streamSource, exchange));
         } catch (IOException e) {
           LOG.error("Cannot Create StreamSourceCache ", e);
         }
       }
     } else if (!(source instanceof DOMSource)) {
       Document document =
           exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, source);
       if (document != null) {
         li.set(new DOMSource(document));
       }
     }
   }
 }
 @Converter
 public static String cxfPayloadToString(final CxfPayload payload) {
   XmlConverter converter = new XmlConverter();
   StringBuilder buf = new StringBuilder();
   for (Object element : payload.getBody()) {
     String elementString = "";
     try {
       elementString = converter.toString((Element) element, null);
     } catch (TransformerException e) {
       elementString = element.toString();
     }
     buf.append(elementString);
   }
   return buf.toString();
 }
  @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);
  }