/** * Returns the media type defined in the specified serialization parameters. * * @param sopts serialization parameters * @return media type */ public static String mediaType(final SerializerOptions sopts) { // set content type final String type = sopts.get(SerializerOptions.MEDIA_TYPE); if (!type.isEmpty()) return type; // determine content type dependent on output method final SerialMethod sm = sopts.get(SerializerOptions.METHOD); if (sm == SerialMethod.RAW) return APP_OCTET; if (sm == SerialMethod.XML) return APP_XML; if (sm == SerialMethod.XHTML || sm == SerialMethod.HTML) return TEXT_HTML; if (sm == SerialMethod.JSON) { final JsonSerialOptions jprop = sopts.get(SerializerOptions.JSON); return jprop.get(JsonOptions.FORMAT) == JsonFormat.JSONML ? APP_JSONML : APP_JSON; } return TEXT_PLAIN; }
/** Initializes the output. Sets the expected encoding and content type. */ public void initResponse() { // set content type and encoding final SerializerOptions opts = sopts(); final String enc = opts.get(SerializerOptions.ENCODING); res.setCharacterEncoding(enc); final String ct = mediaType(opts); res.setContentType(new TokenBuilder(ct).add(CHARSET).add(enc).toString()); }