net.fortuna.ical4j.data.CalendarOutputter Java Examples

The following examples show how to use net.fortuna.ical4j.data.CalendarOutputter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IcsCalendarBuilder.java    From fredbet with Creative Commons Attribution Share Alike 4.0 International 7 votes vote down vote up
public byte[] build() {
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    TimeZone timezone = registry.getTimeZone(timeZone);
    VTimeZone tz = timezone.getVTimeZone();
    VEvent vEvent = new VEvent(toDate(this.start), toDate(this.end), this.title);
    vEvent.getProperties().add(tz.getTimeZoneId());
    vEvent.getProperties().add(uidGenerator.generateUid());
    vEvent.getProperties().add(new Description(this.content));
    vEvent.getProperties().add(new Location(this.location));

    net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
    icsCalendar.getProperties().add(new ProdId("-//FredBet//iCal4j 1.0//EN"));
    icsCalendar.getProperties().add(CalScale.GREGORIAN);
    icsCalendar.getProperties().add(Version.VERSION_2_0);

    icsCalendar.getComponents().add(vEvent);

    try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
        CalendarOutputter outputter = new CalendarOutputter();
        outputter.output(icsCalendar, out);
        return out.toByteArray();
    } catch (IOException e) {
        LOG.error(e.getMessage());
        return null;
    }
}
 
Example #2
Source File: ICalendarService.java    From axelor-open-suite with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Export the calendar to the given output writer.
 *
 * @param calendar the source {@link ICalendar}
 * @param writer the output writer
 * @throws IOException
 * @throws ParseException
 * @throws ValidationException
 */
public void export(ICalendar calendar, Writer writer)
    throws IOException, ParseException, ValidationException {
  Preconditions.checkNotNull(calendar, "calendar can't be null");
  Preconditions.checkNotNull(writer, "writer can't be null");
  Preconditions.checkNotNull(getICalendarEvents(calendar), "can't export empty calendar");

  Calendar cal = newCalendar();
  cal.getProperties().add(new XProperty(X_WR_CALNAME, calendar.getName()));

  for (ICalendarEvent item : getICalendarEvents(calendar)) {
    VEvent event = createVEvent(item);
    cal.getComponents().add(event);
  }

  CalendarOutputter outputter = new CalendarOutputter();
  outputter.output(cal, writer);
}
 
Example #3
Source File: BaseCalendarService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected void printICalSchedule(String calendarName, List<String> calRefs, OutputStream os)
//	protected void printICalSchedule(String calRef, OutputStream os) 
		throws PermissionException
	{
		// generate iCal text file 
		net.fortuna.ical4j.model.Calendar ical = new net.fortuna.ical4j.model.Calendar();
		ical.getProperties().add(new ProdId("-//SakaiProject//iCal4j 1.0//EN"));
		ical.getProperties().add(Version.VERSION_2_0);
		ical.getProperties().add(CalScale.GREGORIAN);
		ical.getProperties().add(new XProperty("X-WR-CALNAME", calendarName));
		ical.getProperties().add(new XProperty("X-WR-CALDESC", calendarName));
		
		TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); 
		TzId tzId = new TzId( m_timeService.getLocalTimeZone().getID() ); 
		ical.getComponents().add(registry.getTimeZone(tzId.getValue()).getVTimeZone());
		
		CalendarOutputter icalOut = new CalendarOutputter();
		int numEvents = generateICal(ical, calRefs);
			
		try 
		{
			if (numEvents > 0) {
				icalOut.output( ical, os );
			}
		}
		catch (Exception e)
		{
           log.warn(".printICalSchedule(): ", e);
		}
	}
 
Example #4
Source File: Calendar_EventServiceAdv.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 获取一个日程事件转换为ICAL后的内容信息
 * @param o2_calendar_event
 * @throws ValidationException
 * @throws IOException
 */
