Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WebService code
package eu.spb.poc.routing.blob;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.activation.DataHandler;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import com.sun.istack.internal.ByteArrayDataSource;
import eu.spb.blobwebservicemtom.CreateDocument;
import eu.spb.blobwebservicemtom.CreateDocumentResponse;
import eu.spb.blobwebservicemtom.Document;
import eu.spb.blobwebservicemtom.SoapMessageAttachments;
import eu.spb.poc.util.BlobMessage;
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.message.MessageContentsList;
public class FromWSCreateDocumentToJMS extends RouteBuilder {
@EndpointInject(ref = "wsBlob")
Endpoint wsBlobEndpoint;
@EndpointInject(ref = "wsBlobClient")
Endpoint wsBlobClientEndpoint;
@EndpointInject(ref = "jmsAdhesion")
Endpoint jmsAdhesionEndpoint;
@Override public void configure() throws Exception {
from(jmsAdhesionEndpoint).routeId("adhesion-queue-to-ws-blob")
.log(LoggingLevel.INFO, ">> Adhesion to be created")
.process(new Processor() {
@Override public void process(Exchange exchange) throws Exception {
// Create SOAP Message using dataFormat = MESSAGE
/*
String body = BlobMessage.REQ_ADHESION;
*/
// Create SOAP Message using dataFormat = PAYLOAD
/*
List<Source> elements = new ArrayList<Source>();
elements.add(new DOMSource(DOMUtils.readXml(new StringReader(BlobMessage.REQ_ADHESION_1)).getDocumentElement()));
CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(),
elements, null);
exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "createDocument");
exchange.getIn().setBody(body);
// Add attachment
exchange.getIn().addAttachment("123",
new DataHandler(new ByteArrayDataSource(BlobMessage.requestJpeg, "image/jpeg")));
*/
List<CreateDocument> params = new ArrayList<CreateDocument>();
// Create POJO createDocument
CreateDocument createDoc = new CreateDocument();
createDoc.setReqId(123L);
Document doc = new Document();
doc.setCode("123");
createDoc.setDocument(doc);
SoapMessageAttachments attachment = new SoapMessageAttachments();
attachment.setHandler(new DataHandler(new ByteArrayDataSource(BlobMessage.requestJpeg, "image/jpeg")));
createDoc.setMessageAttachments(attachment);
params.add(createDoc);
exchange.getIn().setBody(params);
exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "createDocument");
}
})
.to(wsBlobClientEndpoint)
.log(">> Response received : ${body}");
// Blob WS
from(wsBlobEndpoint).routeId("ws-blob-to-create-adhesion")
//.inOnly("jms:queue:blob")
.process(new Processor() {
@Override public void process(Exchange exchange) throws Exception {
// PAYLOAD
/*
CxfPayload<SoapHeader> body = (CxfPayload) exchange.getIn().getBody();
Element el = new XmlConverter().toDOMElement(body.getBody().get(0));
Source domSource = new DOMSource(el);
String payload = getStringFromDoc((DOMSource) domSource);
System.out.println(">> Payload : " + payload);
// Attachment
DataHandler handler = exchange.getIn().getAttachment("123");
byte[] result = IOUtils.readBytesFromStream(handler.getInputStream());
String value = new String(result, "UTF-8");
System.out.println(">> Attachment : " + value);
*/
// POJO Mode
MessageContentsList list = (MessageContentsList) exchange.getIn().getBody();
// Attachment
SoapMessageAttachments attachment = (SoapMessageAttachments) list.get(0);
DataHandler handler = attachment.getHandler();
byte[] result = IOUtils.readBytesFromStream(handler.getInputStream());
String value = new String(result, "UTF-8");
System.out.println(">> Attachment : " + value);
Document doc = (Document) list.get(1);
System.out.println(">> Code : " + doc.getCode());
// Prepare response using PAYLOAD
// exchange.getOut().setBody(BlobMessage.RESP_ADHESION);
// Prepare Response using POJO
CreateDocumentResponse response = new CreateDocumentResponse();
response.setReturn("123");
exchange.getIn().setBody(response);
}
})
//.marshal(soapDoc)
.log(">> Response prepared : ${body}");
}
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
public String getStringFromDoc(DOMSource doc) {
try {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(doc, result);
writer.flush();
return writer.toString();
}
catch(TransformerException ex) {
ex.printStackTrace();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment