attachmentpartimpl.java

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

JAVA
574
字号
     */
    public void setDataHandler(DataHandler datahandler) {
        if (datahandler != null) {
            this.dataHandler = datahandler;
            setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, datahandler.getContentType());
            omText = DOOMAbstractFactory.getOMFactory().createOMText(datahandler, true);
        } else {
            throw new IllegalArgumentException("Cannot set null DataHandler");
        }
    }

    /**
     * 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);
    }

    /** Removes all the MIME header entries. */
    public void removeAllMimeHeaders() {
        mimeHeaders.removeAllHeaders();
    }

    /**
     * Gets all the values of the header identified by the given <CODE>String</CODE>.
     *
     * @param name the name of the header; example: "Content-Type"
     * @return a <CODE>String</CODE> array giving the value for the specified header
     * @see #setMimeHeader(String, String) setMimeHeader(java.lang.String, java.lang.String)
     */
    public String[] getMimeHeader(String name) {
        return mimeHeaders.getHeader(name);
    }

    /**
     * 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/>
     * <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 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);
    }

    /**
     * Adds a MIME header with the specified name and value to this <CODE>AttachmentPart</CODE>
     * object.
     * <p/>
     * <P>Note that RFC822 headers can contain only US-ASCII characters.</P>
     *
     * @param name  a <CODE>String</CODE> giving the name of the header to be added
     * @param value a <CODE>String</CODE> giving the value of the header to be added
     * @throws IllegalArgumentException if there was a problem with the specified mime header name
     *                                  or value
     */
    public void addMimeHeader(String name, String value) {
        mimeHeaders.addHeader(name, value);
    }

    /**
     * Retrieves all the headers for this <CODE> AttachmentPart</CODE> object as an iterator over
     * the <CODE> MimeHeader</CODE> objects.
     *
     * @return an <CODE>Iterator</CODE> object with all of the Mime headers for this
     *         <CODE>AttachmentPart</CODE> object
     */
    public Iterator getAllMimeHeaders() {
        return mimeHeaders.getAllHeaders();
    }

    /**
     * Retrieves all <CODE>MimeHeader</CODE> objects that match a name in the given array.
     *
     * @param names a <CODE>String</CODE> array with the name(s) of the MIME headers to be returned
     * @return all of the MIME headers that match one of the names in the given array as an
     *         <CODE>Iterator</CODE> object
     */
    public Iterator getMatchingMimeHeaders(String names[]) {
        return mimeHeaders.getMatchingHeaders(names);
    }

    /**
     * Retrieves all <CODE>MimeHeader</CODE> objects whose name does not match a name in the given
     * array.
     *
     * @param names a <CODE>String</CODE> array with the name(s) of the MIME headers not to be
     *              returned
     * @return all of the MIME headers in this <CODE> AttachmentPart</CODE> object except those that
     *         match one of the names in the given array. The nonmatching MIME headers are returned
     *         as an <CODE>Iterator</CODE> object.
     */
    public Iterator getNonMatchingMimeHeaders(String names[]) {
        return mimeHeaders.getNonMatchingHeaders(names);
    }

    public InputStream getBase64Content() throws SOAPException {
        byte[] rawData = getRawContentBytes();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            Base64.encode(rawData, 0, rawData.length, out);
            return new ByteArrayInputStream(out.toByteArray());
        } catch (IOException e) {
            throw new SOAPException(e);
        }
    }


    /**
     * Gets the content of this AttachmentPart object as an InputStream as if a call had been made
     * to getContent and no DataContentHandler had been registered for the content-type of this
     * AttachmentPart.Note that reading from the returned InputStream would result in consuming the
     * data in the stream. It is the responsibility of the caller to reset the InputStream
     * appropriately before calling a Subsequent API. If a copy of the raw attachment content is
     * required then the getRawContentBytes() API should be used instead.
     *
     * @return an InputStream from which the raw data contained by the AttachmentPart can be
     *         accessed.
     * @throws SOAPException - if there is no content set into this AttachmentPart object or if
     *                       there was a data transformation error.
     * @since SAAJ 1.3
     */
    public InputStream getRawContent() throws SOAPException {
        try {
            if (dataHandler == null) {
                throw new SOAPException("No content set");
            }
            return dataHandler.getInputStream();
        } catch (IOException e) {
            throw new SOAPException(e);
        }
    }

    /**
     * Gets the content of this AttachmentPart object as a byte[] array as if a call had been made
     * to getContent and no DataContentHandler had been registered for the content-type of this
     * AttachmentPart.
     *
     * @return a byte[] array containing the raw data of the AttachmentPart.
     * @throws SOAPException - if there is no content set into this AttachmentPart object or if
     *                       there was a data transformation error.
     * @since SAAJ 1.3
     */
    public byte[] getRawContentBytes() throws SOAPException {
        if (dataHandler == null) {
            throw new SOAPException("Content is null");
        }
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            dataHandler.writeTo(bout);
        } catch (Exception ex) {
            throw new SOAPException(ex);
        }
        return bout.toByteArray();
    }


    /**
     * Sets the content of this attachment part from the Base64 source InputStream and sets the
     * value of the Content-Type header to the value contained in contentType, This method would
     * first decode the base64 input and write the resulting raw bytes to the attachment. A
     * subsequent call to getSize() may not be an exact measure of the content size.
     *
     * @param content - the base64 encoded data to add to the attachment part contentType - the
     *                value to set into the Content-Type header
     * @throws SOAPException - if there is an error in setting the content java.lang.NullPointerException
     *                       - if content is null
     */
    public void setBase64Content(InputStream content, String contentType) throws SOAPException {
        if (content == null) {
            throw new SOAPException("Content is null");
        }
        OutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int read;
        try {
            while ((read = content.read(buffer, 0, buffer.length)) > 0) {
                outputStream.write(buffer, 0, read);
            }
            String contentString = outputStream.toString();
            if (Base64.isValidBase64Encoding(contentString)) {
                setContent(Base64.decode(contentString), contentType);
            } else {
                throw new SOAPException("Not a valid Base64 encoding");
            }
        } catch (IOException ex) {
            throw new SOAPException(ex);
        }
    }

    /**
     * Sets the content of this attachment part to that contained by the InputStream content and
     * sets the value of the Content-Type header to the value contained in contentType.A subsequent
     * call to getSize() may not be an exact measure of the content size.
     *
     * @param content - the raw data to add to the attachment part contentType - the value to set
     *                into the Content-Type header
     * @throws SOAPException - if there is an error in setting the content java.lang.NullPointerException
     *                       - if content is null
     */
    public void setRawContent(InputStream content, String contentType) throws SOAPException {
        if (content == null) {
            throw new SOAPException("content is null");
        }
        setContent(content, contentType);
    }


    /**
     * Sets the content of this attachment part to that contained by the byte[] array content and
     * sets the value of the Content-Type header to the value contained in contentType.
     *
     * @param content - the raw data to add to the attachment part contentType - the value to set
     *                into the Content-Type header offset - the offset in the byte array of the
     *                content len - the number of bytes that form the content
     * @throws SOAPException - if an there is an error in setting the content or content is null
     * @since SAAJ 1.3
     */

    public void setRawContentBytes(byte[] content, int offset, int len, String contentType)
            throws SOAPException {
        //TODO - how to use offset & len?
        if (content == null) {
            throw new SOAPException("Content is null");
        }
        setContent(content, contentType);
    }

    /**
     * Retrieve the OMText
     *
     * @return the OMText
     * @throws SOAPException If omText is not available
     */
    public OMText getOMText() throws SOAPException {
        if (omText == null) {
            throw new SOAPException("OMText set to null");
        }
        return omText;
    }

    public TextImpl getText(DocumentImpl doc) {
        return new TextImpl(doc, omText.getText(), doc.getOMFactory());
    }

    /**
     * Set the filename of this attachment part.
     *
     * @param path the new file path
     */
    protected void setAttachmentFile(String path) {
        attachmentFile = path;
    }

    /**
     * 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 <code>null</code>.
     */
    public void detachAttachmentFile() {
        attachmentFile = null;
    }

    /**
     * Get the filename of this attachment.
     *
     * @return the filename or null for an uncached file
     */
    public String getAttachmentFile() {
        return attachmentFile;
    }

    private void extractFilename(SAAJDataSource source) {
        if (source.getDiskCacheFile() != null) {
            String path = source.getDiskCacheFile().getAbsolutePath();
            setAttachmentFile(path);
        }
    }
}

⌨️ 快捷键说明

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