public String getiCalContent( Calendar_Event o2_calendar_event) throws ValidationException, IOException{
	net.fortuna.ical4j.model.Calendar calendar = parseToiCal(o2_calendar_event);
	CalendarOutputter calendarOutputter = new CalendarOutputter(false); 
	ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
	calendarOutputter.output(calendar, baos); 
	return new String(baos.toByteArray(), "utf-8");
}
 
Example #5
Source File: CalendarDaoICalFileImpl.java    From olat with Apache License 2.0 5 votes vote down vote up
private boolean writeCalendarFile(final Calendar calendar, final String calType, final String calId) {
    final File fCalendarFile = getCalendarFile(calType, calId);
    OutputStream os = null;
    try {
        os = new BufferedOutputStream(new FileOutputStream(fCalendarFile, false));
        final CalendarOutputter calOut = new CalendarOutputter(false);
        calOut.output(calendar, os);
    } catch (final Exception e) {
        return false;
    } finally {
        FileUtils.closeSafely(os);
    }
    return true;
}
 
Example #6
Source File: Calendar_EventServiceAdv.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 获取一个日程事件转换为ICAL后的内容信息
 * @param o2_calendar_event
 * @throws ValidationException
 * @throws IOException
 */
public String getiCalContent( Calendar_Event o2_calendar_event) throws ValidationException, IOException{
	net.fortuna.ical4j.model.Calendar calendar = parseToiCal(o2_calendar_event);
	CalendarOutputter calendarOutputter = new CalendarOutputter(false); 
	ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
	calendarOutputter.output(calendar, baos); 
	return new String(baos.toByteArray(), "utf-8");
}
 
Example #7
Source File: IcalHandler.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
/**
 * Write iCal to File
 *
 * @param _filerPath
 *            - path to '*.ics' file
 * @throws Exception
 *             - in case of error during writing to the file
 */
public void writeDataToFile(String _filerPath) throws Exception {
	String filerPath = _filerPath.endsWith(".ics") ? _filerPath
			: String.format("%s.ics", _filerPath);

	try (FileOutputStream fout = new FileOutputStream(filerPath)) {
		CalendarOutputter outputter = new CalendarOutputter();
		outputter.output(icsCalendar, fout);
	}
}
 
Example #8
Source File: CalendarDaoICalFileImpl.java    From olat with Apache License 2.0 5 votes vote down vote up
private boolean writeCalendarFile(final Calendar calendar, final String calType, final String calId) {
    final File fCalendarFile = getCalendarFile(calType, calId);
    OutputStream os = null;
    try {
        os = new BufferedOutputStream(new FileOutputStream(fCalendarFile, false));
        final CalendarOutputter calOut = new CalendarOutputter(false);
        calOut.output(calendar, os);
    } catch (final Exception e) {
        return false;
    } finally {
        FileUtils.closeSafely(os);
    }
    return true;
}
 
Example #9
Source File: BaseCalendarService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected void printICalSchedule(String calendarName, List<String> calRefs, OutputStream os)
//	protected void printICalSchedule(String calRef, OutputStream os) 
		throws PermissionException
	{
		// generate iCal text file 
		net.fortuna.ical4j.model.Calendar ical = new net.fortuna.ical4j.model.Calendar();
		ical.getProperties().add(new ProdId("-//SakaiProject//iCal4j 1.0//EN"));
		ical.getProperties().add(Version.VERSION_2_0);
		ical.getProperties().add(CalScale.GREGORIAN);
		ical.getProperties().add(new XProperty("X-WR-CALNAME", calendarName));
		ical.getProperties().add(new XProperty("X-WR-CALDESC", calendarName));
		
		TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); 
		TzId tzId = new TzId( m_timeService.getLocalTimeZone().getID() ); 
		ical.getComponents().add(registry.getTimeZone(tzId.getValue()).getVTimeZone());
		
		CalendarOutputter icalOut = new CalendarOutputter();
		int numEvents = generateICal(ical, calRefs);
			
		try 
		{
			if (numEvents > 0) {
				icalOut.output( ical, os );
			}
		}
		catch (Exception e)
		{
           log.warn(".printICalSchedule(): ", e);
		}
	}
 
