예제 #1
0
  /**
   * Deletes a file.
   *
   * @param id File ID
   * @return Response
   * @throws JSONException
   */
  @DELETE
  @Path("{id: [a-z0-9\\-]+}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response delete(@PathParam("id") String id) throws JSONException {
    if (!authenticate()) {
      throw new ForbiddenClientException();
    }

    // Get the file
    FileDao fileDao = new FileDao();
    DocumentDao documentDao = new DocumentDao();
    File file;
    try {
      file = fileDao.getFile(id);
      documentDao.getDocument(file.getDocumentId(), principal.getId());
    } catch (NoResultException e) {
      throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", id));
    }

    // Delete the file
    fileDao.delete(file.getId());

    // Raise a new file deleted event
    FileDeletedAsyncEvent fileDeletedAsyncEvent = new FileDeletedAsyncEvent();
    fileDeletedAsyncEvent.setFile(file);
    AppContext.getInstance().getAsyncEventBus().post(fileDeletedAsyncEvent);

    // Always return ok
    JSONObject response = new JSONObject();
    response.put("status", "ok");
    return Response.ok().entity(response).build();
  }
예제 #2
0
  /**
   * Build Lucene document from file.
   *
   * @param file File
   * @param document Document linked to the file
   * @return Document
   */
  private org.apache.lucene.document.Document getDocumentFromFile(File file, Document document) {
    org.apache.lucene.document.Document luceneDocument = new org.apache.lucene.document.Document();
    luceneDocument.add(new StringField("id", file.getId(), Field.Store.YES));
    luceneDocument.add(new StringField("user_id", document.getUserId(), Field.Store.YES));
    luceneDocument.add(new StringField("type", "file", Field.Store.YES));
    luceneDocument.add(new StringField("document_id", file.getDocumentId(), Field.Store.YES));
    if (file.getContent() != null) {
      luceneDocument.add(new TextField("content", file.getContent(), Field.Store.NO));
    }

    return luceneDocument;
  }
예제 #3
0
  /**
   * Returns files linked to a document.
   *
   * @param documentId Document ID
   * @return Response
   * @throws JSONException
   */
  @GET
  @Path("list")
  @Produces(MediaType.APPLICATION_JSON)
  public Response list(@QueryParam("id") String documentId, @QueryParam("share") String shareId)
      throws JSONException {
    authenticate();

    // Check document visibility
    try {
      DocumentDao documentDao = new DocumentDao();
      Document document = documentDao.getDocument(documentId);
      ShareDao shareDao = new ShareDao();
      if (!shareDao.checkVisibility(document, principal.getId(), shareId)) {
        throw new ForbiddenClientException();
      }
    } catch (NoResultException e) {
      throw new ClientException(
          "DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId));
    }

    FileDao fileDao = new FileDao();
    List<File> fileList = fileDao.getByDocumentId(documentId);

    JSONObject response = new JSONObject();
    List<JSONObject> files = new ArrayList<>();

    for (File fileDb : fileList) {
      JSONObject file = new JSONObject();
      file.put("id", fileDb.getId());
      file.put("mimetype", fileDb.getMimeType());
      file.put("document_id", fileDb.getDocumentId());
      file.put("create_date", fileDb.getCreateDate().getTime());
      files.add(file);
    }

    response.put("files", files);
    return Response.ok().entity(response).build();
  }
예제 #4
0
  /**
   * Reorder files.
   *
   * @param documentId Document ID
   * @param idList List of files ID in the new order
   * @return Response
   * @throws JSONException
   */
  @POST
  @Path("reorder")
  @Produces(MediaType.APPLICATION_JSON)
  public Response reorder(
      @FormParam("id") String documentId, @FormParam("order") List<String> idList)
      throws JSONException {
    if (!authenticate()) {
      throw new ForbiddenClientException();
    }

    // Validate input data
    ValidationUtil.validateRequired(documentId, "id");
    ValidationUtil.validateRequired(idList, "order");

    // Get the document
    DocumentDao documentDao = new DocumentDao();
    try {
      documentDao.getDocument(documentId, principal.getId());
    } catch (NoResultException e) {
      throw new ClientException(
          "DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId));
    }

    // Reorder files
    FileDao fileDao = new FileDao();
    for (File file : fileDao.getByDocumentId(documentId)) {
      int order = idList.lastIndexOf(file.getId());
      if (order != -1) {
        file.setOrder(order);
      }
    }

    // Always return ok
    JSONObject response = new JSONObject();
    response.put("status", "ok");
    return Response.ok().entity(response).build();
  }