private void fetch() throws IOException { if (getAttachmentUrl() != null && (!getAttachmentUrl().isEmpty())) { InputStream input = null; ByteArrayOutputStream output = null; try { URL url = new URL(getAttachmentUrl()); input = url.openStream(); output = new ByteArrayOutputStream(); int i; while ((i = input.read()) != -1) { output.write(i); } this.data = output.toByteArray(); } finally { if (input != null) { input.close(); } if (output != null) { output.close(); } } } } }
/** * Constructs an attachment with data in bytes and caption. * * @param caption the caption of the attachment. * @param data the attachment data as bytes. */ public Attachment(String caption, byte[] data) { super(ElementType.ATTACHMENT); setCaption(caption); this.data = data; }
/** * Sets the caption for the attachment. * * @param caption the caption to display for the attachment. */ public void setCaption(String caption) { setProperty(CAPTION, caption); }
public void testSetProperty(){ Attachment attachment = new Attachment(CAPTION, DATA); try { attachment.setProperty(Attachment.ATTACHMENT_ID, "attachment1"); fail("Should have thrown exception when trying to set attachment id."); } catch (IllegalArgumentException e) { attachment.setProperty(Attachment.MIME_TYPE, "m1"); fail("Should have thrown exception when trying to set mime type."); } catch (IllegalArgumentException e) { attachment.setProperty(Attachment.ATTACHMENT_URL, "a_url"); fail("Should have thrown exception when trying to set attachment url."); } catch (IllegalArgumentException e) { attachment.setProperty(Attachment.DATA, "data"); fail("Should have thrown exception when trying to set data."); } catch (IllegalArgumentException e) { attachment.setCaption("new caption"); assertEquals("new caption", attachment.getCaption());
public void testConstructorWithCaptionAndData() { Attachment attachment = new Attachment(CAPTION, DATA); assertEquals(ElementType.ATTACHMENT, attachment.getType()); assertEquals(CAPTION, attachment.getCaption()); assertEquals(DATA, attachment.getData()); }
public void testAttachmentSerialization() { convertBackAndForth("<image attachment=\"id\"></image>"); convertBackAndForth("<image attachment=\"id\"><caption>caption</caption></image>"); Element element = createApiElementFromXml( "<image attachment=\"id\"><gadge>something</gadge>" + "<caption>caption</caption><fake>fake</fake></image>", createWavelet("id")); assertTrue(element instanceof Attachment); assertEquals("caption", ((Attachment) element).getCaption()); assertEquals("id", ((Attachment) element).getAttachmentId()); assertEquals(ATTACHMENT_MIMETYPE, ((Attachment) element).getMimeType()); assertEquals(ATTACHMENT_URL, ((Attachment) element).getAttachmentUrl()); }
@Override public Element fromXml(Document doc, E element, Wavelet wavelet) { Map<String, String> properties = Maps.newHashMap(); String attachmentId = doc.getAttribute(element, ATTACHMENT_STR); if (attachmentId != null) { properties.put(Attachment.ATTACHMENT_ID, attachmentId); } String caption = getCaption(doc, element); if (caption != null) { properties.put(Attachment.CAPTION, caption); } if (wavelet != null && attachmentId != null) { Document attachmentDataDoc = wavelet.getDocument(IdConstants.ATTACHMENT_METADATA_PREFIX + "+" + attachmentId); if (attachmentDataDoc != null) { String dataDocument = attachmentDataDoc.toXmlString(); if (dataDocument != null) { properties.put(Attachment.MIME_TYPE, extractValue(dataDocument, MIME_TYPE_PATTERN)); properties.put(Attachment.ATTACHMENT_URL, ElementSerializer.attachmentDownloadHostUrl + getAttachmentUrl(dataDocument)); } } } return new Attachment(properties, null); }
public void testElementMatcherWithRestrictions() { String xml = "text <image attachment=\"nR_0YFT75\" style=\"full\">" + "<caption>good times</caption></image> hello"; ApiView apiView = createApiViewFromXml(xml); Map<String, String> restrictions = Maps.newHashMap(); assertEquals(1, countHits(new DocumentHitIterator.ElementMatcher( apiView, ElementType.ATTACHMENT, Collections.<String, String>emptyMap(), -1))); Restriction restriction = Attachment.restrictByAttachmentId("nR_0YFT75"); assertEquals(1, countHits(new DocumentHitIterator.ElementMatcher(apiView, ElementType.ATTACHMENT, ImmutableMap.of(restriction.getKey(), restriction.getValue()), -1))); restriction = Attachment.restrictByAttachmentId("nR_0YFT76"); assertEquals(0, countHits(new DocumentHitIterator.ElementMatcher(apiView, ElementType.ATTACHMENT, ImmutableMap.of(restriction.getKey(), restriction.getValue()), -1))); }
public void testSetProperty(){ Attachment attachment = new Attachment(CAPTION, DATA); try { attachment.setProperty(Attachment.ATTACHMENT_ID, "attachment1"); fail("Should have thrown exception when trying to set attachment id."); } catch (IllegalArgumentException e) { attachment.setProperty(Attachment.MIME_TYPE, "m1"); fail("Should have thrown exception when trying to set mime type."); } catch (IllegalArgumentException e) { attachment.setProperty(Attachment.ATTACHMENT_URL, "a_url"); fail("Should have thrown exception when trying to set attachment url."); } catch (IllegalArgumentException e) { attachment.setProperty(Attachment.DATA, "data"); fail("Should have thrown exception when trying to set data."); } catch (IllegalArgumentException e) { attachment.setCaption("new caption"); assertEquals("new caption", attachment.getCaption());
public void testConstructorWithCaptionAndData() { Attachment attachment = new Attachment(CAPTION, DATA); assertEquals(ElementType.ATTACHMENT, attachment.getType()); assertEquals(CAPTION, attachment.getCaption()); assertEquals(DATA, attachment.getData()); }
public void testAttachmentSerialization() { convertBackAndForth("<image attachment=\"id\"></image>"); convertBackAndForth("<image attachment=\"id\"><caption>caption</caption></image>"); Element element = createApiElementFromXml( "<image attachment=\"id\"><gadge>something</gadge>" + "<caption>caption</caption><fake>fake</fake></image>", createWavelet("id")); assertTrue(element instanceof Attachment); assertEquals("caption", ((Attachment) element).getCaption()); assertEquals("id", ((Attachment) element).getAttachmentId()); assertEquals(ATTACHMENT_MIMETYPE, ((Attachment) element).getMimeType()); assertEquals(ATTACHMENT_URL, ((Attachment) element).getAttachmentUrl()); }
result = new Attachment(properties, data); } else if (type == ElementType.LINE) { result = new Line(properties);
public void testElementMatcherWithRestrictions() { String xml = "text <image attachment=\"nR_0YFT75\" style=\"full\">" + "<caption>good times</caption></image> hello"; ApiView apiView = createApiViewFromXml(xml); Map<String, String> restrictions = Maps.newHashMap(); assertEquals(1, countHits(new DocumentHitIterator.ElementMatcher( apiView, ElementType.ATTACHMENT, Collections.<String, String>emptyMap(), -1))); Restriction restriction = Attachment.restrictByAttachmentId("nR_0YFT75"); assertEquals(1, countHits(new DocumentHitIterator.ElementMatcher(apiView, ElementType.ATTACHMENT, ImmutableMap.of(restriction.getKey(), restriction.getValue()), -1))); restriction = Attachment.restrictByAttachmentId("nR_0YFT76"); assertEquals(0, countHits(new DocumentHitIterator.ElementMatcher(apiView, ElementType.ATTACHMENT, ImmutableMap.of(restriction.getKey(), restriction.getValue()), -1))); }