use of org.apache.cxf.interceptor.Fault 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.interceptor.Fault in project cxf by apache.
the class SoapFaultFactory method createFault.
public Fault createFault(JMSFault jmsFault) {
final Fault f;
if (version == Soap11.getInstance()) {
f = createSoap11Fault(jmsFault);
// so we can encode the SequenceFault as header
f.initCause(jmsFault);
} else {
f = createSoap12Fault(jmsFault);
}
return f;
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class SoapJMSInInterceptor method checkRequestURI.
/**
* @param message
* @param headers
*/
private void checkRequestURI(SoapMessage message, Map<String, List<String>> headers) {
List<String> ru = headers.get(SoapJMSConstants.REQUESTURI_FIELD);
JMSFault jmsFault = null;
if (ru != null && !ru.isEmpty()) {
String requestURI = ru.get(0);
List<String> mr = headers.get(SoapJMSConstants.MALFORMED_REQUESTURI);
if (mr != null && !mr.isEmpty() && "true".equals(mr.get(0))) {
jmsFault = JMSFaultFactory.createMalformedRequestURIFault(requestURI);
}
List<String> trn = headers.get(SoapJMSConstants.TARGET_SERVICE_IN_REQUESTURI);
if (trn != null && !trn.isEmpty() && "true".equals(trn.get(0))) {
jmsFault = JMSFaultFactory.createTargetServiceNotAllowedInRequestURIFault();
}
} else {
jmsFault = JMSFaultFactory.createMissingRequestURIFault();
}
if (jmsFault != null) {
Fault f = createFault(message, jmsFault);
if (f != null) {
throw f;
}
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class JAXBEncoderDecoder method unmarshallException.
public static Exception unmarshallException(Unmarshaller u, Object source, MessagePartInfo part) {
XMLStreamReader reader;
if (source instanceof XMLStreamReader) {
reader = (XMLStreamReader) source;
} else if (source instanceof Element) {
reader = StaxUtils.createXMLStreamReader((Element) source);
try {
// advance into the node
reader.nextTag();
} catch (XMLStreamException e) {
// ignore
}
} else {
throw new Fault(new Message("UNKNOWN_SOURCE", LOG, source.getClass().getName()));
}
try {
QName qn = part.getElementQName();
if (!qn.equals(reader.getName())) {
throw new Fault(new Message("ELEMENT_NAME_MISMATCH", LOG, qn, reader.getName()));
}
Class<?> cls = part.getTypeClass();
Object obj;
try {
Constructor<?> cons = cls.getConstructor();
obj = cons.newInstance();
} catch (NoSuchMethodException nse) {
Constructor<?> cons = cls.getConstructor(new Class[] { String.class });
obj = cons.newInstance(new Object[1]);
}
XmlAccessType accessType = Utils.getXmlAccessType(cls);
reader.nextTag();
while (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
QName q = reader.getName();
String fieldName = q.getLocalPart();
Field f = Utils.getField(cls, accessType, fieldName);
if (f != null) {
Type type = f.getGenericType();
ReflectionUtil.setAccessible(f);
if (JAXBSchemaInitializer.isArray(type)) {
Class<?> compType = JAXBSchemaInitializer.getArrayComponentType(type);
List<Object> ret = unmarshallArray(u, reader, q, compType, createList(type));
Object o = ret;
if (!isList(type)) {
if (compType.isPrimitive()) {
o = java.lang.reflect.Array.newInstance(compType, ret.size());
for (int x = 0; x < ret.size(); x++) {
Array.set(o, x, ret.get(x));
}
} else {
o = ret.toArray((Object[]) Array.newInstance(compType, ret.size()));
}
}
f.set(obj, o);
} else {
Object o = getElementValue(u.unmarshal(reader, Utils.getFieldType(f)));
Utils.setFieldValue(f, obj, o);
}
} else {
String s = StringUtils.capitalize(q.getLocalPart());
Method m = Utils.getMethod(cls, accessType, "get" + s);
if (m == null) {
m = Utils.getMethod(cls, accessType, "is" + s);
}
Type type = m.getGenericReturnType();
Object o;
if (JAXBSchemaInitializer.isArray(type)) {
Class<?> compType = JAXBSchemaInitializer.getArrayComponentType(type);
List<Object> ret = unmarshallArray(u, reader, q, compType, createList(type));
o = ret;
if (!isList(type)) {
if (compType.isPrimitive()) {
o = java.lang.reflect.Array.newInstance(compType, ret.size());
for (int x = 0; x < ret.size(); x++) {
Array.set(o, x, ret.get(x));
}
} else {
o = ret.toArray((Object[]) Array.newInstance(compType, ret.size()));
}
}
} else {
o = getElementValue(u.unmarshal(reader, Utils.getMethodReturnType(m)));
}
Method m2 = Utils.getMethod(cls, accessType, "set" + s, m.getReturnType());
if (m2 != null) {
if (JAXBSchemaInitializer.isArray(type)) {
m2.invoke(obj, o);
} else {
Utils.setMethodValue(m, m2, obj, o);
}
} else {
Field fn = ReflectionUtil.getDeclaredField(cls, q.getLocalPart());
if (fn != null) {
ReflectionUtil.setAccessible(fn);
fn.set(obj, o);
}
}
}
if (reader.getEventType() == XMLStreamConstants.END_ELEMENT && q.equals(reader.getName())) {
reader.next();
}
}
return (Exception) obj;
} catch (Exception e) {
throw new Fault(new Message("MARSHAL_ERROR", LOG, e.getMessage()), e);
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class JAXBEncoderDecoder method marshallException.
public static void marshallException(Marshaller marshaller, Exception elValue, MessagePartInfo part, Object source) {
XMLStreamWriter writer = getStreamWriter(source);
QName qn = part.getElementQName();
try {
writer.writeStartElement("ns1", qn.getLocalPart(), qn.getNamespaceURI());
Class<?> cls = part.getTypeClass();
XmlAccessType accessType = Utils.getXmlAccessType(cls);
String namespace = part.getElementQName().getNamespaceURI();
String attNs = namespace;
SchemaInfo sch = part.getMessageInfo().getOperation().getInterface().getService().getSchema(namespace);
if (sch == null) {
LOG.warning("Schema associated with " + namespace + " is null");
namespace = null;
attNs = null;
} else {
if (!sch.isElementFormQualified()) {
namespace = null;
}
if (!sch.isAttributeFormQualified()) {
attNs = null;
}
}
List<Member> combinedMembers = new ArrayList<>();
for (Field f : Utils.getFields(cls, accessType)) {
XmlAttribute at = f.getAnnotation(XmlAttribute.class);
if (at == null) {
combinedMembers.add(f);
} else {
QName fname = new QName(attNs, StringUtils.isEmpty(at.name()) ? f.getName() : at.name());
ReflectionUtil.setAccessible(f);
Object o = Utils.getFieldValue(f, elValue);
DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
writeObject(marshaller, frag, newJAXBElement(fname, String.class, o));
if (attNs != null) {
writer.writeAttribute(attNs, fname.getLocalPart(), DOMUtils.getAllContent(frag));
} else {
writer.writeAttribute(fname.getLocalPart(), DOMUtils.getAllContent(frag));
}
}
}
for (Method m : Utils.getGetters(cls, accessType)) {
if (!m.isAnnotationPresent(XmlAttribute.class)) {
combinedMembers.add(m);
} else {
int idx = m.getName().startsWith("get") ? 3 : 2;
String name = m.getName().substring(idx);
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
XmlAttribute at = m.getAnnotation(XmlAttribute.class);
QName mname = new QName(namespace, StringUtils.isEmpty(at.name()) ? name : at.name());
DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
Object o = Utils.getMethodValue(m, elValue);
writeObject(marshaller, frag, newJAXBElement(mname, String.class, o));
if (attNs != null) {
writer.writeAttribute(attNs, mname.getLocalPart(), DOMUtils.getAllContent(frag));
} else {
writer.writeAttribute(mname.getLocalPart(), DOMUtils.getAllContent(frag));
}
}
}
XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL)) {
Collections.sort(combinedMembers, new Comparator<Member>() {
public int compare(Member m1, Member m2) {
return m1.getName().compareTo(m2.getName());
}
});
}
XmlType xmlType = cls.getAnnotation(XmlType.class);
if (xmlType != null && xmlType.propOrder().length > 1 && !xmlType.propOrder()[0].isEmpty()) {
final List<String> orderList = Arrays.asList(xmlType.propOrder());
Collections.sort(combinedMembers, new Comparator<Member>() {
public int compare(Member m1, Member m2) {
String m1Name = getName(m1);
String m2Name = getName(m2);
int m1Index = orderList.indexOf(m1Name);
int m2Index = orderList.indexOf(m2Name);
if (m1Index != -1 && m2Index != -1) {
return m1Index - m2Index;
}
if (m1Index == -1 && m2Index != -1) {
return 1;
}
if (m1Index != -1 && m2Index == -1) {
return -1;
}
return 0;
}
});
}
for (Member member : combinedMembers) {
if (member instanceof Field) {
Field f = (Field) member;
QName fname = new QName(namespace, f.getName());
ReflectionUtil.setAccessible(f);
if (JAXBSchemaInitializer.isArray(f.getGenericType())) {
writeArrayObject(marshaller, writer, fname, f.get(elValue));
} else {
Object o = Utils.getFieldValue(f, elValue);
writeObject(marshaller, writer, newJAXBElement(fname, String.class, o));
}
} else {
// it's a Method
Method m = (Method) member;
int idx = m.getName().startsWith("get") ? 3 : 2;
String name = m.getName().substring(idx);
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
QName mname = new QName(namespace, name);
if (JAXBSchemaInitializer.isArray(m.getGenericReturnType())) {
writeArrayObject(marshaller, writer, mname, m.invoke(elValue));
} else {
Object o = Utils.getMethodValue(m, elValue);
writeObject(marshaller, writer, newJAXBElement(mname, String.class, o));
}
}
}
writer.writeEndElement();
writer.flush();
} catch (Exception e) {
throw new Fault(new Message("MARSHAL_ERROR", LOG, e.getMessage()), e);
} finally {
StaxUtils.close(writer);
}
}
Aggregations