attachmentpartimpl.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 574 行 · 第 1/2 页

JAVA
574
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.axis2.saaj;

import org.apache.axiom.om.OMText;
import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
import org.apache.axiom.om.impl.dom.DocumentImpl;
import org.apache.axiom.om.impl.dom.TextImpl;
import org.apache.axiom.om.util.Base64;
import org.apache.axis2.saaj.util.SAAJDataSource;
import org.apache.axis2.transport.http.HTTPConstants;

import javax.activation.DataHandler;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPException;
import javax.xml.transform.stream.StreamSource;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

/**
 * 
 */
public class AttachmentPartImpl extends AttachmentPart {
    private DataHandler dataHandler;

    private MimeHeaders mimeHeaders = new MimeHeaders();
    private String attachmentFile;

    private OMText omText;
    private boolean isAttachmentReferenced;

    /**
     * Check whether at least one of the headers of this object matches a provided header
     *
     * @param headers
     * @return <b>true</b> if at least one header of this AttachmentPart matches a header in the
     *         provided <code>headers</code> parameter, <b>false</b> if none of the headers of this
     *         AttachmentPart matches at least one of the header in the provided
     *         <code>headers</code> parameter
     */
    public boolean matches(MimeHeaders headers) {
        for (Iterator i = headers.getAllHeaders(); i.hasNext();) {
            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 boolean isAttachmentReferenced() {
        return isAttachmentReferenced;
    }

    public void setAttachmentReferenced(boolean attachmentReferenced) {
        isAttachmentReferenced = attachmentReferenced;
    }

    /**
     * Returns the number of bytes in this <CODE> AttachmentPart</CODE> object.
     *
     * @return the size of this <CODE>AttachmentPart</CODE> object in bytes or -1 if the size cannot
     *         be determined
     * @throws javax.xml.soap.SOAPException if the content of this attachment is corrupted of if
     *                                      there was an exception while trying to determine the
     *                                      size.
     */
    public int getSize() throws SOAPException {
        if (dataHandler == null) {
            return 0;
        }
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            dataHandler.writeTo(bout);
        } catch (Exception ex) {
            throw new SOAPException(ex);
        }
        return bout.size();
    }

    /**
     * Clears out the content of this <CODE> AttachmentPart</CODE> object. The MIME header portion
     * is left untouched.
     */
    public void clearContent() {
        dataHandler = null;
        omText = null;
    }

    /**
     * Gets the content of this <code>AttachmentPart</code> object as a Java object. The type of the
     * returned Java object depends on <ol> <li> the <code>DataContentHandler</code> object that is
     * used to interpret the bytes </li> <li> the <code>Content-Type</code> given in the header</li>
     * </ol>
     * <p/>
     * For the MIME content types "text/plain", "text/html" and "text/xml", the
     * <code>DataContentHandler</code> object does the conversions to and from the Java types
     * corresponding to the MIME types. For other MIME types,the <code>DataContentHandler</code>
     * object can return an <code>InputStream</code> object that contains the content data as raw
     * bytes.
     * <p/>
     * A JAXM-compliant implementation must, as a minimum, return a <code>java.lang.String</code>
     * object corresponding to any content stream with a <code>Content-Type</code> value of
     * <code>text/plain</code>, a <code>javax.xml.transform.StreamSource</code> object corresponding
     * to a content stream with a <code>Content-Type</code> value of <code>text/xml</code>, a
     * <code>java.awt.Image</code> object corresponding to a content stream with a
     * <code>Content-Type</code> value of <code>image/gif</code> or <code>image/jpeg</code>.  For
     * those content types that an installed <code>DataContentHandler</code> object does not
     * understand, the <code>DataContentHandler</code> object is required to return a
     * <code>java.io.InputStream</code> object with the raw bytes.
     *
     * @return a Java object with the content of this <CODE> AttachmentPart</CODE> object
     * @throws javax.xml.soap.SOAPException if there is no content set into this <CODE>AttachmentPart</CODE>
     *                                      object or if there was a data transformation error
     */
    public Object getContent() throws SOAPException {
        if (dataHandler == null) {
            throw new SOAPException("No content is present in this AttachmentPart");
        }
        try {
            String contentType = dataHandler.getContentType();
            if (contentType.equals(HTTPConstants.MEDIA_TYPE_TEXT_XML) ||
                    contentType.equals(HTTPConstants.MEDIA_TYPE_APPLICATION_XML)) {
                StreamSource streamSource = new StreamSource();
                streamSource.setInputStream(dataHandler.getInputStream());
                return streamSource;
            } else if (contentType.equals("text/plain") ||
                    contentType.equals("text/html")) {
                return (String)dataHandler.getContent();
            } else {
                try {
                    return dataHandler.getContent();
                } catch (Exception e) {
                    //If the underlying DataContentHandler can't handle the object contents,
                    //we will return an inputstream of raw bytes representing the content data
                    return dataHandler.getDataSource().getInputStream();
                }
            }
        } catch (IOException e) {
            throw new SOAPException(e.getMessage());
        }
    }

    /**
     * Sets the content of this attachment part to that of the given <CODE>Object</CODE> and sets
     * the value of the <CODE> Content-Type</CODE> header to the given type. The type of the
     * <CODE>Object</CODE> should correspond to the value given for the <CODE>Content-Type</CODE>.
     * This depends on the particular set of <CODE>DataContentHandler</CODE> objects in use.
     *
     * @param object      the Java object that makes up the content for this attachment part
     * @param contentType the MIME string that specifies the type of the content
     * @throws IllegalArgumentException if the contentType does not match the type of the content
     *                                  object, or if there was no <CODE> DataContentHandler</CODE>
     *                                  object for this content object
     * @see #getContent()
     */
    public void setContent(Object object, String contentType) {
        SAAJDataSource source;
        setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType);
        Object contentObject;
        if (object instanceof String) {
            try {
                String s = (String)object;
                java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(s.getBytes());
                source = new SAAJDataSource(bais,
                                            SAAJDataSource.MAX_MEMORY_DISK_CACHED,
                                            contentType, true);
                extractFilename(source);
                this.dataHandler = new DataHandler(source);
                contentObject = object;
            } catch (java.io.IOException io) {
                throw new java.lang.IllegalArgumentException("Illegal Argument");
            }
        } else if (object instanceof java.io.InputStream) {
            try {

                source = new SAAJDataSource((java.io.InputStream)object,
                                            SAAJDataSource.MIN_MEMORY_DISK_CACHED,
                                            contentType, true);
                extractFilename(source);
                this.dataHandler = new DataHandler(source);
                contentObject = null; // the stream has been consumed
            } catch (java.io.IOException io) {
                throw new java.lang.IllegalArgumentException("Illegal Argument");
            }
        } else if (object instanceof StreamSource) {
            try {
                source = new SAAJDataSource(((StreamSource)object).getInputStream(),
                                            SAAJDataSource.MAX_MEMORY_DISK_CACHED,
                                            contentType, true);
                extractFilename(source);
                this.dataHandler = new DataHandler(source);
                contentObject = null; // the stream has been consumed
            } catch (java.io.IOException io) {
                throw new java.lang.IllegalArgumentException("Illegal Argument");
            }
        } else if (object instanceof BufferedImage) {
            try {
                this.dataHandler = new DataHandler(object, contentType);
                contentObject = null; // the stream has been consumed
            } catch (Exception e) {
                throw new java.lang.IllegalArgumentException(e.getMessage());
            }
        } else if (object instanceof byte[]) {
            try {
                contentObject = null;
                java.io.ByteArrayInputStream bais =
                        new java.io.ByteArrayInputStream((byte[])object);
                source = new SAAJDataSource(bais,
                                            SAAJDataSource.MAX_MEMORY_DISK_CACHED,
                                            contentType, true);
                extractFilename(source);

                this.dataHandler = new DataHandler(source);
                contentObject = object;
            } catch (Exception e) {
                throw new java.lang.IllegalArgumentException(e.getMessage());
            }
        } else {
            throw new java.lang.IllegalArgumentException("Illegal Argument");
        }
    }

    /**
     * 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 javax.xml.soap.SOAPException if there is no data in this <CODE>AttachmentPart</CODE>
     *                                      object
     */
    public DataHandler getDataHandler() throws SOAPException {
        //if (getContent() == null) {
        //    throw new SOAPException("No Content present in the Attachment part");
        //}
        //commented to fix AXIS2-778
        if (dataHandler == null) {
            throw new SOAPException("No Content present in the Attachment part");
        }

        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 IllegalArgumentException if there was a problem with the specified <CODE>
     *                                  DataHandler</CODE> object

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?