Major refactoring: remove non thread safe WebdavResource object
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@453 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
018d30e51f
commit
5cb7f75832
|
@ -61,6 +61,14 @@ public class ExchangeSession {
|
||||||
DISPLAY_NAME.add("DAV:displayname");
|
DISPLAY_NAME.add("DAV:displayname");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static final Vector<String> FOLDER_PROPERTIES = new Vector<String>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
FOLDER_PROPERTIES.add("DAV:hassubs");
|
||||||
|
FOLDER_PROPERTIES.add("DAV:nosubs");
|
||||||
|
FOLDER_PROPERTIES.add("urn:schemas:httpmail:unreadcount");
|
||||||
|
}
|
||||||
|
|
||||||
protected static final Vector<String> CONTENT_TAG = new Vector<String>();
|
protected static final Vector<String> CONTENT_TAG = new Vector<String>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
@ -100,7 +108,7 @@ public class ExchangeSession {
|
||||||
private String mailPath;
|
private String mailPath;
|
||||||
private String email;
|
private String email;
|
||||||
private String alias;
|
private String alias;
|
||||||
private WebdavResource wdr = null;
|
private HttpClient httpClient;
|
||||||
|
|
||||||
private final ExchangeSessionFactory.PoolKey poolKey;
|
private final ExchangeSessionFactory.PoolKey poolKey;
|
||||||
|
|
||||||
|
@ -132,13 +140,8 @@ public class ExchangeSession {
|
||||||
public boolean isExpired() {
|
public boolean isExpired() {
|
||||||
boolean isExpired = false;
|
boolean isExpired = false;
|
||||||
try {
|
try {
|
||||||
wdr.propfindMethod(0);
|
DavGatewayHttpClientFacade.executePropFindMethod(
|
||||||
int status = wdr.getStatusCode();
|
httpClient, URIUtil.encodePath(inboxUrl), 0, DISPLAY_NAME);
|
||||||
|
|
||||||
if (status != HttpStatus.SC_MULTI_STATUS) {
|
|
||||||
isExpired = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
isExpired = true;
|
isExpired = true;
|
||||||
}
|
}
|
||||||
|
@ -350,12 +353,9 @@ public class ExchangeSession {
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Invalid URL: " + poolKey.url);
|
throw new IllegalArgumentException("Invalid URL: " + poolKey.url);
|
||||||
}
|
}
|
||||||
wdr = new WebdavResource(httpURL, WebdavResource.NOACTION, 0);
|
|
||||||
|
|
||||||
// get the internal HttpClient instance
|
|
||||||
HttpClient httpClient = wdr.retrieveSessionInstance();
|
|
||||||
|
|
||||||
DavGatewayHttpClientFacade.configureClient(httpClient);
|
httpClient = DavGatewayHttpClientFacade.getInstance(httpURL);
|
||||||
|
|
||||||
// get webmail root url
|
// get webmail root url
|
||||||
// providing credentials
|
// providing credentials
|
||||||
|
@ -390,10 +390,7 @@ public class ExchangeSession {
|
||||||
buildMailPath(method);
|
buildMailPath(method);
|
||||||
|
|
||||||
// got base http mailbox http url
|
// got base http mailbox http url
|
||||||
wdr.setPath(mailPath);
|
|
||||||
getWellKnownFolders();
|
getWellKnownFolders();
|
||||||
// set current folder to Inbox
|
|
||||||
wdr.setPath(URIUtil.getPath(inboxUrl));
|
|
||||||
|
|
||||||
} catch (AuthenticationException exc) {
|
} catch (AuthenticationException exc) {
|
||||||
LOGGER.error(exc.toString());
|
LOGGER.error(exc.toString());
|
||||||
|
@ -421,7 +418,8 @@ public class ExchangeSession {
|
||||||
|
|
||||||
protected void getWellKnownFolders() throws IOException {
|
protected void getWellKnownFolders() throws IOException {
|
||||||
// Retrieve well known URLs
|
// Retrieve well known URLs
|
||||||
Enumeration foldersEnum = wdr.propfindMethod(0, WELL_KNOWN_FOLDERS);
|
Enumeration foldersEnum = DavGatewayHttpClientFacade.executePropFindMethod(
|
||||||
|
httpClient, URIUtil.encodePath(mailPath), 0, WELL_KNOWN_FOLDERS);
|
||||||
if (!foldersEnum.hasMoreElements()) {
|
if (!foldersEnum.hasMoreElements()) {
|
||||||
throw new IOException("Unable to get mail folders");
|
throw new IOException("Unable to get mail folders");
|
||||||
}
|
}
|
||||||
|
@ -480,7 +478,7 @@ public class ExchangeSession {
|
||||||
try {
|
try {
|
||||||
// update message with blind carbon copy and other flags
|
// update message with blind carbon copy and other flags
|
||||||
addProperties(patchMethod, properties);
|
addProperties(patchMethod, properties);
|
||||||
int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod);
|
int statusCode = httpClient.executeMethod(patchMethod);
|
||||||
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
|
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
|
||||||
throw new IOException("Unable to create message " + messageUrl + ": " + statusCode + " " + patchMethod.getStatusLine());
|
throw new IOException("Unable to create message " + messageUrl + ": " + statusCode + " " + patchMethod.getStatusLine());
|
||||||
}
|
}
|
||||||
|
@ -498,7 +496,7 @@ public class ExchangeSession {
|
||||||
// use same encoding as client socket reader
|
// use same encoding as client socket reader
|
||||||
bodyStream = new ByteArrayInputStream(messageBody.getBytes());
|
bodyStream = new ByteArrayInputStream(messageBody.getBytes());
|
||||||
putmethod.setRequestBody(bodyStream);
|
putmethod.setRequestBody(bodyStream);
|
||||||
int code = wdr.retrieveSessionInstance().executeMethod(putmethod);
|
int code = httpClient.executeMethod(putmethod);
|
||||||
|
|
||||||
if (code != HttpStatus.SC_OK && code != HttpStatus.SC_CREATED) {
|
if (code != HttpStatus.SC_OK && code != HttpStatus.SC_CREATED) {
|
||||||
throw new IOException("Unable to create message " + messageUrl + ": " + code + " " + putmethod.getStatusLine());
|
throw new IOException("Unable to create message " + messageUrl + ": " + code + " " + putmethod.getStatusLine());
|
||||||
|
@ -520,7 +518,7 @@ public class ExchangeSession {
|
||||||
try {
|
try {
|
||||||
// update message with blind carbon copy and other flags
|
// update message with blind carbon copy and other flags
|
||||||
addProperties(patchMethod, properties);
|
addProperties(patchMethod, properties);
|
||||||
int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod);
|
int statusCode = httpClient.executeMethod(patchMethod);
|
||||||
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
|
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
|
||||||
throw new IOException("Unable to patch message " + messageUrl + ": " + statusCode + " " + patchMethod.getStatusLine());
|
throw new IOException("Unable to patch message " + messageUrl + ": " + statusCode + " " + patchMethod.getStatusLine());
|
||||||
}
|
}
|
||||||
|
@ -603,7 +601,7 @@ public class ExchangeSession {
|
||||||
PropPatchMethod patchMethod = new PropPatchMethod(URIUtil.encodePathQuery(message.messageUrl));
|
PropPatchMethod patchMethod = new PropPatchMethod(URIUtil.encodePathQuery(message.messageUrl));
|
||||||
try {
|
try {
|
||||||
addProperties(patchMethod, properties);
|
addProperties(patchMethod, properties);
|
||||||
int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod);
|
int statusCode = httpClient.executeMethod(patchMethod);
|
||||||
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
|
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
|
||||||
throw new IOException("Unable to update message properties");
|
throw new IOException("Unable to update message properties");
|
||||||
}
|
}
|
||||||
|
@ -627,10 +625,11 @@ public class ExchangeSession {
|
||||||
" FROM Scope('SHALLOW TRAVERSAL OF \"" + folderUrl + "\"')\n" +
|
" FROM Scope('SHALLOW TRAVERSAL OF \"" + folderUrl + "\"')\n" +
|
||||||
" WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = False\n";
|
" WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = False\n";
|
||||||
if (conditions != null) {
|
if (conditions != null) {
|
||||||
searchRequest += conditions.replaceAll("<", "<").replaceAll(">", ">");
|
searchRequest += conditions;
|
||||||
}
|
}
|
||||||
searchRequest += " ORDER BY \"urn:schemas:httpmail:date\" ASC";
|
searchRequest += " ORDER BY \"urn:schemas:httpmail:date\" ASC";
|
||||||
Enumeration folderEnum = DavGatewayHttpClientFacade.executeSearchMethod(wdr.retrieveSessionInstance(), folderUrl, searchRequest);
|
Enumeration folderEnum = DavGatewayHttpClientFacade.executeSearchMethod(
|
||||||
|
httpClient, URIUtil.encodePath(folderUrl), searchRequest);
|
||||||
|
|
||||||
while (folderEnum.hasMoreElements()) {
|
while (folderEnum.hasMoreElements()) {
|
||||||
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
|
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
|
||||||
|
@ -650,7 +649,8 @@ public class ExchangeSession {
|
||||||
" FROM Scope('" + mode + " TRAVERSAL OF \"" + getFolderPath(folderName) + "\"')\n" +
|
" FROM Scope('" + mode + " TRAVERSAL OF \"" + getFolderPath(folderName) + "\"')\n" +
|
||||||
" WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = True \n" +
|
" WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = True \n" +
|
||||||
" AND (\"DAV:contentclass\"='urn:content-classes:mailfolder' OR \"DAV:contentclass\"='urn:content-classes:folder')";
|
" AND (\"DAV:contentclass\"='urn:content-classes:mailfolder' OR \"DAV:contentclass\"='urn:content-classes:folder')";
|
||||||
Enumeration folderEnum = DavGatewayHttpClientFacade.executeSearchMethod(wdr.retrieveSessionInstance(), mailPath, searchRequest);
|
Enumeration folderEnum = DavGatewayHttpClientFacade.executeSearchMethod(
|
||||||
|
httpClient, URIUtil.encodePath(mailPath), searchRequest);
|
||||||
|
|
||||||
while (folderEnum.hasMoreElements()) {
|
while (folderEnum.hasMoreElements()) {
|
||||||
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
|
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
|
||||||
|
@ -730,14 +730,15 @@ public class ExchangeSession {
|
||||||
" FROM Scope('SHALLOW TRAVERSAL OF \"" + folderUrl + "\"')\n" +
|
" FROM Scope('SHALLOW TRAVERSAL OF \"" + folderUrl + "\"')\n" +
|
||||||
" WHERE \"DAV:isfolder\" = False\n" +
|
" WHERE \"DAV:isfolder\" = False\n" +
|
||||||
" AND \"DAV:getlastmodified\" < '" + dateFormatter.format(cal.getTime()) + "'\n";
|
" AND \"DAV:getlastmodified\" < '" + dateFormatter.format(cal.getTime()) + "'\n";
|
||||||
Enumeration folderEnum = DavGatewayHttpClientFacade.executeSearchMethod(wdr.retrieveSessionInstance(), folderUrl, searchRequest);
|
Enumeration folderEnum = DavGatewayHttpClientFacade.executeSearchMethod(
|
||||||
|
httpClient, URIUtil.encodePath(folderUrl), searchRequest);
|
||||||
|
|
||||||
while (folderEnum.hasMoreElements()) {
|
while (folderEnum.hasMoreElements()) {
|
||||||
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
|
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
|
||||||
String messageUrl = URIUtil.decode(entity.getHref());
|
String messageUrl = entity.getHref();
|
||||||
|
|
||||||
LOGGER.debug("Delete " + messageUrl);
|
LOGGER.debug("Delete " + messageUrl);
|
||||||
wdr.deleteMethod(messageUrl);
|
DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, messageUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -808,12 +809,11 @@ public class ExchangeSession {
|
||||||
|
|
||||||
// warning : slide library expects *unencoded* urls
|
// warning : slide library expects *unencoded* urls
|
||||||
String tempUrl = draftsUrl + "/" + messageName + ".EML";
|
String tempUrl = draftsUrl + "/" + messageName + ".EML";
|
||||||
boolean sent = wdr.moveMethod(tempUrl, sendmsgUrl);
|
MoveMethod method = new MoveMethod(URIUtil.encodePath(tempUrl), URIUtil.encodePath(sendmsgUrl));
|
||||||
if (!sent) {
|
int status = DavGatewayHttpClientFacade.executeHttpMethod(httpClient, method);
|
||||||
throw new IOException("Unable to send message: " + wdr.getStatusCode()
|
if (status != HttpStatus.SC_OK) {
|
||||||
+ " " + wdr.getStatusMessage());
|
throw DavGatewayHttpClientFacade.buildHttpException(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFolderPath(String folderName) {
|
public String getFolderPath(String folderName) {
|
||||||
|
@ -847,11 +847,8 @@ public class ExchangeSession {
|
||||||
* @throws IOException when unable to change folder
|
* @throws IOException when unable to change folder
|
||||||
*/
|
*/
|
||||||
public Folder getFolder(String folderName) throws IOException {
|
public Folder getFolder(String folderName) throws IOException {
|
||||||
Vector<String> reqProps = new Vector<String>();
|
Enumeration folderEnum = DavGatewayHttpClientFacade.executePropFindMethod(
|
||||||
reqProps.add("DAV:hassubs");
|
httpClient, URIUtil.encodePath(getFolderPath(folderName)), 0, FOLDER_PROPERTIES);
|
||||||
reqProps.add("DAV:nosubs");
|
|
||||||
reqProps.add("urn:schemas:httpmail:unreadcount");
|
|
||||||
Enumeration folderEnum = wdr.propfindMethod(getFolderPath(folderName), 0, reqProps);
|
|
||||||
Folder folder = null;
|
Folder folder = null;
|
||||||
if (folderEnum.hasMoreElements()) {
|
if (folderEnum.hasMoreElements()) {
|
||||||
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
|
ResponseEntity entity = (ResponseEntity) folderEnum.nextElement();
|
||||||
|
@ -870,7 +867,7 @@ public class ExchangeSession {
|
||||||
};
|
};
|
||||||
method.addPropertyToSet("outlookfolderclass", "IPF.Note", "ex", "http://schemas.microsoft.com/exchange/");
|
method.addPropertyToSet("outlookfolderclass", "IPF.Note", "ex", "http://schemas.microsoft.com/exchange/");
|
||||||
try {
|
try {
|
||||||
wdr.retrieveSessionInstance().executeMethod(method);
|
httpClient.executeMethod(method);
|
||||||
// ok or alredy exists
|
// ok or alredy exists
|
||||||
if (method.getStatusCode() != HttpStatus.SC_MULTI_STATUS && method.getStatusCode() != HttpStatus.SC_METHOD_NOT_ALLOWED) {
|
if (method.getStatusCode() != HttpStatus.SC_MULTI_STATUS && method.getStatusCode() != HttpStatus.SC_METHOD_NOT_ALLOWED) {
|
||||||
HttpException ex = new HttpException();
|
HttpException ex = new HttpException();
|
||||||
|
@ -890,14 +887,11 @@ public class ExchangeSession {
|
||||||
method.setOverwrite(false);
|
method.setOverwrite(false);
|
||||||
method.addRequestHeader("Allow-Rename", "t");
|
method.addRequestHeader("Allow-Rename", "t");
|
||||||
try {
|
try {
|
||||||
int statusCode = wdr.retrieveSessionInstance().executeMethod(method);
|
int statusCode = httpClient.executeMethod(method);
|
||||||
if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
|
if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
|
||||||
throw new HttpException("Unable to move message, target already exists");
|
throw new HttpException("Unable to move message, target already exists");
|
||||||
} else if (statusCode != HttpStatus.SC_CREATED) {
|
} else if (statusCode != HttpStatus.SC_CREATED) {
|
||||||
HttpException ex = new HttpException();
|
throw DavGatewayHttpClientFacade.buildHttpException(method);
|
||||||
ex.setReasonCode(method.getStatusCode());
|
|
||||||
ex.setReason(method.getStatusText());
|
|
||||||
throw ex;
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
method.releaseConnection();
|
method.releaseConnection();
|
||||||
|
@ -911,14 +905,11 @@ public class ExchangeSession {
|
||||||
URIUtil.encodePath(targetPath));
|
URIUtil.encodePath(targetPath));
|
||||||
method.setOverwrite(false);
|
method.setOverwrite(false);
|
||||||
try {
|
try {
|
||||||
int statusCode = wdr.retrieveSessionInstance().executeMethod(method);
|
int statusCode = httpClient.executeMethod(method);
|
||||||
if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
|
if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
|
||||||
throw new HttpException("Unable to move folder, target already exists");
|
throw new HttpException("Unable to move folder, target already exists");
|
||||||
} else if (statusCode != HttpStatus.SC_CREATED) {
|
} else if (statusCode != HttpStatus.SC_CREATED) {
|
||||||
HttpException ex = new HttpException();
|
throw DavGatewayHttpClientFacade.buildHttpException(method);
|
||||||
ex.setReasonCode(method.getStatusCode());
|
|
||||||
ex.setReason(method.getStatusText());
|
|
||||||
throw ex;
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
method.releaseConnection();
|
method.releaseConnection();
|
||||||
|
@ -926,26 +917,23 @@ public class ExchangeSession {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveToTrash(String encodedPath, String encodedMessageName) throws IOException {
|
public void moveToTrash(String encodedPath, String encodedMessageName) throws IOException {
|
||||||
String source = encodedPath+"/"+encodedMessageName;
|
String source = encodedPath + "/" + encodedMessageName;
|
||||||
String destination = URIUtil.encodePath(deleteditemsUrl) + "/"+encodedMessageName;
|
String destination = URIUtil.encodePath(deleteditemsUrl) + "/" + encodedMessageName;
|
||||||
LOGGER.debug("Deleting : " + source + " to " + destination);
|
LOGGER.debug("Deleting : " + source + " to " + destination);
|
||||||
MoveMethod method = new MoveMethod(source, destination);
|
MoveMethod method = new MoveMethod(source, destination);
|
||||||
method.addRequestHeader("Overwrite", "f");
|
method.addRequestHeader("Overwrite", "f");
|
||||||
method.addRequestHeader("Allow-rename", "t");
|
method.addRequestHeader("Allow-rename", "t");
|
||||||
|
|
||||||
int status = wdr.retrieveSessionInstance().executeMethod(method);
|
int status = DavGatewayHttpClientFacade.executeHttpMethod(httpClient, method);
|
||||||
// do not throw error if already deleted
|
// do not throw error if already deleted
|
||||||
if (status != HttpStatus.SC_CREATED && status != HttpStatus.SC_NOT_FOUND) {
|
if (status != HttpStatus.SC_CREATED && status != HttpStatus.SC_NOT_FOUND) {
|
||||||
HttpException ex = new HttpException();
|
throw DavGatewayHttpClientFacade.buildHttpException(method);
|
||||||
ex.setReasonCode(status);
|
|
||||||
ex.setReason(method.getStatusText());
|
|
||||||
throw ex;
|
|
||||||
}
|
}
|
||||||
if (method.getResponseHeader("Location") != null) {
|
if (method.getResponseHeader("Location") != null) {
|
||||||
destination = method.getResponseHeader("Location").getValue();
|
destination = method.getResponseHeader("Location").getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGGER.debug("Deleted to :" + destination + " " + wdr.getStatusCode() + " " + wdr.getStatusMessage());
|
LOGGER.debug("Deleted to :" + destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Folder {
|
public static class Folder {
|
||||||
|
@ -1025,7 +1013,7 @@ public class ExchangeSession {
|
||||||
method = new GetMethod(URIUtil.encodePath(messageUrl));
|
method = new GetMethod(URIUtil.encodePath(messageUrl));
|
||||||
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");
|
||||||
wdr.retrieveSessionInstance().executeMethod(method);
|
httpClient.executeMethod(method);
|
||||||
|
|
||||||
boolean inHTML = false;
|
boolean inHTML = false;
|
||||||
|
|
||||||
|
@ -1081,14 +1069,7 @@ public class ExchangeSession {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete() throws IOException {
|
public void delete() throws IOException {
|
||||||
wdr.deleteMethod(messageUrl);
|
DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, URIUtil.encodePath(messageUrl));
|
||||||
// do not send error on event not found
|
|
||||||
if (wdr.getStatusCode() != HttpStatus.SC_OK && wdr.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
|
|
||||||
HttpException ex = new HttpException();
|
|
||||||
ex.setReasonCode(wdr.getStatusCode());
|
|
||||||
ex.setReason(wdr.getStatusMessage());
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveToTrash() throws IOException {
|
public void moveToTrash() throws IOException {
|
||||||
|
@ -1099,10 +1080,10 @@ public class ExchangeSession {
|
||||||
|
|
||||||
int index = messageUrl.lastIndexOf('/');
|
int index = messageUrl.lastIndexOf('/');
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
throw new IOException("Invalid message url: "+messageUrl);
|
throw new IOException("Invalid message url: " + messageUrl);
|
||||||
}
|
}
|
||||||
String encodedPath = URIUtil.encodePath(messageUrl.substring(0, index));
|
String encodedPath = URIUtil.encodePath(messageUrl.substring(0, index));
|
||||||
String encodedMessageName = URIUtil.encodePath(messageUrl.substring(index+1));
|
String encodedMessageName = URIUtil.encodePath(messageUrl.substring(index + 1));
|
||||||
ExchangeSession.this.moveToTrash(encodedPath, encodedMessageName);
|
ExchangeSession.this.moveToTrash(encodedPath, encodedMessageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1163,7 +1144,7 @@ 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 = wdr.retrieveSessionInstance().executeMethod(method);
|
int status = httpClient.executeMethod(method);
|
||||||
if (status != HttpStatus.SC_OK) {
|
if (status != HttpStatus.SC_OK) {
|
||||||
LOGGER.warn("Unable to get event at " + href + " status: " + status);
|
LOGGER.warn("Unable to get event at " + href + " status: " + status);
|
||||||
}
|
}
|
||||||
|
@ -1247,7 +1228,7 @@ public class ExchangeSession {
|
||||||
|
|
||||||
SearchMethod searchMethod = new SearchMethod(URIUtil.encodePath(path), searchRequest);
|
SearchMethod searchMethod = new SearchMethod(URIUtil.encodePath(path), searchRequest);
|
||||||
try {
|
try {
|
||||||
int status = wdr.retrieveSessionInstance().executeMethod(searchMethod);
|
int status = httpClient.executeMethod(searchMethod);
|
||||||
// Also accept OK sent by buggy servers.
|
// Also accept OK sent by buggy servers.
|
||||||
if (status != HttpStatus.SC_MULTI_STATUS
|
if (status != HttpStatus.SC_MULTI_STATUS
|
||||||
&& status != HttpStatus.SC_OK) {
|
&& status != HttpStatus.SC_OK) {
|
||||||
|
@ -1267,16 +1248,9 @@ public class ExchangeSession {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Event getEvent(String path, String eventName) throws IOException {
|
public Event getEvent(String path, String eventName) throws IOException {
|
||||||
String eventPath = URIUtil.encodePath(getFolderPath(path))+"/"+URIUtil.encodeWithinQuery(eventName);
|
String eventPath = URIUtil.encodePath(getFolderPath(path)) + "/" + URIUtil.encodeWithinQuery(eventName);
|
||||||
LOGGER.debug("getEvent("+eventPath+"/"+eventName+")");
|
LOGGER.debug("getEvent(" + eventPath + "/" + eventName + ")");
|
||||||
PropFindMethod propFindMethod = new PropFindMethod(eventPath, 0, EVENT_REQUEST_PROPERTIES.elements());
|
Enumeration calendarEnum = DavGatewayHttpClientFacade.executePropFindMethod(httpClient, eventPath, 0, EVENT_REQUEST_PROPERTIES);
|
||||||
int status = wdr.retrieveSessionInstance().executeMethod(propFindMethod);
|
|
||||||
if (status != HttpStatus.SC_MULTI_STATUS) {
|
|
||||||
HttpException ex = new HttpException();
|
|
||||||
ex.setReasonCode(status);
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
Enumeration calendarEnum = propFindMethod.getResponses();
|
|
||||||
if (!calendarEnum.hasMoreElements()) {
|
if (!calendarEnum.hasMoreElements()) {
|
||||||
throw new IOException("Unable to get calendar event");
|
throw new IOException("Unable to get calendar event");
|
||||||
}
|
}
|
||||||
|
@ -1456,12 +1430,12 @@ public class ExchangeSession {
|
||||||
if (status != HttpStatus.SC_CREATED) {
|
if (status != HttpStatus.SC_CREATED) {
|
||||||
return status;
|
return status;
|
||||||
} else {
|
} else {
|
||||||
boolean sent = wdr.moveMethod(messageUrl, sendmsgUrl);
|
MoveMethod method = new MoveMethod(URIUtil.encodePath(messageUrl), URIUtil.encodePath(sendmsgUrl));
|
||||||
if (!sent) {
|
status = DavGatewayHttpClientFacade.executeHttpMethod(httpClient, method);
|
||||||
throw new IOException("Unable to send message: " + wdr.getStatusCode()
|
if (status != HttpStatus.SC_OK) {
|
||||||
+ " " + wdr.getStatusMessage());
|
throw DavGatewayHttpClientFacade.buildHttpException(method);
|
||||||
}
|
}
|
||||||
return wdr.getStatusCode();
|
return status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1578,7 +1552,7 @@ public class ExchangeSession {
|
||||||
putmethod.setRequestBody(body.toString());
|
putmethod.setRequestBody(body.toString());
|
||||||
try {
|
try {
|
||||||
if (status == 0) {
|
if (status == 0) {
|
||||||
status = wdr.retrieveSessionInstance().executeMethod(putmethod);
|
status = httpClient.executeMethod(putmethod);
|
||||||
if (status == HttpURLConnection.HTTP_OK) {
|
if (status == HttpURLConnection.HTTP_OK) {
|
||||||
if (etag != null) {
|
if (etag != null) {
|
||||||
LOGGER.debug("Updated event " + messageUrl);
|
LOGGER.debug("Updated event " + messageUrl);
|
||||||
|
@ -1602,19 +1576,11 @@ public class ExchangeSession {
|
||||||
|
|
||||||
|
|
||||||
public void deleteFolder(String path) throws IOException {
|
public void deleteFolder(String path) throws IOException {
|
||||||
wdr.deleteMethod(getFolderPath(path));
|
DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, URIUtil.encodePath(getFolderPath(path)));
|
||||||
int status = wdr.getStatusCode();
|
|
||||||
if (status != HttpStatus.SC_OK) {
|
|
||||||
HttpException ex = new HttpException();
|
|
||||||
ex.setReasonCode(status);
|
|
||||||
ex.setReason(wdr.getStatusMessage());
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int deleteMessage(String path) throws IOException {
|
public int deleteMessage(String path) throws IOException {
|
||||||
wdr.deleteMethod(path);
|
return DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, URIUtil.encodePath(path));
|
||||||
return wdr.getStatusCode();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int deleteEvent(String path, String eventName) throws IOException {
|
public int deleteEvent(String path, String eventName) throws IOException {
|
||||||
|
@ -1624,15 +1590,7 @@ public class ExchangeSession {
|
||||||
moveToTrash(URIUtil.encodePath(getFolderPath(URIUtil.decode(path))), eventName);
|
moveToTrash(URIUtil.encodePath(getFolderPath(URIUtil.decode(path))), eventName);
|
||||||
status = HttpStatus.SC_OK;
|
status = HttpStatus.SC_OK;
|
||||||
} else {
|
} else {
|
||||||
DeleteMethod method = new DeleteMethod(URIUtil.encodePath(getFolderPath(URIUtil.decode(path)))+"/"+eventName);
|
status = DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, URIUtil.encodePath(getFolderPath(URIUtil.decode(path))) + "/" + eventName);
|
||||||
status = wdr.retrieveSessionInstance().executeMethod(method);
|
|
||||||
// do not throw error if already deleted
|
|
||||||
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_NOT_FOUND) {
|
|
||||||
HttpException ex = new HttpException();
|
|
||||||
ex.setReasonCode(status);
|
|
||||||
ex.setReason(wdr.getStatusMessage());
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
@ -1647,7 +1605,8 @@ public class ExchangeSession {
|
||||||
|
|
||||||
public String getFolderCtag(String folderUrl) throws IOException {
|
public String getFolderCtag(String folderUrl) throws IOException {
|
||||||
String ctag = null;
|
String ctag = null;
|
||||||
Enumeration calendarEnum = wdr.propfindMethod(folderUrl, 0, CONTENT_TAG);
|
Enumeration calendarEnum = DavGatewayHttpClientFacade.executePropFindMethod(
|
||||||
|
httpClient, URIUtil.encodePath(folderUrl), 0, CONTENT_TAG);
|
||||||
if (!calendarEnum.hasMoreElements()) {
|
if (!calendarEnum.hasMoreElements()) {
|
||||||
throw new IOException("Unable to get folder object");
|
throw new IOException("Unable to get folder object");
|
||||||
}
|
}
|
||||||
|
@ -1671,7 +1630,8 @@ public class ExchangeSession {
|
||||||
|
|
||||||
public String getCalendarEtag() throws IOException {
|
public String getCalendarEtag() throws IOException {
|
||||||
String etag = null;
|
String etag = null;
|
||||||
Enumeration calendarEnum = wdr.propfindMethod(calendarUrl, 0, EVENT_REQUEST_PROPERTIES);
|
Enumeration calendarEnum = DavGatewayHttpClientFacade.executePropFindMethod(
|
||||||
|
httpClient, URIUtil.encodePath(calendarUrl), 0, EVENT_REQUEST_PROPERTIES);
|
||||||
if (!calendarEnum.hasMoreElements()) {
|
if (!calendarEnum.hasMoreElements()) {
|
||||||
throw new IOException("Unable to get calendar object");
|
throw new IOException("Unable to get calendar object");
|
||||||
}
|
}
|
||||||
|
@ -1730,7 +1690,8 @@ public class ExchangeSession {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String alias = null;
|
String alias = null;
|
||||||
Enumeration folderEnum = wdr.propfindMethod(mailPath, 0, DISPLAY_NAME);
|
Enumeration folderEnum = DavGatewayHttpClientFacade.executePropFindMethod(
|
||||||
|
httpClient, URIUtil.encodePath(mailPath), 0, DISPLAY_NAME);
|
||||||
if (!folderEnum.hasMoreElements()) {
|
if (!folderEnum.hasMoreElements()) {
|
||||||
throw new IOException("Unable to get mail folder");
|
throw new IOException("Unable to get mail folder");
|
||||||
}
|
}
|
||||||
|
@ -1755,7 +1716,7 @@ public class ExchangeSession {
|
||||||
if (alias != null) {
|
if (alias != null) {
|
||||||
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + alias);
|
GetMethod getMethod = new GetMethod("/public/?Cmd=galfind&AN=" + alias);
|
||||||
try {
|
try {
|
||||||
int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
|
int status = httpClient.executeMethod(getMethod);
|
||||||
if (status != HttpStatus.SC_OK) {
|
if (status != HttpStatus.SC_OK) {
|
||||||
throw new IOException("Unable to get user email from: " + getMethod.getPath());
|
throw new IOException("Unable to get user email from: " + getMethod.getPath());
|
||||||
}
|
}
|
||||||
|
@ -1788,7 +1749,7 @@ public class ExchangeSession {
|
||||||
}
|
}
|
||||||
if (email == null) {
|
if (email == null) {
|
||||||
// failover : get email from Exchange 2007 Options page
|
// failover : get email from Exchange 2007 Options page
|
||||||
alias = getAliasFromOptions(methodPath);
|
alias = getAliasFromOptions(methodPath);
|
||||||
email = getEmail(alias);
|
email = getEmail(alias);
|
||||||
}
|
}
|
||||||
if (email == null) {
|
if (email == null) {
|
||||||
|
@ -1805,7 +1766,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 {
|
||||||
wdr.retrieveSessionInstance().executeMethod(optionsMethod);
|
httpClient.executeMethod(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
|
||||||
|
@ -1843,7 +1804,7 @@ public class ExchangeSession {
|
||||||
return email;
|
return email;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get current user alias
|
* Get current user alias
|
||||||
*
|
*
|
||||||
* @return user email
|
* @return user email
|
||||||
|
@ -1864,7 +1825,7 @@ public class ExchangeSession {
|
||||||
Map<String, Map<String, String>> results;
|
Map<String, Map<String, String>> results;
|
||||||
GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery("/public/?Cmd=galfind&" + searchAttribute + "=" + searchValue));
|
GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery("/public/?Cmd=galfind&" + searchAttribute + "=" + searchValue));
|
||||||
try {
|
try {
|
||||||
int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
|
int status = httpClient.executeMethod(getMethod);
|
||||||
if (status != HttpStatus.SC_OK) {
|
if (status != HttpStatus.SC_OK) {
|
||||||
throw new IOException(status + "Unable to find users from: " + getMethod.getURI());
|
throw new IOException(status + "Unable to find users from: " + getMethod.getURI());
|
||||||
}
|
}
|
||||||
|
@ -1881,7 +1842,7 @@ public class ExchangeSession {
|
||||||
GetMethod getMethod = null;
|
GetMethod getMethod = null;
|
||||||
try {
|
try {
|
||||||
getMethod = new GetMethod(URIUtil.encodePathQuery("/public/?Cmd=gallookup&ADDR=" + person.get("EM")));
|
getMethod = new GetMethod(URIUtil.encodePathQuery("/public/?Cmd=gallookup&ADDR=" + person.get("EM")));
|
||||||
int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
|
int status = httpClient.executeMethod(getMethod);
|
||||||
if (status != HttpStatus.SC_OK) {
|
if (status != HttpStatus.SC_OK) {
|
||||||
throw new IOException(status + "Unable to find users from: " + getMethod.getURI());
|
throw new IOException(status + "Unable to find users from: " + getMethod.getURI());
|
||||||
}
|
}
|
||||||
|
@ -1950,7 +1911,7 @@ public class ExchangeSession {
|
||||||
getMethod.setRequestHeader("Content-Type", "text/xml");
|
getMethod.setRequestHeader("Content-Type", "text/xml");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int status = wdr.retrieveSessionInstance().executeMethod(getMethod);
|
int status = httpClient.executeMethod(getMethod);
|
||||||
if (status != HttpStatus.SC_OK) {
|
if (status != HttpStatus.SC_OK) {
|
||||||
throw new IOException("Unable to get free-busy from: " + getMethod.getPath());
|
throw new IOException("Unable to get free-busy from: " + getMethod.getPath());
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,14 @@ import davmail.Settings;
|
||||||
import davmail.tray.DavGatewayTray;
|
import davmail.tray.DavGatewayTray;
|
||||||
import org.apache.commons.httpclient.*;
|
import org.apache.commons.httpclient.*;
|
||||||
import org.apache.commons.httpclient.methods.GetMethod;
|
import org.apache.commons.httpclient.methods.GetMethod;
|
||||||
import org.apache.commons.httpclient.util.URIUtil;
|
import org.apache.commons.httpclient.methods.DeleteMethod;
|
||||||
|
import org.apache.webdav.lib.methods.PropFindMethod;
|
||||||
import org.apache.webdav.lib.methods.SearchMethod;
|
import org.apache.webdav.lib.methods.SearchMethod;
|
||||||
|
import org.apache.webdav.lib.methods.XMLResponseMethodBase;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create HttpClient instance according to DavGateway Settings
|
* Create HttpClient instance according to DavGateway Settings
|
||||||
|
@ -37,6 +40,14 @@ public final class DavGatewayHttpClientFacade {
|
||||||
return httpClient;
|
return httpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HttpClient getInstance(HttpURL httpURL) {
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
HostConfiguration hostConfig = httpClient.getHostConfiguration();
|
||||||
|
hostConfig.setHost(httpURL);
|
||||||
|
configureClient(httpClient);
|
||||||
|
return httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update http client configuration (proxy)
|
* Update http client configuration (proxy)
|
||||||
*
|
*
|
||||||
|
@ -156,36 +167,94 @@ public final class DavGatewayHttpClientFacade {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute webdav search method
|
* Execute webdav search method.
|
||||||
* @param httpClient http client instance
|
*
|
||||||
* @param folderUrl searched folder
|
* @param httpClient http client instance
|
||||||
|
* @param path <i>encoded</i> searched folder path
|
||||||
* @param searchRequest (SQL like) search request
|
* @param searchRequest (SQL like) search request
|
||||||
* @return Responses
|
* @return Responses enumeration
|
||||||
* @throws IOException on error
|
* @throws IOException on error
|
||||||
*/
|
*/
|
||||||
public static Enumeration executeSearchMethod(HttpClient httpClient, String folderUrl, String searchRequest) throws IOException {
|
public static Enumeration executeSearchMethod(HttpClient httpClient, String path, String searchRequest) throws IOException {
|
||||||
Enumeration folderEnum = null;
|
|
||||||
String searchBody = "<?xml version=\"1.0\"?>\n" +
|
String searchBody = "<?xml version=\"1.0\"?>\n" +
|
||||||
"<d:searchrequest xmlns:d=\"DAV:\">\n" +
|
"<d:searchrequest xmlns:d=\"DAV:\">\n" +
|
||||||
" <d:sql>" + searchRequest + "</d:sql>\n" +
|
" <d:sql>" + searchRequest.replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&") + "</d:sql>\n" +
|
||||||
"</d:searchrequest>";
|
"</d:searchrequest>";
|
||||||
SearchMethod searchMethod = new SearchMethod(URIUtil.encodePath(folderUrl), searchBody);
|
SearchMethod searchMethod = new SearchMethod(path, searchBody);
|
||||||
//searchMethod.setDebug(4);
|
return executeMethod(httpClient, searchMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute webdav propfind method.
|
||||||
|
*
|
||||||
|
* @param httpClient http client instance
|
||||||
|
* @param path <i>encoded</i> searched folder path
|
||||||
|
* @param depth propfind request depth
|
||||||
|
* @param properties propfind requested properties
|
||||||
|
* @return Responses enumeration
|
||||||
|
* @throws IOException on error
|
||||||
|
*/
|
||||||
|
public static Enumeration executePropFindMethod(HttpClient httpClient, String path, int depth, Vector properties) throws IOException {
|
||||||
|
PropFindMethod propFindMethod = new PropFindMethod(path, depth, properties.elements());
|
||||||
|
return executeMethod(httpClient, propFindMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int executeDeleteMethod(HttpClient httpClient, String path) throws IOException {
|
||||||
|
DeleteMethod deleteMethod = new DeleteMethod(path);
|
||||||
|
|
||||||
|
int status = executeHttpMethod(httpClient, deleteMethod);
|
||||||
|
// do not throw error if already deleted
|
||||||
|
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_NOT_FOUND) {
|
||||||
|
throw DavGatewayHttpClientFacade.buildHttpException(deleteMethod);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute webdav request.
|
||||||
|
*
|
||||||
|
* @param httpClient http client instance
|
||||||
|
* @param method webdav method
|
||||||
|
* @return Responses enumeration
|
||||||
|
* @throws IOException on error
|
||||||
|
*/
|
||||||
|
public static Enumeration executeMethod(HttpClient httpClient, XMLResponseMethodBase method) throws IOException {
|
||||||
|
Enumeration responseEnumeration = null;
|
||||||
try {
|
try {
|
||||||
int status = httpClient.executeMethod(searchMethod);
|
int status = httpClient.executeMethod(method);
|
||||||
// Also accept OK sent by buggy servers.
|
|
||||||
if (status != HttpStatus.SC_MULTI_STATUS
|
if (status != HttpStatus.SC_MULTI_STATUS) {
|
||||||
&& status != HttpStatus.SC_OK) {
|
throw buildHttpException(method);
|
||||||
HttpException ex = new HttpException();
|
|
||||||
ex.setReasonCode(status);
|
|
||||||
throw ex;
|
|
||||||
}
|
}
|
||||||
folderEnum = searchMethod.getResponses();
|
responseEnumeration = method.getResponses();
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
searchMethod.releaseConnection();
|
method.releaseConnection();
|
||||||
}
|
}
|
||||||
return folderEnum;
|
return responseEnumeration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int executeHttpMethod(HttpClient httpClient, HttpMethod method) throws IOException {
|
||||||
|
int status = 0;
|
||||||
|
try {
|
||||||
|
status = httpClient.executeMethod(method);
|
||||||
|
} finally {
|
||||||
|
method.releaseConnection();
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build Http Exception from methode status
|
||||||
|
*
|
||||||
|
* @param method Http Method
|
||||||
|
* @return Http Exception
|
||||||
|
*/
|
||||||
|
public static HttpException buildHttpException(HttpMethod method) {
|
||||||
|
HttpException httpException = new HttpException();
|
||||||
|
httpException.setReasonCode(method.getStatusCode());
|
||||||
|
httpException.setReason(method.getStatusText());
|
||||||
|
return httpException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void stop() {
|
public static void stop() {
|
||||||
|
|
Loading…
Reference in New Issue