001: /*
002: * $Id: CxfMessageAdapter.java 11405 2008-03-18 00:13:00Z dirk.olmes $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.cxf;
012:
013: import org.mule.api.DefaultMuleException;
014: import org.mule.api.MuleException;
015: import org.mule.transport.AbstractMessageAdapter;
016: import org.mule.transport.cxf.i18n.CxfMessages;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.List;
021:
022: import javax.activation.DataHandler;
023:
024: import org.apache.cxf.attachment.AttachmentImpl;
025: import org.apache.cxf.helpers.CastUtils;
026: import org.apache.cxf.message.AbstractWrappedMessage;
027: import org.apache.cxf.message.Attachment;
028: import org.apache.cxf.message.Message;
029:
030: /**
031: *
032: */
033: public class CxfMessageAdapter extends AbstractMessageAdapter {
034: /**
035: * Serial version
036: */
037: private static final long serialVersionUID = -1L;
038:
039: private final Message payload;
040:
041: public CxfMessageAdapter(Message message) throws MuleException {
042: if (message == null) {
043: throw new DefaultMuleException(CxfMessages
044: .unableToConstructAdapterForNullMessage());
045: }
046: this .payload = message;
047: }
048:
049: /**
050: * @return the current payload
051: */
052: public Object getPayload() {
053: List<Object> objs = CastUtils.cast(payload
054: .getContent(List.class));
055:
056: if (objs == null) {
057: // Seems Providers get objects stored this way
058: Object o = payload.getContent(Object.class);
059: if (o != null) {
060: return o;
061: } else {
062: return new Object[0];
063: }
064: }
065:
066: return objs.toArray();
067: }
068:
069: public void addAttachment(String name, DataHandler dataHandler)
070: throws Exception {
071: Collection<Attachment> attachments = getAttachments();
072: AttachmentImpl newA = new AttachmentImpl(name);
073: newA.setDataHandler(dataHandler);
074: attachments.add(newA);
075: }
076:
077: public void removeAttachment(String name) throws Exception {
078: Collection<Attachment> attachments = getAttachments();
079: List<Attachment> newAttachments = new ArrayList<Attachment>();
080: for (Attachment attachment : attachments) {
081: // @TODO: Get some clarify around expected contract, e.g., is <null> an
082: // ID? Ever?
083: if (attachment.getId() != null
084: && attachment.getId().equals(name)) {
085: continue;
086: }
087: newAttachments.add(attachment);
088: }
089: payload.setAttachments(newAttachments);
090: }
091:
092: protected Collection<Attachment> getAttachments()
093: throws MuleException {
094: if (payload instanceof AbstractWrappedMessage) {
095: AbstractWrappedMessage soap = (AbstractWrappedMessage) payload;
096: return soap.getAttachments();
097: } else {
098: // @TODO: Maybe pass the connector down and use connector exception
099: // instead?
100: throw new DefaultMuleException(CxfMessages
101: .inappropriateMessageTypeForAttachments(payload
102: .getClass().getName()));
103: }
104: }
105: }
|