@Override @SuppressWarnings("unchecked") public void writeTo(final Template template, final Viewable viewable, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream out) throws IOException { try { Object model = viewable.getModel(); if (!(model instanceof Map)) { model = new HashMap<String, Object>() {{ put("model", viewable.getModel()); }}; } VelocityContext velocityContext = new VelocityContext(); for (String key : ((Map<String, Object>) model).keySet()) { velocityContext.put(key, ((Map<String, Object>) model).get(key)); } Charset encoding = setContentType(mediaType, httpHeaders); VelocityWriter writer = new VelocityWriter(new OutputStreamWriter(out, encoding)); template.merge(velocityContext, writer); writer.flush(); } catch (VelocityException te) { throw new ContainerException(te); } }