Class FileBasedUploadFilter

java.lang.Object
de.espirit.firstspirit.server.mediamanagement.FileBasedUploadFilter
All Implemented Interfaces:
SetServiceLocatorCallback, UploadFilter, Closeable, AutoCloseable

public abstract class FileBasedUploadFilter extends Object implements UploadFilter, SetServiceLocatorCallback
A basic file based implementation of a UploadFilter.
Since:
4.1
Example:
This is a basic example how to extend this class
import de.espirit.firstspirit.server.mediamanagement.*;

import de.espirit.firstspirit.access.store.mediastore.UploadRejectedException;

import java.io.File;
import java.io.IOException;


public class FileBasedUploadFilterExample extends FileBasedUploadFilter {

	/**
	 * IOException indicates to not store the File tempFile in the repository
	 *
	 * @param tempFile
	 * @throws java.io.IOException
	 */
	@Override
	public void doFilter(final File tempFile) throws IOException {
		final boolean failure = false;

		// do something. for example scan the <code>tempFile</code> for a virus
		//scan(tempFile)

		if (failure) {
			// indicates  not to store the File tempFile in the repository
			throw new UploadRejectedException("A Virus has been found.");
		}
	}
}
  • Constructor Details

    • FileBasedUploadFilter

      public FileBasedUploadFilter()
  • Method Details

    • setServiceLocator

      public final void setServiceLocator(@NotNull @NotNull ServiceLocator value)
      Set a service locator which will be used to lookup a service.
      Specified by:
      setServiceLocator in interface SetServiceLocatorCallback
      Parameters:
      value -
      Since:
      4.1
    • init

      protected void init()
      Method stub. Override to init some filter stuff.
      Since:
      4.1
    • getServiceLocator

      protected ServiceLocator getServiceLocator()
      Gets the current service locator.
      Returns:
      the service locator
      Since:
      4.1
    • initUploadFilter

      public void initUploadFilter(@NotNull @NotNull InputStream in, @NotNull @NotNull String extension) throws IOException
      Creates a new tempfile with the given extension and copies the given inputstream to this tempfile. Initialize the filter instance. It is not allowed to change the content of the data provided by in.
      Specified by:
      initUploadFilter in interface UploadFilter
      Parameters:
      in - input stream containing the data
      extension - file extension - it is not save to guess the file content from this extension
      Throws:
      IOException - indicates to not store the File tempFile in the repository
      Since:
      4.1
    • doFilter

      public abstract void doFilter(File tempFile) throws IOException
      IOException indicates to not store the File tempFile in the repository
      Parameters:
      tempFile - the temp file
      Throws:
      IOException - indicates to not store the File tempFile in the repository
      Since:
      4.1
    • read

      public int read(@NotNull @org.jetbrains.annotations.NotNull byte[] b, int off, int len) throws IOException
      Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read should returned as an integer. See InputStream.read(byte[], int, int).
      Specified by:
      read in interface UploadFilter
      Parameters:
      b - the bytes to read
      off - the offset
      len - the length
      Returns:
      the int The number of bytes actually read
      Throws:
      IOException - on any error
      Since:
      4.1
    • close

      public void close()
      Callback method which may be used to do some clean up. A call to this method should at least close the input stream provided in method UploadFilter.initUploadFilter(java.io.InputStream, String). Closes the stream and the tempfile used for this file based implementation.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface UploadFilter
      Since:
      4.1