protected Representation formatJSON(JSON json, Variant variant) { Representation r = null; // TODO: Firefox 2 seems to not send the specified application/json header // (especially after a redirect) // so we will send application/json always for now // (as we don't support this API any other way right now) if (true /* TODO: This is a hack */) { r = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); } else if (MediaType.APPLICATION_JSON.equals(variant.getMediaType()) || MediaType.APPLICATION_JAVASCRIPT.equals(variant.getMediaType()) || MediaType.TEXT_JAVASCRIPT.equals(variant.getMediaType())) { r = new StringRepresentation(json.toString(), variant.getMediaType()); // TODO: make this output streaming! (this library does not support it, which is fairly // crazy!) } else if (MediaType.APPLICATION_XML.equals(variant.getMediaType()) || MediaType.TEXT_XML.equals(variant.getMediaType())) { r = new StringRepresentation(new XMLSerializer().write(json), variant.getMediaType()); } else if (MediaType.TEXT_PLAIN.equals(variant.getMediaType()) || MediaType.TEXT_HTML.equals(variant.getMediaType())) { r = new StringRepresentation(json.toString(4), variant.getMediaType()); } else { getResponse().setStatus(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE); } r.setCharacterSet(CharacterSet.UTF_8); return r; }
protected XStreamRepresentation createRepresentation(Variant variant) throws ResourceException { XStreamRepresentation representation = null; try { // check is this variant a supported one, to avoid calling getText() on potentially huge // representations if (MediaType.APPLICATION_JSON.equals(variant.getMediaType(), true) || MediaType.APPLICATION_XML.equals(variant.getMediaType(), true) || MediaType.TEXT_HTML.equals(variant.getMediaType(), true)) { String text = (variant instanceof Representation) ? ((Representation) variant).getText() : ""; XStream xstream; if (MediaType.APPLICATION_JSON.equals(variant.getMediaType(), true) || MediaType.TEXT_HTML.equals(variant.getMediaType(), true)) { xstream = (XStream) getContext().getAttributes().get(PlexusRestletApplicationBridge.JSON_XSTREAM); } else if (MediaType.APPLICATION_XML.equals(variant.getMediaType(), true)) { xstream = (XStream) getContext().getAttributes().get(PlexusRestletApplicationBridge.XML_XSTREAM); } else { return null; } if (text != null) { CharacterSet charset = variant.getCharacterSet(); if (charset == null) { charset = CharacterSet.ISO_8859_1; } if (!CharacterSet.UTF_8.equals(charset)) { // must fix text encoding NXCM-2494 text = new String(new String(text.getBytes(), "UTF-8").getBytes(charset.getName())); } } representation = new XStreamRepresentation(xstream, text, variant.getMediaType()); return representation; } else { return null; } } catch (IOException e) { throw new ResourceException( Status.SERVER_ERROR_INTERNAL, "Cannot get the representation!", e); } }
public boolean write(final Object result, final Response response) throws ResourceException { MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType(); if (MediaType.APPLICATION_JSON.equals(type)) { if (result instanceof String || result instanceof Number || result instanceof Boolean) { StringRepresentation representation = new StringRepresentation(result.toString(), MediaType.APPLICATION_JSON); response.setEntity(representation); return true; } } return false; }