Esempio n. 1
0
 /**
  * Converts the inbound body to a {@link Source}, if the body is <b>not</b> already a {@link
  * Source}.
  *
  * <p>This implementation will prefer to source in the following order:
  *
  * <ul>
  *   <li>StAX - If StAX is allowed
  *   <li>SAX - SAX as 2nd choice
  *   <li>Stream - Stream as 3rd choice
  *   <li>DOM - DOM as 4th choice
  * </ul>
  */
 protected Source getSource(Exchange exchange, Object body) {
   // body may already be a source
   if (body instanceof Source) {
     return (Source) body;
   }
   Source source = null;
   if (body != null) {
     if (isAllowStAX()) {
       // try StAX if enabled
       source =
           exchange.getContext().getTypeConverter().tryConvertTo(StAXSource.class, exchange, body);
     }
     if (source == null) {
       // then try SAX
       source =
           exchange.getContext().getTypeConverter().tryConvertTo(SAXSource.class, exchange, body);
       tryAddEntityResolver((SAXSource) source);
     }
     if (source == null) {
       // then try stream
       source =
           exchange
               .getContext()
               .getTypeConverter()
               .tryConvertTo(StreamSource.class, exchange, body);
     }
     if (source == null) {
       // and fallback to DOM
       source =
           exchange.getContext().getTypeConverter().tryConvertTo(DOMSource.class, exchange, body);
     }
     // as the TypeConverterRegistry will look up source the converter differently if the type
     // converter is loaded different
     // now we just put the call of source converter at last
     if (source == null) {
       TypeConverter tc =
           exchange.getContext().getTypeConverterRegistry().lookup(Source.class, body.getClass());
       if (tc != null) {
         source = tc.convertTo(Source.class, exchange, body);
       }
     }
   }
   if (source == null) {
     if (isFailOnNullBody()) {
       throw new ExpectedBodyTypeException(exchange, Source.class);
     } else {
       try {
         source = converter.toDOMSource(converter.createDocument());
       } catch (ParserConfigurationException e) {
         throw new RuntimeTransformException(e);
       }
     }
   }
   return source;
 }
 @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();
 }
Esempio n. 3
0
  /**
   * Sets the XSLT transformer from a Source
   *
   * @param source the source
   * @throws TransformerConfigurationException is thrown if creating a XSLT transformer failed.
   */
  public void setTransformerSource(Source source) throws TransformerConfigurationException {
    TransformerFactory factory = converter.getTransformerFactory();
    if (errorListener != null) {
      factory.setErrorListener(errorListener);
    } else {
      // use a logger error listener so users can see from the logs what the error may be
      factory.setErrorListener(new XsltErrorListener());
    }
    if (getUriResolver() != null) {
      factory.setURIResolver(getUriResolver());
    }

    // Check that the call to newTemplates() returns a valid template instance.
    // In case of an xslt parse error, it will return null and we should stop the
    // deployment and raise an exception as the route will not be setup properly.
    Templates templates = factory.newTemplates(source);
    if (templates != null) {
      setTemplate(templates);
    } else {
      throw new TransformerConfigurationException(
          "Error creating XSLT template. "
              + "This is most likely be caused by a XML parse error. "
              + "Please verify your XSLT file configured.");
    }
  }
Esempio n. 4
0
 @Override
 public void writeTo(OutputStream os) throws IOException {
   // no body no write
   if (getBodySources().size() == 0) {
     return;
   }
   Source body = getBodySources().get(0);
   if (body instanceof StreamCache) {
     ((StreamCache) body).writeTo(os);
   } else {
     StreamResult sr = new StreamResult(os);
     try {
       xml.toResult(body, sr);
     } catch (TransformerException e) {
       throw new IOException("Transformation failed", e);
     }
   }
 }