Package org.waveprotocol.box.server.persistence.AttachmentStore

Examples of org.waveprotocol.box.server.persistence.AttachmentStore.AttachmentData


  public void testStoreCanStoreData() throws Exception {
    String testData = "some file data";
    AttachmentId id = new AttachmentId("", "id_1");
    AttachmentStore store = makeStoreWithData(id, testData);

    AttachmentData data = store.getAttachment(id);
    assertEquals(testData, dataToString(data));
  }
View Full Code Here


  public void testContentLengthMatchesDataSize() throws Exception {
    String testData = "blah blah blah";
    AttachmentId id = new AttachmentId("", "id_2");
    AttachmentStore store = makeStoreWithData(id, testData);

    AttachmentData data = store.getAttachment(id);
    assertEquals(testData.length(), data.getSize());
  }
View Full Code Here

    String testData = "some day, I'm going to run out of test strings";
    AttachmentId id = new AttachmentId("", "id_3");
    AttachmentStore store = makeStoreWithData(id, testData);

    store.deleteAttachment(id);
    AttachmentData data = store.getAttachment(id);
    assertNull(data);
  }
View Full Code Here

  public void testAttachmentCanWriteToOutputStream() throws Exception {
    String testData = "maybe there's some easy way to generate test strings";
    AttachmentId id = new AttachmentId("", "id_4");
    AttachmentStore store = makeStoreWithData(id, testData);
    AttachmentData data = store.getAttachment(id);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    InputStream io = data.getInputStream();
    try {
      AttachmentUtil.writeTo(io, stream);
      assertEquals(testData, stream.toString("UTF-8"));
    } finally {
      io.close();
View Full Code Here

  public void testAttachmentHasWorkingInputStream() throws Exception {
    String testData = "I suppose these strings don't actually need to be different";
    AttachmentId id = new AttachmentId("", "id_5");
    AttachmentStore store = makeStoreWithData(id, testData);
    AttachmentData data = store.getAttachment(id);

    BufferedReader reader = new BufferedReader(new InputStreamReader(data.getInputStream()));

    StringBuilder builder = new StringBuilder();
    String line;
    try {
      while ((line = reader.readLine()) != null) {
View Full Code Here

  public void testGetStreamReturnsNewStream() throws Exception {
    String testData = "There's something quite peaceful about writing tests.";
    AttachmentId id = new AttachmentId("", "id_6");
    AttachmentStore store = makeStoreWithData(id, testData);
    AttachmentData data = store.getAttachment(id);

    InputStream is1 = data.getInputStream();
    InputStream is2 = data.getInputStream();
    InputStream is3 = null;
    try {
      assertNotSame(is1, is2);

      int firstByte = is1.read();
      assertSame(firstByte, is2.read());

      // Check that a new input stream created now still has the same first
      // byte.
      is3 = data.getInputStream();
      assertSame(firstByte, is3.read());
    } finally {
      is1.close();
      is2.close();
      if (is3 != null) {
View Full Code Here

    if (metadata == null) {
      metadata = service.buildAndStoreMetadataWithThumbnail(attachmentId, waveletName, fileName, null);
    }

    String contentType;
    AttachmentData data;
    if (request.getRequestURI().startsWith(ATTACHMENT_URL)) {
      contentType = metadata.getMimeType();
      data = service.getAttachment(attachmentId);
      if (data == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }
    } else if (request.getRequestURI().startsWith(THUMBNAIL_URL)) {
      if (metadata.hasImageMetadata()) {
        contentType = AttachmentService.THUMBNAIL_MIME_TYPE;
        data = service.getThumbnail(attachmentId);
        if (data == null) {
          response.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
        }
      } else {
        contentType = THUMBNAIL_PATTERN_FORMAT_NAME;
        data = getThumbnailByContentType(metadata.getMimeType());
      }
    } else {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
    if (data == null) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    response.setContentType(contentType);
    response.setContentLength((int)data.getSize());
    response.setHeader("Content-Disposition", "attachment; filename=\"" + metadata.getFileName() + "\"");
    response.setStatus(HttpServletResponse.SC_OK);
    response.setDateHeader("Last-Modified", Calendar.getInstance().getTimeInMillis());
    AttachmentUtil.writeTo(data.getInputStream(), response.getOutputStream());

    LOG.info("Fetched attachment with id '" + attachmentId + "'");
  }
View Full Code Here

    File file = new File(thumbnailPattternsDirectory, contentType.replaceAll("/", "_"));
    if (!file.exists()) {
      file = new File(thumbnailPattternsDirectory, THUMBNAIL_PATTERN_DEFAULT);
    }
    final File thumbFile = file;
    return new AttachmentData() {

      @Override
      public InputStream getInputStream() throws IOException {
        return new FileInputStream(thumbFile);
      }
View Full Code Here

    buildAndStoreMetadataWithThumbnail(attachmentId, waveletName, fileName, creator);
  }

  public AttachmentMetadata buildAndStoreMetadataWithThumbnail(AttachmentId attachmentId,
      WaveletName waveletName, String fileName, ParticipantId creator) throws IOException {
    AttachmentData data = store.getAttachment(attachmentId);
    if (data == null) {
      throw new IOException("No such atachment " + attachmentId.serialise());
    }
    AttachmentMetadata metadata = new AttachmentMetadataImpl();
    metadata.setAttachmentId(attachmentId.serialise());
    metadata.setAttachmentUrl(AttachmentServlet.ATTACHMENT_URL + "/" + attachmentId.serialise());
    metadata.setThumbnailUrl(AttachmentServlet.THUMBNAIL_URL + "/" + attachmentId.serialise());
    metadata.setWaveRef(waveletName2WaveRef(waveletName));
    metadata.setFileName(fileName);
    String contentType = getMimeType(fileName);
    metadata.setMimeType(contentType);
    metadata.setSize(data.getSize());
    metadata.setCreator((creator != null) ? creator.getAddress() : "");
    BufferedImage image = null;
    try {
      image = ImageIO.read(data.getInputStream());
    } catch (IOException ex) {
      LOG.log(Level.SEVERE, "Identifying attachment", ex);
    }
    if (image != null) {
      ImageMetadata imageMetadata = new ImageMetadataImpl();
View Full Code Here

    if (!isAuthorized) {
      response.sendError(HttpServletResponse.SC_FORBIDDEN);
      return;
    }

    AttachmentData data = store.getAttachment(waveletName, attachmentId);
    if (data == null) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
    response.setContentType(getMimeTypeByFileName(fileName));
    response.setContentLength((int) data.getContentSize());
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentLength((int) data.getContentSize());
    response.setDateHeader("Last-Modified", data.getLastModifiedDate().getTime());
    data.writeDataTo(response.getOutputStream());

    LOG.info("Fetched attachment with id '" + attachmentId + "'");
  }
View Full Code Here

TOP

Related Classes of org.waveprotocol.box.server.persistence.AttachmentStore.AttachmentData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.