Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

REST easy with the JavaBeans Activation Framework

Use JAF for seamless data exchange in RESTful systems

  • Print
  • Feedback

Page 6 of 7

JAF's DataSource class

JAF's javax.activation.FileDataSource class implements the javax.activation.DataSource interface to expose a simple object representing a file resource.

Listing 6 shows a specific implementation of the Representation interface that encapsulates file resources.

Listing 6. Using Representation to encapsulate file resources

public class FileRepresentation extends Representation
{
  public FileRepresentation(File file)
  {
    super(new FileDataSource(file));
  }
  
  public FileDataSource getDataSource()
  {
    try
    {
      return this.getDataSource();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    
    return null;
  }
  
  public File getFile()
  {
    try
    {
      return this.getDataSource().getFile();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    
    return null;
  }
}

The FileDataSource class provides content-typing using a FileTypeMap object. The FileTypeMap class relies on a default, MIME-based map to determine content types. Customization of the default content types can be facilitated using the setFileTypeMap method.

JAF content types for REST resources

Implementations of the FileTypeMap class implement the abstract getContentType methods to return the content type of a resource using whatever mechanism is appropriate for the resource. This might entail reading the file extension of a file resource or opening the file and reading its contents to determine the content type. The FileDataSource class uses the default FileTypeMap to determine the content type of file resources. The default FileTypeMap is an instance of the MimetypesFileTypeMap class.

The MimetypesFileTypeMap class extends FileTypeMap and determines the content type of files by mapping their file extensions. The default mechanism employed by the MimetypesFileTypeMap to find the extension mapping is as follows:

  • Use any entries programmatically added to the MimetypesFileTypeMap instance.
  • Look for a file named "mime.types" in the user's home directory.
  • Look for a file named "mime.types" in the <java.home>/lib directory.
  • Look for a file named "mime.types" in the META-INF directory of the resource classpath.
  • Look for a file named "mime.types.default" in the META-INF directory of the resource classpath.
  • The file or resource named META-INF/mimetypes.default. This is usually provided by the activation.jar file installed with the JAF reference implementation.

Listing 7 is an example of the MIME-types mapping file.

Listing 7. A MIME-types mapping file

MIME Type             File Extension
========================================
text/html              html htm HTML HTM
text/plain             txt text TXT TEXT
image/gif              gif GIF
image/ief              ief
image/jpeg             jpeg jpg jpe JPG
image/tiff             tiff tif
image/png              png PNG
image/x-xwindowdump    xwd
application/postscript ai eps ps
application/rtf        rtf
application/x-tex      tex
application/x-texinfo  texinfo texi
application/x-troff    t tr roff
audio/basic            au
audio/midi             midi mid
audio/x-aifc           aifc
audio/x-aiff           aif aiff
audio/x-mpeg           mpeg mpg
audio/x-wav            wav
video/mpeg             mpeg mpg mpe
video/quicktime        qt mov
video/x-msvideo        avi

  • Print
  • Feedback

Resources