use of javax.xml.soap.MessageFactory in project jaffa-framework by jaffa-projects.
the class WebServiceInvoker method createSOAPMessage.
/**
* Generate SOAPMessage based on the input arguments.
* <p>
* For example:
* <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:j="http://product1.mirotechnologies.com/material/core/StockBalanceFinder">
* <SOAP-ENV:Header/>
* <SOAP-ENV:Body>
* <j:performInquiry>
* <arg0>
* <part><operator>Equals</operator><values>P1000</values></part>
* </arg0>
* </j:performInquiry>
* </SOAP-ENV:Body>
* </SOAP-ENV:Envelope>
* @param arguments the arguments for the WebService.
* @return a SOAPMessage.
* @throws SOAPException if any SOAP error occurs.
* @throws JAXBException if any XML (un)marshalling error occurs.
*/
protected SOAPMessage createSOAPMessage(Object... arguments) throws SOAPException, JAXBException {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart messagePart = message.getSOAPPart();
SOAPEnvelope envelope = messagePart.getEnvelope();
String tns = this.obtainTargetNamespace();
if (tns != null) {
envelope.addNamespaceDeclaration(CUSTOM_PREFIX, tns);
}
SOAPBody body = message.getSOAPBody();
SOAPElement operationElement = tns != null ? body.addBodyElement(envelope.createQName(getOperationName(), CUSTOM_PREFIX)) : body.addBodyElement(envelope.createName(getOperationName()));
// Add authentication
addAuthorization(message);
// An arg{i} node will be created for each argument
if (arguments != null) {
Annotation[][] parameterAnnotations = null;
for (Method m : getWebServiceClass().getMethods()) {
if (m.getName().equals(getOperationName()) && m.getAnnotation(WebMethod.class) != null) {
parameterAnnotations = m.getParameterAnnotations();
}
}
for (int i = 0; i < arguments.length; i++) {
Object argument = arguments[i];
Class argumentClass = argument.getClass();
String webParamName = null;
if (parameterAnnotations != null && parameterAnnotations.length > 0) {
for (Annotation[] annotations : parameterAnnotations) {
for (Annotation annotation : annotations) {
if (annotation instanceof WebParam) {
WebParam webParam = (WebParam) annotation;
webParamName = webParam.name();
if (log.isDebugEnabled()) {
log.debug("webParamName :" + webParamName);
}
}
}
}
}
if (Collection.class.isAssignableFrom(argumentClass)) {
JAXBContext jc = null;
Marshaller marshaller = null;
List list = (List) argument;
for (Object arrayElement : list) {
if (jc == null) {
jc = JAXBHelper.obtainJAXBContext(arrayElement.getClass());
marshaller = jc.createMarshaller();
}
if (webParamName != null) {
marshaller.marshal(new JAXBElement(new QName(webParamName), arrayElement.getClass(), arrayElement), operationElement);
} else {
marshaller.marshal(new JAXBElement(new QName("arg0"), arrayElement.getClass(), arrayElement), operationElement);
}
}
} else if (argumentClass.isArray()) {
argumentClass = argumentClass.getComponentType();
JAXBContext jc = JAXBHelper.obtainJAXBContext(argumentClass);
Marshaller marshaller = jc.createMarshaller();
for (int j = 0, len = Array.getLength(argument); j < len; j++) {
Object arrayElement = Array.get(argument, j);
if (webParamName != null) {
marshaller.marshal(new JAXBElement(new QName(webParamName), argumentClass, arrayElement), operationElement);
} else {
marshaller.marshal(new JAXBElement(new QName("arg0"), argumentClass, arrayElement), operationElement);
}
}
} else {
JAXBContext jc = JAXBHelper.obtainJAXBContext(argumentClass);
Marshaller marshaller = jc.createMarshaller();
if (webParamName != null) {
marshaller.marshal(new JAXBElement(new QName(webParamName), argumentClass, argument), operationElement);
} else {
marshaller.marshal(new JAXBElement(new QName("arg0"), argumentClass, argument), operationElement);
}
}
}
}
// Save all changes to the Message
message.saveChanges();
if (log.isDebugEnabled()) {
log.debug("Created SOAPMessage: " + message);
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
message.writeTo(os);
log.debug("Contents of SOAPMessage: " + os.toString());
} catch (Exception e) {
// do nothing
}
}
return message;
}
use of javax.xml.soap.MessageFactory in project cxf by apache.
the class SOAPHandlerInterceptorTest method prepareSOAPMessage.
private SOAPMessage prepareSOAPMessage(String resouceName) throws Exception {
InputStream is = this.getClass().getResourceAsStream(resouceName);
MessageFactory factory = MessageFactory.newInstance();
MimeHeaders mhs = null;
return factory.createMessage(mhs, is);
}
use of javax.xml.soap.MessageFactory in project cxf by apache.
the class W3CDOMStreamReaderTest method testReader.
@Test
public void testReader() throws Exception {
ByteArrayInputStream is = new ByteArrayInputStream("<Test xmlns=\"http://example.org/types\"><argument>foobar</argument></Test>".getBytes());
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage msg = factory.createMessage();
SOAPPart part = msg.getSOAPPart();
Document doc = docBuilder.parse(is);
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(part.getEnvelope());
XMLStreamReader reader = StaxUtils.createXMLStreamReader(new DOMSource(doc));
StaxUtils.copy(reader, writer);
assertTrue(StaxUtils.toString(writer.getDocument()).endsWith(RESULT));
}
use of javax.xml.soap.MessageFactory in project cxf by apache.
the class SAAJInInterceptor method handleMessage.
@SuppressWarnings("unchecked")
public void handleMessage(SoapMessage message) throws Fault {
if (isGET(message)) {
return;
}
Boolean bodySet = (Boolean) message.get(BODY_FILLED_IN);
if (Boolean.TRUE.equals(bodySet)) {
return;
}
message.put(BODY_FILLED_IN, Boolean.TRUE);
try {
SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
if (soapMessage == null) {
MessageFactory factory = preInterceptor.getFactory(message);
soapMessage = factory.createMessage();
message.setContent(SOAPMessage.class, soapMessage);
}
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
if (xmlReader == null) {
return;
}
final SOAPPart part = soapMessage.getSOAPPart();
Document node = (Document) message.getContent(Node.class);
if (node != part && node != null) {
StaxUtils.copy(node, new SAAJStreamWriter(part));
} else {
SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope();
if (node == null) {
adjustPrefixes(env, (String) message.get(ReadHeadersInterceptor.ENVELOPE_PREFIX), (String) message.get(ReadHeadersInterceptor.BODY_PREFIX));
}
List<XMLEvent> events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.ENVELOPE_EVENTS);
applyEvents(events, env);
SOAPBody body = soapMessage.getSOAPBody();
events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.BODY_EVENTS);
applyEvents(events, body);
}
message.setContent(Node.class, soapMessage.getSOAPPart());
Collection<Attachment> atts = message.getAttachments();
if (atts != null) {
for (Attachment a : atts) {
if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
try {
((AttachmentDataSource) a.getDataHandler().getDataSource()).cache(message);
} catch (IOException e) {
throw new Fault(e);
}
}
AttachmentPart ap = soapMessage.createAttachmentPart(a.getDataHandler());
Iterator<String> i = a.getHeaderNames();
while (i != null && i.hasNext()) {
String h = i.next();
String val = a.getHeader(h);
ap.addMimeHeader(h, val);
}
if (StringUtils.isEmpty(ap.getContentId())) {
ap.setContentId(a.getId());
}
soapMessage.addAttachmentPart(ap);
}
}
// replace header element if necessary
if (message.hasHeaders()) {
replaceHeaders(soapMessage, message);
}
if (soapMessage.getSOAPPart().getEnvelope().getHeader() == null) {
soapMessage.getSOAPPart().getEnvelope().addHeader();
}
// If we have an xmlReader that already is counting the attributes and such
// then we don't want to rely on the system level defaults in StaxUtils.copy
// CXF-6173
boolean secureReader = StaxUtils.isSecureReader(xmlReader, message);
StaxUtils.copy(xmlReader, new SAAJStreamWriter(soapMessage.getSOAPPart(), soapMessage.getSOAPPart().getEnvelope().getBody()), true, !secureReader);
DOMSource bodySource = new DOMSource(soapMessage.getSOAPPart().getEnvelope().getBody());
xmlReader = StaxUtils.createXMLStreamReader(bodySource);
xmlReader.nextTag();
// move past body tag
xmlReader.nextTag();
message.setContent(XMLStreamReader.class, xmlReader);
} catch (SOAPException soape) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), soape, message.getVersion().getSender());
} catch (XMLStreamException e) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), e, message.getVersion().getSender());
}
}
use of javax.xml.soap.MessageFactory in project cxf by apache.
the class SAAJFactoryResolver method createMessageFactory.
public static MessageFactory createMessageFactory(SoapVersion version) throws SOAPException {
MessageFactory messageFactory;
String messageFactoryClassName = SystemPropertyAction.getPropertyOrNull(MESSAGE_FACTORY_KEY);
if (messageFactoryClassName != null) {
messageFactory = newInstanceCxfSAAJFactory(messageFactoryClassName, MessageFactory.class);
} else if (version instanceof Soap11) {
try {
messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
} catch (Throwable t) {
messageFactory = MessageFactory.newInstance();
}
} else if (version instanceof Soap12) {
try {
messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
} catch (Throwable t) {
messageFactory = MessageFactory.newInstance();
}
} else {
messageFactory = MessageFactory.newInstance();
}
return messageFactory;
}
Aggregations