001package ball.activation;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: FilterDataSource.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/activation/FilterDataSource.java $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.beans.ConstructorProperties;
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.OutputStream;
027import java.util.Objects;
028import javax.activation.DataSource;
029
030/**
031 * Abstract {@link DataSource} base class that wraps another
032 * {@link DataSource}.
033 *
034 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
035 * @version $Revision: 5285 $
036 */
037public abstract class FilterDataSource extends AbstractDataSource {
038    private final DataSource ds;
039
040    /**
041     * @param   ds              The filtered {@link DataSource}.
042     */
043    @ConstructorProperties({ "dataSource" })
044    protected FilterDataSource(DataSource ds) {
045        super();
046
047        this.ds = Objects.requireNonNull(ds);
048    }
049
050    /**
051     * Private no-argument constructor (for JAXB annotated subclasses).
052     */
053    private FilterDataSource() { this(new ByteArrayDataSource(null, null)); }
054
055    /**
056     * Method to get the filtered {@link DataSource}.
057     *
058     * @return  The filtered {@link DataSource}.
059     */
060    protected DataSource getDataSource() { return ds; }
061
062    @Override
063    public String getName() { return getDataSource().getName(); }
064
065    @Override
066    public void setName(String name) {
067        try {
068            DataSource ds = getDataSource();
069
070            ds.getClass()
071                .getMethod("setName", String.class)
072                .invoke(ds, name);
073        } catch (Exception exception) {
074            throw new UnsupportedOperationException(exception);
075        }
076    }
077
078    @Override
079    public String getContentType() { return getDataSource().getContentType(); }
080
081    @Override
082    public void setContentType(String type) {
083        try {
084            DataSource ds = getDataSource();
085
086            ds.getClass()
087                .getMethod("setContentType", String.class)
088                .invoke(ds, type);
089        } catch (Exception exception) {
090            throw new UnsupportedOperationException(exception);
091        }
092    }
093
094    @Override
095    public InputStream getInputStream() throws IOException {
096        return getDataSource().getInputStream();
097    }
098
099    @Override
100    public OutputStream getOutputStream() throws IOException {
101        return getDataSource().getOutputStream();
102    }
103}