Example #10
Source File: CalendarFeed.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException
{
  if (WebConfiguration.isUpAndRunning() == false) {
    log.error("System isn't up and running, CalendarFeed call denied. The system is may-be in start-up phase or in maintenance mode.");
    resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    return;
  }
  PFUserDO user = null;
  String logMessage = null;
  try {
    MDC.put("ip", req.getRemoteAddr());
    MDC.put("session", req.getSession().getId());
    if (StringUtils.isBlank(req.getParameter("user")) || StringUtils.isBlank(req.getParameter("q"))) {
      resp.sendError(HttpStatus.SC_BAD_REQUEST);
      log.error("Bad request, parameters user and q not given. Query string is: " + req.getQueryString());
      return;
    }
    final String encryptedParams = req.getParameter("q");
    final Integer userId = NumberHelper.parseInteger(req.getParameter("user"));
    if (userId == null) {
      log.error("Bad request, parameter user is not an integer: " + req.getQueryString());
      return;
    }
    final Registry registry = Registry.instance();
    user = registry.getUserGroupCache().getUser(userId);
    if (user == null) {
      log.error("Bad request, user not found: " + req.getQueryString());
      return;
    }
    PFUserContext.setUser(user);
    MDC.put("user", user.getUsername());
    final String decryptedParams = registry.getDao(UserDao.class).decrypt(userId, encryptedParams);
    if (decryptedParams == null) {
      log.error("Bad request, can't decrypt parameter q (may-be the user's authentication token was changed): " + req.getQueryString());
      return;
    }
    final Map<String, String> params = StringHelper.getKeyValues(decryptedParams, "&");
    final Calendar calendar = createCal(params, userId, params.get("token"), params.get(PARAM_NAME_TIMESHEET_USER));
    final StringBuffer buf = new StringBuffer();
    boolean first = true;
    for (final Map.Entry<String, String> entry : params.entrySet()) {
      if ("token".equals(entry.getKey()) == true) {
        continue;
      }
      first = StringHelper.append(buf, first, entry.getKey(), ", ");
      buf.append("=").append(entry.getValue());
    }
    logMessage = buf.toString();
    log.info("Getting calendar entries for: " + logMessage);

    if (calendar == null) {
      resp.sendError(HttpStatus.SC_BAD_REQUEST);
      log.error("Bad request, can't find calendar.");
      return;
    }

    resp.setContentType("text/calendar");
    final CalendarOutputter output = new CalendarOutputter(false);
    try {
      output.output(calendar, resp.getOutputStream());
    } catch (final ValidationException ex) {
      ex.printStackTrace();
    }
  } finally {
    log.info("Finished request: " + logMessage);
    PFUserContext.setUser(null);
    MDC.remove("ip");
    MDC.remove("session");
    if (user != null) {
      MDC.remove("user");
    }
  }
}
 
Example #11
Source File: TestSendIcalMessage.java    From openmeetings with Apache License 2.0 4 votes vote down vote up
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(randomUUID().toString());
	meeting.getProperties().add(uid);

	// add attendees..
	Attendee dev1 = new Attendee(URI.create("mailto:[email protected]"));
	dev1.getParameters().add(Role.REQ_PARTICIPANT);
	dev1.getParameters().add(new Cn("Developer 1"));
	meeting.getProperties().add(dev1);

	Attendee dev2 = new Attendee(URI.create("mailto:[email protected]"));
	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("[email protected]"));
	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);
	}
}
 
Example #12
Source File: CalendarUtils.java    From cosmo with Apache License 2.0 3 votes vote down vote up
/**
    * Convert Calendar object to String.
    * 
    * @param calendar
    * @return string representation of calendar
    * @throws ValidationException
    *             - if something is wrong this exception is thrown.
    * @throws IOException
    *             - if something is wrong this exception is thrown.
    */
   public static String outputCalendar(Calendar calendar) throws ValidationException, IOException {
if (calendar == null) {
    return null;
}
CalendarOutputter outputter = new CalendarOutputter();
StringWriter sw = new StringWriter();
outputter.output(calendar, sw);
return sw.toString();
   }
 
