private String guessMimeTypeFromContents ( final ChannelBuffer buf ) { if ( ! buf . readable ( ) ) { logWarn ( "Sending an empty result?! buf=" + buf ) ; return "text/plain" ; } final int firstbyte = buf . getUnsignedByte ( buf . readerIndex ( ) ) ; switch ( firstbyte ) { case ' ' : // <html or <!DOCTYPE return HTML_CONTENT_TYPE ; case ' ' : // JSON object case ' ' : // JSON array return "application/json" ; // RFC 4627 section 6 mandates this. case 0x89 : // magic number in PNG files. return "image/png" ; } return "text/plain" ; // Default. }
Simple content sniffing . May not be a great idea but will do until this class has a better API .