diff --git a/src/java/davmail/exchange/ews/BaseShapeType.java b/src/java/davmail/exchange/ews/BaseShapeType.java new file mode 100644 index 00000000..350180df --- /dev/null +++ b/src/java/davmail/exchange/ews/BaseShapeType.java @@ -0,0 +1,58 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2010 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.exchange.ews; + +import java.io.IOException; +import java.io.Writer; + +/** + * Item or folder base shape. + */ +public final class BaseShapeType { + private final String value; + + private BaseShapeType(String value) { + this.value = value; + } + + /** + * Write XML content to writer. + * + * @param writer writer + * @throws IOException on error + */ + public void write(Writer writer) throws IOException { + writer.write(""); + writer.write(value); + writer.write(""); + } + + /** + * Return id only. + */ + public static final BaseShapeType IdOnly = new BaseShapeType("IdOnly"); + /** + * Return default properties. + */ + public static final BaseShapeType Default = new BaseShapeType("Default"); + /** + * Return all properties, except MAPI extended properties. + */ + public static final BaseShapeType AllProperties = new BaseShapeType("AllProperties"); +} \ No newline at end of file diff --git a/src/java/davmail/exchange/ews/DistinguishedFolderIdType.java b/src/java/davmail/exchange/ews/DistinguishedFolderIdType.java new file mode 100644 index 00000000..61650209 --- /dev/null +++ b/src/java/davmail/exchange/ews/DistinguishedFolderIdType.java @@ -0,0 +1,54 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2010 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.exchange.ews; + +import java.io.IOException; +import java.io.Writer; + +public class DistinguishedFolderIdType extends FolderIdType { + + private DistinguishedFolderIdType(String value) { + super(value); + } + + @Override + public void write(Writer writer) throws IOException { + writer.write(""); + } + + public static final DistinguishedFolderIdType calendar = new DistinguishedFolderIdType("calendar"); + public static final DistinguishedFolderIdType contacts = new DistinguishedFolderIdType("contacts"); + public static final DistinguishedFolderIdType deleteditems = new DistinguishedFolderIdType("deleteditems"); + public static final DistinguishedFolderIdType drafts = new DistinguishedFolderIdType("drafts"); + public static final DistinguishedFolderIdType inbox = new DistinguishedFolderIdType("inbox"); + public static final DistinguishedFolderIdType journal = new DistinguishedFolderIdType("journal"); + public static final DistinguishedFolderIdType notes = new DistinguishedFolderIdType("notes"); + public static final DistinguishedFolderIdType outbox = new DistinguishedFolderIdType("outbox"); + public static final DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType("sentitems"); + public static final DistinguishedFolderIdType tasks = new DistinguishedFolderIdType("tasks"); + public static final DistinguishedFolderIdType msgfolderroot = new DistinguishedFolderIdType("msgfolderroot"); + public static final DistinguishedFolderIdType publicfoldersroot = new DistinguishedFolderIdType("publicfoldersroot"); + public static final DistinguishedFolderIdType root = new DistinguishedFolderIdType("root"); + public static final DistinguishedFolderIdType junkemail = new DistinguishedFolderIdType("junkemail"); + public static final DistinguishedFolderIdType searchfolders = new DistinguishedFolderIdType("searchfolders"); + public static final DistinguishedFolderIdType voicemail = new DistinguishedFolderIdType("voicemail"); + +} \ No newline at end of file diff --git a/src/java/davmail/exchange/ews/EWSMethod.java b/src/java/davmail/exchange/ews/EWSMethod.java index e1303f0e..8354b7aa 100644 --- a/src/java/davmail/exchange/ews/EWSMethod.java +++ b/src/java/davmail/exchange/ews/EWSMethod.java @@ -39,62 +39,11 @@ import java.util.List; public abstract class EWSMethod extends PostMethod { protected static final Logger logger = Logger.getLogger(EWSMethod.class); - public static final class BaseShape { - private final String value; + protected FolderQueryTraversalType traversal; + protected BaseShapeType baseShape; + protected FolderIdType folderId; + protected FolderIdType parentFolderId; - private BaseShape(String value) { - this.value = value; - } - - public void write(Writer writer) throws IOException { - writer.write(""); - writer.write(value); - writer.write(""); - } - - public static final BaseShape IdOnly = new BaseShape("IdOnly"); - public static final BaseShape Default = new BaseShape("Default"); - public static final BaseShape AllProperties = new BaseShape("AllProperties"); - } - - public static final class DistinguishedFolderId { - private final String value; - - private DistinguishedFolderId(String value) { - this.value = value; - } - - public void write(Writer writer) throws IOException { - writer.write(""); - } - - public static final DistinguishedFolderId msgfolderroot = new DistinguishedFolderId("msgfolderroot"); - public static final DistinguishedFolderId inbox = new DistinguishedFolderId("inbox"); - public static final DistinguishedFolderId publicfoldersroot = new DistinguishedFolderId("publicfoldersroot"); - } - - public static final class Traversal { - private final String value; - - private Traversal(String value) { - this.value = value; - } - - public void write(Writer writer) throws IOException { - writer.write(" Traversal=\""); - writer.write(value); - writer.write("\""); - } - - public static final Traversal Shallow = new Traversal("Shallow"); - public static final Traversal Deep = new Traversal("Deep"); - } - - protected Traversal traversal = null; - protected BaseShape baseShape; - protected DistinguishedFolderId distinguishedFolderId = DistinguishedFolderId.msgfolderroot; /** * Build EWS method @@ -134,15 +83,19 @@ public abstract class EWSMethod extends PostMethod { return "POST"; } - protected void setBaseShape(BaseShape baseShape) { - this.baseShape = baseShape; + protected void setBaseShape(BaseShapeType baseShapeType) { + this.baseShape = baseShapeType; } - protected void setDistinguishedFolderId(DistinguishedFolderId distinguishedFolderId) { - this.distinguishedFolderId = distinguishedFolderId; + protected void setFolderId(FolderIdType folderId) { + this.folderId = folderId; } - protected void generateShape(Writer writer) throws IOException { + protected void setParentFolderId(FolderIdType folderId) { + this.parentFolderId = folderId; + } + + protected void writeShape(Writer writer) throws IOException { if (baseShape != null) { writer.write(""); + folderId.write(writer); + writer.write(""); + } + } + + protected void writeParentFolderId(Writer writer) throws IOException { + if (parentFolderId != null) { + writer.write(""); + parentFolderId.write(writer); + writer.write(""); + } + } + protected byte[] generateSoapEnvelope() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { @@ -168,7 +137,7 @@ public abstract class EWSMethod extends PostMethod { traversal.write(writer); } writer.write(">"); - generateSoapBody(writer); + writeSoapBody(writer); writer.write(""); @@ -181,7 +150,11 @@ public abstract class EWSMethod extends PostMethod { return baos.toByteArray(); } - protected abstract void generateSoapBody(Writer writer) throws IOException; + protected void writeSoapBody(Writer writer) throws IOException { + writeShape(writer); + writeParentFolderId(writer); + writeFolderId(writer); + } /** * Build a new XMLInputFactory. @@ -199,10 +172,11 @@ public abstract class EWSMethod extends PostMethod { public String id; public String changeKey; public String displayName; + public String type; @Override public String toString() { - return "id: " + id + " changeKey:" + changeKey + " displayName:" + displayName; + return "type: " + type + " id: " + id + " changeKey:" + changeKey + " displayName:" + displayName; } } @@ -215,6 +189,8 @@ public abstract class EWSMethod extends PostMethod { protected abstract String getResponseItemId(); + protected abstract String getResponseCollectionName(); + public List getResponseItems() { return responseItems; } @@ -245,31 +221,40 @@ public abstract class EWSMethod extends PostMethod { } } + protected boolean isStartTag(XMLStreamReader reader) { + return (reader.getEventType() == XMLStreamConstants.START_ELEMENT); + } + + protected boolean isStartTag(XMLStreamReader reader, String tagLocalName) { + return (reader.getEventType() == XMLStreamConstants.START_ELEMENT) && (reader.getLocalName().equals(tagLocalName)); + } + + protected boolean isEndTag(XMLStreamReader reader, String tagLocalName) { + return (reader.getEventType() == XMLStreamConstants.END_ELEMENT) && (reader.getLocalName().equals(tagLocalName)); + } + protected Item handleItem(XMLStreamReader reader) throws XMLStreamException { - Item result = null; + Item item = new Item(); + item.type = reader.getLocalName(); int event = reader.getEventType(); - if (event == XMLStreamConstants.START_ELEMENT && getResponseItemName().equals(reader.getLocalName())) { - result = new Item(); - while (reader.hasNext() && - !((event == XMLStreamConstants.END_ELEMENT && getResponseItemName().equals(reader.getLocalName())))) { - event = reader.next(); - if (event == XMLStreamConstants.START_ELEMENT && getResponseItemId().equals(reader.getLocalName())) { - for (int i = 0; i < reader.getAttributeCount(); i++) { - if ("Id".equals(reader.getAttributeLocalName(i))) { - result.id = reader.getAttributeValue(i); - } else if ("ChangeKey".equals(reader.getAttributeLocalName(i))) { - result.changeKey = reader.getAttributeValue(i); - } - } - } else { - String displayName = handleTag(reader, "DisplayName"); - if (displayName != null) { - result.displayName = displayName; + while (reader.hasNext() && !isEndTag(reader, item.type)) { + event = reader.next(); + if (event == XMLStreamConstants.START_ELEMENT && getResponseItemId().equals(reader.getLocalName())) { + for (int i = 0; i < reader.getAttributeCount(); i++) { + if ("Id".equals(reader.getAttributeLocalName(i))) { + item.id = reader.getAttributeValue(i); + } else if ("ChangeKey".equals(reader.getAttributeLocalName(i))) { + item.changeKey = reader.getAttributeValue(i); } } + } else { + String displayName = handleTag(reader, "DisplayName"); + if (displayName != null) { + item.displayName = displayName; + } } } - return result; + return item; } @Override @@ -282,11 +267,10 @@ public abstract class EWSMethod extends PostMethod { XMLInputFactory xmlInputFactory = getXmlInputFactory(); reader = xmlInputFactory.createXMLStreamReader(getResponseBodyAsStream()); while (reader.hasNext()) { - int event = reader.next(); + reader.next(); handleErrors(reader); - Item item = handleItem(reader); - if (item != null) { - responseItems.add(item); + if (isStartTag(reader, getResponseCollectionName())) { + handleItems(reader); } } @@ -301,4 +285,14 @@ public abstract class EWSMethod extends PostMethod { } } + private void handleItems(XMLStreamReader reader) throws XMLStreamException { + while (reader.hasNext() && !isEndTag(reader, getResponseCollectionName())) { + reader.next(); + if (isStartTag(reader)) { + responseItems.add(handleItem(reader)); + } + } + + } + } diff --git a/src/java/davmail/exchange/ews/FindFolderMethod.java b/src/java/davmail/exchange/ews/FindFolderMethod.java index 2be96aae..48a6ee6b 100644 --- a/src/java/davmail/exchange/ews/FindFolderMethod.java +++ b/src/java/davmail/exchange/ews/FindFolderMethod.java @@ -18,23 +18,15 @@ */ package davmail.exchange.ews; -import java.io.IOException; -import java.io.Writer; - /** * EWS Find Folder. */ public class FindFolderMethod extends EWSMethod { - public FindFolderMethod(Traversal traversal) { + public FindFolderMethod(FolderQueryTraversalType traversal, BaseShapeType baseShape, FolderIdType parentFolderId) { this.traversal = traversal; - } - @Override - protected void generateSoapBody(Writer writer) throws IOException { - generateShape(writer); - writer.write(" \n"); - distinguishedFolderId.write(writer); - writer.write(" \n"); + this.baseShape = baseShape; + this.parentFolderId = parentFolderId; } @Override @@ -44,11 +36,16 @@ public class FindFolderMethod extends EWSMethod { @Override protected String getResponseItemName() { - return "Folder"; + return "Folder"; } @Override protected String getResponseItemId() { return "FolderId"; } + + @Override + protected String getResponseCollectionName() { + return getResponseItemName()+"s"; + } } diff --git a/src/java/davmail/exchange/ews/FindItemMethod.java b/src/java/davmail/exchange/ews/FindItemMethod.java index 9c865d6d..2e66f1e8 100644 --- a/src/java/davmail/exchange/ews/FindItemMethod.java +++ b/src/java/davmail/exchange/ews/FindItemMethod.java @@ -25,15 +25,14 @@ import java.io.Writer; * EWS Find Item Method. */ public class FindItemMethod extends EWSMethod { - public FindItemMethod(EWSMethod.Traversal traversal) { - this.traversal = traversal; + public FindItemMethod(FolderQueryTraversalType folderQueryTraversalType) { + this.traversal = folderQueryTraversalType; } + @Override - protected void generateSoapBody(Writer writer) throws IOException { - generateShape(writer); - writer.write(" \n"); - distinguishedFolderId.write(writer); - writer.write(" \n"); + protected void writeSoapBody(Writer writer) throws IOException { + writeShape(writer); + writeParentFolderId(writer); } @Override @@ -43,12 +42,17 @@ public class FindItemMethod extends EWSMethod { @Override protected String getResponseItemName() { - return "Message"; + return "Item"; } @Override protected String getResponseItemId() { return "ItemId"; } + + @Override + protected String getResponseCollectionName() { + return "Items"; + } } diff --git a/src/java/davmail/exchange/ews/FolderIdType.java b/src/java/davmail/exchange/ews/FolderIdType.java new file mode 100644 index 00000000..18b1d10c --- /dev/null +++ b/src/java/davmail/exchange/ews/FolderIdType.java @@ -0,0 +1,40 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2010 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.exchange.ews; + +import java.io.IOException; +import java.io.Writer; + +/** + * Folder Id. + */ +public class FolderIdType { + protected final String value; + + public FolderIdType(String value) { + this.value = value; + } + + public void write(Writer writer) throws IOException { + writer.write(""); + } + +} diff --git a/src/java/davmail/exchange/ews/FolderQueryTraversalType.java b/src/java/davmail/exchange/ews/FolderQueryTraversalType.java new file mode 100644 index 00000000..10a3c360 --- /dev/null +++ b/src/java/davmail/exchange/ews/FolderQueryTraversalType.java @@ -0,0 +1,54 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2010 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.exchange.ews; + +import java.io.IOException; +import java.io.Writer; + +/** + * Folder folderQueryTraversalType search mode. + */ +public final class FolderQueryTraversalType { + private final String value; + + private FolderQueryTraversalType(String value) { + this.value = value; + } + + /** + * Write XML content to writer. + * + * @param writer writer + * @throws IOException on error + */ + public void write(Writer writer) throws IOException { + writer.write(" Traversal=\""); + writer.write(value); + writer.write("\""); + } + + /** + * Search only in current folder. + */ + public static final FolderQueryTraversalType Shallow = new FolderQueryTraversalType("Shallow"); + /** + * Recursive search. + */ + public static final FolderQueryTraversalType Deep = new FolderQueryTraversalType("Deep"); +} \ No newline at end of file diff --git a/src/java/davmail/exchange/ews/GetFolderMethod.java b/src/java/davmail/exchange/ews/GetFolderMethod.java index db9b23a9..9952989a 100644 --- a/src/java/davmail/exchange/ews/GetFolderMethod.java +++ b/src/java/davmail/exchange/ews/GetFolderMethod.java @@ -26,15 +26,7 @@ import java.io.Writer; */ public class GetFolderMethod extends EWSMethod { - - @Override - protected void generateSoapBody(Writer writer) throws IOException { - generateShape(writer); - writer.write(" \n"); - distinguishedFolderId.write(writer); - writer.write(" \n"); - } - + @Override protected String getMethodName() { return "GetFolder"; @@ -49,4 +41,9 @@ public class GetFolderMethod extends EWSMethod { protected String getResponseItemId() { return getResponseItemName()+"Id"; } + + @Override + protected String getResponseCollectionName() { + return getResponseItemName()+ 's'; + } }