1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-14 03:32:22 -05:00

Refactor ExchangeSession, use StringUtil to simplify code

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@817 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-11-03 10:01:33 +00:00
parent 46db035ddd
commit 06e1a4896f
3 changed files with 82 additions and 58 deletions

View File

@ -393,42 +393,19 @@ public class ExchangeSession {
for (Object content : contents) {
if (content instanceof CommentToken) {
String scriptValue = ((CommentToken) content).getCommentedContent();
int sUrlIndex = scriptValue.indexOf("var a_sUrl = \"");
int sLgnIndex = scriptValue.indexOf("var a_sLgn = \"");
if (sUrlIndex >= 0 && sLgnIndex >= 0) {
sUrlIndex += "var a_sUrl = \"".length();
sLgnIndex += "var a_sLgn = \"".length();
int sUrlEndIndex = scriptValue.indexOf('\"', sUrlIndex);
int sLgnEndIndex = scriptValue.indexOf('\"', sLgnIndex);
if (sUrlEndIndex >= 0 && sLgnEndIndex >= 0) {
String pathQuery = scriptValue.substring(sLgnIndex, sLgnEndIndex) +
scriptValue.substring(sUrlIndex, sUrlEndIndex);
String src = getScriptBasedFormURL(initmethod, pathQuery);
LOGGER.debug("Detected script based logon, redirect to form at " + src);
HttpMethod newInitMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, src);
logonMethod = buildLogonMethod(httpClient, newInitMethod);
}
} else {
sLgnIndex = scriptValue.indexOf("var a_sLgnQS = \"");
if (sUrlIndex >= 0 && sLgnIndex >= 0) {
sUrlIndex += "var a_sUrl = \"".length();
sLgnIndex += "var a_sLgnQS = \"".length();
int sUrlEndIndex = scriptValue.indexOf('\"', sUrlIndex);
int sLgnEndIndex = scriptValue.indexOf('\"', sLgnIndex);
if (sUrlEndIndex >= 0 && sLgnEndIndex >= 0) {
String pathQuery = scriptValue.substring(sLgnIndex, sLgnEndIndex) +
scriptValue.substring(sUrlIndex, sUrlEndIndex);
String src = getScriptBasedFormURL(initmethod, pathQuery);
LOGGER.debug("Detected script based logon, redirect to form at " + src);
HttpMethod newInitMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, src);
logonMethod = buildLogonMethod(httpClient, newInitMethod);
}
}
String sUrl = StringUtil.getToken(scriptValue, "var a_sUrl = \"", "\"");
String sLgn = StringUtil.getToken(scriptValue, "var a_sLgn = \"", "\"");
if (sLgn == null) {
sLgn = StringUtil.getToken(scriptValue, "var a_sLgnQS = \"", "\"");
}
if (sUrl != null && sLgn != null) {
String src = getScriptBasedFormURL(initmethod, sLgn + sUrl);
LOGGER.debug("Detected script based logon, redirect to form at " + src);
HttpMethod newInitMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, src);
logonMethod = buildLogonMethod(httpClient, newInitMethod);
}
}
}
}
}
}
@ -2010,9 +1987,9 @@ public class ExchangeSession {
} else if (fromServer && currentAllDayState.isCdoAllDay && line.startsWith("DTEND") && !line.startsWith("DTEND;VALUE=DATE")) {
line = getAllDayLine(line);
} else if (!fromServer && currentAllDayState.isAllDay && line.startsWith("DTSTART") && line.startsWith("DTSTART;VALUE=DATE")) {
line = "DTSTART;TZID=\""+ExchangeSession.this.getVTimezone().timezoneId+"\":" + line.substring(19) + "T000000";
line = "DTSTART;TZID=\"" + ExchangeSession.this.getVTimezone().timezoneId + "\":" + line.substring(19) + "T000000";
} else if (!fromServer && currentAllDayState.isAllDay && line.startsWith("DTEND") && line.startsWith("DTEND;VALUE=DATE")) {
line = "DTEND;TZID=\""+ExchangeSession.this.getVTimezone().timezoneId+"\":" + line.substring(17) + "T000000";
line = "DTEND;TZID=\"" + ExchangeSession.this.getVTimezone().timezoneId + "\":" + line.substring(17) + "T000000";
} else if (line.startsWith("TZID:") && validTimezoneId != null) {
line = "TZID:" + validTimezoneId;
} else if ("BEGIN:VEVENT".equals(line)) {
@ -2136,13 +2113,7 @@ public class ExchangeSession {
}
protected String fixTimezoneId(String line, String validTimezoneId) {
int startIndex = line.indexOf("TZID=");
int endIndex = line.indexOf(':', startIndex + 5);
if (startIndex >= 0 && endIndex >= 0) {
return line.substring(0, startIndex + 5) + validTimezoneId + line.substring(endIndex);
} else {
return line;
}
return StringUtil.replaceToken(line, "TZID=", ":", validTimezoneId);
}
protected void splitExDate(ICSBufferedWriter result, String line) {
@ -2225,16 +2196,12 @@ public class ExchangeSession {
}
protected String getICSMethod(String icsBody) {
int methodIndex = icsBody.indexOf("METHOD:");
if (methodIndex < 0) {
return "REQUEST";
String icsMethod = StringUtil.getToken(icsBody, "METHOD:", "\r");
if (icsMethod == null) {
// default method is REQUEST
icsMethod = "REQUEST";
}
int startIndex = methodIndex + "METHOD:".length();
int endIndex = icsBody.indexOf('\r', startIndex);
if (endIndex < 0) {
return "REQUEST";
}
return icsBody.substring(startIndex, endIndex);
return icsMethod;
}
protected String getICSValue(String icsBody, String prefix, String defval) throws IOException {
@ -3074,11 +3041,8 @@ public class ExchangeSession {
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true);
String body = getMethod.getResponseBodyAsString();
int startIndex = body.lastIndexOf("<a:fbdata>");
int endIndex = body.lastIndexOf("</a:fbdata>");
if (startIndex >= 0 && endIndex >= 0) {
String fbdata = body.substring(startIndex + "<a:fbdata>".length(), endIndex);
String fbdata = StringUtil.getLastToken(getMethod.getResponseBodyAsString(), "<a:fbdata>", "</a:fbdata>");
if (fbdata != null) {
freeBusy = new FreeBusy(icalDateFormat, startDate, fbdata);
}
} finally {

View File

@ -26,7 +26,7 @@ public class StringUtil {
}
/**
* Return the characters between startDelimiter and endDelimiter or null.
* Return the substring between startDelimiter and endDelimiter or null.
*
* @param value String value
* @param startDelimiter start delimiter
@ -47,4 +47,52 @@ public class StringUtil {
}
return token;
}
/**
* Return the substring between startDelimiter and endDelimiter or null,
* look for last token in string.
*
* @param value String value
* @param startDelimiter start delimiter
* @param endDelimiter end delimiter
* @return token value
*/
public static String getLastToken(String value, String startDelimiter, String endDelimiter) {
String token = null;
if (value != null) {
int startIndex = value.lastIndexOf(startDelimiter);
if (startIndex >= 0) {
startIndex += startDelimiter.length();
int endIndex = value.indexOf(endDelimiter, startIndex);
if (endIndex >= 0) {
token = value.substring(startIndex, endIndex);
}
}
}
return token;
}
/**
* Return the substring between startDelimiter and endDelimiter with newToken.
*
* @param value String value
* @param startDelimiter start delimiter
* @param endDelimiter end delimiter
* @param newToken new token value
* @return token value
*/
public static String replaceToken(String value, String startDelimiter, String endDelimiter, String newToken) {
String result = null;
if (value != null) {
int startIndex = value.indexOf(startDelimiter);
if (startIndex >= 0) {
startIndex += startDelimiter.length();
int endIndex = value.indexOf(endDelimiter, startIndex);
if (endIndex >= 0) {
result = value.substring(0, startIndex) + newToken + value.substring(endIndex);
}
}
}
return result;
}
}

View File

@ -19,12 +19,14 @@
package davmail.util;
import junit.framework.TestCase;
import davmail.util.StringUtil;
/**
* Test StringUtil.
*/
public class StringUtilTest extends TestCase {
/**
* Test get token
*/
public void testGetToken() {
assertNull(StringUtil.getToken(null, null, null));
assertNull(StringUtil.getToken(null, "\'", "\'"));
@ -32,4 +34,14 @@ public class StringUtilTest extends TestCase {
assertEquals("test", StringUtil.getToken("'test'", "'", "'"));
assertEquals("test", StringUtil.getToken("value=\"test\"", "value=\"", "\""));
}
/**
* Test replace token
*/
public void testReplaceToken() {
assertNull(StringUtil.replaceToken(null, null, null, null));
assertNull(StringUtil.replaceToken(null, null, null, "new"));
assertEquals("'new'", StringUtil.replaceToken("'old'", "'", "'", "new"));
assertEquals("value=\"new\"", StringUtil.replaceToken("value=\"old\"", "value=\"", "\"", "new"));
}
}