What are generic mappers

Generic mappers are a set of java classes used to quickly read key value pairs. For example the relationship between file extension and their content type is determined this way.

This mechanism is intended to be a general purpose mechanism for reading a dictionary of values

The current implementation is a bit inefficient but a future implementation can amend that.

Approach

1. Get a generic mapper that returns a map interface such as IStringDictionary

2. Keep a local static copy if you are going to reuse it

3. Use that static copy to get the needed mappping.

Sourcecode for mapping of content types: Example of a static holder


package com.ai.htmlgen.streamwriters;

import com.ai.application.interfaces.ConfigException;
import com.ai.application.interfaces.IInitializable;
import com.ai.application.interfaces.RequestExecutionException;
import com.ai.application.utils.AppObjects;
import com.ai.common.IStringDictionary;
import com.ai.common.StringUtils;
import com.ai.common.mapper.GenericMapper;
import com.ai.servlets.AspireServletException;

public class ExtensionToContentTypeMapping 
{
   private static IStringDictionary extensionToContentTypeDictionary;
   static 
   {
      try
      {
         extensionToContentTypeDictionary =
         GenericMapper.getMapperForRequest("aspire.contentTypeMapping");
      }
      catch(RequestExecutionException x)
      {
         throw new RuntimeException(
               "Could not get generic mapper for content type mapping",x);
      }
   }


   public static String getContentType(String fileExtensionWithoutDot) 
   throws AspireServletException 
   {
      try
      {
         if (StringUtils.isEmpty(fileExtensionWithoutDot))
         {
            return "application/octet-stream"; 
         }
         String contentType = 
            extensionToContentTypeDictionary
               .getAsString(fileExtensionWithoutDot);
         if (StringUtils.isValid(contentType))
         {
            return contentType;
         }
         //invalid content type or null
         return "application/octet-stream"; 
      }
      catch(ConfigException x)
      {
         throw new AspireServletException("Error:cannot get content type",x);
      }
   }

}

Content type mapping key


aspire.contentTypeMapping

RequestBasedMappper


package com.ai.common.mapper;

import com.ai.application.interfaces.ConfigException;
import com.ai.application.interfaces.IInitializable;
import com.ai.application.interfaces.ISingleThreaded;
import com.ai.application.utils.AppObjects;
import com.ai.common.DDictionary;

public class RequestBasedMapper extends DDictionary
implements IInitializable, ISingleThreaded
{
   private String mappingPrefix = null;

   public void initialize(String requestName) 
   {
      mappingPrefix = requestName + ".mapping.";
   }

   @Override
   public Object internalGet(Object key) 
   {
      if (!(key instanceof String))
      {
         throw new RuntimeException("expecting String type");
      }
      try
      {
         return AppObjects.getValue(mappingPrefix + (String)key);
      }
      catch(ConfigException x)
      {
         throw new RuntimeException("Could not find key:" + (String)key,x);
      }
   }//eof-function
}//eof-class

Actual config file


