1
0
mirror of https://github.com/moparisthebest/davmail synced 2025-02-28 17:31:52 -05:00

EWS: refactor EWS code

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1058 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-05-18 17:05:12 +00:00
parent 48afdb8f8b
commit 23a5f3e560
8 changed files with 313 additions and 115 deletions

View File

@ -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("<t:BaseShape>");
writer.write(value);
writer.write("</t:BaseShape>");
}
/**
* 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");
}

View File

@ -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("<t:DistinguishedFolderId Id=\"");
writer.write(value);
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");
}

View File

@ -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("<t:BaseShape>");
writer.write(value);
writer.write("</t:BaseShape>");
}
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("<t:DistinguishedFolderId Id=\"");
writer.write(value);
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("<m:");
writer.write(getResponseItemName());
@ -154,6 +107,22 @@ public abstract class EWSMethod extends PostMethod {
}
}
protected void writeFolderId(Writer writer) throws IOException {
if (folderId != null) {
writer.write("<m:FolderIds>");
folderId.write(writer);
writer.write("</m:FolderIds>");
}
}
protected void writeParentFolderId(Writer writer) throws IOException {
if (parentFolderId != null) {
writer.write("<m:ParentFolderIds>");
parentFolderId.write(writer);
writer.write("</m:ParentFolderIds>");
}
}
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("</m:");
writer.write(getMethodName());
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<Item> 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));
}
}
}
}

View File

@ -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(" <m:ParentFolderIds>\n");
distinguishedFolderId.write(writer);
writer.write(" </m:ParentFolderIds>\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";
}
}

View File

@ -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(" <m:ParentFolderIds>\n");
distinguishedFolderId.write(writer);
writer.write(" </m:ParentFolderIds>\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";
}
}

View File

@ -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("<t:FolderId Id=\"");
writer.write(value);
writer.write("\"/>");
}
}

View File

@ -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");
}

View File

@ -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(" <m:FolderIds>\n");
distinguishedFolderId.write(writer);
writer.write(" </m:FolderIds>\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';
}
}