Search in sources :

Example 1 with Attach

use of net.fortuna.ical4j.model.property.Attach in project zm-mailbox by Zimbra.

the class TestUserServlet method testIcsImportExport.

/**
     * Test that can import into a new calendar from ICALENDAR containing an inline ATTACHment.
     * Test that it is possible to export calendar entry with an attachment with the attachment
     * inlined if icalAttach=inline or ignoring the attachment if icalAttach=none
     */
@Test
public void testIcsImportExport() throws IOException, ValidationException, ServiceException {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String calName = NAME_PREFIX + "2ndCalendar";
    String calUri = String.format("/%s?fmt=ics", calName);
    TestUtil.createFolder(mbox, calName, ZFolder.View.appointment);
    net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
    calendar.getProperties().add(new ProdId("-//ZimbraTest 1.0//EN"));
    calendar.getProperties().add(Version.VERSION_2_0);
    calendar.getProperties().add(CalScale.GREGORIAN);
    java.util.Calendar start = java.util.Calendar.getInstance();
    start.set(java.util.Calendar.MONTH, java.util.Calendar.SEPTEMBER);
    start.set(java.util.Calendar.DAY_OF_MONTH, 03);
    VEvent wwII = new VEvent(new Date(start.getTime()), NAME_PREFIX + " Declarations of war");
    wwII.getProperties().getProperty(Property.DTSTART).getParameters().add(Value.DATE);
    wwII.getProperties().add(new Uid("3-14159"));
    Attach attach = new Attach("Attachment.\nIsn't it short.".getBytes(MimeConstants.P_CHARSET_ASCII));
    attach.getParameters().add(new XParameter("X-APPLE-FILENAME", "short.txt"));
    attach.getParameters().add(new FmtType(MimeConstants.CT_TEXT_PLAIN));
    wwII.getProperties().add(attach);
    calendar.getComponents().add(wwII);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    CalendarOutputter outputter = new CalendarOutputter();
    outputter.setValidating(false);
    outputter.output(calendar, buf);
    URI uri = mbox.getRestURI(calUri);
    HttpClient client = mbox.getHttpClient(uri);
    PostMethod post = new PostMethod(uri.toString());
    post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(buf.toByteArray()), MimeConstants.CT_TEXT_CALENDAR));
    ZimbraLog.test.info("testIcsImportExport:ICS to be imported:%s", new String(buf.toByteArray()));
    TestCalDav.HttpMethodExecutor.execute(client, post, HttpStatus.SC_OK);
    uri = mbox.getRestURI(calUri + "&icalAttach=inline");
    GetMethod get = new GetMethod(uri.toString());
    TestCalDav.HttpMethodExecutor executor = new TestCalDav.HttpMethodExecutor(client, get, HttpStatus.SC_OK);
    String respIcal = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    ZimbraLog.test.info("testIcsImportExport:ICS exported (with icalAttach=inline):%s", respIcal);
    int attachNdx = respIcal.indexOf("ATTACH;");
    Assert.assertTrue("ATTACH should be present", -1 != attachNdx);
    String fromAttach = respIcal.substring(attachNdx);
    Assert.assertTrue("BINARY should be present", -1 != fromAttach.indexOf("VALUE=BINARY"));
    uri = mbox.getRestURI(calUri + "&icalAttach=none");
    get = new GetMethod(uri.toString());
    executor = new TestCalDav.HttpMethodExecutor(client, get, HttpStatus.SC_OK);
    respIcal = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    ZimbraLog.test.debug("testIcsImportExport:ICS exported (with icalAttach=none):%s", respIcal);
    Assert.assertTrue("ATTACH should be present", -1 == respIcal.indexOf("ATTACH;"));
    uri = mbox.getRestURI(calUri);
    get = new GetMethod(uri.toString());
    executor = new TestCalDav.HttpMethodExecutor(client, get, HttpStatus.SC_OK);
    respIcal = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    ZimbraLog.test.debug("testIcsImportExport:ICS exported (default - same as icalAttach=none):%s", respIcal);
    Assert.assertTrue("ATTACH should be present", -1 == respIcal.indexOf("ATTACH;"));
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ProdId(net.fortuna.ical4j.model.property.ProdId) URI(java.net.URI) ZMailbox(com.zimbra.client.ZMailbox) XParameter(net.fortuna.ical4j.model.parameter.XParameter) VEvent(net.fortuna.ical4j.model.component.VEvent) Attach(net.fortuna.ical4j.model.property.Attach) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(net.fortuna.ical4j.model.Date) Uid(net.fortuna.ical4j.model.property.Uid) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) HttpClient(org.apache.commons.httpclient.HttpClient) FmtType(net.fortuna.ical4j.model.parameter.FmtType) GetMethod(org.apache.commons.httpclient.methods.GetMethod) CalendarOutputter(net.fortuna.ical4j.data.CalendarOutputter) Test(org.junit.Test)

Aggregations

ZMailbox (com.zimbra.client.ZMailbox)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URI (java.net.URI)1 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)1 CalendarOutputter (net.fortuna.ical4j.data.CalendarOutputter)1 Date (net.fortuna.ical4j.model.Date)1 VEvent (net.fortuna.ical4j.model.component.VEvent)1 FmtType (net.fortuna.ical4j.model.parameter.FmtType)1 XParameter (net.fortuna.ical4j.model.parameter.XParameter)1 Attach (net.fortuna.ical4j.model.property.Attach)1 ProdId (net.fortuna.ical4j.model.property.ProdId)1 Uid (net.fortuna.ical4j.model.property.Uid)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)1 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 Test (org.junit.Test)1