request.aspire.contentTypeMapping.classname=com.ai.common.mapper.RequestBasedMapper
request.aspire.contentTypeMapping.mapping.ai=application/postscript
request.aspire.contentTypeMapping.mapping.aif=audio/x-aiff
request.aspire.contentTypeMapping.mapping.aifc=audio/x-aiff
request.aspire.contentTypeMapping.mapping.aiff=audio/x-aiff
request.aspire.contentTypeMapping.mapping.asc=text/plain
request.aspire.contentTypeMapping.mapping.au=audio/basic
request.aspire.contentTypeMapping.mapping.avi=video/x-msvideo
request.aspire.contentTypeMapping.mapping.bcpio=application/x-bcpio
request.aspire.contentTypeMapping.mapping.bin=application/octet-stream
request.aspire.contentTypeMapping.mapping.c=text/plain
request.aspire.contentTypeMapping.mapping.cc=text/plain
request.aspire.contentTypeMapping.mapping.ccad=application/clariscad
request.aspire.contentTypeMapping.mapping.cdf=application/x-netcdf
request.aspire.contentTypeMapping.mapping.class=application/octet-stream
request.aspire.contentTypeMapping.mapping.cpio=application/x-cpio
request.aspire.contentTypeMapping.mapping.cpt=application/mac-compactpro
request.aspire.contentTypeMapping.mapping.csh=application/x-csh
request.aspire.contentTypeMapping.mapping.css=text/css
request.aspire.contentTypeMapping.mapping.dcr=application/x-director
request.aspire.contentTypeMapping.mapping.dir=application/x-director
request.aspire.contentTypeMapping.mapping.dms=application/octet-stream
request.aspire.contentTypeMapping.mapping.doc=application/msword
request.aspire.contentTypeMapping.mapping.drw=application/drafting
request.aspire.contentTypeMapping.mapping.dvi=application/x-dvi
request.aspire.contentTypeMapping.mapping.dwg=application/acad
request.aspire.contentTypeMapping.mapping.dxf=application/dxf
request.aspire.contentTypeMapping.mapping.dxr=application/x-director
request.aspire.contentTypeMapping.mapping.eps=application/postscript
request.aspire.contentTypeMapping.mapping.etx=text/x-setext
request.aspire.contentTypeMapping.mapping.exe=application/octet-stream
request.aspire.contentTypeMapping.mapping.ez=application/andrew-inset
request.aspire.contentTypeMapping.mapping.f=text/plain
request.aspire.contentTypeMapping.mapping.f90=text/plain
request.aspire.contentTypeMapping.mapping.fli=video/x-fli
request.aspire.contentTypeMapping.mapping.gif=image/gif
request.aspire.contentTypeMapping.mapping.gtar=application/x-gtar
request.aspire.contentTypeMapping.mapping.gz=application/x-gzip
request.aspire.contentTypeMapping.mapping.h=text/plain
request.aspire.contentTypeMapping.mapping.hdf=application/x-hdf
request.aspire.contentTypeMapping.mapping.hh=text/plain
request.aspire.contentTypeMapping.mapping.hqx=application/mac-binhex40
request.aspire.contentTypeMapping.mapping.htm=text/html
request.aspire.contentTypeMapping.mapping.html=text/html
request.aspire.contentTypeMapping.mapping.ice=x-conference/x-cooltalk
request.aspire.contentTypeMapping.mapping.ief=image/ief
request.aspire.contentTypeMapping.mapping.iges=model/iges
request.aspire.contentTypeMapping.mapping.igs=model/iges
request.aspire.contentTypeMapping.mapping.ips=application/x-ipscript
request.aspire.contentTypeMapping.mapping.ipx=application/x-ipix
request.aspire.contentTypeMapping.mapping.jpe=image/jpeg
request.aspire.contentTypeMapping.mapping.jpeg=image/jpeg
request.aspire.contentTypeMapping.mapping.jpg=image/jpeg
request.aspire.contentTypeMapping.mapping.js=application/x-javascript
request.aspire.contentTypeMapping.mapping.kar=audio/midi
request.aspire.contentTypeMapping.mapping.latex=application/x-latex
request.aspire.contentTypeMapping.mapping.lha=application/octet-stream
request.aspire.contentTypeMapping.mapping.lsp=application/x-lisp
request.aspire.contentTypeMapping.mapping.lzh=application/octet-stream
request.aspire.contentTypeMapping.mapping.m=text/plain
request.aspire.contentTypeMapping.mapping.man=application/x-troff-man
request.aspire.contentTypeMapping.mapping.me=application/x-troff-me
request.aspire.contentTypeMapping.mapping.mesh=model/mesh
request.aspire.contentTypeMapping.mapping.mid=audio/midi
request.aspire.contentTypeMapping.mapping.midi=audio/midi
request.aspire.contentTypeMapping.mapping.mif=application/vnd.mif
request.aspire.contentTypeMapping.mapping.mime=www/mime
request.aspire.contentTypeMapping.mapping.mov=video/quicktime
request.aspire.contentTypeMapping.mapping.movie=video/x-sgi-movie
request.aspire.contentTypeMapping.mapping.mp2=audio/mpeg
request.aspire.contentTypeMapping.mapping.mp3=audio/mpeg
request.aspire.contentTypeMapping.mapping.mpe=video/mpeg
request.aspire.contentTypeMapping.mapping.mpeg=video/mpeg
request.aspire.contentTypeMapping.mapping.mpg=video/mpeg
request.aspire.contentTypeMapping.mapping.mpga=audio/mpeg
request.aspire.contentTypeMapping.mapping.ms=application/x-troff-ms
request.aspire.contentTypeMapping.mapping.msh=model/mesh
request.aspire.contentTypeMapping.mapping.nc=application/x-netcdf
request.aspire.contentTypeMapping.mapping.oda=application/oda
request.aspire.contentTypeMapping.mapping.pbm=image/x-portable-bitmap
request.aspire.contentTypeMapping.mapping.pdb=chemical/x-pdb
request.aspire.contentTypeMapping.mapping.pdf=application/pdf
request.aspire.contentTypeMapping.mapping.pgm=image/x-portable-graymap
request.aspire.contentTypeMapping.mapping.pgn=application/x-chess-pgn
request.aspire.contentTypeMapping.mapping.png=image/png
request.aspire.contentTypeMapping.mapping.pnm=image/x-portable-anymap
request.aspire.contentTypeMapping.mapping.pot=application/mspowerpoint
request.aspire.contentTypeMapping.mapping.ppm=image/x-portable-pixmap
request.aspire.contentTypeMapping.mapping.pps=application/mspowerpoint
request.aspire.contentTypeMapping.mapping.ppt=application/mspowerpoint
request.aspire.contentTypeMapping.mapping.ppz=application/mspowerpoint
request.aspire.contentTypeMapping.mapping.pre=application/x-freelance
request.aspire.contentTypeMapping.mapping.prt=application/pro_eng
request.aspire.contentTypeMapping.mapping.ps=application/postscript
request.aspire.contentTypeMapping.mapping.qt=video/quicktime
request.aspire.contentTypeMapping.mapping.ra=audio/x-realaudio
request.aspire.contentTypeMapping.mapping.ram=audio/x-pn-realaudio
request.aspire.contentTypeMapping.mapping.ras=image/cmu-raster
request.aspire.contentTypeMapping.mapping.rgb=image/x-rgb
request.aspire.contentTypeMapping.mapping.rm=audio/x-pn-realaudio
request.aspire.contentTypeMapping.mapping.roff=application/x-troff
request.aspire.contentTypeMapping.mapping.rpm=audio/x-pn-realaudio-plugin
request.aspire.contentTypeMapping.mapping.rtf=text/rtf
request.aspire.contentTypeMapping.mapping.rtx=text/richtext
request.aspire.contentTypeMapping.mapping.scm=application/x-lotusscreencam
request.aspire.contentTypeMapping.mapping.set=application/set
request.aspire.contentTypeMapping.mapping.sgm=text/sgml
request.aspire.contentTypeMapping.mapping.sgml=text/sgml
request.aspire.contentTypeMapping.mapping.sh=application/x-sh
request.aspire.contentTypeMapping.mapping.shar=application/x-shar
request.aspire.contentTypeMapping.mapping.silo=model/mesh
request.aspire.contentTypeMapping.mapping.sit=application/x-stuffit
request.aspire.contentTypeMapping.mapping.skd=application/x-koan
request.aspire.contentTypeMapping.mapping.skm=application/x-koan
request.aspire.contentTypeMapping.mapping.skp=application/x-koan
request.aspire.contentTypeMapping.mapping.skt=application/x-koan
request.aspire.contentTypeMapping.mapping.smi=application/smil
request.aspire.contentTypeMapping.mapping.smil=application/smil
request.aspire.contentTypeMapping.mapping.snd=audio/basic
request.aspire.contentTypeMapping.mapping.sol=application/solids
request.aspire.contentTypeMapping.mapping.spl=application/x-futuresplash
request.aspire.contentTypeMapping.mapping.src=application/x-wais-source
request.aspire.contentTypeMapping.mapping.step=application/STEP
request.aspire.contentTypeMapping.mapping.stl=application/SLA
request.aspire.contentTypeMapping.mapping.stp=application/STEP
request.aspire.contentTypeMapping.mapping.sv4cpio=application/x-sv4cpio
request.aspire.contentTypeMapping.mapping.sv4crc=application/x-sv4crc
request.aspire.contentTypeMapping.mapping.swf=application/x-shockwave-flash
request.aspire.contentTypeMapping.mapping.t=application/x-troff
request.aspire.contentTypeMapping.mapping.tar=application/x-tar
request.aspire.contentTypeMapping.mapping.tcl=application/x-tcl
request.aspire.contentTypeMapping.mapping.tex=application/x-tex
request.aspire.contentTypeMapping.mapping.texi=application/x-texinfo
request.aspire.contentTypeMapping.mapping.texinfo=pplication/x-texinfo
request.aspire.contentTypeMapping.mapping.tif=image/tiff
request.aspire.contentTypeMapping.mapping.tiff=image/tiff
request.aspire.contentTypeMapping.mapping.tr=application/x-troff
request.aspire.contentTypeMapping.mapping.tsi=audio/TSP-audio
request.aspire.contentTypeMapping.mapping.tsp=application/dsptype
request.aspire.contentTypeMapping.mapping.tsv=text/tab-separated-values
request.aspire.contentTypeMapping.mapping.txt=text/plain
request.aspire.contentTypeMapping.mapping.unv=application/i-deas
request.aspire.contentTypeMapping.mapping.ustar=application/x-ustar
request.aspire.contentTypeMapping.mapping.vcd=application/x-cdlink
request.aspire.contentTypeMapping.mapping.vda=application/vda
request.aspire.contentTypeMapping.mapping.viv=video/vnd.vivo
request.aspire.contentTypeMapping.mapping.vivo=video/vnd.vivo
request.aspire.contentTypeMapping.mapping.vrml=model/vrml
request.aspire.contentTypeMapping.mapping.wav=audio/x-wav
request.aspire.contentTypeMapping.mapping.wrl=model/vrml
request.aspire.contentTypeMapping.mapping.xbm=image/x-xbitmap
request.aspire.contentTypeMapping.mapping.xlc=application/vnd.ms-excel
request.aspire.contentTypeMapping.mapping.xll=application/vnd.ms-excel
request.aspire.contentTypeMapping.mapping.xlm=application/vnd.ms-excel
request.aspire.contentTypeMapping.mapping.xls=application/vnd.ms-excel
request.aspire.contentTypeMapping.mapping.xlw=application/vnd.ms-excel
request.aspire.contentTypeMapping.mapping.xml=text/xml
request.aspire.contentTypeMapping.mapping.xpm=image/x-xpixmap
request.aspire.contentTypeMapping.mapping.xwd=image/x-xwindowdump
request.aspire.contentTypeMapping.mapping.xyz=chemical/x-pdb
request.aspire.contentTypeMapping.mapping.zip=application/zip