Simple "content sniffing". May not be a great idea, but will do until this class has a better API. @param buf The content of the reply to send. @return The MIME type guessed from buf.
(final ChannelBuffer buf)
| 752 | * @return The MIME type guessed from {@code buf}. |
| 753 | */ |
| 754 | private String guessMimeTypeFromContents(final ChannelBuffer buf) { |
| 755 | if (!buf.readable()) { |
| 756 | logWarn("Sending an empty result?! buf=" + buf); |
| 757 | return "text/plain"; |
| 758 | } |
| 759 | final int firstbyte = buf.getUnsignedByte(buf.readerIndex()); |
| 760 | switch (firstbyte) { |
| 761 | case '<': // <html or <!DOCTYPE |
| 762 | return HTML_CONTENT_TYPE; |
| 763 | case '{': // JSON object |
| 764 | case '[': // JSON array |
| 765 | return "application/json"; // RFC 4627 section 6 mandates this. |
| 766 | case 0x89: // magic number in PNG files. |
| 767 | return "image/png"; |
| 768 | } |
| 769 | return "text/plain"; // Default. |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Loads the serializer maps with present, implemented serializers. If no |