Methods Summary |
---|
public void | addMimeHeader(java.lang.String header, java.lang.String value)Add the specified MIME header, as per JAXM.
mimeHeaders.addHeader(header, value);
|
public void | clearContent()Clears out the content of this
AttachmentPart object. The MIME header portion is left
untouched.
datahandler = null;
contentObject = null;
|
public void | detachAttachmentFile()Detach the attachment file from this class, so it is not cleaned up.
This has the side-effect of making subsequent calls to
getAttachmentFile() return null .
attachmentFile=null;
|
public synchronized void | dispose()when an attachment part is disposed, any associated files
are deleted, and the datahandler itself nulled. The object
is no longer completely usable, at this point
if (attachmentFile != null) {
javax.activation.DataSource ds = datahandler.getDataSource();
if (ds instanceof ManagedMemoryDataSource) {
((ManagedMemoryDataSource) ds).delete(); //and delete the file
} else {
File f = new File(attachmentFile);
//no need to check for existence here.
f.delete();
}
//set the filename to null to stop repeated use
setAttachmentFile(null);
}
//clean up the datahandler, as it will have been
//invalidated if it was bound to a file; if it wasnt
//we get to release memory anyway
datahandler = null;
|
private void | extractFilename(ManagedMemoryDataSource source)Maybe add file name to the attachment.
//check for there being a file
if(source.getDiskCacheFile()!=null) {
String path = source.getDiskCacheFile().getAbsolutePath();
setAttachmentFile(path);
}
|
protected void | finalize()On death, we clean up our file.
dispose();
|
public javax.activation.DataHandler | getActivationDataHandler()Get the data handler.
return datahandler;
|
public java.util.Iterator | getAllMimeHeaders()
return mimeHeaders.getAllHeaders();
|
public java.lang.String | getAttachmentFile()Get the filename of this attachment.
return attachmentFile;
|
public java.lang.Object | getContent()Gets the content of this AttachmentPart object
as a Java object. The type of the returned Java object
depends on (1) the DataContentHandler object
that is used to interpret the bytes and (2) the
Content-Type given in the header.
For the MIME content types "text/plain", "text/html" and
"text/xml", the DataContentHandler object does
the conversions to and from the Java types corresponding to
the MIME types. For other MIME types,the
DataContentHandler object can return an
InputStream object that contains the content data as
raw bytes.
A JAXM-compliant implementation must, as a minimum,
return a java.lang.String object corresponding
to any content stream with a Content-Type
value of text/plain and a
javax.xml.transform.StreamSource object
corresponding to a content stream with a
Content-Type value of text/xml . For
those content types that an installed
DataContentHandler object does not understand, the
DataContentHandler object is required to
return a java.io.InputStream object with the
raw bytes.
if(contentObject != null) {
return contentObject;
}
if(datahandler == null) {
throw new SOAPException(Messages.getMessage("noContent"));
}
javax.activation.DataSource ds = datahandler.getDataSource();
InputStream is = null;
try {
is = ds.getInputStream();;
} catch (java.io.IOException io) {
log.error(Messages.getMessage("javaIOException00"), io);
throw new SOAPException(io);
}
if (ds.getContentType().equals("text/plain")) {
try {
byte[] bytes = new byte[is.available()];
IOUtils.readFully(is, bytes);
return new String(bytes);
} catch (java.io.IOException io) {
log.error(Messages.getMessage("javaIOException00"), io);
throw new SOAPException(io);
}
} else if (ds.getContentType().equals("text/xml")) {
return new StreamSource(is);
} else if (ds.getContentType().equals("image/gif") ||
ds.getContentType().equals("image/jpeg")) {
try {
return ImageIOFactory.getImageIO().loadImage(is);
} catch (Exception ex) {
log.error(Messages.getMessage("javaIOException00"), ex);
throw new SOAPException(ex);
}
}
return is;
|
public java.lang.String | getContentId()
return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_ID);
|
public java.lang.String | getContentIdRef()Content ID.
return Attachments.CIDprefix + getContentId();
|
public java.lang.String | getContentLocation()
return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION);
|
public java.lang.String | getContentType()getContentType
return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE);
|
public javax.activation.DataHandler | getDataHandler()Gets the DataHandler object for this
AttachmentPart object.
if(datahandler == null) {
throw new SOAPException(Messages.getMessage("noContent"));
}
return datahandler;
|
public java.lang.String | getFirstMimeHeader(java.lang.String header)Get the specified MIME header.
String[] values = mimeHeaders.getHeader(header.toLowerCase());
if ((values != null) && (values.length > 0)) {
return values[0];
}
return null;
|
public java.util.Iterator | getMatchingMimeHeaders(java.lang.String[] match)
return mimeHeaders.getMatchingHeaders(match);
|
public java.lang.String[] | getMimeHeader(java.lang.String name)Gets all the values of the header identified by the given
String .
return mimeHeaders.getHeader(name);
|
public java.util.Iterator | getNonMatchingMimeHeaders(java.lang.String[] match)
return mimeHeaders.getNonMatchingHeaders(match);
|
public int | getSize()Returns the number of bytes in this
AttachmentPart object.
if (datahandler == null) {
return 0;
}
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try {
datahandler.writeTo(bout);
} catch (java.io.IOException ex) {
log.error(Messages.getMessage("javaIOException00"), ex);
throw new SOAPException(Messages.getMessage("javaIOException01", ex.getMessage()), ex);
}
return bout.size();
|
public boolean | matches(javax.xml.soap.MimeHeaders headers)check if this Part's mimeheaders matches the one passed in.
TODO: Am not sure about the logic.
for (Iterator i = headers.getAllHeaders(); i.hasNext();) {
javax.xml.soap.MimeHeader hdr = (javax.xml.soap.MimeHeader) i.next();
String values[] = mimeHeaders.getHeader(hdr.getName());
boolean found = false;
if (values != null) {
for (int j = 0; j < values.length; j++) {
if (!hdr.getValue().equalsIgnoreCase(values[j])) {
continue;
}
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
|
public void | removeAllMimeHeaders()Removes all the MIME header entries.
mimeHeaders.removeAllHeaders();
|
public void | removeMimeHeader(java.lang.String header)Removes all MIME headers that match the given name.
mimeHeaders.removeHeader(header);
|
protected void | setAttachmentFile(java.lang.String path)Set the filename of this attachment part.
attachmentFile=path;
|
public void | setContent(java.lang.Object object, java.lang.String contentType)Sets the content of this attachment part to that of the
given Object and sets the value of the
Content-Type header to the given type. The type of the
Object should correspond to the value given for
the Content-Type . This depends on the particular
set of DataContentHandler objects in use.
ManagedMemoryDataSource source = null;
setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType);
if (object instanceof String) {
try {
String s = (String) object;
java.io.ByteArrayInputStream bais =
new java.io.ByteArrayInputStream(s.getBytes());
source = new ManagedMemoryDataSource(bais,
ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED,
contentType, true);
extractFilename(source);
datahandler = new DataHandler(source);
contentObject = object;
return;
} catch (java.io.IOException io) {
log.error(Messages.getMessage("javaIOException00"), io);
throw new java.lang.IllegalArgumentException(
Messages.getMessage("illegalArgumentException00"));
}
} else if (object instanceof java.io.InputStream) {
try {
source = new ManagedMemoryDataSource((java.io.InputStream) object,
ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED,
contentType, true);
extractFilename(source);
datahandler = new DataHandler(source);
contentObject = null; // the stream has been consumed
return;
} catch (java.io.IOException io) {
log.error(Messages.getMessage("javaIOException00"), io);
throw new java.lang.IllegalArgumentException(Messages.getMessage
("illegalArgumentException00"));
}
} else if (object instanceof StreamSource) {
try {
source = new ManagedMemoryDataSource(((StreamSource)object).getInputStream(),
ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED,
contentType, true);
extractFilename(source);
datahandler = new DataHandler(source);
contentObject = null; // the stream has been consumed
return;
} catch (java.io.IOException io) {
log.error(Messages.getMessage("javaIOException00"), io);
throw new java.lang.IllegalArgumentException(Messages.getMessage
("illegalArgumentException00"));
}
} else {
throw new java.lang.IllegalArgumentException(
Messages.getMessage("illegalArgumentException00"));
}
|
public void | setContentId(java.lang.String newCid)
setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, newCid);
|
public void | setContentLocation(java.lang.String loc)
setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION, loc);
|
public void | setDataHandler(javax.activation.DataHandler datahandler)Sets the given DataHandler object as the
data handler for this AttachmentPart object.
Typically, on an incoming message, the data handler is
automatically set. When a message is being created and
populated with content, the setDataHandler
method can be used to get data from various data sources into
the message.
if(datahandler == null) {
throw new java.lang.IllegalArgumentException(
Messages.getMessage("illegalArgumentException00"));
}
this.datahandler = datahandler;
setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, datahandler.getContentType());
//now look at the source of the data
javax.activation.DataSource ds = datahandler.getDataSource();
if (ds instanceof ManagedMemoryDataSource) {
//and get the filename if appropriate
extractFilename((ManagedMemoryDataSource)ds);
}
|
public void | setMimeHeader(java.lang.String name, java.lang.String value)Changes the first header entry that matches the given name
to the given value, adding a new header if no existing
header matches. This method also removes all matching
headers but the first.
Note that RFC822 headers can only contain US-ASCII
characters.
mimeHeaders.setHeader(name, value);
|