Exemplo 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;
  }
Exemplo n.º 2
0
  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 void testAccMediaType() throws IOException {
    Response response = get("accMediaTypes", MediaType.TEXT_PLAIN);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("[" + MediaType.TEXT_PLAIN.toString() + "]", response.getEntity().getText());

    ClientInfo clientInfo = new ClientInfo();
    clientInfo.getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_PLAIN, 0.5f));
    clientInfo.getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_HTML, 0.8f));
    clientInfo.getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_XML, 0.2f));
    response = get("accMediaTypes", clientInfo);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals(
        "["
            + MediaType.TEXT_HTML.toString()
            + ", "
            + MediaType.TEXT_PLAIN.toString()
            + ", "
            + MediaType.TEXT_XML.toString()
            + "]",
        response.getEntity().getText());
  }