Ejemplo n.º 1
0
  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;
  }
Ejemplo n.º 2
0
 @Test
 public void isConcrete() {
   assertTrue("text/plain not concrete", MediaType.TEXT_PLAIN.isConcrete());
   assertFalse("*/* concrete", MediaType.ALL.isConcrete());
   assertFalse("text/* concrete", new MediaType("text", "*").isConcrete());
 }