diff --git a/src/main/java/org/olat/commons/calendar/ICalServlet.java b/src/main/java/org/olat/commons/calendar/ICalServlet.java index 42879439b156ddad66e5df0e306839d98e703c73..545c4f991b30dfcf40a6136992ec0457eb44ae4e 100644 --- a/src/main/java/org/olat/commons/calendar/ICalServlet.java +++ b/src/main/java/org/olat/commons/calendar/ICalServlet.java @@ -205,7 +205,7 @@ public class ICalServlet extends HttpServlet { response.setCharacterEncoding("UTF-8"); setCacheControl(response); } catch (Exception e) { - e.printStackTrace(); + log.error("", e); } CalendarManager calendarManager = CoreSpringFactory.getImpl(CalendarManager.class); @@ -244,7 +244,7 @@ public class ICalServlet extends HttpServlet { private void outputCalendar(Calendar calendar, HttpServletRequest request, HttpServletResponse response) throws ValidationException, IOException { - boolean outlook = isOutlook(request); + Agent agent = getAgent(request); updateUrlProperties(calendar); Writer out = response.getWriter(); @@ -277,8 +277,8 @@ public class ICalServlet extends HttpServlet { outputTTL(out); Set<String> timezoneIds = new HashSet<>(); - outputCalendarComponents(calendar, out, outlook, timezoneIds); - if(outlook) { + outputCalendarComponents(calendar, out, agent, timezoneIds); + if(agent == Agent.outlook) { outputTimeZoneForOutlook(timezoneIds, out); } @@ -302,7 +302,7 @@ public class ICalServlet extends HttpServlet { } else { List<CalendarFileInfos> iCalFiles = homeCalendarManager.getListOfCalendarsFiles(identity); DBFactory.getInstance().commitAndCloseSession(); - boolean outlook = isOutlook(request); + Agent agent = getAgent(request); Writer out = response.getWriter(); out.write(Calendar.BEGIN); @@ -317,9 +317,9 @@ public class ICalServlet extends HttpServlet { Set<String> timezoneIds = new HashSet<>(); int numOfFiles = iCalFiles.size(); for(int i=0; i<numOfFiles; i++) { - outputCalendar(iCalFiles.get(i), out, outlook, timezoneIds); + outputCalendar(iCalFiles.get(i), out, agent, timezoneIds); } - if(outlook) { + if(agent == Agent.outlook) { outputTimeZoneForOutlook(timezoneIds, out); } @@ -329,12 +329,16 @@ public class ICalServlet extends HttpServlet { } } - private boolean isOutlook(HttpServletRequest request) { + private Agent getAgent(HttpServletRequest request) { String userAgent = request.getHeader("User-Agent"); - if(userAgent != null && userAgent.indexOf("Microsoft Outlook") >= 0) { - return true; + if(userAgent == null) { + return Agent.unkown; + } else if(userAgent != null && userAgent.indexOf("Microsoft Outlook") >= 0) { + return Agent.outlook; + } else if(userAgent != null && userAgent.indexOf("Google") >= 0 && userAgent.indexOf("Calendar") >= 0) { + return Agent.googleCalendar; } - return false; + return Agent.unkown; } /** @@ -368,7 +372,7 @@ public class ICalServlet extends HttpServlet { } } - private void outputCalendar(CalendarFileInfos fileInfos, Writer out, boolean outlook, Set<String> timezoneIds) + private void outputCalendar(CalendarFileInfos fileInfos, Writer out, Agent agent, Set<String> timezoneIds) throws IOException { try { CalendarManager calendarManager = CoreSpringFactory.getImpl(CalendarManager.class); @@ -378,22 +382,29 @@ public class ICalServlet extends HttpServlet { String prefix = fileInfos.getType() + "-" + fileInfos.getCalendarId() + "-"; updateUUID(calendar, prefix); - outputCalendarComponents(calendar, out, outlook, timezoneIds); + outputCalendarComponents(calendar, out, agent, timezoneIds); } catch (IOException | OLATRuntimeException e) { log.error("", e); } } - private void outputCalendarComponents(Calendar calendar, Writer out, boolean outlook, Set<String> timezoneIds) + private void outputCalendarComponents(Calendar calendar, Writer out, Agent agent, Set<String> timezoneIds) throws IOException { try { ComponentList events = calendar.getComponents(); for (final Iterator<?> i = events.iterator(); i.hasNext();) { Object comp = i.next(); String event = comp.toString(); - if (outlook && comp instanceof VEvent) { + if (agent == Agent.outlook && comp instanceof VEvent) { event = quoteTimeZone(event, (VEvent)comp, timezoneIds); } + if(agent == Agent.googleCalendar) { + event = event.replace("CLASS:PRIVATE" + Strings.LINE_SEPARATOR, ""); + event = event.replace("X-OLAT-MANAGED:all" + Strings.LINE_SEPARATOR, ""); + event = event.replace("DESCRIPTION:" + Strings.LINE_SEPARATOR, ""); + event = event.replace("LOCATION:" + Strings.LINE_SEPARATOR, ""); + } + out.write(event); } } catch (IOException | OLATRuntimeException e) { @@ -486,4 +497,10 @@ public class ICalServlet extends HttpServlet { } }); } + + private enum Agent { + unkown, + outlook, + googleCalendar + } } \ No newline at end of file