Search in sources :

Example 1 with FmtType

use of net.fortuna.ical4j.model.parameter.FmtType in project opencast by opencast.

the class CalendarGenerator method addEvent.

/**
 * Adds an SchedulerEvent as a new entry to this iCalendar
 *
 * @param mp
 *          {@link MediaPackage} of event
 * @param agentId
 *          the agent identifier
 * @param start
 *          the start date
 * @param end
 *          the end date
 * @param captureAgentMetadata
 *          properties for capture agent metadata
 *
 * @return true if the event could be added.
 */
public boolean addEvent(MediaPackage mp, DublinCoreCatalog catalog, String agentId, Date start, Date end, Date lastModified, String captureAgentMetadata) {
    String eventId = mp.getIdentifier().compact();
    logger.debug("Creating iCaleandar VEvent from scheduled event '{}'", eventId);
    DateTime startDate = new DateTime(start);
    DateTime endDate = new DateTime(end);
    Date marginEndDate = new org.joda.time.DateTime(endDate.getTime()).plusHours(1).toDate();
    if (marginEndDate.before(new Date())) {
        logger.debug("Event has already passed more than an hour, skipping!");
        return false;
    }
    startDate.setUtc(true);
    endDate.setUtc(true);
    String seriesID = null;
    VEvent event = new VEvent(startDate, endDate, catalog.getFirst(DublinCore.PROPERTY_TITLE));
    try {
        ParameterList pl = new ParameterList();
        if (StringUtils.isNotEmpty(catalog.getFirst(DublinCore.PROPERTY_CREATOR))) {
            pl.add(new Cn(catalog.getFirst(DublinCore.PROPERTY_CREATOR)));
        }
        event.getProperties().add(new Uid(eventId));
        DateTime lastModifiedDate = new DateTime(lastModified);
        lastModifiedDate.setUtc(true);
        event.getProperties().add(new LastModified(lastModifiedDate));
        // TODO Organizer should be URI (email-address?) created fake address
        if (StringUtils.isNotEmpty(catalog.getFirst(DublinCore.PROPERTY_CREATOR))) {
            URI organizer = new URI("mailto", catalog.getFirst(DublinCore.PROPERTY_CREATOR) + "@opencast.tld", null);
            event.getProperties().add(new Organizer(pl, organizer));
        }
        if (StringUtils.isNotEmpty(catalog.getFirst(DublinCore.PROPERTY_DESCRIPTION))) {
            event.getProperties().add(new Description(catalog.getFirst(DublinCore.PROPERTY_DESCRIPTION)));
        }
        event.getProperties().add(new Location(agentId));
        if (StringUtils.isNotEmpty(catalog.getFirst(DublinCore.PROPERTY_IS_PART_OF))) {
            seriesID = catalog.getFirst(DublinCore.PROPERTY_IS_PART_OF);
            event.getProperties().add(new RelatedTo(seriesID));
        }
        ParameterList dcParameters = new ParameterList();
        dcParameters.add(new FmtType("application/xml"));
        dcParameters.add(Value.BINARY);
        dcParameters.add(Encoding.BASE64);
        dcParameters.add(new XParameter("X-APPLE-FILENAME", "episode.xml"));
        Attach metadataAttachment = new Attach(dcParameters, catalog.toXmlString().getBytes("UTF-8"));
        event.getProperties().add(metadataAttachment);
        String seriesDC = getSeriesDublinCoreAsString(seriesID);
        if (seriesDC != null) {
            logger.debug("Attaching series {} information to event {}", seriesID, eventId);
            ParameterList sDcParameters = new ParameterList();
            sDcParameters.add(new FmtType("application/xml"));
            sDcParameters.add(Value.BINARY);
            sDcParameters.add(Encoding.BASE64);
            sDcParameters.add(new XParameter("X-APPLE-FILENAME", "series.xml"));
            Attach seriesAttachment = new Attach(sDcParameters, seriesDC.getBytes("UTF-8"));
            event.getProperties().add(seriesAttachment);
        } else {
            logger.debug("No series provided for event {}.", eventId);
        }
        ParameterList caParameters = new ParameterList();
        caParameters.add(new FmtType("application/text"));
        caParameters.add(Value.BINARY);
        caParameters.add(Encoding.BASE64);
        caParameters.add(new XParameter("X-APPLE-FILENAME", "org.opencastproject.capture.agent.properties"));
        Attach agentsAttachment = new Attach(caParameters, captureAgentMetadata.getBytes("UTF-8"));
        event.getProperties().add(agentsAttachment);
    } catch (Exception e) {
        logger.error("Unable to add event '{}' to recording calendar: {}", eventId, ExceptionUtils.getStackTrace(e));
        return false;
    }
    cal.getComponents().add(event);
    logger.debug("new VEvent = {} ", event.toString());
    return true;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) Description(net.fortuna.ical4j.model.property.Description) Organizer(net.fortuna.ical4j.model.property.Organizer) Attach(net.fortuna.ical4j.model.property.Attach) Cn(net.fortuna.ical4j.model.parameter.Cn) URI(java.net.URI) DateTime(net.fortuna.ical4j.model.DateTime) Date(java.util.Date) SeriesException(org.opencastproject.series.api.SeriesException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) LastModified(net.fortuna.ical4j.model.property.LastModified) Uid(net.fortuna.ical4j.model.property.Uid) FmtType(net.fortuna.ical4j.model.parameter.FmtType) ParameterList(net.fortuna.ical4j.model.ParameterList) RelatedTo(net.fortuna.ical4j.model.property.RelatedTo) XParameter(net.fortuna.ical4j.model.parameter.XParameter) Location(net.fortuna.ical4j.model.property.Location)

