use of org.apache.handlers.types.ObjectFactory in project cxf by apache.
the class LogicalMessageImplTest method testGetPayloadOfJAXB.
@Test
public void testGetPayloadOfJAXB() throws Exception {
// using Dispatch
JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
Message message = new MessageImpl();
Exchange e = new ExchangeImpl();
message.setExchange(e);
LogicalMessageContextImpl lmci = new LogicalMessageContextImpl(message);
JAXBElement<AddNumbers> el = new ObjectFactory().createAddNumbers(req);
LogicalMessageImpl lmi = new LogicalMessageImpl(lmci);
lmi.setPayload(el, ctx);
Object obj = lmi.getPayload(ctx);
assertTrue(obj instanceof JAXBElement);
JAXBElement<?> el2 = (JAXBElement<?>) obj;
assertTrue(el2.getValue() instanceof AddNumbers);
AddNumbers resp = (AddNumbers) el2.getValue();
assertEquals(req.getArg0(), resp.getArg0());
assertEquals(req.getArg1(), resp.getArg1());
}
use of org.apache.handlers.types.ObjectFactory in project cxf by apache.
the class HandlerInvocationUsingAddNumbersTest method testInvokeFromDispatchWithJAXBPayload.
@Test
public void testInvokeFromDispatchWithJAXBPayload() throws Exception {
URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
assertNotNull(wsdl);
AddNumbersService service = new AddNumbersService(wsdl, serviceName);
assertNotNull(service);
JAXBContext jc = JAXBContext.newInstance("org.apache.handlers.types");
Dispatch<Object> disp = service.createDispatch(portName, jc, Service.Mode.PAYLOAD);
setAddress(disp, addNumbersAddress);
SmallNumberHandler sh = new SmallNumberHandler();
TestSOAPHandler soapHandler = new TestSOAPHandler(false) {
public boolean handleMessage(SOAPMessageContext ctx) {
super.handleMessage(ctx);
Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
try {
SOAPMessage msg = ctx.getMessage();
// System.out.println("aaaaaaaaaaaa");
// msg.writeTo(System.out);
assertNotNull(msg);
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
}
return true;
}
};
addHandlersProgrammatically(disp, sh, soapHandler);
org.apache.handlers.types.AddNumbers req = new org.apache.handlers.types.AddNumbers();
req.setArg0(10);
req.setArg1(20);
ObjectFactory factory = new ObjectFactory();
JAXBElement<org.apache.handlers.types.AddNumbers> e = factory.createAddNumbers(req);
JAXBElement<?> response = (JAXBElement<?>) disp.invoke(e);
assertNotNull(response);
AddNumbersResponse value = (AddNumbersResponse) response.getValue();
assertEquals(200, value.getReturn());
}
use of org.apache.handlers.types.ObjectFactory in project cxf by apache.
the class SmallNumberHandler method handleMessage.
public final boolean handleMessage(LogicalMessageContext messageContext) {
try {
boolean outbound = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
// get the LogicalMessage from our context
LogicalMessage msg = messageContext.getMessage();
// check the payload, if its an AddNumbers request, we'll intervene
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Object payload = msg.getPayload(jaxbContext);
if (payload instanceof JAXBElement) {
payload = ((JAXBElement<?>) payload).getValue();
}
if (payload instanceof AddNumbers) {
AddNumbers req = (AddNumbers) payload;
// now, if the arguments are small, let's do the calculation here
int a = req.getArg0();
int b = req.getArg1();
if (isSmall(a) && isSmall(b)) {
int answer = a + b;
// System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
// ok, we've done the calculation, so build the
// response and set it as the payload of the message
AddNumbersResponse resp = new AddNumbersResponse();
resp.setReturn(answer);
msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp), jaxbContext);
Source src = msg.getPayload();
msg.setPayload(src);
payload = msg.getPayload(jaxbContext);
if (payload instanceof JAXBElement) {
payload = ((JAXBElement<?>) payload).getValue();
}
AddNumbersResponse resp2 = (AddNumbersResponse) payload;
if (resp2 == resp) {
throw new WebServiceException("Shouldn't be the same object");
}
// returned to the client
return false;
}
}
}
return true;
} catch (JAXBException ex) {
throw new ProtocolException(ex);
}
}
use of org.apache.handlers.types.ObjectFactory in project cxf by apache.
the class SmallNumberHandler method handleMessage.
// Implementation of javax.xml.ws.handler.Handler
public final boolean handleMessage(LogicalMessageContext messageContext) {
System.out.println("LogicalMessageHandler handleMessage called");
try {
boolean outbound = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
// get the LogicalMessage from our context
//
LogicalMessage msg = messageContext.getMessage();
// check the payload, if its an AddNumbers request, we'll intervene
//
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Object payload = msg.getPayload(jaxbContext);
if (payload instanceof JAXBElement) {
payload = ((JAXBElement) payload).getValue();
}
if (payload instanceof AddNumbers) {
AddNumbers req = (AddNumbers) payload;
// now, if the arguments are small, let's do the calculation here
//
int a = req.getArg0();
int b = req.getArg1();
if (isSmall(a) && isSmall(b)) {
int answer = a + b;
// System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
// ok, we've done the calculation, so build the
// response and set it as the payload of the message
AddNumbersResponse resp = new AddNumbersResponse();
resp.setReturn(answer);
msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp), jaxbContext);
Source src = msg.getPayload();
msg.setPayload(src);
payload = msg.getPayload(jaxbContext);
if (payload instanceof JAXBElement) {
payload = ((JAXBElement) payload).getValue();
}
AddNumbersResponse resp2 = (AddNumbersResponse) payload;
if (resp2 == resp) {
throw new WebServiceException("Shouldn't be the same object");
}
// returned to the client
return false;
}
}
}
return true;
} catch (JAXBException ex) {
throw new ProtocolException(ex);
}
}
use of org.apache.handlers.types.ObjectFactory in project cxf by apache.
the class DispatchHandlerInvocationTest method testInvokeWithJAXBPayloadMode.
@Test
public void testInvokeWithJAXBPayloadMode() throws Exception {
URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
assertNotNull(wsdl);
AddNumbersService service = new AddNumbersService(wsdl, serviceName);
assertNotNull(service);
JAXBContext jc = JAXBContext.newInstance("org.apache.handlers.types");
Dispatch<Object> disp = service.createDispatch(portName, jc, Service.Mode.PAYLOAD);
setAddress(disp, addNumbersAddress);
TestHandler handler = new TestHandler();
TestSOAPHandler soapHandler = new TestSOAPHandler();
addHandlersProgrammatically(disp, handler, soapHandler);
org.apache.handlers.types.AddNumbers req = new org.apache.handlers.types.AddNumbers();
req.setArg0(10);
req.setArg1(20);
ObjectFactory factory = new ObjectFactory();
JAXBElement<AddNumbers> e = factory.createAddNumbers(req);
JAXBElement<?> response = (JAXBElement<?>) disp.invoke(e);
assertNotNull(response);
AddNumbersResponse value = (AddNumbersResponse) response.getValue();
assertEquals(222, value.getReturn());
}
Aggregations