001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.mail2.javax.activation;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023
024import javax.activation.DataSource;
025
026/**
027 * A JavaBeans Activation Framework {@link DataSource} specialized for {@link InputStream}.
028 * <p>
029 * Copied from <a href="https://cxf.apache.org/">Apache CXF</a> and modified.
030 * </p>
031 *
032 * @since 1.6.0
033 */
034public final class InputStreamDataSource implements DataSource {
035
036    /**
037     * Default content type documented in {@link DataSource#getContentType()}.
038     */
039    private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
040
041    /**
042     * The MIME content type.
043     */
044    private final String contentType;
045
046    /**
047     * The source.
048     */
049    private final InputStream inputStream;
050
051    /**
052     * The optional name.
053     */
054    private final String name;
055
056    /**
057     * Constructs a new instance.
058     *
059     * @param inputStream An input stream.
060     * @param contentType A content type.
061     */
062    public InputStreamDataSource(final InputStream inputStream, final String contentType) {
063        this(inputStream, contentType, null);
064    }
065
066    /**
067     * Constructs a new instance.
068     *
069     * @param inputStream An input stream.
070     * @param contentType A content type.
071     * @param name        A name.
072     */
073    public InputStreamDataSource(final InputStream inputStream, final String contentType, final String name) {
074        this.inputStream = inputStream;
075        this.contentType = contentType != null ? contentType : DEFAULT_CONTENT_TYPE;
076        this.name = name;
077    }
078
079    @Override
080    public String getContentType() {
081        return contentType;
082    }
083
084    @Override
085    public InputStream getInputStream() throws IOException {
086        return inputStream;
087    }
088
089    @Override
090    public String getName() {
091        return name;
092    }
093
094    /**
095     * Always throws {@link UnsupportedOperationException}.
096     *
097     * @return Always throws {@link UnsupportedOperationException}.
098     * @throws UnsupportedOperationException Always throws {@link UnsupportedOperationException}.
099     */
100    @Override
101    public OutputStream getOutputStream() {
102        throw new UnsupportedOperationException();
103    }
104
105}