/** * Build Lucene document from database document. * * @param document Document * @return Document */ private org.apache.lucene.document.Document getDocumentFromDocument(Document document) { org.apache.lucene.document.Document luceneDocument = new org.apache.lucene.document.Document(); luceneDocument.add(new StringField("id", document.getId(), Field.Store.YES)); luceneDocument.add(new StringField("user_id", document.getUserId(), Field.Store.YES)); luceneDocument.add(new StringField("type", "document", Field.Store.YES)); if (document.getTitle() != null) { luceneDocument.add(new TextField("title", document.getTitle(), Field.Store.NO)); } if (document.getDescription() != null) { luceneDocument.add(new TextField("description", document.getDescription(), Field.Store.NO)); } return luceneDocument; }
/** * Add a file to a document. * * @param documentId Document ID * @param fileBodyPart File to add * @return Response * @throws JSONException */ @PUT @Consumes("multipart/form-data") @Produces(MediaType.APPLICATION_JSON) public Response add( @FormDataParam("id") String documentId, @FormDataParam("file") FormDataBodyPart fileBodyPart) throws JSONException { if (!authenticate()) { throw new ForbiddenClientException(); } // Validate input data ValidationUtil.validateRequired(documentId, "id"); ValidationUtil.validateRequired(fileBodyPart, "file"); // Get the document DocumentDao documentDao = new DocumentDao(); FileDao fileDao = new FileDao(); UserDao userDao = new UserDao(); Document document; User user; try { document = documentDao.getDocument(documentId, principal.getId()); user = userDao.getById(principal.getId()); } catch (NoResultException e) { throw new ClientException( "DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); } // Keep unencrypted data in memory, because we will need it two times byte[] fileData; try { fileData = ByteStreams.toByteArray(fileBodyPart.getValueAs(InputStream.class)); } catch (IOException e) { throw new ServerException("StreamError", "Error reading the input file", e); } InputStream fileInputStream = new ByteArrayInputStream(fileData); // Validate mime type String mimeType; try { mimeType = MimeTypeUtil.guessMimeType(fileInputStream); } catch (Exception e) { throw new ServerException("ErrorGuessMime", "Error guessing mime type", e); } if (mimeType == null) { throw new ClientException("InvalidFileType", "File type not recognized"); } try { // Get files of this document int order = 0; for (File file : fileDao.getByDocumentId(documentId)) { file.setOrder(order++); } // Create the file File file = new File(); file.setOrder(order); file.setDocumentId(document.getId()); file.setMimeType(mimeType); String fileId = fileDao.create(file); // Save the file FileUtil.save(fileInputStream, file, user.getPrivateKey()); // Raise a new file created event FileCreatedAsyncEvent fileCreatedAsyncEvent = new FileCreatedAsyncEvent(); fileCreatedAsyncEvent.setDocument(document); fileCreatedAsyncEvent.setFile(file); fileCreatedAsyncEvent.setInputStream(fileInputStream); AppContext.getInstance().getAsyncEventBus().post(fileCreatedAsyncEvent); // Always return ok JSONObject response = new JSONObject(); response.put("status", "ok"); response.put("id", fileId); return Response.ok().entity(response).build(); } catch (Exception e) { throw new ServerException("FileError", "Error adding a file", e); } }