Caldav response refactoring: replace StringBuilder with CaldavResponse object

and ctag implementation on inbox

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@410 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-03-03 11:09:07 +00:00
parent 33bd67e13f
commit 4a2903efc3
2 changed files with 278 additions and 208 deletions

View File

@ -16,8 +16,7 @@ import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
import java.io.IOException; import java.io.*;
import java.io.StringReader;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
@ -270,128 +269,96 @@ public class CaldavConnection extends AbstractConnection {
} }
} }
protected void appendEventsResponses(StringBuilder buffer, CaldavRequest request, String path, List<ExchangeSession.Event> events) throws IOException { protected void appendEventsResponses(CaldavResponse response, CaldavRequest request, String path, List<ExchangeSession.Event> events) throws IOException {
int size = events.size(); int size = events.size();
int count = 0; int count = 0;
for (ExchangeSession.Event event : events) { for (ExchangeSession.Event event : events) {
DavGatewayTray.debug("Retrieving event " + (++count) + "/" + size); DavGatewayTray.debug("Retrieving event " + (++count) + "/" + size);
appendEventResponse(buffer, request, path, event); appendEventResponse(response, request, path, event);
} }
} }
protected void appendEventResponse(StringBuilder buffer, CaldavRequest request, String path, ExchangeSession.Event event) throws IOException { protected void appendEventResponse(CaldavResponse response, CaldavRequest request, String path, ExchangeSession.Event event) throws IOException {
String eventPath = event.getPath().replaceAll("<", "&lt;").replaceAll(">", "&gt;"); String eventPath = event.getPath().replaceAll("<", "&lt;").replaceAll(">", "&gt;");
buffer.append("<D:response>"); response.startResponse("/users/" + session.getEmail() + "/" + path + "/" + URIUtil.encodeWithinQuery(eventPath));
buffer.append("<D:href>/users/").append(session.getEmail()).append("/").append(path).append("/").append(URIUtil.encodeWithinQuery(eventPath)).append("</D:href>"); response.startPropstat();
buffer.append("<D:propstat>");
buffer.append("<D:prop>");
if (request.hasProperty("calendar-data")) { if (request.hasProperty("calendar-data")) {
String ics = event.getICS(); response.appendCalendarData(event.getICS());
if (ics != null && ics.length() > 0) {
ics = ics.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
buffer.append("<C:calendar-data xmlns:C=\"urn:ietf:params:xml:ns:caldav\"");
buffer.append(" C:content-type=\"text/calendar\" C:version=\"2.0\">");
buffer.append(ics);
buffer.append("</C:calendar-data>");
}
} }
if (request.hasProperty("getetag")) { if (request.hasProperty("getetag")) {
buffer.append("<D:getetag>").append(event.getEtag()).append("</D:getetag>"); response.appendProperty("D:getetag", event.getEtag());
} }
if (request.hasProperty("resourcetype")) { if (request.hasProperty("resourcetype")) {
buffer.append("<D:resourcetype/>"); response.appendProperty("D:resourcetype");
} }
if (request.hasProperty("displayname")) { if (request.hasProperty("displayname")) {
buffer.append("<D:displayname>").append(eventPath).append("</D:displayname>"); response.appendProperty("D:displayname", eventPath);
} }
buffer.append("</D:prop>"); response.endPropStatOK();
buffer.append("<D:status>HTTP/1.1 200 OK</D:status>"); response.endResponse();
buffer.append("</D:propstat>");
buffer.append("</D:response>");
} }
public void appendCalendar(StringBuilder buffer, String principal, CaldavRequest request) throws IOException { public void appendCalendar(CaldavResponse response, String principal, CaldavRequest request) throws IOException {
buffer.append("<D:response>"); response.startResponse("/users/" + principal + "/calendar");
buffer.append("<D:href>/users/").append(principal).append("/calendar</D:href>"); response.startPropstat();
buffer.append("<D:propstat>");
buffer.append("<D:prop>");
if (request.hasProperty("resourcetype")) { if (request.hasProperty("resourcetype")) {
buffer.append("<D:resourcetype>"); response.appendProperty("D:resourcetype", "<D:collection/>" +
buffer.append("<D:collection/>"); "<C:calendar xmlns:C=\"urn:ietf:params:xml:ns:caldav\"/>");
buffer.append("<C:calendar xmlns:C=\"urn:ietf:params:xml:ns:caldav\"/>");
buffer.append("</D:resourcetype>");
} }
if (request.hasProperty("owner")) { if (request.hasProperty("owner")) {
buffer.append("<D:owner>"); response.appendProperty("D:owner", "<D:href>/principals/users/" + principal + "</D:href>");
buffer.append("<D:href>/principals/users/").append(principal).append("</D:href>");
buffer.append("</D:owner>");
} }
if (request.hasProperty("getetag")) { if (request.hasProperty("getetag")) {
buffer.append("<D:getetag>") response.appendProperty("D:getetag", session.getCalendarEtag());
.append(session.getCalendarEtag())
.append("</D:getetag>");
} }
if (request.hasProperty("getctag")) { if (request.hasProperty("getctag")) {
buffer.append("<CS:getctag xmlns:CS=\"http://calendarserver.org/ns/\">") response.appendProperty("CS:getctag", "CS=\"http://calendarserver.org/ns/\"",
.append(base64Encode(session.getCalendarCtag())) base64Encode(session.getCalendarCtag()));
.append("</CS:getctag>");
} }
if (request.hasProperty("displayname")) { if (request.hasProperty("displayname")) {
buffer.append("<D:displayname>calendar</D:displayname>"); response.appendProperty("D:displayname", "calendar");
} }
buffer.append("</D:prop>"); response.endPropStatOK();
buffer.append("<D:status>HTTP/1.1 200 OK</D:status>"); response.endResponse();
buffer.append("</D:propstat>");
buffer.append("</D:response>");
} }
public void appendInbox(StringBuilder buffer, String principal, CaldavRequest request) { public void appendInbox(CaldavResponse response, String principal, CaldavRequest request) throws IOException {
buffer.append("<D:response>"); response.startResponse("/users/" + principal + "/inbox");
buffer.append("<D:href>/users/").append(principal).append("/inbox</D:href>"); response.startPropstat();
buffer.append("<D:propstat>");
buffer.append("<D:prop>");
if (request.hasProperty("resourcetype")) { if (request.hasProperty("resourcetype")) {
buffer.append("<D:resourcetype>"); response.appendProperty("D:resourcetype", "<D:collection/>" +
buffer.append("<D:collection/>"); "<C:schedule-inbox xmlns:C=\"urn:ietf:params:xml:ns:caldav\"/>");
buffer.append("<C:schedule-inbox xmlns:C=\"urn:ietf:params:xml:ns:caldav\"/>");
buffer.append("</D:resourcetype>");
} }
if (request.hasProperty("getctag")) { if (request.hasProperty("getctag")) {
buffer.append("<CS:getctag xmlns:CS=\"http://calendarserver.org/ns/\">0</CS:getctag>"); response.appendProperty("CS:getctag", "CS=\"http://calendarserver.org/ns/\"",
base64Encode(session.getInboxCtag()));
} }
if (request.hasProperty("displayname")) { if (request.hasProperty("displayname")) {
buffer.append("<D:displayname>inbox</D:displayname>"); response.appendProperty("<D:displayname>", "inbox");
} }
buffer.append("</D:prop>"); response.endPropStatOK();
buffer.append("<D:status>HTTP/1.1 200 OK</D:status>"); response.endResponse();
buffer.append("</D:propstat>");
buffer.append("</D:response>");
} }
public void appendOutbox(StringBuilder buffer, String principal, CaldavRequest request) { public void appendOutbox(CaldavResponse response, String principal, CaldavRequest request) throws IOException {
buffer.append("<D:response>"); response.startResponse("/users/" + principal + "/outbox");
buffer.append("<D:href>/users/").append(principal).append("/outbox</D:href>"); response.startPropstat();
buffer.append("<D:propstat>");
buffer.append("<D:prop>");
if (request.hasProperty("resourcetype")) { if (request.hasProperty("resourcetype")) {
buffer.append("<D:resourcetype>"); response.appendProperty("D:resourcetype", "<D:collection/>" +
buffer.append("<D:collection/>"); "<C:schedule-outbox xmlns:C=\"urn:ietf:params:xml:ns:caldav\"/>");
buffer.append("<C:schedule-outbox xmlns:C=\"urn:ietf:params:xml:ns:caldav\"/>");
buffer.append("</D:resourcetype>");
} }
if (request.hasProperty("getctag")) { if (request.hasProperty("getctag")) {
buffer.append("<CS:getctag xmlns:CS=\"http://calendarserver.org/ns/\">0</CS:getctag>"); response.appendProperty("CS:getctag", "CS=\"http://calendarserver.org/ns/\"",
"0");
} }
if (request.hasProperty("displayname")) { if (request.hasProperty("displayname")) {
buffer.append("<D:displayname>outbox</D:displayname>"); response.appendProperty("<D:displayname>", "outbox");
} }
buffer.append("</D:prop>"); response.endPropStatOK();
buffer.append("<D:status>HTTP/1.1 200 OK</D:status>"); response.endResponse();
buffer.append("</D:propstat>");
buffer.append("</D:response>");
} }
public void sendGetRoot() throws IOException { public void sendGetRoot() throws IOException {
@ -403,42 +370,39 @@ public class CaldavConnection extends AbstractConnection {
} }
public void sendInbox(CaldavRequest request, int depth, String principal) throws IOException { public void sendInbox(CaldavRequest request, int depth, String principal) throws IOException {
StringBuilder buffer = new StringBuilder(); CaldavResponse response = new CaldavResponse();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); response.startMultistatus();
buffer.append("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">"); appendInbox(response, principal, request);
appendInbox(buffer, principal, request);
if (depth == 1) { if (depth == 1) {
DavGatewayTray.debug("Searching calendar messages..."); DavGatewayTray.debug("Searching calendar messages...");
List<ExchangeSession.Event> events = session.getEventMessages(); List<ExchangeSession.Event> events = session.getEventMessages();
DavGatewayTray.debug("Found " + events.size() + " calendar messages"); DavGatewayTray.debug("Found " + events.size() + " calendar messages");
appendEventsResponses(buffer, request, "inbox", events); appendEventsResponses(response, request, "inbox", events);
} }
buffer.append("</D:multistatus>"); response.endMultistatus();
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true); sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, response, true);
} }
public void sendOutbox(CaldavRequest request, String principal) throws IOException { public void sendOutbox(CaldavRequest request, String principal) throws IOException {
StringBuilder buffer = new StringBuilder(); CaldavResponse response = new CaldavResponse();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); response.startMultistatus();
buffer.append("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">"); appendOutbox(response, principal, request);
appendOutbox(buffer, principal, request); response.endMultistatus();
buffer.append("</D:multistatus>"); sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, response, true);
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true);
} }
public void sendCalendar(CaldavRequest request, int depth, String principal) throws IOException { public void sendCalendar(CaldavRequest request, int depth, String principal) throws IOException {
StringBuilder buffer = new StringBuilder(); CaldavResponse response = new CaldavResponse();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); response.startMultistatus();
buffer.append("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">"); appendCalendar(response, principal, request);
appendCalendar(buffer, principal, request);
if (depth == 1) { if (depth == 1) {
DavGatewayTray.debug("Searching calendar events..."); DavGatewayTray.debug("Searching calendar events...");
List<ExchangeSession.Event> events = session.getAllEvents(); List<ExchangeSession.Event> events = session.getAllEvents();
DavGatewayTray.debug("Found " + events.size() + " calendar events"); DavGatewayTray.debug("Found " + events.size() + " calendar events");
appendEventsResponses(buffer, request, "calendar", events); appendEventsResponses(response, request, "calendar", events);
} }
buffer.append("</D:multistatus>"); response.endMultistatus();
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true); sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, response, true);
} }
protected String getEventFileNameFromPath(String path) { protected String getEventFileNameFromPath(String path) {
@ -476,77 +440,61 @@ public class CaldavConnection extends AbstractConnection {
events = session.getAllEvents(); events = session.getAllEvents();
} }
StringBuilder buffer = new StringBuilder(); CaldavResponse response = new CaldavResponse();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + response.startMultistatus();
"<D:multistatus xmlns:D=\"DAV:\">"); appendEventsResponses(response, request, path, events);
appendEventsResponses(buffer, request, path, events);
// send not found events errors // send not found events errors
for (String href : notFound) { for (String href : notFound) {
buffer.append("<D:response>"); response.startResponse(URIUtil.encodeWithinQuery(href));
buffer.append("<D:href>").append(URIUtil.encodeWithinQuery(href)).append("</D:href>"); response.appendPropstatNotFound();
buffer.append("<D:propstat>"); response.endResponse();
buffer.append("<D:status>HTTP/1.1 404 Not Found</D:status>");
buffer.append("</D:propstat>");
buffer.append("</D:response>");
} }
buffer.append("</D:multistatus>"); response.endMultistatus();
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true); sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, response, true);
} }
public void sendUserRoot(CaldavRequest request, int depth, String principal) throws IOException { public void sendUserRoot(CaldavRequest request, int depth, String principal) throws IOException {
StringBuilder buffer = new StringBuilder(); CaldavResponse response = new CaldavResponse();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); response.startMultistatus();
buffer.append("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">"); response.startResponse("/users/" + principal);
buffer.append("<D:response>"); response.startPropstat();
buffer.append("<D:href>/users/").append(principal).append("</D:href>");
buffer.append("<D:propstat>");
buffer.append("<D:prop>");
if (request.hasProperty("resourcetype")) { if (request.hasProperty("resourcetype")) {
buffer.append("<D:resourcetype>"); response.appendProperty("D:resourcetype", "<D:collection/>");
buffer.append("<D:collection/>");
buffer.append("</D:resourcetype>");
} }
if (request.hasProperty("displayname")) { if (request.hasProperty("displayname")) {
buffer.append("<D:displayname>").append(principal).append("</D:displayname>"); response.appendProperty("D:displayname", principal);
} }
buffer.append("</D:prop>"); response.endPropStatOK();
buffer.append("<D:status>HTTP/1.1 200 OK</D:status>");
buffer.append("</D:propstat>");
buffer.append("</D:response>");
if (depth == 1) { if (depth == 1) {
appendInbox(buffer, principal, request); appendInbox(response, principal, request);
appendOutbox(buffer, principal, request); appendOutbox(response, principal, request);
appendCalendar(buffer, principal, request); appendCalendar(response, principal, request);
} }
buffer.append("</D:multistatus>"); response.endResponse();
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true); response.endMultistatus();
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, response, true);
} }
public void sendRoot(CaldavRequest request) throws IOException { public void sendRoot(CaldavRequest request) throws IOException {
StringBuilder buffer = new StringBuilder(); CaldavResponse response = new CaldavResponse();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); response.startMultistatus();
buffer.append("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">"); response.startResponse("/");
buffer.append("<D:response>"); response.startPropstat();
buffer.append("<D:href>/</D:href>");
buffer.append("<D:propstat>");
buffer.append("<D:prop>");
if (request.hasProperty("principal-collection-set")) { if (request.hasProperty("principal-collection-set")) {
buffer.append("<D:principal-collection-set>"); response.appendProperty("D:principal-collection-set", "<D:href>/principals/users/" + session.getEmail() + "</D:href>");
buffer.append("<D:href>/principals/users/").append(session.getEmail()).append("</D:href>");
buffer.append("</D:principal-collection-set>");
} }
if (request.hasProperty("displayname")) { if (request.hasProperty("displayname")) {
buffer.append("<D:displayname>ROOT</D:displayname>"); response.appendProperty("D:displayname", "ROOT");
} }
buffer.append("</D:prop>"); response.endPropStatOK();
buffer.append("<D:status>HTTP/1.1 200 OK</D:status>"); response.endResponse();
buffer.append("</D:propstat>"); response.endMultistatus();
buffer.append("</D:response>"); sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, response, true);
buffer.append("</D:multistatus>");
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true);
} }
public void sendPrincipal(CaldavRequest request, String principal) throws IOException { public void sendPrincipal(CaldavRequest request, String principal) throws IOException {
@ -556,52 +504,37 @@ public class CaldavConnection extends AbstractConnection {
actualPrincipal = session.getEmail(); actualPrincipal = session.getEmail();
} }
StringBuilder buffer = new StringBuilder(); CaldavResponse response = new CaldavResponse();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); response.startMultistatus();
buffer.append("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">"); response.startResponse("/principals/users/" + principal);
buffer.append("<D:response>"); response.startPropstat();
buffer.append("<D:href>/principals/users/").append(principal).append("</D:href>");
buffer.append("<D:propstat>");
buffer.append("<D:prop>");
if (request.hasProperty("calendar-home-set")) { if (request.hasProperty("calendar-home-set")) {
buffer.append("<C:calendar-home-set>"); response.appendProperty("C:calendar-home-set", "<D:href>/users/" + actualPrincipal + "</D:href>");
buffer.append("<D:href>/users/").append(actualPrincipal).append("</D:href>");
buffer.append("</C:calendar-home-set>");
} }
if (request.hasProperty("calendar-user-address-set")) { if (request.hasProperty("calendar-user-address-set")) {
buffer.append("<C:calendar-user-address-set>"); response.appendProperty("C:calendar-user-address-set", "<D:href>mailto:" + actualPrincipal + "</D:href>");
buffer.append("<D:href>mailto:").append(actualPrincipal).append("</D:href>");
buffer.append("</C:calendar-user-address-set>");
} }
if (request.hasProperty("schedule-inbox-URL")) { if (request.hasProperty("schedule-inbox-URL")) {
buffer.append("<C:schedule-inbox-URL>"); response.appendProperty("C:schedule-inbox-URL", "<D:href>/users/" + actualPrincipal + "/inbox</D:href>");
buffer.append("<D:href>/users/").append(actualPrincipal).append("/inbox</D:href>");
buffer.append("</C:schedule-inbox-URL>");
} }
if (request.hasProperty("schedule-outbox-URL")) { if (request.hasProperty("schedule-outbox-URL")) {
buffer.append("<C:schedule-outbox-URL>"); response.appendProperty("C:schedule-outbox-URL", "<D:href>/users/" + actualPrincipal + "/outbox</D:href>");
buffer.append("<D:href>/users/").append(actualPrincipal).append("/outbox</D:href>");
buffer.append("</C:schedule-outbox-URL>");
} }
if (request.hasProperty("displayname")) { if (request.hasProperty("displayname")) {
buffer.append("<D:displayname>").append(actualPrincipal).append("</D:displayname>"); response.appendProperty("D:displayname", actualPrincipal);
} }
if (request.hasProperty("resourcetype")) { if (request.hasProperty("resourcetype")) {
buffer.append("<D:resourcetype>"); response.appendProperty("D:resourcetype", "<D:collection/><D:principal/>");
buffer.append("<D:collection/>");
buffer.append("<D:principal/>");
buffer.append("</D:resourcetype>");
} }
buffer.append("</D:prop>"); response.endPropStatOK();
buffer.append("<D:status>HTTP/1.1 200 OK</D:status>"); response.endResponse();
buffer.append("</D:propstat>"); response.endMultistatus();
buffer.append("</D:response>"); sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, response, true);
buffer.append("</D:multistatus>");
sendHttpResponse(HttpStatus.SC_MULTI_STATUS, null, "text/xml;charset=UTF-8", buffer.toString(), true);
} }
public void sendFreeBusy(String body) throws IOException { public void sendFreeBusy(String body) throws IOException {
@ -640,18 +573,15 @@ public class CaldavConnection extends AbstractConnection {
} }
} }
if (!freeBusyMap.isEmpty()) { if (!freeBusyMap.isEmpty()) {
StringBuilder response = new StringBuilder(); CaldavResponse response = new CaldavResponse();
response.startScheduleResponse();
response.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>")
.append("<C:schedule-response xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">");
for (Map.Entry<String, String> entry : freeBusyMap.entrySet()) { for (Map.Entry<String, String> entry : freeBusyMap.entrySet()) {
String attendee = entry.getKey(); String attendee = entry.getKey();
response.append("<C:response>") response.startRecipientResponse(attendee);
.append("<C:recipient>")
.append("<D:href>").append(attendee).append("</D:href>") StringBuilder ics = new StringBuilder();
.append("</C:recipient>") ics.append("<C:calendar-data>BEGIN:VCALENDAR").append((char) 13).append((char) 10)
.append("<C:request-status>2.0;Success</C:request-status>")
.append("<C:calendar-data>BEGIN:VCALENDAR").append((char) 13).append((char) 10)
.append("VERSION:2.0").append((char) 13).append((char) 10) .append("VERSION:2.0").append((char) 13).append((char) 10)
.append("PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN").append((char) 13).append((char) 10) .append("PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN").append((char) 13).append((char) 10)
.append("METHOD:REPLY").append((char) 13).append((char) 10) .append("METHOD:REPLY").append((char) 13).append((char) 10)
@ -663,15 +593,16 @@ public class CaldavConnection extends AbstractConnection {
.append("UID:").append(valueMap.get("UID")).append("").append((char) 13).append((char) 10) .append("UID:").append(valueMap.get("UID")).append("").append((char) 13).append((char) 10)
.append(attendeeKeyMap.get(attendee)).append(":").append(attendee).append("").append((char) 13).append((char) 10); .append(attendeeKeyMap.get(attendee)).append(":").append(attendee).append("").append((char) 13).append((char) 10);
if (entry.getValue().length() > 0) { if (entry.getValue().length() > 0) {
response.append("FREEBUSY;FBTYPE=BUSY-UNAVAILABLE:").append(entry.getValue()).append("").append((char) 13).append((char) 10); ics.append("FREEBUSY;FBTYPE=BUSY-UNAVAILABLE:").append(entry.getValue()).append("").append((char) 13).append((char) 10);
} }
response.append("END:VFREEBUSY").append((char) 13).append((char) 10) ics.append("END:VFREEBUSY").append((char) 13).append((char) 10)
.append("END:VCALENDAR") .append("END:VCALENDAR");
.append("</C:calendar-data>") response.appendCalendarData(ics.toString());
.append("</C:response>"); response.endRecipientResponse();
} }
response.append("</C:schedule-response>"); response.endScheduleResponse();
sendHttpResponse(HttpStatus.SC_OK, null, "text/xml;charset=UTF-8", response.toString(), true); sendHttpResponse(HttpStatus.SC_OK, null, response, true);
} else { } else {
sendHttpResponse(HttpStatus.SC_NOT_FOUND, null, "text/plain", "Unknown recipient: " + valueMap.get("ATTENDEE"), true); sendHttpResponse(HttpStatus.SC_NOT_FOUND, null, "text/plain", "Unknown recipient: " + valueMap.get("ATTENDEE"), true);
} }
@ -687,7 +618,7 @@ public class CaldavConnection extends AbstractConnection {
buffer.append(path); buffer.append(path);
Map<String, String> responseHeaders = new HashMap<String, String>(); Map<String, String> responseHeaders = new HashMap<String, String>();
responseHeaders.put("Location", buffer.toString()); responseHeaders.put("Location", buffer.toString());
sendHttpResponse(HttpStatus.SC_MOVED_PERMANENTLY, responseHeaders, null, null, true); sendHttpResponse(HttpStatus.SC_MOVED_PERMANENTLY, responseHeaders, true);
} }
public void sendErr(int status, Exception e) throws IOException { public void sendErr(int status, Exception e) throws IOException {
@ -705,23 +636,35 @@ public class CaldavConnection extends AbstractConnection {
public void sendOptions() throws IOException { public void sendOptions() throws IOException {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Allow", "OPTIONS, GET, PROPFIND, PUT, POST"); headers.put("Allow", "OPTIONS, GET, PROPFIND, PUT, POST");
sendHttpResponse(HttpStatus.SC_OK, headers, null, null, true); sendHttpResponse(HttpStatus.SC_OK, headers, true);
} }
public void sendUnauthorized() throws IOException { public void sendUnauthorized() throws IOException {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
headers.put("WWW-Authenticate", "Basic realm=\"" + Settings.getProperty("davmail.url") + "\""); headers.put("WWW-Authenticate", "Basic realm=\"" + Settings.getProperty("davmail.url") + "\"");
sendHttpResponse(HttpStatus.SC_UNAUTHORIZED, headers, null, null, true); sendHttpResponse(HttpStatus.SC_UNAUTHORIZED, headers, true);
} }
public void sendHttpResponse(int status, boolean keepAlive) throws IOException { public void sendHttpResponse(int status, boolean keepAlive) throws IOException {
sendHttpResponse(status, null, null, null, keepAlive); sendHttpResponse(status, null, null, (byte[]) null, keepAlive);
}
public void sendHttpResponse(int status, Map<String, String> headers, boolean keepAlive) throws IOException {
sendHttpResponse(status, headers, null, (byte[]) null, keepAlive);
}
public void sendHttpResponse(int status, Map<String, String> headers, CaldavResponse response, boolean keepAlive) throws IOException {
sendHttpResponse(status, headers, "text/xml;charset=UTF-8", response.getBytes(), keepAlive);
} }
public void sendHttpResponse(int status, Map<String, String> headers, String contentType, String content, boolean keepAlive) throws IOException { public void sendHttpResponse(int status, Map<String, String> headers, String contentType, String content, boolean keepAlive) throws IOException {
sendHttpResponse(status, headers, contentType, content.getBytes("UTF-8"), keepAlive);
}
public void sendHttpResponse(int status, Map<String, String> headers, String contentType, byte[] content, boolean keepAlive) throws IOException {
sendClient("HTTP/1.1 " + status + " " + HttpStatus.getStatusText(status)); sendClient("HTTP/1.1 " + status + " " + HttpStatus.getStatusText(status));
sendClient("Server: DavMail Gateway"); sendClient("Server: DavMail Gateway");
sendClient("DAV: 1, 2, 3, access-control, calendar-access, ticket, calendar-schedule, calendarserver-private-events"); sendClient("DAV: 1, 2, 3, access-control, calendar-access, ticket, calendar-auto-schedule, calendar-schedule, calendarserver-private-events");
SimpleDateFormat formatter = new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH); SimpleDateFormat formatter = new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
sendClient("Date: " + formatter.format(new java.util.Date())); sendClient("Date: " + formatter.format(new java.util.Date()));
if (headers != null) { if (headers != null) {
@ -734,18 +677,18 @@ public class CaldavConnection extends AbstractConnection {
} }
closed = closed || !keepAlive; closed = closed || !keepAlive;
sendClient("Connection: " + (closed ? "close" : "keep-alive")); sendClient("Connection: " + (closed ? "close" : "keep-alive"));
if (content != null && content.length() > 0) { if (content != null && content.length > 0) {
sendClient("Content-Length: " + content.getBytes("UTF-8").length); sendClient("Content-Length: " + content.length);
} else { } else {
sendClient("Content-Length: 0"); sendClient("Content-Length: 0");
} }
sendClient(""); sendClient("");
if (content != null && content.length() > 0) { if (content != null && content.length > 0) {
// full debug trace // full debug trace
if (wireLogger.isDebugEnabled()) { if (wireLogger.isDebugEnabled()) {
wireLogger.debug("> " + content); wireLogger.debug("> " + new String(content, "UTF-8"));
} }
sendClient(content.getBytes("UTF-8")); sendClient(content);
} }
} }
@ -845,5 +788,124 @@ public class CaldavConnection extends AbstractConnection {
return hrefs; return hrefs;
} }
} }
protected static class CaldavResponse {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter writer;
public CaldavResponse() throws IOException {
writer = new OutputStreamWriter(baos, "UTF-8");
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
public void startMultistatus() throws IOException {
writer.write("<D:multistatus xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">");
}
public void startResponse(String href) throws IOException {
writer.write("<D:response>");
writer.write("<D:href>");
writer.write(href);
writer.write("</D:href>");
}
public void startPropstat() throws IOException {
writer.write("<D:propstat>");
writer.write("<D:prop>");
}
public void appendCalendarData(String ics) throws IOException {
if (ics != null && ics.length() > 0) {
ics = ics.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
writer.write("<C:calendar-data xmlns:C=\"urn:ietf:params:xml:ns:caldav\"");
writer.write(" C:content-type=\"text/calendar\" C:version=\"2.0\">");
writer.write(ics);
writer.write("</C:calendar-data>");
}
}
public void appendProperty(String propertyName) throws IOException {
appendProperty(propertyName, null);
}
public void appendProperty(String propertyName, String propertyValue) throws IOException {
appendProperty(propertyName, null, propertyValue);
}
public void appendProperty(String propertyName, String namespace, String propertyValue) throws IOException {
if (propertyValue != null) {
writer.write('<');
writer.write(propertyName);
if (namespace != null) {
writer.write(" xmlns:");
writer.write(namespace);
}
writer.write('>');
writer.write(propertyValue);
writer.write("</");
writer.write(propertyName);
writer.write('>');
} else {
writer.write('<');
writer.write(propertyName);
writer.write("/>");
}
}
public void endPropStatOK() throws IOException {
writer.write("</D:prop>");
writer.write("<D:status>HTTP/1.1 200 OK</D:status>");
writer.write("</D:propstat>");
}
public void appendPropstatNotFound() throws IOException {
writer.write("<D:propstat>");
writer.write("<D:status>HTTP/1.1 404 Not Found</D:status>");
writer.write("</D:propstat>");
}
public void endResponse() throws IOException {
writer.write("</D:response>");
}
public void endMultistatus() throws IOException {
writer.write("</D:multistatus>");
}
public void startScheduleResponse() throws IOException {
writer.write("<C:schedule-response xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">");
}
public void startRecipientResponse(String recipient) throws IOException {
writer.write("<C:response>");
writer.write("<C:recipient>");
writer.write("<D:href>");
writer.write(recipient);
writer.write("</D:href>");
writer.write("</C:recipient>");
writer.write("<C:request-status>2.0;Success</C:request-status>");
}
public void endRecipientResponse() throws IOException {
writer.write("</C:response>");
}
public void endScheduleResponse() throws IOException {
writer.write("</C:schedule-response>");
}
public void close() throws IOException {
try {
writer.close();
} finally {
baos.close();
}
}
public byte[] getBytes() throws IOException {
close();
return baos.toByteArray();
}
}
} }

View File

@ -1576,9 +1576,17 @@ public class ExchangeSession {
return wdr.getStatusCode(); return wdr.getStatusCode();
} }
public String getInboxCtag() throws IOException {
return getFolderCtag(inboxUrl);
}
public String getCalendarCtag() throws IOException { public String getCalendarCtag() throws IOException {
return getFolderCtag(calendarUrl);
}
public String getFolderCtag(String folderUrl) throws IOException {
String ctag = null; String ctag = null;
Enumeration calendarEnum = wdr.propfindMethod(calendarUrl, 0); Enumeration calendarEnum = wdr.propfindMethod(folderUrl, 0);
if (!calendarEnum.hasMoreElements()) { if (!calendarEnum.hasMoreElements()) {
throw new IOException("Unable to get calendar object"); throw new IOException("Unable to get calendar object");
} }