コード例 #1
0
  @Post("application/x-www-form-urlencoded|multipart/form-data")
  public Representation postWebFormMedia(Representation entity) {
    setServerHeader();

    Request request = getRequest();
    //		The HTTP API sets the headers
    //		addCORSHeaders(request);

    String auth = getQueryValue(Constants.AUTH_QUERY);

    String userId = null;
    String token = null;

    try {
      userId = getUserId(request, auth);
      token = getTransactionId(request, auth);
    } catch (Throwable t) {
      setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      return new StringRepresentation(t.getLocalizedMessage(), MediaType.APPLICATION_JSON);
    }

    Representation checkRequest =
        checkRequest(userId, token, request.getResourceRef().getIdentifier());
    if (checkRequest != null) {
      return checkRequest;
    }

    MediaDAO mediaDAO = DAOFactory.getInstance().getDAO();

    String entityId = (String) request.getAttributes().get(Constants.ENTITY_ARG);

    String result = "";
    try {
      if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
        result = mediaDAO.insertFormDataMedia(userId, entityId, getRequest(), false);
      } else {
        result = mediaDAO.insertWebFormMedia(userId, entityId, new Form(entity), false);
      }

      setStatus(Status.SUCCESS_CREATED);
    } catch (FileUploadException e) {
      setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
    } catch (UserNotAllowedException e) {
      setStatus(Status.CLIENT_ERROR_FORBIDDEN);
    } catch (Throwable t) {
      return unexpectedError(t);
    }

    return new StringRepresentation(result, MediaType.APPLICATION_JSON);
  }
コード例 #2
0
  /** Gets media's information list (GET /<channel>) */
  @Get
  public Representation getMediasInfo() {
    setServerHeader();
    //		The HTTP API sets the headers
    //		addCORSHeaders();

    Request request = getRequest();

    String userId = null;
    String token = null;

    String entityId = (String) request.getAttributes().get(Constants.ENTITY_ARG);

    boolean isChannelPublic = XMPPToolBox.getInstance().getPubSubClient().isChannelPublic(entityId);

    if (!isChannelPublic) {
      String auth = getQueryValue(Constants.AUTH_QUERY);

      try {
        userId = getUserId(request, auth);
        token = getTransactionId(request, auth);
      } catch (Throwable t) {
        setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        return new StringRepresentation(
            "Error while getting auth params", MediaType.APPLICATION_JSON);
      }

      Representation verifyRequest =
          checkRequest(userId, token, request.getResourceRef().getIdentifier());
      if (verifyRequest != null) {
        return verifyRequest;
      }
    }

    Integer max = null;
    String after = null;

    try {
      String queryValue = getQueryValue(Constants.MAX_QUERY);
      if (queryValue != null) {
        max = Integer.valueOf(queryValue);
      }

      after = getQueryValue(Constants.AFTER_QUERY);
    } catch (Throwable t) {
      setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      return new StringRepresentation("Invalid query value!", MediaType.APPLICATION_JSON);
    }

    MediaDAO mediaDAO = DAOFactory.getInstance().getDAO();

    try {
      return new StringRepresentation(
          mediaDAO.getMediasInfo(userId, entityId, max, after), MediaType.APPLICATION_JSON);
    } catch (MetadataSourceException e) {
      setStatus(Status.SERVER_ERROR_INTERNAL);
    } catch (UserNotAllowedException e) {
      setStatus(Status.CLIENT_ERROR_FORBIDDEN);
    } catch (Throwable t) {
      return unexpectedError(t);
    }

    return new EmptyRepresentation();
  }