Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish iCalendar Feed with Java

Microsoft Outlook and other calendar clients have the ability to subscribe to "Internet Calendars".

In the case of Outlook, it accepts a URL (either http: or webcal:). Once configured correctly, the "Internet Calendar" appears in Outlook client stays up-to-date.

I would like to know how to publish an "Internet Calendar" on my own. I am using Java. I am already creating ".ics files" events using the iCal4j library. I vaguely assume that I need to create a servlet that sends a stream of ics events.

Any example or reference documentation to get me started would be appreciated.

like image 492
Frederic Fortier Avatar asked Dec 17 '15 18:12

Frederic Fortier


People also ask

How do I set up an iCalendar feed?

Go to Settings > Sharing > Create Link. Label the link accordingly (e.g., 'Selected Sub-calendars for iCalendar Feed' or whatever is appropriate.) Click 'Selected Sub-calendars' from the drop-down box and choose the sub-calendars you wish to see.

How do I publish an ICS file?

ics file), import it into Outlook.com, then share it with the people who need to see it. Under the settings in Outlook on the web, go to Calendar > Shared calendars. Choose the calendar you wish to publish and the level of details that you want others to see.


1 Answers

I don't know how your implementation looks like, but when I tried some dummy implementation with Spring Boot I was able to make this work. Here is the implementation I used:

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@Controller
public class CalendarController {

  @GetMapping("/calendar.ics")
  @ResponseBody
  public byte[] getCalendars() throws IOException {
    ClassPathResource classPathResource = new ClassPathResource("Test.ics");
    InputStream inputStream = classPathResource.getInputStream();
    byte[] bytes = inputStream.readAllBytes();
    inputStream.close();
    return bytes;
  }

  @GetMapping("/downloadCalendar.ics")
  public ResponseEntity<Resource> downloadFile(HttpServletRequest request) {
    Resource resource = new ClassPathResource("Test.ics");
    String contentType = null;
    try {
      contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
    } catch (IOException ex) {
      log.info("Could not determine file type.");
    }

    if(contentType == null) {
      contentType = "application/octet-stream";
    }

    return ResponseEntity.ok()
        .contentType(MediaType.parseMediaType(contentType))
        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
        .body(resource);
  }
}

Both of the methods work fine in Outlook 2016 and above if I try to add either http://localhost:8080/calendar.ics or http://localhost:8080/downloadCalendar.ics. The Test.ics I am returning is just an exported appointment from my calendar in ics format. Also as a side note here are the headers that get sent with the request from Outlook:

headers = [
    accept: "*/*", 
    user-agent: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Tablet PC 2.0; Microsoft Outlook 16.0.4861; ms-office; MSOffice 16)", 
    accept-encoding: "gzip, deflate", 
    host: "localhost:8080", 
    connection: "Keep-Alive"
]

I would also imagine that there might be some issues with authentication if https is used and outlook might send different headers in that case. There is some issue report in Microsoft support center here: https://support.microsoft.com/en-us/help/4025591/you-can-t-add-an-internet-calendar-in-outlook, but with a broken link to the "new modern authentication" page :). Hope this helps.

like image 131
vl4d1m1r4 Avatar answered Oct 09 '22 20:10

vl4d1m1r4