1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-08-13 16:53:51 -04:00

Refactor ExchangeSession: do not follow redirects with GET methods

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@729 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-09-17 21:40:47 +00:00
parent 4a175cbe82
commit 392ef6448a
4 changed files with 41 additions and 45 deletions

View File

@ -1356,18 +1356,12 @@ public class ExchangeSession {
* @throws IOException on error * @throws IOException on error
*/ */
public void write(OutputStream os) throws IOException { public void write(OutputStream os) throws IOException {
HttpMethod method = null; GetMethod method = new GetMethod(URIUtil.encodePath(messageUrl));
method.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
method.setRequestHeader("Translate", "f");
BufferedReader reader = null; BufferedReader reader = null;
try { try {
method = new GetMethod(URIUtil.encodePath(messageUrl)); DavGatewayHttpClientFacade.executeGetMethod(httpClient, method);
method.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
method.setRequestHeader("Translate", "f");
// do not follow redirects in expired session
method.setFollowRedirects(false);
int status = httpClient.executeMethod(method);
if (status != HttpStatus.SC_OK) {
throw DavGatewayHttpClientFacade.buildHttpException(method);
}
reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
OutputStreamWriter isoWriter = new OutputStreamWriter(os); OutputStreamWriter isoWriter = new OutputStreamWriter(os);
@ -1405,9 +1399,7 @@ public class ExchangeSession {
LOGGER.warn("Error closing message input stream", e); LOGGER.warn("Error closing message input stream", e);
} }
} }
if (method != null) { method.releaseConnection();
method.releaseConnection();
}
} }
} }
@ -1524,10 +1516,8 @@ public class ExchangeSession {
method.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); method.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
method.setRequestHeader("Translate", "f"); method.setRequestHeader("Translate", "f");
try { try {
int status = httpClient.executeMethod(method); DavGatewayHttpClientFacade.executeGetMethod(httpClient, method);
if (status != HttpStatus.SC_OK) {
LOGGER.warn("Unable to get event at " + href + " status: " + status);
}
MimeMessage mimeMessage = new MimeMessage(null, method.getResponseBodyAsStream()); MimeMessage mimeMessage = new MimeMessage(null, method.getResponseBodyAsStream());
Object mimeBody = mimeMessage.getContent(); Object mimeBody = mimeMessage.getContent();
MimePart bodyPart; MimePart bodyPart;
@ -1548,9 +1538,10 @@ public class ExchangeSession {
bodyPart.getDataHandler().writeTo(baos); bodyPart.getDataHandler().writeTo(baos);
baos.close(); baos.close();
result = fixICS(new String(baos.toByteArray(), "UTF-8"), true); result = fixICS(new String(baos.toByteArray(), "UTF-8"), true);
} catch (IOException e) {
LOGGER.warn("Unable to get event at " + href+": "+e.getMessage());
} catch (MessagingException e) { } catch (MessagingException e) {
throw new DavMailException("EXCEPTION_INVALID_MESSAGE_CONTENT", e.getMessage()); LOGGER.warn("Unable to get event at " + href+": "+e.getMessage());
} finally { } finally {
method.releaseConnection(); method.releaseConnection();
} }
@ -2502,10 +2493,7 @@ public class ExchangeSession {
try { try {
path = getCmdBasePath() + "?Cmd=galfind&AN=" + URIUtil.encodeWithinQuery(alias); path = getCmdBasePath() + "?Cmd=galfind&AN=" + URIUtil.encodeWithinQuery(alias);
getMethod = new GetMethod(path); getMethod = new GetMethod(path);
int status = httpClient.executeMethod(getMethod); DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod);
if (status != HttpStatus.SC_OK) {
throw new DavMailException("EXCEPTION_UNABLE_TO_GET_EMAIL", getMethod.getPath());
}
Map<String, Map<String, String>> results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN"); Map<String, Map<String, String>> results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN");
Map<String, String> result = results.get(alias.toLowerCase()); Map<String, String> result = results.get(alias.toLowerCase());
if (result != null) { if (result != null) {
@ -2581,7 +2569,7 @@ public class ExchangeSession {
BufferedReader optionsPageReader = null; BufferedReader optionsPageReader = null;
GetMethod optionsMethod = new GetMethod(path + "?ae=Options&t=About"); GetMethod optionsMethod = new GetMethod(path + "?ae=Options&t=About");
try { try {
httpClient.executeMethod(optionsMethod); DavGatewayHttpClientFacade.executeGetMethod(httpClient, optionsMethod);
optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream())); optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream()));
String line; String line;
// find mailbox full name // find mailbox full name
@ -2616,7 +2604,7 @@ public class ExchangeSession {
BufferedReader optionsPageReader = null; BufferedReader optionsPageReader = null;
GetMethod optionsMethod = new GetMethod(path + "?ae=Options&t=About"); GetMethod optionsMethod = new GetMethod(path + "?ae=Options&t=About");
try { try {
httpClient.executeMethod(optionsMethod); DavGatewayHttpClientFacade.executeGetMethod(httpClient, optionsMethod);
optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream())); optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream()));
String line; String line;
// find email // find email
@ -2677,10 +2665,7 @@ public class ExchangeSession {
Map<String, Map<String, String>> results; Map<String, Map<String, String>> results;
GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery(getCmdBasePath() + "?Cmd=galfind&" + searchAttribute + '=' + searchValue)); GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery(getCmdBasePath() + "?Cmd=galfind&" + searchAttribute + '=' + searchValue));
try { try {
int status = httpClient.executeMethod(getMethod); DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod);
if (status != HttpStatus.SC_OK) {
throw new DavMailException("EXCEPTION_UNABLE_TO_FIND_USERS", status, getMethod.getURI());
}
results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN"); results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN");
} finally { } finally {
getMethod.releaseConnection(); getMethod.releaseConnection();
@ -2802,10 +2787,7 @@ public class ExchangeSession {
GetMethod getMethod = null; GetMethod getMethod = null;
try { try {
getMethod = new GetMethod(URIUtil.encodePathQuery(getCmdBasePath() + "?Cmd=gallookup&ADDR=" + person.get("EM"))); getMethod = new GetMethod(URIUtil.encodePathQuery(getCmdBasePath() + "?Cmd=gallookup&ADDR=" + person.get("EM")));
int status = httpClient.executeMethod(getMethod); DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod);
if (status != HttpStatus.SC_OK) {
throw new DavMailException("EXCEPTION_UNABLE_TO_FIND_USERS", status, getMethod.getURI());
}
Map<String, Map<String, String>> results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "person", "alias"); Map<String, Map<String, String>> results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "person", "alias");
// add detailed information // add detailed information
if (!results.isEmpty()) { if (!results.isEmpty()) {
@ -2873,11 +2855,7 @@ public class ExchangeSession {
getMethod.setRequestHeader("Content-Type", "text/xml"); getMethod.setRequestHeader("Content-Type", "text/xml");
try { try {
int status = httpClient.executeMethod(getMethod); DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod);
if (status != HttpStatus.SC_OK) {
throw new DavMailException("EXCEPTION_UNABLE_TO_GET_FREEBUSY", getMethod.getPath(),
status, getMethod.getResponseBodyAsString());
}
String body = getMethod.getResponseBodyAsString(); String body = getMethod.getResponseBodyAsString();
int startIndex = body.lastIndexOf("<a:fbdata>"); int startIndex = body.lastIndexOf("<a:fbdata>");
int endIndex = body.lastIndexOf("</a:fbdata>"); int endIndex = body.lastIndexOf("</a:fbdata>");

View File

@ -35,6 +35,7 @@ import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.client.methods.DavMethodBase; import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet; import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
import org.apache.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -43,6 +44,8 @@ import java.util.ArrayList;
* Create HttpClient instance according to DavGateway Settings * Create HttpClient instance according to DavGateway Settings
*/ */
public final class DavGatewayHttpClientFacade { public final class DavGatewayHttpClientFacade {
static final Logger LOGGER = Logger.getLogger("davmail.http.DavGatewayHttpClientFacade");
static final String IE_USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"; static final String IE_USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
static final int MAX_REDIRECTS = 10; static final int MAX_REDIRECTS = 10;
static MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager; static MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager;
@ -291,6 +294,7 @@ public final class DavGatewayHttpClientFacade {
*/ */
public static int executeDeleteMethod(HttpClient httpClient, String path) throws IOException { public static int executeDeleteMethod(HttpClient httpClient, String path) throws IOException {
DeleteMethod deleteMethod = new DeleteMethod(path); DeleteMethod deleteMethod = new DeleteMethod(path);
deleteMethod.setFollowRedirects(false);
int status = executeHttpMethod(httpClient, deleteMethod); int status = executeHttpMethod(httpClient, deleteMethod);
// do not throw error if already deleted // do not throw error if already deleted
@ -344,6 +348,24 @@ public final class DavGatewayHttpClientFacade {
return status; return status;
} }
/**
* Execute Get method, do not follow redirects.
*
* @param httpClient Http client instance
* @param method Http method
* @throws IOException on error
*/
public static void executeGetMethod(HttpClient httpClient, GetMethod method) throws IOException {
// do not follow redirects in expired sessions
method.setFollowRedirects(false);
int status = httpClient.executeMethod(method);
if (status != HttpStatus.SC_OK) {
LOGGER.warn("GET failed with status "+status+" at "+method.getURI()+": "+method.getResponseBodyAsString());
throw new DavMailException("EXCEPTION_GET_FAILED", status, method.getURI());
}
}
/** /**
* Build Http Exception from methode status * Build Http Exception from methode status
* *

View File

@ -25,10 +25,8 @@ EXCEPTION_INVALID_REQUEST=Invalid request: {0}
EXCEPTION_INVALID_SEARCH_PARAMETERS=Invalid search parameters: {0} EXCEPTION_INVALID_SEARCH_PARAMETERS=Invalid search parameters: {0}
EXCEPTION_NETWORK_DOWN=All network interfaces down or host unreachable ! EXCEPTION_NETWORK_DOWN=All network interfaces down or host unreachable !
EXCEPTION_UNABLE_TO_CREATE_MESSAGE=Unable to create message {0}: {1}{2}{3} EXCEPTION_UNABLE_TO_CREATE_MESSAGE=Unable to create message {0}: {1}{2}{3}
EXCEPTION_UNABLE_TO_FIND_USERS={0} Unable to find users from: {1} EXCEPTION_GET_FAILED=Get request failed with status {0} at {1}
EXCEPTION_UNABLE_TO_GET_EMAIL=Unable to get user email from: {0}
EXCEPTION_UNABLE_TO_GET_FOLDER=Unable to get folder at {0} EXCEPTION_UNABLE_TO_GET_FOLDER=Unable to get folder at {0}
EXCEPTION_UNABLE_TO_GET_FREEBUSY=Unable to get free-busy from: {0} status:{1} message:{2}
EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER=Unable to get mail folder EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER=Unable to get mail folder
EXCEPTION_UNABLE_TO_GET_MAIL_FOLDERS=Unable to get mail folders EXCEPTION_UNABLE_TO_GET_MAIL_FOLDERS=Unable to get mail folders
EXCEPTION_UNABLE_TO_GET_PROPERTY=Unable to get property {0} EXCEPTION_UNABLE_TO_GET_PROPERTY=Unable to get property {0}

View File

@ -25,10 +25,7 @@ EXCEPTION_INVALID_REQUEST=Requ
EXCEPTION_INVALID_SEARCH_PARAMETERS=Paremètres de recherche invalides : {0} EXCEPTION_INVALID_SEARCH_PARAMETERS=Paremètres de recherche invalides : {0}
EXCEPTION_NETWORK_DOWN=Toutes les interfaces réseaux sont indisponibles ou serveur non joignable ! EXCEPTION_NETWORK_DOWN=Toutes les interfaces réseaux sont indisponibles ou serveur non joignable !
EXCEPTION_UNABLE_TO_CREATE_MESSAGE=Impossible de créer le message {0} : {1}{2}{3} EXCEPTION_UNABLE_TO_CREATE_MESSAGE=Impossible de créer le message {0} : {1}{2}{3}
EXCEPTION_UNABLE_TO_FIND_USERS={0} Impossible de chercher les utilisateurs à l''adresse : {1}
EXCEPTION_UNABLE_TO_GET_EMAIL=Impossible d''obtenir l''adresse de messagerie depuis : {0}
EXCEPTION_UNABLE_TO_GET_FOLDER=Impossible d''obtenir le dossier {0} EXCEPTION_UNABLE_TO_GET_FOLDER=Impossible d''obtenir le dossier {0}
EXCEPTION_UNABLE_TO_GET_FREEBUSY=Impossible d''obtenir les informations de disponibilité depuis : {0} statut:{1} message:{2}
EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER=Impossible d''obtenir le dossier de messagerie EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER=Impossible d''obtenir le dossier de messagerie
EXCEPTION_UNABLE_TO_GET_MAIL_FOLDERS=Impossible d''obtenir les répertoires de messagerie EXCEPTION_UNABLE_TO_GET_MAIL_FOLDERS=Impossible d''obtenir les répertoires de messagerie
EXCEPTION_UNABLE_TO_GET_PROPERTY=Impossible d''obtenir la propriété {0} EXCEPTION_UNABLE_TO_GET_PROPERTY=Impossible d''obtenir la propriété {0}
@ -228,4 +225,5 @@ LOG_INVALID_TIMEZONE=Fuseau horaire invalide : {0}
MEETING_REQUEST=Invitation MEETING_REQUEST=Invitation
LOG_ACCESS_FORBIDDEN=Accès à {0} non autorisé: {1} LOG_ACCESS_FORBIDDEN=Accès à {0} non autorisé: {1}
LOG_LDAP_REQ_BIND_INVALID_CREDENTIALS=LDAP_REQ_BIND Utilisateur ou mot de passe invalide LOG_LDAP_REQ_BIND_INVALID_CREDENTIALS=LDAP_REQ_BIND Utilisateur ou mot de passe invalide
LOG_LDAP_REQ_BIND_SUCCESS=LOG_LDAP_REQ_BIND Authentification réussie LOG_LDAP_REQ_BIND_SUCCESS=LOG_LDAP_REQ_BIND Authentification réussie
EXCEPTION_GET_FAILED=La requête GET a échoué avec le statut {0} à l''adresse {1}