Example 2 with FmtType

use of net.fortuna.ical4j.model.parameter.FmtType 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
 * @throws Exception
 */
@Test
public void testIcsImportExport() throws Exception {
    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);
    HttpPost post = new HttpPost(uri.toString());
    post.setEntity(new InputStreamEntity(new ByteArrayInputStream(buf.toByteArray()), ContentType.create(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");
    HttpGet get = new HttpGet(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 HttpGet(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 HttpGet(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 : HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) 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) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Uid(net.fortuna.ical4j.model.property.Uid) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) FmtType(net.fortuna.ical4j.model.parameter.FmtType) CalendarOutputter(net.fortuna.ical4j.data.CalendarOutputter) Test(org.junit.Test)

Example 3 with FmtType

use of net.fortuna.ical4j.model.parameter.FmtType in project bw-calendar-engine by Bedework.

the class IcalUtil method setAttachment.

/**
 * make an attachment
 *
 * @param val
 * @return Attendee
 * @throws Throwable
 */
public static Attach setAttachment(final BwAttachment val) throws Throwable {
    ParameterList pars = new ParameterList();
    String temp = val.getFmtType();
    if (temp != null) {
        pars.add(new FmtType(temp));
    }
    temp = val.getEncoding();
    if (temp == null) {
        return new Attach(pars, val.getUri());
    } else {
        pars.add(new Encoding(temp));
        temp = val.getValueType();
        if (temp != null) {
            pars.add(new Value(temp));
        }
        return new Attach(pars, val.getValue());
    }
}
Also used : Attach(net.fortuna.ical4j.model.property.Attach) FmtType(net.fortuna.ical4j.model.parameter.FmtType) AbbreviatedValue(org.bedework.calfacade.base.AbbreviatedValue) Value(net.fortuna.ical4j.model.parameter.Value) ParameterList(net.fortuna.ical4j.model.ParameterList) Encoding(net.fortuna.ical4j.model.parameter.Encoding)

Aggregations

FmtType (net.fortuna.ical4j.model.parameter.FmtType)3 Attach (net.fortuna.ical4j.model.property.Attach)3 URI (java.net.URI)2 ParameterList (net.fortuna.ical4j.model.ParameterList)2 VEvent (net.fortuna.ical4j.model.component.VEvent)2 XParameter (net.fortuna.ical4j.model.parameter.XParameter)2 Uid (net.fortuna.ical4j.model.property.Uid)2 ZMailbox (com.zimbra.client.ZMailbox)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Date (java.util.Date)1 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)1 CalendarOutputter (net.fortuna.ical4j.data.CalendarOutputter)1 Date (net.fortuna.ical4j.model.Date)1 DateTime (net.fortuna.ical4j.model.DateTime)1 Cn (net.fortuna.ical4j.model.parameter.Cn)1 Encoding (net.fortuna.ical4j.model.parameter.Encoding)1 Value (net.fortuna.ical4j.model.parameter.Value)1 Description (net.fortuna.ical4j.model.property.Description)1