Class GridFsResource

java.lang.Object
org.springframework.core.io.AbstractResource
org.springframework.core.io.InputStreamResource
org.springframework.data.mongodb.gridfs.GridFsResource
All Implemented Interfaces:
org.springframework.core.io.InputStreamSource, org.springframework.core.io.Resource, GridFsObject<Object,InputStream>

public class GridFsResource extends org.springframework.core.io.InputStreamResource implements GridFsObject<Object,InputStream>
GridFSFile based Resource implementation.
Author:
Oliver Gierke, Christoph Strobl, Hartmut Lang, Mark Paluch
  • Constructor Details

    • GridFsResource

      public GridFsResource(com.mongodb.client.gridfs.model.GridFSFile file)
      Creates a new GridFsResource from the given GridFSFile.
      Parameters:
      file - must not be null.
    • GridFsResource

      public GridFsResource(com.mongodb.client.gridfs.model.GridFSFile file, InputStream inputStream)
      Creates a new GridFsResource from the given GridFSFile and InputStream.
      Parameters:
      file - must not be null.
      inputStream - must not be null.
  • Method Details

    • absent

      public static GridFsResource absent(String filename)
      Obtain an absent GridFsResource.
      Parameters:
      filename - filename of the absent resource, must not be null.
      Returns:
      never null.
      Since:
      2.1
    • getInputStream

      public InputStream getInputStream() throws IOException, IllegalStateException
      Specified by:
      getInputStream in interface org.springframework.core.io.InputStreamSource
      Overrides:
      getInputStream in class org.springframework.core.io.InputStreamResource
      Throws:
      IOException
      IllegalStateException
    • contentLength

      public long contentLength() throws IOException
      Specified by:
      contentLength in interface org.springframework.core.io.Resource
      Overrides:
      contentLength in class org.springframework.core.io.AbstractResource
      Throws:
      IOException
    • getFilename

      public String getFilename() throws IllegalStateException
      Description copied from interface: GridFsObject
      The filename.
      Specified by:
      getFilename in interface GridFsObject<Object,InputStream>
      Specified by:
      getFilename in interface org.springframework.core.io.Resource
      Overrides:
      getFilename in class org.springframework.core.io.AbstractResource
      Throws:
      IllegalStateException
    • exists

      public boolean exists()
      Specified by:
      exists in interface org.springframework.core.io.Resource
      Overrides:
      exists in class org.springframework.core.io.InputStreamResource
    • lastModified

      public long lastModified() throws IOException
      Specified by:
      lastModified in interface org.springframework.core.io.Resource
      Overrides:
      lastModified in class org.springframework.core.io.AbstractResource
      Throws:
      IOException
    • getDescription

      public String getDescription()
      Specified by:
      getDescription in interface org.springframework.core.io.Resource
      Overrides:
      getDescription in class org.springframework.core.io.InputStreamResource
    • getId

      public Object getId()
      Returns the Resource's id.
      Returns:
      never null.
      Throws:
      IllegalStateException - if the file does not exists().
    • getFileId

      public Object getFileId()
      Description copied from interface: GridFsObject
      The GridFSFile.getId() value converted into its simple java type.
      A BsonString will be converted to plain String.
      Specified by:
      getFileId in interface GridFsObject<Object,InputStream>
      Returns:
      can be null depending on the implementation.
    • getGridFSFile

      @Nullable public com.mongodb.client.gridfs.model.GridFSFile getGridFSFile()
      Returns:
      the underlying GridFSFile. Can be null if absent.
      Since:
      2.2
    • getContentType

      public String getContentType()
      Returns the Resource's content type.
      Returns:
      never null.
      Throws:
      com.mongodb.MongoGridFSException - in case no content type declared on GridFSFile.getMetadata() nor provided via GridFSFile.
      IllegalStateException - if the file does not exists().
    • getContent

      public InputStream getContent()
      Description copied from interface: GridFsObject
      The actual file content.
      Specified by:
      getContent in interface GridFsObject<Object,InputStream>
      Returns:
    • getOptions

      public GridFsObject.Options getOptions()
      Description copied from interface: GridFsObject
      Additional information like file metadata (eg. contentType).
      Specified by:
      getOptions in interface GridFsObject<Object,InputStream>
      Returns:
      never null.