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());
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);
@Override public void execute(OperationRequest operation, OperationContext context, ParticipantId participant) throws InvalidRequestException { AttachmentId attachmentId; try { attachmentId = AttachmentId.deserialise(OperationUtil.<String>getRequiredParameter(operation, ParamsProperty.ATTACHMENT_ID)); } catch (InvalidIdException ex) { throw new InvalidRequestException("Invalid id", operation, ex); } AttachmentMetadata meta; byte[] data; try { meta = attachmentService.getMetadata(attachmentId); data = readInputStreamToBytes(attachmentService.getAttachment(attachmentId).getInputStream()); } catch (IOException ex) { LOG.info("Get attachment", ex); context.constructErrorResponse(operation, ex.toString()); return; } RawAttachmentData attachment = new RawAttachmentData(meta.getFileName(), meta.getCreator(), data); Map<ParamsProperty, Object> parameters = ImmutableMap.<ParamsProperty, Object> of(ParamsProperty.ATTACHMENT_DATA, attachment); context.constructResponse(operation, parameters); }
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) { is3.close(); } } }
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) { is3.close(); } } }
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) { // This little snippet will discard any "\n" characters, but it shouldn't // matter. builder.append(line); } } finally { reader.close(); } assertEquals(testData, builder.toString()); }
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) { // This little snippet will discard any "\n" characters, but it shouldn't // matter. builder.append(line); } } finally { reader.close(); } assertEquals(testData, builder.toString()); }
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(); } }
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(); } }
/** * Write the attachment out to a string. * * @param encoding The string encoding format of the data. Eg, "UTF-8". * @return A string representation of the attachment data. * @throws IOException */ public static String writeAttachmentDataToString( AttachmentData data, String encoding) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); writeTo(data.getInputStream(), stream); return stream.toString(encoding); }
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()); }
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()); }
protected String dataToString(AttachmentData data) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream io = data.getInputStream(); try { AttachmentUtil.writeTo(io, out); } finally { io.close(); } return out.toString("UTF-8"); } }
protected String dataToString(AttachmentData data) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream io = data.getInputStream(); try { AttachmentUtil.writeTo(io, out); } finally { io.close(); } return out.toString("UTF-8"); } }