use of net.fortuna.ical4j.data.CalendarOutputter 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;"));
}
use of net.fortuna.ical4j.data.CalendarOutputter in project bw-calendar-engine by Bedework.
the class IcalTranslator method toStringTzCalendar.
/**
* Create a Calendar object from the named timezone and convert to
* a String representation
*
* @param tzid String timezone id
* @return String
* @throws CalFacadeException
*/
public String toStringTzCalendar(final String tzid) throws CalFacadeException {
Calendar ical = getTzCalendar(tzid);
CalendarOutputter calOut = new CalendarOutputter(true);
StringWriter sw = new StringWriter();
try {
calOut.output(ical, sw);
return sw.toString();
} catch (Throwable t) {
throw new CalFacadeException(t);
}
}
use of net.fortuna.ical4j.data.CalendarOutputter in project bw-calendar-engine by Bedework.
the class SimpleMailer method mailEntity.
@Override
public boolean mailEntity(final Calendar cal, String originator, final Collection<String> recipients, String subject) throws CalFacadeException {
if (debug) {
debug("mailEntity called with " + cal);
}
if (config.getDisabled()) {
return false;
}
try {
/* Create a message with the appropriate mime-type
*/
MimeMessage msg = new MimeMessage(sess);
if (originator == null) {
originator = config.getFrom();
}
msg.setFrom(new InternetAddress(originator));
InternetAddress[] tos = new InternetAddress[recipients.size()];
int i = 0;
for (String recip : recipients) {
tos[i] = new InternetAddress(recip);
i++;
}
msg.setRecipients(javax.mail.Message.RecipientType.TO, tos);
if (subject == null) {
subject = config.getSubject();
}
msg.setSubject(subject);
msg.setSentDate(new Date());
CalendarOutputter co = new CalendarOutputter(false);
Writer wtr = new StringWriter();
co.output(cal, wtr);
String content = wtr.toString();
msg.setContent(content, "text/calendar");
Transport tr = sess.getTransport(config.getProtocol());
tr.connect();
tr.sendMessage(msg, tos);
return true;
} catch (Throwable t) {
if (debug) {
t.printStackTrace();
}
throw new CalFacadeException(t);
}
}
use of net.fortuna.ical4j.data.CalendarOutputter in project openmeetings by apache.
the class TestSendIcalMessage method simpleInvitionIcalLink.
public void simpleInvitionIcalLink() {
// Create a TimeZone
TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
TimeZone timezone = registry.getTimeZone("America/Mexico_City");
VTimeZone tz = timezone.getVTimeZone();
// Start Date is on: April 1, 2008, 9:00 am
java.util.Calendar startDate = new GregorianCalendar();
startDate.setTimeZone(timezone);
startDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
startDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
startDate.set(java.util.Calendar.YEAR, 2008);
startDate.set(java.util.Calendar.HOUR_OF_DAY, 9);
startDate.set(java.util.Calendar.MINUTE, 0);
startDate.set(java.util.Calendar.SECOND, 0);
// End Date is on: April 1, 2008, 13:00
java.util.Calendar endDate = new GregorianCalendar();
endDate.setTimeZone(timezone);
endDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
endDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
endDate.set(java.util.Calendar.YEAR, 2008);
endDate.set(java.util.Calendar.HOUR_OF_DAY, 13);
endDate.set(java.util.Calendar.MINUTE, 0);
endDate.set(java.util.Calendar.SECOND, 0);
// Create the event
String eventName = "Progress Meeting";
DateTime start = new DateTime(startDate.getTime());
DateTime end = new DateTime(endDate.getTime());
VEvent meeting = new VEvent(start, end, eventName);
// add timezone info..
meeting.getProperties().add(tz.getTimeZoneId());
// generate unique identifier..
Uid uid = new Uid(UUID.randomUUID().toString());
meeting.getProperties().add(uid);
// add attendees..
Attendee dev1 = new Attendee(URI.create("mailto:dev1@mycompany.com"));
dev1.getParameters().add(Role.REQ_PARTICIPANT);
dev1.getParameters().add(new Cn("Developer 1"));
meeting.getProperties().add(dev1);
Attendee dev2 = new Attendee(URI.create("mailto:dev2@mycompany.com"));
dev2.getParameters().add(Role.OPT_PARTICIPANT);
dev2.getParameters().add(new Cn("Developer 2"));
meeting.getProperties().add(dev2);
// Create a calendar
net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
icsCalendar.getProperties().add(CalScale.GREGORIAN);
icsCalendar.getProperties().add(Version.VERSION_2_0);
// Add the event and print
icsCalendar.getComponents().add(meeting);
Organizer orger = new Organizer(URI.create("seba.wagner@gmail.com"));
orger.getParameters().add(new Cn("Sebastian Wagner"));
meeting.getProperties().add(orger);
icsCalendar.getProperties().add(Method.REQUEST);
log.debug(icsCalendar.toString());
ByteArrayOutputStream bout = new ByteArrayOutputStream();
CalendarOutputter outputter = new CalendarOutputter();
try {
outputter.output(icsCalendar, bout);
iCalMimeBody = bout.toByteArray();
sendIcalMessage();
} catch (Exception e) {
log.error("Error", e);
}
}
use of net.fortuna.ical4j.data.CalendarOutputter in project openolat by klemens.
the class ICalFileCalendarManagerTest method synchronizeCalendarFrom.
@Test
public void synchronizeCalendarFrom() throws ValidationException, IOException {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("s1-");
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
String eventId1 = "id-not-managed-event";
// 1. Add a standard event, not managed
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
Date start = calendar.getTime();
calendar.add(Calendar.HOUR_OF_DAY, 1);
Date end = calendar.getTime();
KalendarEvent notManagedEvent = new KalendarEvent(eventId1, null, "testEvent", start, end);
calendarManager.addEventTo(cal, notManagedEvent);
// 2. Synchronize with a first calendar
Kalendar baseCalendar = calendarManager.createCalendar("user", "first-sync");
String eventIdManaged1 = "managed-event-1";
KalendarEvent managedEvent1 = new KalendarEvent(eventIdManaged1, null, "managedEvent", start, end);
baseCalendar.addEvent(managedEvent1);
String eventIdManaged2 = "managed-event-2";
KalendarEvent managedEvent2 = new KalendarEvent(eventIdManaged2, null, "managedEvent", start, end);
baseCalendar.addEvent(managedEvent2);
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
new CalendarOutputter(false).output(calendarManager.buildCalendar(baseCalendar), os1);
InputStream in1 = new ByteArrayInputStream(os1.toByteArray());
calendarManager.synchronizeCalendarFrom(in1, "http://localhost:8080/unittest", cal);
in1.close();
// 3. Synchronize with a second calendar
Kalendar resyncCalendar = calendarManager.createCalendar("user", "first-sync");
KalendarEvent managedEvent1Alt = new KalendarEvent(eventIdManaged1, null, "managedEvent resync", start, end);
resyncCalendar.addEvent(managedEvent1Alt);
String eventIdManaged3 = "managed-event-3";
KalendarEvent managedEvent3 = new KalendarEvent(eventIdManaged3, null, "managedEvent 3", start, end);
resyncCalendar.addEvent(managedEvent3);
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
new CalendarOutputter(false).output(calendarManager.buildCalendar(resyncCalendar), os2);
InputStream in2 = new ByteArrayInputStream(os2.toByteArray());
calendarManager.synchronizeCalendarFrom(in2, "http://localhost:8080/unittest", cal);
in2.close();
emptyCalendarCache();
// check
Kalendar synchedCal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent notManagedEvent1 = synchedCal.getEvent(eventId1, null);
Assert.assertNotNull(notManagedEvent1);
Assert.assertEquals("testEvent", notManagedEvent1.getSubject());
KalendarEvent event1 = synchedCal.getEvent(eventIdManaged1, null);
Assert.assertNotNull(event1);
Assert.assertEquals("managedEvent resync", event1.getSubject());
KalendarEvent event2 = synchedCal.getEvent(eventIdManaged2, null);
Assert.assertNull(event2);
KalendarEvent event3 = synchedCal.getEvent(eventIdManaged3, null);
Assert.assertNotNull(event3);
Assert.assertEquals("managedEvent 3", event3.getSubject());
}
Aggregations