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