use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class SoapHeaderInterceptor method handleMessage.
public void handleMessage(Message m) throws Fault {
SoapMessage message = (SoapMessage) m;
SoapVersion soapVersion = message.getVersion();
Exchange exchange = message.getExchange();
MessageContentsList parameters = MessageContentsList.getContentsList(message);
if (null == parameters) {
parameters = new MessageContentsList();
}
BindingOperationInfo bop = exchange.getBindingOperationInfo();
if (null == bop) {
return;
}
if (bop.isUnwrapped()) {
bop = bop.getWrappedOperation();
}
boolean client = isRequestor(message);
BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
if (bmi == null) {
// one way operation.
return;
}
List<SoapHeaderInfo> headers = bmi.getExtensors(SoapHeaderInfo.class);
if (headers == null || headers.isEmpty()) {
return;
}
boolean supportsNode = this.supportsDataReader(message, Node.class);
Service service = ServiceModelUtil.getService(message.getExchange());
Schema schema = null;
final boolean schemaValidationEnabled = ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message);
if (schemaValidationEnabled) {
schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
}
for (SoapHeaderInfo header : headers) {
MessagePartInfo mpi = header.getPart();
try {
if (schemaValidationEnabled && schema != null) {
validateHeader(message, mpi, schema);
}
} catch (Fault f) {
if (!isRequestor(message)) {
f.setFaultCode(Fault.FAULT_CODE_CLIENT);
}
throw f;
}
if (mpi.getTypeClass() != null) {
Header param = findHeader(message, mpi);
Object object = null;
if (param != null) {
message.getHeaders().remove(param);
if (param.getDataBinding() == null) {
Node source = (Node) param.getObject();
if (source instanceof Element) {
// need to remove these attributes as they
// would cause validation failures
Element el = (Element) source;
el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
}
if (supportsNode) {
object = getNodeDataReader(message).read(mpi, source);
} else {
W3CDOMStreamReader reader = new W3CDOMStreamReader((Element) source);
try {
// advance into the first tag
reader.nextTag();
} catch (XMLStreamException e) {
// ignore
}
object = getDataReader(message, XMLStreamReader.class).read(mpi, reader);
}
} else {
object = param.getObject();
}
}
parameters.put(mpi, object);
}
}
if (!parameters.isEmpty()) {
message.setContent(List.class, parameters);
}
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class SoapBindingFactoryTest method testFactory.
@Test
public void testFactory() throws Exception {
Definition d = createDefinition("/wsdl_soap/hello_world.wsdl");
Bus bus = getMockBus();
BindingFactoryManager bfm = getBindingFactoryManager(WSDLConstants.NS_SOAP11, bus);
bus.getExtension(BindingFactoryManager.class);
expectLastCall().andReturn(bfm).anyTimes();
DestinationFactoryManager dfm = control.createMock(DestinationFactoryManager.class);
expect(bus.getExtension(DestinationFactoryManager.class)).andStubReturn(dfm);
control.replay();
WSDLServiceBuilder builder = new WSDLServiceBuilder(bus);
ServiceInfo serviceInfo = builder.buildServices(d, new QName("http://apache.org/hello_world_soap_http", "SOAPService")).get(0);
BindingInfo bi = serviceInfo.getBindings().iterator().next();
assertTrue(bi instanceof SoapBindingInfo);
SoapBindingInfo sbi = (SoapBindingInfo) bi;
assertEquals("document", sbi.getStyle());
assertTrue(WSDLConstants.NS_SOAP11_HTTP_TRANSPORT.equalsIgnoreCase(sbi.getTransportURI()));
assertTrue(sbi.getSoapVersion() instanceof Soap11);
BindingOperationInfo boi = sbi.getOperation(new QName("http://apache.org/hello_world_soap_http", "sayHi"));
SoapOperationInfo sboi = boi.getExtensor(SoapOperationInfo.class);
assertNotNull(sboi);
assertEquals("document", sboi.getStyle());
assertEquals("", sboi.getAction());
BindingMessageInfo input = boi.getInput();
SoapBodyInfo bodyInfo = input.getExtensor(SoapBodyInfo.class);
assertEquals("literal", bodyInfo.getUse());
List<MessagePartInfo> parts = bodyInfo.getParts();
assertNotNull(parts);
assertEquals(1, parts.size());
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class RPCOutInterceptor method handleMessage.
public void handleMessage(Message message) {
XMLStreamWriter origXmlWriter = null;
try {
NSStack nsStack = new NSStack();
nsStack.push();
BindingOperationInfo operation = message.getExchange().getBindingOperationInfo();
assert operation.getName() != null;
XMLStreamWriter xmlWriter = getXMLStreamWriter(message);
CachingXmlEventWriter cache = null;
// need to cache the events in case validation fails or buffering is enabled
if (shouldBuffer(message)) {
origXmlWriter = xmlWriter;
cache = new CachingXmlEventWriter();
try {
cache.setNamespaceContext(xmlWriter.getNamespaceContext());
} catch (XMLStreamException e) {
// ignorable, will just get extra namespace decls
}
message.setContent(XMLStreamWriter.class, cache);
xmlWriter = cache;
}
final List<MessagePartInfo> parts;
final boolean output;
if (!isRequestor(message)) {
if (operation.getOutput() == null) {
return;
}
parts = operation.getOutput().getMessageParts();
output = true;
} else {
parts = operation.getInput().getMessageParts();
output = false;
}
MessageContentsList objs = MessageContentsList.getContentsList(message);
if (objs == null) {
addOperationNode(nsStack, message, xmlWriter, output, operation);
xmlWriter.writeEndElement();
return;
}
for (MessagePartInfo part : parts) {
if (objs.hasValue(part)) {
Object o = objs.get(part);
if (o == null) {
// WSI-BP R2211 - RPC/Lit parts are not allowed to be xsi:nil
throw new Fault(new org.apache.cxf.common.i18n.Message("BP_2211_RPCLIT_CANNOT_BE_NULL", LOG, part.getConcreteName()));
}
// WSI-BP R2737 -RPC/LIG part name space is empty
// part.setConcreteName(new QName("", part.getConcreteName().getLocalPart()));
}
}
addOperationNode(nsStack, message, xmlWriter, output, operation);
writeParts(message, message.getExchange(), operation, objs, parts);
// Finishing the writing.
xmlWriter.writeEndElement();
if (cache != null) {
try {
for (XMLEvent event : cache.getEvents()) {
StaxUtils.writeEvent(event, origXmlWriter);
}
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
} catch (XMLStreamException e) {
throw new Fault(e);
} finally {
if (origXmlWriter != null) {
message.setContent(XMLStreamWriter.class, origXmlWriter);
}
}
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallIntoStaxEventWriter.
@Test
public void testMarshallIntoStaxEventWriter() throws Exception {
GreetMe obj = new GreetMe();
obj.setRequestType("Hello");
QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
FixNamespacesXMLEventWriter writer = new FixNamespacesXMLEventWriter(opFactory.createXMLEventWriter(baos));
assertNull(writer.getMarshaller());
// STARTDOCUMENT/ENDDOCUMENT is not required
// writer.add(eFactory.createStartDocument("utf-8", "1.0"));
Marshaller m = context.createMarshaller();
JAXBEncoderDecoder.marshall(m, obj, part, writer);
assertEquals(m, writer.getMarshaller());
// writer.add(eFactory.createEndDocument());
writer.flush();
writer.close();
// System.out.println(baos.toString());
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
XMLInputFactory ipFactory = XMLInputFactory.newInstance();
XMLEventReader reader = ipFactory.createXMLEventReader(bais);
Unmarshaller um = context.createUnmarshaller();
Object val = um.unmarshal(reader, GreetMe.class);
assertTrue(val instanceof JAXBElement);
val = ((JAXBElement<?>) val).getValue();
assertTrue(val instanceof GreetMe);
assertEquals(obj.getRequestType(), ((GreetMe) val).getRequestType());
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallWithFormQualifiedElement.
@Test
public void testMarshallWithFormQualifiedElement() throws Exception {
ObjectWithQualifiedElementElement testObject = new ObjectWithQualifiedElementElement();
testObject.setString1("twine");
testObject.setString2("cord");
QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
StringWriter stringWriter = new StringWriter();
XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
XMLEventWriter writer = opFactory.createXMLEventWriter(stringWriter);
JAXBEncoderDecoder.marshall(context.createMarshaller(), testObject, part, writer);
writer.flush();
writer.close();
String xmlResult = stringWriter.toString();
// the following is a bit of a crock, but, to tell the truth, this test case most exists
// so that it could be examined inside the debugger to see how JAXB works.
assertTrue(xmlResult.contains(":string2>cord</ns"));
}
Aggregations