package org.jboss.axis.attachments;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Message;
import org.jboss.axis.MessagePart;
import org.jboss.axis.Part;
import org.jboss.axis.utils.Messages;
import org.jboss.logging.Logger;
import javax.activation.DataHandler;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class AttachmentsImpl implements Attachments
{
private static Logger log = Logger.getLogger(AttachmentsImpl.class.getName());
private HashMap attachments = new java.util.HashMap();
private LinkedList orderedAttachments = new LinkedList();
protected MessagePart soapPart = null;
protected MultiPartInputStream mpartStream =
null;
protected int sendtype = Attachments.SEND_TYPE_NOTSET;
protected String contentLocation = null;
private HashMap stackDataHandler = new HashMap();
public AttachmentsImpl(Object intialContents, String contentType, String contentLocation)
throws org.jboss.axis.AxisFault
{
if (contentLocation != null)
{
contentLocation = contentLocation.trim();
if (contentLocation.length() == 0)
{
contentLocation = null;
}
}
this.contentLocation = contentLocation;
if (contentType != null)
{
if (contentType.equals(Message.MIME_UNKNOWN))
{
log.warn("Unknown contentType");
}
else
{
StringTokenizer st = new StringTokenizer(contentType, " \t;");
if (st.hasMoreTokens())
{
String mimetype = st.nextToken();
if (mimetype.equalsIgnoreCase(Message.MIME_MULTIPART_RELATED))
{
sendtype = SEND_TYPE_MIME;
mpartStream = new MultiPartRelatedInputStream(contentType, (InputStream)intialContents);
if (null == contentLocation)
{
contentLocation = mpartStream.getContentLocation();
if (contentLocation != null)
{
contentLocation = contentLocation.trim();
if (contentLocation.length() == 0)
{
contentLocation = null;
}
}
}
soapPart = new MessagePart(null, mpartStream, false);
}
else if (mimetype.equalsIgnoreCase(Message.MIME_APPLICATION_DIME))
{
try
{
mpartStream = new MultiPartDimeInputStream((InputStream)intialContents);
soapPart = new MessagePart(null, mpartStream, false);
}
catch (Exception e)
{
throw AxisFault.makeFault(e);
}
sendtype = SEND_TYPE_DIME;
}
}
}
}
}
private void mergeinAttachments() throws AxisFault
{
if (mpartStream != null)
{
Collection atts = mpartStream.getAttachments();
if (contentLocation == null)
contentLocation = mpartStream.getContentLocation();
mpartStream = null;
setAttachmentParts(atts);
}
}
public Part removeAttachmentPart(String reference)
throws org.jboss.axis.AxisFault
{
multipart = null;
dimemultipart = null;
mergeinAttachments();
Part removedPart = getAttachmentByReference(reference);
if (removedPart != null)
{
attachments.remove(removedPart.getContentId());
attachments.remove(removedPart.getContentLocation());
orderedAttachments.remove(removedPart);
}
return removedPart;
}
public Part addAttachmentPart(Part newPart)
throws org.jboss.axis.AxisFault
{
multipart = null;
dimemultipart = null;
mergeinAttachments();
Part oldPart = (Part)attachments.put(newPart.getContentId(), newPart);
if (oldPart != null)
{
orderedAttachments.remove(oldPart);
attachments.remove(oldPart.getContentLocation());
}
orderedAttachments.add(newPart);
if (newPart.getContentLocation() != null)
{
attachments.put(newPart.getContentLocation(), newPart);
}
return oldPart;
}
public Part createAttachmentPart(Object datahandler)
throws org.jboss.axis.AxisFault
{
Integer key = new Integer(datahandler.hashCode());
if (stackDataHandler.containsKey(key))
{
return (Part)stackDataHandler.get(key);
}
multipart = null;
dimemultipart = null;
mergeinAttachments();
if (!(datahandler instanceof javax.activation.DataHandler))
{
throw new org.jboss.axis.AxisFault(Messages.getMessage("unsupportedAttach", datahandler.getClass().getName(),
javax.activation.DataHandler.class.getName()));
}
Part ret =
new AttachmentPartImpl((javax.activation.DataHandler)datahandler);
addAttachmentPart(ret);
stackDataHandler.put(key, ret);
return ret;
}
public void setAttachmentParts(java.util.Collection parts)
throws org.jboss.axis.AxisFault
{
removeAllAttachments();
if ((parts != null) && !parts.isEmpty())
{
for (java.util.Iterator i = parts.iterator(); i.hasNext();)
{
Object part = i.next();
if (null != part)
{
if (part instanceof Part)
addAttachmentPart((Part)part);
else
createAttachmentPart(part);
}
}
}
}
public Part getAttachmentByReference(String reference)
throws org.jboss.axis.AxisFault
{
if (null == reference)
{
return null;
}
reference = reference.trim();
if (0 == reference.length())
{
return null;
}
mergeinAttachments();
Part ret = (Part)attachments.get(reference);
if (null != ret) return ret;
if (!reference.startsWith(Attachments.CIDprefix) && (null != contentLocation))
{
String fqreference = contentLocation;
if (!fqreference.endsWith("/"))
{
fqreference += "/";
}
if (reference.startsWith("/"))
{
fqreference += reference.substring(1);
}
else
{
fqreference += reference;
}
ret = (AttachmentPartImpl)attachments.get(fqreference);
}
if (null == ret && reference.startsWith(Attachments.CIDprefix))
{
ret = (Part)attachments.get(reference.substring(4));
}
return ret;
}
public java.util.Collection getAttachments()
throws org.jboss.axis.AxisFault
{
mergeinAttachments();
return new LinkedList(orderedAttachments);
}
public Part getRootPart()
{
return soapPart;
}
public void setRootPart(Part newRoot)
{
try
{
this.soapPart = (MessagePart)newRoot;
multipart = null;
dimemultipart = null;
}
catch (ClassCastException e)
{
throw new ClassCastException(Messages.getMessage("onlySOAPParts"));
}
}
javax.mail.internet.MimeMultipart multipart = null;
DimeMultiPart dimemultipart = null;
public long getContentLength() throws org.jboss.axis.AxisFault
{
mergeinAttachments();
int sendtype = this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT : this.sendtype;
try
{
if (sendtype == SEND_TYPE_MIME)
return org.jboss.axis.attachments.MimeUtils.getContentLength(multipart != null ? multipart : (multipart = org.jboss.axis.attachments.MimeUtils.createMP(soapPart.getAsString(), orderedAttachments)));
else if (sendtype == SEND_TYPE_DIME) return createDimeMessage().getTransmissionSize();
}
catch (Exception e)
{
throw AxisFault.makeFault(e);
}
return 0;
}
protected DimeMultiPart createDimeMessage() throws org.jboss.axis.AxisFault
{
int sendtype = this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT : this.sendtype;
if (sendtype == SEND_TYPE_DIME)
{
if (dimemultipart == null)
{
dimemultipart = new DimeMultiPart();
dimemultipart.addBodyPart(new DimeBodyPart(soapPart.getAsBytes(), DimeTypeNameFormat.URI,
"http://schemas.xmlsoap.org/soap/envelope/",
"uuid:714C6C40-4531-442E-A498-3AC614200295"));
for (java.util.Iterator i = orderedAttachments.iterator();
i.hasNext();)
{
AttachmentPartImpl part = (AttachmentPartImpl)i.next();
DataHandler dh = AttachmentUtils.
getActivationDataHandler(part);
dimemultipart.addBodyPart(new
DimeBodyPart(dh, part.getContentId()));
}
}
}
return dimemultipart;
}
public void writeContentToStream(java.io.OutputStream os)
throws org.jboss.axis.AxisFault
{
int sendtype = this.sendtype == SEND_TYPE_NOTSET ?
SEND_TYPE_DEFAULT : this.sendtype;
try
{
mergeinAttachments();
if (sendtype == SEND_TYPE_MIME)
{
org.jboss.axis.attachments.MimeUtils.writeToMultiPartStream(os,
(multipart != null) ? multipart : (multipart =
org.jboss.axis.attachments.MimeUtils.createMP(soapPart.getAsString(), orderedAttachments)));
}
else if (sendtype == SEND_TYPE_DIME) createDimeMessage().write(os);
}
catch (Exception e)
{
throw org.jboss.axis.AxisFault.makeFault(e);
}
}
public String getContentType() throws org.jboss.axis.AxisFault
{
mergeinAttachments();
int sendtype = this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT :
this.sendtype;
if (sendtype == SEND_TYPE_MIME)
return org.jboss.axis.attachments.MimeUtils.getContentType((multipart
!= null)
? multipart
: (multipart =
org.jboss.axis.attachments.MimeUtils.createMP(soapPart.getAsString(),
orderedAttachments)));
else
return org.jboss.axis.Message.MIME_APPLICATION_DIME;
}
public int getAttachmentCount()
{
try
{
mergeinAttachments();
soapPart.getAsString();
return orderedAttachments.size();
}
catch (AxisFault e)
{
log.warn(Messages.getMessage("exception00"), e);
}
return 0;
}
public boolean isAttachment(Object value)
{
return AttachmentUtils.isAttachment(value);
}
public void removeAllAttachments()
{
try
{
multipart = null;
dimemultipart = null;
mergeinAttachments();
attachments.clear();
orderedAttachments.clear();
stackDataHandler.clear();
}
catch (AxisFault af)
{
log.warn(Messages.getMessage("exception00"), af);
}
}
public java.util.Iterator getAttachments(javax.xml.soap.MimeHeaders headers)
{
java.util.Vector vecParts = new java.util.Vector();
java.util.Iterator iterator = GetAttachmentsIterator();
while (iterator.hasNext())
{
Part part = (Part)iterator.next();
if (part instanceof AttachmentPartImpl)
{
if (((AttachmentPartImpl)part).matches(headers))
{
vecParts.add(part);
}
}
}
return vecParts.iterator();
}
private java.util.Iterator GetAttachmentsIterator()
{
java.util.Iterator iterator = attachments.values().iterator();
return iterator;
}
public Part createAttachmentPart() throws org.jboss.axis.AxisFault
{
return new AttachmentPartImpl();
}
public void setSendType(int sendtype)
{
if (sendtype < 1)
throw new IllegalArgumentException("");
if (sendtype > SEND_TYPE_MAX)
throw new IllegalArgumentException("");
this.sendtype = sendtype;
}
public int getSendType()
{
return sendtype;
}
public void dispose()
{
java.util.Iterator iterator = GetAttachmentsIterator();
while (iterator.hasNext())
{
Part part = (Part)iterator.next();
if (part instanceof AttachmentPartImpl)
{
AttachmentPartImpl apart = (AttachmentPartImpl)part;
apart.dispose();
}
}
}
public static int getSendType(String value)
{
if (value.equalsIgnoreCase("MIME")) return SEND_TYPE_MIME;
if (value.equalsIgnoreCase("DIME")) return SEND_TYPE_DIME;
if (value.equalsIgnoreCase("NONE")) return SEND_TYPE_NONE;
return SEND_TYPE_NOTSET;
}
public static String getSendTypeString(int value)
{
if (value == SEND_TYPE_MIME)
{
return "MIME";
}
if (value == SEND_TYPE_DIME)
{
return "DIME";
}
if (value == SEND_TYPE_NONE)
{
return "NONE";
}
return null;
}
}