Example #13
Source File: ICalendarOutputter.java    From cosmo with Apache License 2.0 3 votes vote down vote up
/**
 * Writes an iCalendar string representing the calendar items contained within the given calendar collection to the
 * given output stream.
 *
 * Since the calendar content stored with each calendar items is parsed and validated when the item is created,
 * these errors should not reoccur when the calendar is being outputted.
 *
 * @param collection the <code>CollectionItem</code> to format
 *
 * @throws IllegalArgumentException if the collection is not stamped as a calendar collection
 * @throws IOException
 */
public static void output(Calendar calendar, OutputStream out) throws IOException {

    CalendarOutputter outputter = new CalendarOutputter();
    outputter.setValidating(false);
    try {
        outputter.output(calendar, out);
    } catch (ValidationException e) {
        throw new IllegalStateException("unable to validate collection calendar", e);
    }
}
 
Example #14
Source File: IcalHandler.java    From openmeetings with Apache License 2.0 3 votes vote down vote up
/**
 * Get IcalBody as ByteArray
 *
 * @return - calendar in ICS format as byte[]
 * @throws Exception
 *             - in case of error during writing to byte array
 */
public byte[] getIcalAsByteArray() throws Exception {
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	CalendarOutputter outputter = new CalendarOutputter();
	outputter.output(icsCalendar, bout);
	return bout.toByteArray();
}
 
Example #15
Source File: Calendar_EventServiceAdv.java    From o2oa with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * 将一个日程事件写为一个ical文件
 * @param o2_calendar_event
 * @param path
 * @throws ValidationException
 * @throws IOException
 */
public void writeiCal( Calendar_Event o2_calendar_event, String path ) throws ValidationException, IOException{
	net.fortuna.ical4j.model.Calendar calendar = parseToiCal(o2_calendar_event);
	FileOutputStream fout = new FileOutputStream("D://2.ics");
       CalendarOutputter outputter = new CalendarOutputter();
       outputter.output(calendar, fout );
}
 
Example #16
Source File: Calendar_EventServiceAdv.java    From o2oa with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * 将一个日程事件写为一个ical文件
 * @param o2_calendar_event
 * @param path
 * @throws ValidationException
 * @throws IOException
 */
public void writeiCal( Calendar_Event o2_calendar_event, String path ) throws ValidationException, IOException{
	net.fortuna.ical4j.model.Calendar calendar = parseToiCal(o2_calendar_event);
	FileOutputStream fout = new FileOutputStream("D://2.ics");
       CalendarOutputter outputter = new CalendarOutputter();
       outputter.output(calendar, fout );
}
 
Example #17
Source File: Calendar_EventServiceAdv.java    From o2oa with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * 将一个日程事件写为一个ical文件
 * @param o2_calendar_event
 * @param out
 * @throws ValidationException
 * @throws IOException
 */
public void wirteiCal( Calendar_Event o2_calendar_event, OutputStream out ) throws ValidationException, IOException{
	net.fortuna.ical4j.model.Calendar calendar = parseToiCal(o2_calendar_event);
       CalendarOutputter outputter = new CalendarOutputter();
       outputter.output(calendar, out );
}
 
Example #18
Source File: Calendar_EventServiceAdv.java    From o2oa with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * 将一个日程事件写为一个ical文件
 * @param o2_calendar_event
 * @param out
 * @throws ValidationException
 * @throws IOException
 */
public void wirteiCal( Calendar_Event o2_calendar_event, OutputStream out ) throws ValidationException, IOException{
	net.fortuna.ical4j.model.Calendar calendar = parseToiCal(o2_calendar_event);
       CalendarOutputter outputter = new CalendarOutputter();
       outputter.output(calendar, out );
}