use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class CorbaStreamFaultOutInterceptor method setUserException.
protected void setUserException(CorbaMessage message, Throwable ex, RaisesType exType, OperationInfo opInfo, DataWriter<XMLStreamWriter> writer, ServiceInfo service) throws Exception {
QName exIdlType = exType.getException();
QName elName = new QName("", exIdlType.getLocalPart());
MessagePartInfo faultPart = getFaultMessagePartInfo(opInfo, elName);
if (faultPart == null) {
throw new CorbaBindingException("Coulnd't find the message fault part : " + elName);
}
Method faultMethod = ex.getClass().getMethod("getFaultInfo");
if (faultMethod == null) {
return;
}
Object fault = faultMethod.invoke(ex);
// one has not been created on the servant side which throws the UserException.
if (fault == null) {
Class<?> faultClass = faultMethod.getReturnType();
fault = faultClass.newInstance();
}
CorbaFaultStreamWriter faultWriter = new CorbaFaultStreamWriter(orb, exType, message.getCorbaTypeMap(), service);
writer.write(fault, faultPart, faultWriter);
CorbaObjectHandler[] objs = faultWriter.getCorbaObjects();
CorbaStreamable streamable = message.createStreamableObject(objs[0], elName);
message.setStreamableException(streamable);
}
use of org.apache.cxf.service.model.MessagePartInfo 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.service.model.MessagePartInfo in project cxf by apache.
the class SwAInInterceptor method handleMessage.
public void handleMessage(SoapMessage message) throws Fault {
BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
if (bop == null) {
return;
}
if (bop.isUnwrapped()) {
bop = bop.getWrappedOperation();
}
boolean client = isRequestor(message);
BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
if (bmi == null) {
return;
}
SoapBodyInfo sbi = bmi.getExtensor(SoapBodyInfo.class);
if (sbi == null || sbi.getAttachments() == null || sbi.getAttachments().isEmpty()) {
return;
}
Set<Integer> foundAtts = new HashSet<>();
MessageContentsList inObjects = MessageContentsList.getContentsList(message);
for (MessagePartInfo mpi : sbi.getAttachments()) {
String partName = mpi.getConcreteName().getLocalPart();
String start = partName + "=";
boolean found = false;
if (foundAtts.contains(mpi.getIndex())) {
continue;
}
foundAtts.add(mpi.getIndex());
for (Attachment a : message.getAttachments()) {
if (a.getId().startsWith(start)) {
DataHandler dh = a.getDataHandler();
String ct = dh.getContentType();
Object o = null;
Class<?> typeClass = mpi.getTypeClass();
if (DataHandler.class.isAssignableFrom(typeClass)) {
o = dh;
} else if (String.class.isAssignableFrom(typeClass)) {
try {
// o = IOUtils.readBytesFromStream(dh.getInputStream());
o = dh.getContent();
} catch (IOException e) {
throw new Fault(e);
}
} else if (byte[].class.isAssignableFrom(typeClass)) {
try {
o = IOUtils.readBytesFromStream(dh.getInputStream());
} catch (IOException e) {
throw new Fault(e);
}
} else if (ct.startsWith("image/")) {
try {
o = ImageIO.read(dh.getInputStream());
} catch (IOException e) {
throw new Fault(e);
}
} else if (ct.startsWith("text/xml") || ct.startsWith("application/xml")) {
try {
o = new StreamSource(dh.getInputStream());
} catch (IOException e) {
throw new Fault(e);
}
} else {
o = dh;
}
inObjects.put(mpi, o);
found = true;
break;
}
}
if (!found) {
inObjects.put(mpi, null);
}
}
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class SwAOutInterceptor method processAttachments.
protected void processAttachments(SoapMessage message, SoapBodyInfo sbi) {
Collection<Attachment> atts = setupAttachmentOutput(message);
List<Object> outObjects = CastUtils.cast(message.getContent(List.class));
for (MessagePartInfo mpi : sbi.getAttachments()) {
String partName = mpi.getConcreteName().getLocalPart();
String ct = (String) mpi.getProperty(Message.CONTENT_TYPE);
String id = new StringBuilder().append(partName).append("=").append(UUID.randomUUID()).append("@apache.org").toString();
// this assumes things are in order...
int idx = mpi.getIndex();
Object o = outObjects.get(idx);
if (o == null) {
continue;
}
outObjects.set(idx, null);
DataHandler dh = null;
// This code could probably be refactored out somewhere...
if (o instanceof Source) {
dh = new DataHandler(createDataSource((Source) o, ct));
} else if (o instanceof Image) {
final Image img = (Image) o;
final String contentType = ct;
dh = new DataHandler(o, ct) {
@Override
public InputStream getInputStream() throws IOException {
LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
writeTo(bout);
return bout.createInputStream();
}
@Override
public void writeTo(OutputStream out) throws IOException {
ImageWriter writer = null;
Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(contentType);
if (writers.hasNext()) {
writer = writers.next();
}
if (writer != null) {
BufferedImage bimg = convertToBufferedImage(img);
ImageOutputStream iout = ImageIO.createImageOutputStream(out);
writer.setOutput(iout);
writer.write(bimg);
writer.dispose();
iout.flush();
out.flush();
}
}
};
} else if (o instanceof DataHandler) {
dh = (DataHandler) o;
ct = dh.getContentType();
try {
if ("text/xml".equals(ct) && dh.getContent() instanceof Source) {
dh = new DataHandler(createDataSource((Source) dh.getContent(), ct));
}
} catch (IOException e) {
// ignore, use same dh
}
} else if (o instanceof byte[]) {
if (ct == null) {
ct = "application/octet-stream";
}
dh = new DataHandler(new ByteDataSource((byte[]) o, ct));
} else if (o instanceof String) {
if (ct == null) {
ct = "text/plain; charset=\'UTF-8\'";
}
dh = new DataHandler(new ByteDataSource(((String) o).getBytes(StandardCharsets.UTF_8), ct));
} else {
throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_NOT_SUPPORTED", LOG, o.getClass()));
}
AttachmentImpl att = new AttachmentImpl(id);
att.setDataHandler(dh);
att.setHeader("Content-Type", ct);
att.setHeader("Content-ID", "<" + id + ">");
atts.add(att);
}
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class WebFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Fault f = (Fault) message.getContent(Exception.class);
if (f == null) {
return;
}
try {
Throwable thr = f.getCause();
SOAPFaultException sf = null;
if (thr instanceof SOAPFaultException) {
sf = (SOAPFaultException) thr;
} else if (thr.getCause() instanceof SOAPFaultException) {
sf = (SOAPFaultException) thr.getCause();
}
if (sf != null) {
SoapVersion soapVersion = (SoapVersion) message.get(SoapVersion.class.getName());
if (soapVersion != null && soapVersion.getVersion() != 1.1) {
if (f instanceof SoapFault) {
for (Iterator<QName> it = CastUtils.cast(sf.getFault().getFaultSubcodes()); it.hasNext(); ) {
((SoapFault) f).addSubCode(it.next());
}
}
if (sf.getFault().getFaultReasonLocales().hasNext()) {
Locale lang = (Locale) sf.getFault().getFaultReasonLocales().next();
String convertedLang = lang.getLanguage();
String country = lang.getCountry();
if (country.length() > 0) {
convertedLang = convertedLang + '-' + country;
}
f.setLang(convertedLang);
}
}
message.setContent(Exception.class, f);
}
} catch (Exception e) {
// do nothing;
}
Throwable cause = f.getCause();
WebFault fault = null;
if (cause != null) {
fault = getWebFaultAnnotation(cause.getClass());
if (fault == null && cause.getCause() != null) {
fault = getWebFaultAnnotation(cause.getCause().getClass());
if (fault != null || cause instanceof RuntimeException) {
cause = cause.getCause();
}
}
}
if (cause instanceof Exception && fault != null) {
Exception ex = (Exception) cause;
Object faultInfo = null;
try {
Method method = cause.getClass().getMethod("getFaultInfo", new Class[0]);
faultInfo = method.invoke(cause, new Object[0]);
} catch (NoSuchMethodException e) {
faultInfo = createFaultInfoBean(fault, cause);
} catch (InvocationTargetException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("INVOCATION_TARGET_EXC", BUNDLE), e);
} catch (IllegalAccessException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
} catch (IllegalArgumentException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
}
Service service = message.getExchange().getService();
try {
DataWriter<XMLStreamWriter> writer = service.getDataBinding().createWriter(XMLStreamWriter.class);
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message)) {
Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
writer.setSchema(schema);
}
OperationInfo op = message.getExchange().getBindingOperationInfo().getOperationInfo();
QName faultName = getFaultName(fault, cause.getClass(), op);
MessagePartInfo part = getFaultMessagePart(faultName, op);
if (f.hasDetails()) {
writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getDetail()));
} else {
writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getOrCreateDetail()));
if (!f.getDetail().hasChildNodes()) {
f.setDetail(null);
}
}
f.setMessage(ex.getMessage());
} catch (Exception nex) {
if (nex instanceof Fault) {
message.setContent(Exception.class, nex);
super.handleMessage(message);
} else {
// if exception occurs while writing a fault, we'll just let things continue
// and let the rest of the chain try handling it as is.
LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", nex);
}
}
} else {
FaultMode mode = message.get(FaultMode.class);
if (mode == FaultMode.CHECKED_APPLICATION_FAULT) {
// only convert checked exceptions with this
// otherwise delegate down to the normal protocol specific stuff
super.handleMessage(message);
}
}
}
Aggregations