Hello World with Caldav and Annotations

In this tutorial we're going to create a very simple Caldav implementation. This will be useable with caldav clients like Thunderbird/Lightning, Apple's iCal and mobile devices such as iPhone/iPad and Android (using third party Caldav app)

This example just uses in-memory objects for the sake of simplicity, but these objects could just as easily be persistent hibernate objects etc, and that is the intended use case.

Licensing

Milton's caldav support is provided in the enterprised edition jar. Its open source, but not free. To use it commercially you'll need to buy a license (at time of writing $500/server). But to get started you can get a trial license from here, or you can use the files which are in the example source code. More details about licensing are here

Source code

Full source code is here - https://github.com/miltonio/milton2/tree/master/examples/tuts-anno3

And the interesting bits are all in the HelloCaldavController class

Project Structure

We're going to use maven, you might want to check out the annotations hello world tutorial if you havent already for details on setting up the project.

But here's the main files you'll need to get started:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>io.milton.tuts</groupId>
    <artifactId>tuts-anno1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>tuts-anno3-caldav</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <milton.version>2.4.2.3</milton.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.8.v20121106</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <!--<port>8085</port>-->
                            <port>8080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                    <stopKey>stop</stopKey>
                    <stopPort>8089</stopPort>
                </configuration>
            </plugin>            
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>milton-repo</id>
            <url>http://milton.io/maven/</url>
        </repository>       
    </repositories>     


    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.milton</groupId>
            <artifactId>milton-server-ent</artifactId>
            <version>${milton.version}</version>
        </dependency>     
    </dependencies>
</project>

src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">

    <filter>
        <filter-name>MiltonFilter</filter-name>
        <filter-class>io.milton.servlet.MiltonFilter</filter-class>
        <init-param>
            <param-name>resource.factory.class</param-name>
            <param-value>io.milton.http.annotated.AnnotationResourceFactory</param-value>
        </init-param>
        <init-param>
            <param-name>controllerPackagesToScan</param-name>
            <param-value>com.hellocaldav</param-value>
        </init-param>        
    </filter>

    <filter-mapping>
        <filter-name>MiltonFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>