📄 attachmentpart.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.axis.attachments;import org.apache.axis.Part;import org.apache.axis.components.logger.LogFactory;import org.apache.axis.components.image.ImageIOFactory;import org.apache.axis.transport.http.HTTPConstants;import org.apache.axis.utils.Messages;import org.apache.axis.utils.SessionUtils;import org.apache.axis.utils.IOUtils;import org.apache.commons.logging.Log;import javax.activation.DataHandler;import javax.xml.soap.SOAPException;import javax.xml.transform.stream.StreamSource;import java.util.Iterator;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.InputStream;/** * An attachment part. * * */public class AttachmentPart extends javax.xml.soap.AttachmentPart implements Part { /** Field log */ protected static Log log = LogFactory.getLog(AttachmentPart.class.getName()); /** * The data handler. * <p> * TODO: make private? * */ javax.activation.DataHandler datahandler = null; /** Field mimeHeaders. */ private javax.xml.soap.MimeHeaders mimeHeaders = new javax.xml.soap.MimeHeaders(); private Object contentObject; /** * The name of a file used to store the data. */ private String attachmentFile; /** * Bulds a new <code>AttachmentPart</code>. */ public AttachmentPart() { setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, SessionUtils.generateSessionId()); } /** * Bulds a new <code>AttachmentPart</code> with a <code>DataHandler</code>. * * @param dh the <code>DataHandler</code> */ public AttachmentPart(javax.activation.DataHandler dh) { setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, SessionUtils.generateSessionId()); datahandler = dh; if(dh != null) { setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, dh.getContentType()); javax.activation.DataSource ds = dh.getDataSource(); if (ds instanceof ManagedMemoryDataSource) { extractFilename((ManagedMemoryDataSource)ds); //and get the filename if appropriate } } } // fixme: be aware, this may never be called /** * On death, we clean up our file. * * @throws Throwable if anything went wrong during finalization */ protected void finalize() throws Throwable { dispose(); } /** * Get the data handler. * * @return the activation <code>DataHandler</code> */ public javax.activation.DataHandler getActivationDataHandler() { return datahandler; } /** * getContentType * * @return content type */ public String getContentType() { return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE); } /** * Add the specified MIME header, as per JAXM. * * @param header * @param value */ public void addMimeHeader(String header, String value) { mimeHeaders.addHeader(header, value); } /** * Get the specified MIME header. * * @param header * * @return */ public String getFirstMimeHeader(String header) { String[] values = mimeHeaders.getHeader(header.toLowerCase()); if ((values != null) && (values.length > 0)) { return values[0]; } return null; } /** * check if this Part's mimeheaders matches the one passed in. * TODO: Am not sure about the logic. * * @param headers the <code>MimeHeaders</code> to check * @return true if all header name, values in <code>headers</code> are * found, false otherwise */ public boolean matches(javax.xml.soap.MimeHeaders headers) { 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 String getContentLocation() { return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION); } public void setContentLocation(String loc) { setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION, loc); } public void setContentId(String newCid) { setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, newCid); } public String getContentId() { return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_ID); } public java.util.Iterator getMatchingMimeHeaders(final String[] match) { return mimeHeaders.getMatchingHeaders(match); } public java.util.Iterator getNonMatchingMimeHeaders(final String[] match) { return mimeHeaders.getNonMatchingHeaders(match); } public Iterator getAllMimeHeaders() { return mimeHeaders.getAllHeaders(); } /** * 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. * * <P>Note that RFC822 headers can only contain US-ASCII * characters.</P> * @param name a <CODE>String</CODE> giving the * name of the header for which to search * @param value a <CODE>String</CODE> giving the * value to be set for the header whose name matches the * given name * @throws java.lang.IllegalArgumentException if * there was a problem with the specified mime header name * or value */ public void setMimeHeader(String name, String value) { mimeHeaders.setHeader(name, value); } /** Removes all the MIME header entries. */ public void removeAllMimeHeaders() { mimeHeaders.removeAllHeaders(); } /** * Removes all MIME headers that match the given name. * @param header - the string name of the MIME * header/s to be removed */ public void removeMimeHeader(String header) { mimeHeaders.removeHeader(header); } /** * Gets the <CODE>DataHandler</CODE> object for this <CODE> * AttachmentPart</CODE> object. * @return the <CODE>DataHandler</CODE> object associated with * this <CODE>AttachmentPart</CODE> object * @throws SOAPException if there is * no data in this <CODE>AttachmentPart</CODE> object */ public DataHandler getDataHandler() throws SOAPException { if(datahandler == null) { throw new SOAPException(Messages.getMessage("noContent")); } return datahandler; } /** * Sets the given <CODE>DataHandler</CODE> object as the * data handler for this <CODE>AttachmentPart</CODE> object. * Typically, on an incoming message, the data handler is * automatically set. When a message is being created and * populated with content, the <CODE>setDataHandler</CODE> * method can be used to get data from various data sources into * the message. * @param datahandler <CODE>DataHandler</CODE> object to * be set * @throws java.lang.IllegalArgumentException if * there was a problem with the specified <CODE> * DataHandler</CODE> object */ public void setDataHandler(DataHandler datahandler) { 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -