use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class JAXBEncoderDecoderTest method testUnMarshall.
@Test
public void testUnMarshall() throws Exception {
// Hello World Wsdl generated namespace
QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
part.setTypeClass(Class.forName(wrapperAnnotation.className()));
Document doc = DOMUtils.getEmptyDocument();
Element elNode = doc.createElementNS(elName.getNamespaceURI(), elName.getLocalPart());
Element rtEl = doc.createElementNS(elName.getNamespaceURI(), "requestType");
elNode.appendChild(rtEl);
rtEl.appendChild(doc.createTextNode("Hello Test"));
Object obj = JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), elNode, part, true);
assertNotNull(obj);
// Add a Node and then test
assertEquals(GreetMe.class, obj.getClass());
assertEquals("Hello Test", ((GreetMe) obj).getRequestType());
part.setTypeClass(String.class);
Node n = null;
try {
JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), n, part, true);
fail("Should have received a Fault");
} catch (Fault pe) {
// Expected Exception
} catch (Exception ex) {
fail("Should have received a Fault, not: " + ex);
}
// Now test schema validation during unmarshaling
elName = new QName(wrapperAnnotation.targetNamespace(), "stringStruct");
// Create an XML Tree of
// <StringStruct><arg1>World</arg1></StringStruct>
// elNode = soapElFactory.createElement(elName);
// elNode.addNamespaceDeclaration("", elName.getNamespaceURI());
part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
part.setTypeClass(Class.forName("org.apache.hello_world_soap_http.types.StringStruct"));
doc = DOMUtils.getEmptyDocument();
elNode = doc.createElementNS(elName.getNamespaceURI(), elName.getLocalPart());
rtEl = doc.createElementNS(elName.getNamespaceURI(), "arg1");
elNode.appendChild(rtEl);
rtEl.appendChild(doc.createTextNode("World"));
// Should unmarshal without problems when no schema used.
obj = JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), elNode, part, true);
assertNotNull(obj);
assertEquals(StringStruct.class, obj.getClass());
assertEquals("World", ((StringStruct) obj).getArg1());
try {
// unmarshal with schema should raise exception.
Unmarshaller m = context.createUnmarshaller();
m.setSchema(schema);
JAXBEncoderDecoder.unmarshall(m, elNode, part, true);
fail("Should have thrown a Fault");
} catch (Fault ex) {
// expected - schema validation should fail.
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class JAXBAttachmentUnmarshaller method getAttachmentAsByteArray.
@Override
public byte[] getAttachmentAsByteArray(String contentId) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
InputStream is = AttachmentUtil.getAttachmentDataSource(contentId, attachments).getInputStream();
IOUtils.copy(is, bos);
is.close();
bos.close();
} catch (IOException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_READ_ERROR", LOG), e);
}
return bos.toByteArray();
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class DataReaderImpl method createUnmarshaller.
private Unmarshaller createUnmarshaller() {
try {
Unmarshaller um = context.createUnmarshaller();
if (databinding.getUnmarshallerListener() != null) {
um.setListener(databinding.getUnmarshallerListener());
}
if (setEventHandler) {
um.setEventHandler(new WSUIDValidationHandler(veventHandler));
}
if (databinding.getUnmarshallerProperties() != null) {
for (Map.Entry<String, Object> propEntry : databinding.getUnmarshallerProperties().entrySet()) {
try {
um.setProperty(propEntry.getKey(), propEntry.getValue());
} catch (PropertyException pe) {
LOG.log(Level.INFO, "PropertyException setting Marshaller properties", pe);
}
}
}
um.setSchema(schema);
um.setAttachmentUnmarshaller(getAttachmentUnmarshaller());
for (XmlAdapter<?, ?> adapter : databinding.getConfiguredXmlAdapters()) {
um.setAdapter(adapter);
}
return um;
} catch (javax.xml.bind.UnmarshalException ex) {
throw new Fault(new Message("UNMARSHAL_ERROR", LOG, ex.getLinkedException().getMessage()), ex);
} catch (JAXBException ex) {
throw new Fault(new Message("UNMARSHAL_ERROR", LOG, ex.getMessage()), ex);
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class HolderOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
MessageContentsList outObjects = MessageContentsList.getContentsList(message);
Exchange exchange = message.getExchange();
OperationInfo op = exchange.getBindingOperationInfo() == null ? null : exchange.getBindingOperationInfo().getOperationInfo();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("op: " + op);
if (null != op) {
LOG.fine("op.hasOutput(): " + op.hasOutput());
if (op.hasOutput()) {
LOG.fine("op.getOutput().size(): " + op.getOutput().size());
}
}
}
if (op == null || !op.hasOutput() || op.getOutput().size() == 0) {
LOG.fine("Returning.");
return;
}
if (!Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
List<MessagePartInfo> parts = op.getOutput().getMessageParts();
MessageContentsList inObjects = MessageContentsList.getContentsList(exchange.getInMessage());
if (inObjects != null) {
if (inObjects != outObjects) {
for (int x = 0; x < inObjects.size(); x++) {
Object o = inObjects.get(x);
if (o instanceof Holder) {
outObjects.set(x + 1, o);
}
}
} else {
LOG.severe("CANNOT_SET_HOLDER_OBJECTS");
throw new Fault(new org.apache.cxf.common.i18n.Message("CANNOT_SET_HOLDER_OBJECTS", LOG));
}
}
for (MessagePartInfo part : parts) {
if (part.getIndex() > 0 && part.getTypeClass() != null) {
Holder<?> holder = (Holder<?>) outObjects.get(part);
outObjects.put(part, holder.value);
}
}
} else {
List<Object> holders = new ArrayList<>(outObjects);
for (int x = 0; x < outObjects.size(); x++) {
Object o = outObjects.get(x);
if (o instanceof Holder) {
outObjects.set(x, ((Holder<?>) o).value);
} else {
holders.set(x, null);
}
}
message.put(HolderInInterceptor.CLIENT_HOLDERS, holders);
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class MessageModeOutInterceptor method validatePossibleFault.
private void validatePossibleFault(Message message, BindingOperationInfo bop, Node ds) {
Element el = DOMUtils.getFirstElement(ds);
if (!"Fault".equals(el.getLocalName())) {
return;
}
message.put(Message.RESPONSE_CODE, 500);
el = DOMUtils.getFirstElement(el);
while (el != null && !"detail".equals(el.getLocalName())) {
el = DOMUtils.getNextElement(el);
}
if (el != null) {
Schema schema = EndpointReferenceUtils.getSchema(message.getExchange().getService().getServiceInfos().get(0), message.getExchange().getBus());
try {
validateFaultDetail(el, schema, bop);
} catch (Exception e) {
throw new Fault(e);
}
// We validated what we can from a fault standpoint
message.put(Message.SCHEMA_VALIDATION_ENABLED, Boolean.FALSE);
}
}
Aggregations