use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class WrapperClassInInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Exchange ex = message.getExchange();
BindingOperationInfo boi = ex.getBindingOperationInfo();
if (Boolean.TRUE.equals(message.get(Message.PARTIAL_RESPONSE_MESSAGE)) || boi == null) {
return;
}
Method method = ex.get(Method.class);
if (method != null && method.getName().endsWith("Async")) {
Class<?> retType = method.getReturnType();
if (retType.getName().equals("java.util.concurrent.Future") || retType.getName().equals("javax.xml.ws.Response")) {
return;
}
}
if (boi.isUnwrappedCapable()) {
BindingOperationInfo boi2 = boi.getUnwrappedOperation();
OperationInfo op = boi2.getOperationInfo();
BindingMessageInfo bmi;
MessageInfo wrappedMessageInfo = message.get(MessageInfo.class);
MessageInfo messageInfo;
if (wrappedMessageInfo == boi.getOperationInfo().getInput()) {
messageInfo = op.getInput();
bmi = boi2.getInput();
} else {
messageInfo = op.getOutput();
bmi = boi2.getOutput();
}
// Sometimes, an operation can be unwrapped according to WSDLServiceFactory,
// but not according to JAX-WS. We should unify these at some point, but
// for now check for the wrapper class.
MessageContentsList lst = MessageContentsList.getContentsList(message);
if (lst == null) {
return;
}
message.put(MessageInfo.class, messageInfo);
message.put(BindingMessageInfo.class, bmi);
ex.put(BindingOperationInfo.class, boi2);
if (isGET(message)) {
LOG.fine("WrapperClassInInterceptor skipped in HTTP GET method");
return;
}
MessagePartInfo wrapperPart = wrappedMessageInfo.getFirstMessagePart();
Class<?> wrapperClass = wrapperPart.getTypeClass();
Object wrappedObject = lst.get(wrapperPart.getIndex());
if (wrapperClass == null || wrappedObject == null || (wrapperClass != null && !wrapperClass.isInstance(wrappedObject))) {
return;
}
WrapperHelper helper = wrapperPart.getProperty("WRAPPER_CLASS", WrapperHelper.class);
if (helper == null) {
Service service = ServiceModelUtil.getService(message.getExchange());
DataBinding dataBinding = service.getDataBinding();
if (dataBinding instanceof WrapperCapableDatabinding) {
helper = createWrapperHelper((WrapperCapableDatabinding) dataBinding, messageInfo, wrappedMessageInfo, wrapperClass);
wrapperPart.setProperty("WRAPPER_CLASS", helper);
} else {
return;
}
}
MessageContentsList newParams;
try {
newParams = new MessageContentsList(helper.getWrapperParts(wrappedObject));
List<Integer> removes = null;
int count = 0;
for (MessagePartInfo part : messageInfo.getMessageParts()) {
if (Boolean.TRUE.equals(part.getProperty(ReflectionServiceFactoryBean.HEADER))) {
MessagePartInfo mpi = null;
for (MessagePartInfo mpi2 : wrappedMessageInfo.getMessageParts()) {
if (mpi2.getConcreteName().equals(part.getConcreteName())) {
mpi = mpi2;
}
}
if (mpi != null && lst.hasValue(mpi)) {
count++;
newParams.put(part, lst.get(mpi));
} else if (mpi == null || mpi.getTypeClass() == null) {
// header, but not mapped to a param on the method
if (removes == null) {
removes = new ArrayList<>();
}
removes.add(part.getIndex());
}
} else {
++count;
}
}
if (count == 0) {
newParams.clear();
} else if (removes != null) {
Collections.sort(removes, Collections.reverseOrder());
for (Integer i : removes) {
if (i < newParams.size()) {
newParams.remove(i.intValue());
}
}
}
} catch (Exception e) {
throw new Fault(e);
}
message.setContent(List.class, newParams);
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class WrapperClassOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Exchange ex = message.getExchange();
BindingOperationInfo bop = ex.getBindingOperationInfo();
MessageInfo messageInfo = message.get(MessageInfo.class);
if (messageInfo == null || bop == null || !bop.isUnwrapped()) {
return;
}
BindingOperationInfo newbop = bop.getWrappedOperation();
MessageInfo wrappedMsgInfo;
if (Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
wrappedMsgInfo = newbop.getInput().getMessageInfo();
} else {
wrappedMsgInfo = newbop.getOutput().getMessageInfo();
}
Class<?> wrapped = null;
if (wrappedMsgInfo.getMessagePartsNumber() > 0) {
wrapped = wrappedMsgInfo.getFirstMessagePart().getTypeClass();
}
if (wrapped != null) {
MessagePartInfo firstMessagePart = wrappedMsgInfo.getFirstMessagePart();
MessageContentsList objs = MessageContentsList.getContentsList(message);
WrapperHelper helper = firstMessagePart.getProperty("WRAPPER_CLASS", WrapperHelper.class);
if (helper == null) {
helper = getWrapperHelper(message, messageInfo, wrappedMsgInfo, wrapped, firstMessagePart);
}
if (helper == null) {
return;
}
try {
MessageContentsList newObjs = new MessageContentsList();
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message) && helper instanceof AbstractWrapperHelper) {
((AbstractWrapperHelper) helper).setValidate(true);
}
Object o2 = helper.createWrapperObject(objs);
newObjs.put(firstMessagePart, o2);
for (MessagePartInfo p : messageInfo.getMessageParts()) {
if (Boolean.TRUE.equals(p.getProperty(ReflectionServiceFactoryBean.HEADER))) {
MessagePartInfo mpi = wrappedMsgInfo.getMessagePart(p.getName());
if (objs.hasValue(p)) {
newObjs.put(mpi, objs.get(p));
}
}
}
message.setContent(List.class, newObjs);
} catch (Fault f) {
throw f;
} catch (Exception e) {
throw new Fault(e);
}
// we've now wrapped the object, so use the wrapped binding op
ex.put(BindingOperationInfo.class, newbop);
if (messageInfo == bop.getOperationInfo().getInput()) {
message.put(MessageInfo.class, newbop.getOperationInfo().getInput());
message.put(BindingMessageInfo.class, newbop.getInput());
} else if (messageInfo == bop.getOperationInfo().getOutput()) {
message.put(MessageInfo.class, newbop.getOperationInfo().getOutput());
message.put(BindingMessageInfo.class, newbop.getOutput());
}
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class JAXWSMethodInvokerTest method testSuspendedException.
@Test
public void testSuspendedException() throws Throwable {
Exception originalException = new RuntimeException();
ContinuationService serviceObject = new ContinuationService(originalException);
Method serviceMethod = ContinuationService.class.getMethod("invoke", new Class[] {});
Exchange ex = new ExchangeImpl();
Message inMessage = new MessageImpl();
ex.setInMessage(inMessage);
inMessage.setExchange(ex);
inMessage.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
try {
jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] {}));
fail("Suspended invocation swallowed");
} catch (SuspendedInvocationException suspendedEx) {
assertSame(suspendedEx, serviceObject.getSuspendedException());
assertSame(originalException, suspendedEx.getRuntimeException());
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class JAXWSMethodInvokerTest method testProviderInterpretNullAsOneway.
@Test
public void testProviderInterpretNullAsOneway() throws Throwable {
NullableProviderService serviceObject = new NullableProviderService();
Method serviceMethod = NullableProviderService.class.getMethod("invoke", new Class[] { Source.class });
Exchange ex = new ExchangeImpl();
Message inMessage = new MessageImpl();
inMessage.setInterceptorChain(new PhaseInterceptorChain(new TreeSet<Phase>()));
ex.setInMessage(inMessage);
inMessage.setExchange(ex);
inMessage.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
// request-response with non-null response
ex.setOneWay(false);
MessageContentsList obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
assertEquals(1, obj.size());
assertNotNull(obj.get(0));
assertFalse(ex.isOneWay());
// oneway with non-null response
ex.setOneWay(true);
obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
assertNull(obj);
assertTrue(ex.isOneWay());
// request-response with null response, interpretNullAsOneway not set so
// default should be true
ex.setOneWay(false);
serviceObject.setNullable(true);
obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
assertNull(obj);
assertTrue(ex.isOneWay());
// request-response with null response, interpretNullAsOneway disabled
ex.setOneWay(false);
serviceObject.setNullable(true);
inMessage.put("jaxws.provider.interpretNullAsOneway", Boolean.FALSE);
obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
assertEquals(1, obj.size());
assertNull(obj.get(0));
assertFalse(ex.isOneWay());
// request-response with null response, interpretNullAsOneway explicitly enabled
ex.setOneWay(false);
serviceObject.setNullable(true);
inMessage.put("jaxws.provider.interpretNullAsOneway", Boolean.TRUE);
obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
assertNull(obj);
assertTrue(ex.isOneWay());
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class JAXWSMethodInvokerTest method testFaultHeadersCopy.
@Test
public void testFaultHeadersCopy() throws Throwable {
ExceptionService serviceObject = new ExceptionService();
Method serviceMethod = ExceptionService.class.getMethod("invoke", new Class[] {});
Exchange ex = new ExchangeImpl();
prepareInMessage(ex, true);
Message msg = new MessageImpl();
SoapMessage outMessage = new SoapMessage(msg);
ex.setOutMessage(outMessage);
JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
try {
jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] {}));
fail("Expected fault");
} catch (Fault fault) {
Message outMsg = ex.getOutMessage();
Assert.assertNotNull(outMsg);
@SuppressWarnings("unchecked") List<Header> headers = (List<Header>) outMsg.get(Header.HEADER_LIST);
Assert.assertEquals(1, headers.size());
Assert.assertEquals(TEST_HEADER_NAME, headers.get(0).getName());
}
}
Aggregations