org.apache.axiom.attachments.PartImpl Maven / Gradle / Ivy
/*
* 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.axiom.attachments;
import org.apache.axiom.attachments.Part;
import org.apache.axiom.blob.MemoryBlob;
import org.apache.axiom.blob.OverflowableBlob;
import org.apache.axiom.blob.WritableBlob;
import org.apache.axiom.blob.WritableBlobFactory;
import org.apache.axiom.ext.io.StreamCopyException;
import org.apache.axiom.mime.Header;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.util.DetachableInputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.stream.EntityState;
import org.apache.james.mime4j.stream.MimeTokenStream;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
/**
* Actual implementation of the {@link Part} interface.
*/
final class PartImpl implements Part {
/**
* The part has not been read yet. In this case the parser is in state
* {@link EntityState#T_BODY}.
*/
private static final int STATE_UNREAD = 0;
/**
* The part has been read into a memory or file based buffer.
*/
private static final int STATE_BUFFERED = 1;
/**
* The part content is being streamed, i.a. the application code consumes the part content
* without buffering.
*/
private static final int STATE_STREAMING = 2;
/**
* The part content has been discarded and can no longer be read. This state is reached either
* when the content has been streamed or when it is discarded explicitly after being buffered.
*/
private static final int STATE_DISCARDED = 3;
private static final Log log = LogFactory.getLog(PartImpl.class);
private final WritableBlobFactory blobFactory;
private List/**/ headers;
private int state = STATE_UNREAD;
/**
* The MIME parser from which the content of this part is read. This is only set if the state is
* {@link #STATE_UNREAD} or {@link #STATE_STREAMING}.
*/
private MimeTokenStream parser;
/**
* The content of this part. This is only set if the state is {@link #STATE_BUFFERED}.
*/
private WritableBlob content;
private final DataHandler dataHandler;
private DetachableInputStream detachableInputStream;
/**
* The actual parts are constructed with the PartFactory.
* @see org.apache.axiom.attachments.PartContentFactory
* @param headers
*/
PartImpl(WritableBlobFactory blobFactory, List headers, MimeTokenStream parser) {
this.blobFactory = blobFactory;;
this.headers = headers;
this.parser = parser;
this.dataHandler = new PartDataHandler(this);
}
public String getHeader(String name) {
String value = null;
for (int i=0, l=headers.size(); i