From efeaec59351a45b29a78e9c5f04c7db292824eaf Mon Sep 17 00:00:00 2001 From: mguessan Date: Sun, 25 Jul 2010 12:58:25 +0000 Subject: [PATCH] EWS: fix folder name ampersand encoding issue git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1268 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/exchange/ews/EWSMethod.java | 3 +- .../exchange/ews/EwsExchangeSession.java | 4 +-- src/java/davmail/util/StringUtil.java | 32 ++++++++++++++++++- .../exchange/TestExchangeSessionFolder.java | 13 ++++++++ src/test/davmail/util/StringUtilTest.java | 5 +++ 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/java/davmail/exchange/ews/EWSMethod.java b/src/java/davmail/exchange/ews/EWSMethod.java index 70bbc4e3..98877303 100644 --- a/src/java/davmail/exchange/ews/EWSMethod.java +++ b/src/java/davmail/exchange/ews/EWSMethod.java @@ -18,6 +18,7 @@ */ package davmail.exchange.ews; +import davmail.util.StringUtil; import org.apache.commons.codec.binary.Base64; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpConnection; @@ -410,7 +411,7 @@ public abstract class EWSMethod extends PostMethod { writer.write(""); - writer.write(mapEntry.getValue()); + writer.write(StringUtil.xmlEncode(mapEntry.getValue())); writer.write(""); diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 9f00aec3..68f7c8ca 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -496,7 +496,7 @@ public class EwsExchangeSession extends ExchangeSession { protected Folder buildFolder(EWSMethod.Item item) { Folder folder = new Folder(); folder.folderId = new FolderId(item); - folder.displayName = item.get(Field.get("urlcompname").getResponseName()); + folder.displayName = StringUtil.urlDecodeAmpersand(item.get(Field.get("urlcompname").getResponseName())); folder.folderClass = item.get(Field.get("folderclass").getResponseName()); folder.etag = item.get(Field.get("lastmodified").getResponseName()); folder.ctag = item.get(Field.get("ctag").getResponseName()); @@ -1112,7 +1112,7 @@ public class EwsExchangeSession extends ExchangeSession { parentFolderId, FOLDER_PROPERTIES, new TwoOperandExpression(TwoOperandExpression.Operator.IsEqualTo, - Field.get("urlcompname"), folderName) + Field.get("urlcompname"), StringUtil.urlEncodeAmpersand(folderName)) ); executeMethod(findFolderMethod); EWSMethod.Item item = findFolderMethod.getResponseItem(); diff --git a/src/java/davmail/util/StringUtil.java b/src/java/davmail/util/StringUtil.java index 77c53166..b5bcac59 100644 --- a/src/java/davmail/util/StringUtil.java +++ b/src/java/davmail/util/StringUtil.java @@ -129,12 +129,42 @@ public final class StringUtil { private static final Pattern LT_PATTERN = Pattern.compile("<"); private static final Pattern GT_PATTERN = Pattern.compile(">"); + private static final Pattern URLENCODED_AMP_PATTERN = Pattern.compile("%26"); + private static final Pattern ENCODED_AMP_PATTERN = Pattern.compile("&"); private static final Pattern ENCODED_LT_PATTERN = Pattern.compile("<"); private static final Pattern ENCODED_GT_PATTERN = Pattern.compile(">"); /** - * Need to encode xml for iCal + * Encode & to %26 for urlcompname. + * + * @param name decoded name + * @return name encoded name + */ + public static String urlEncodeAmpersand(String name) { + String result = name; + if (name.indexOf('&') >= 0) { + result = AMP_PATTERN.matcher(result).replaceAll("%26"); + } + return result; + } + + /** + * Decode %26 to & for urlcompname. + * + * @param name decoded name + * @return name encoded name + */ + public static String urlDecodeAmpersand(String name) { + String result = name; + if (name.indexOf("%26") >= 0) { + result = URLENCODED_AMP_PATTERN.matcher(result).replaceAll("&"); + } + return result; + } + + /** + * Need to encode xml for iCal. * * @param name decoded name * @return name encoded name diff --git a/src/test/davmail/exchange/TestExchangeSessionFolder.java b/src/test/davmail/exchange/TestExchangeSessionFolder.java index be4347db..28b7e80e 100644 --- a/src/test/davmail/exchange/TestExchangeSessionFolder.java +++ b/src/test/davmail/exchange/TestExchangeSessionFolder.java @@ -105,4 +105,17 @@ public class TestExchangeSessionFolder extends AbstractExchangeSessionTestCase { assertEquals(folderName, folder.folderPath); session.deleteFolder(folderName); } + + public void testSpecialFolderCharacter() throws IOException { + String folderName = "Special & accenté"; + session.deleteFolder(folderName); + session.createMessageFolder(folderName); + ExchangeSession.Folder folder = session.getFolder(folderName); + assertNotNull(folder); + assertEquals(folderName, folder.displayName); + assertEquals(folderName, folder.folderPath); + session.deleteFolder(folderName); + } + + } diff --git a/src/test/davmail/util/StringUtilTest.java b/src/test/davmail/util/StringUtilTest.java index c5fc06a2..e6426875 100644 --- a/src/test/davmail/util/StringUtilTest.java +++ b/src/test/davmail/util/StringUtilTest.java @@ -57,4 +57,9 @@ public class StringUtilTest extends TestCase { assertEquals(">", StringUtil.xmlDecode(">")); assertEquals("<test>", StringUtil.xmlEncode("")); } + + public void testUrlEncodeAmpersand() { + assertEquals("%26", StringUtil.urlEncodeAmpersand("&")); + assertEquals("&", StringUtil.urlDecodeAmpersand("%26")); + } }