use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class XMLStreamDataWriterTest method testWriteRPCLit2.
@Test
public void testWriteRPCLit2() throws Exception {
JAXBDataBinding db = getTestWriterFactory(MyComplexStruct.class);
DataWriter<XMLStreamWriter> dw = db.createWriter(XMLStreamWriter.class);
assertNotNull(dw);
MyComplexStruct val = new MyComplexStruct();
val.setElem1("This is element 1");
val.setElem2("This is element 2");
val.setElem3(1);
QName elName = new QName("http://apache.org/hello_world_rpclit/types", "in");
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
dw.write(val, part, streamWriter);
streamWriter.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
StaxUtils.toNextElement(reader);
assertEquals(new QName("http://apache.org/hello_world_rpclit/types", "in"), reader.getName());
StaxUtils.nextEvent(reader);
StaxUtils.toNextElement(reader);
assertEquals(new QName("http://apache.org/hello_world_rpclit/types", "elem1"), reader.getName());
StaxUtils.nextEvent(reader);
StaxUtils.toNextText(reader);
assertEquals("This is element 1", reader.getText());
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class XMLStreamDataReaderTest method testReadBare.
@Test
public void testReadBare() throws Exception {
JAXBDataBinding db = getDataBinding(TradePriceData.class);
reader = getTestReader("../resources/sayHiDocLitBareReq.xml");
assertNotNull(reader);
DataReader<XMLStreamReader> dr = db.createReader(XMLStreamReader.class);
assertNotNull(dr);
QName elName = new QName("http://apache.org/hello_world_doc_lit_bare/types", "inout");
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
part.setTypeClass(TradePriceData.class);
Object val = dr.read(part, reader);
assertNotNull(val);
assertTrue(val instanceof TradePriceData);
assertEquals("CXF", ((TradePriceData) val).getTickerSymbol());
assertEquals(Float.valueOf(1.0f), new Float(((TradePriceData) val).getTickerPrice()));
}
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 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;
// 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 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();
final Object o;
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);
}
}
}
Aggregations