From 61dcfae33f5557ce7b58eed714481420516f1cec Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 17 Mar 2014 23:43:55 +0000 Subject: [PATCH] From coverity: synchronize HttpClient cookies access git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2263 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 71 ++++++++++--------- .../exchange/dav/DavExchangeSession.java | 49 ++++++------- 2 files changed, 62 insertions(+), 58 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index a188ed7c..805a0a0e 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -3294,43 +3294,46 @@ public abstract class ExchangeSession { static final String MAILBOX_BASE = "/cn="; protected void getEmailAndAliasFromOptions() { - Cookie[] currentCookies = httpClient.getState().getCookies(); - // get user mail URL from html body - BufferedReader optionsPageReader = null; - GetMethod optionsMethod = new GetMethod("/owa/?ae=Options&t=About"); - try { - DavGatewayHttpClientFacade.executeGetMethod(httpClient, optionsMethod, false); - optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream())); - String line; + synchronized (httpClient.getState()) { + Cookie[] currentCookies = httpClient.getState().getCookies(); + // get user mail URL from html body + BufferedReader optionsPageReader = null; + GetMethod optionsMethod = new GetMethod("/owa/?ae=Options&t=About"); + try { + DavGatewayHttpClientFacade.executeGetMethod(httpClient, optionsMethod, false); + optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream())); + String line; - // find email and alias - while ((line = optionsPageReader.readLine()) != null - && (line.indexOf('[') == -1 - || line.indexOf('@') == -1 - || line.indexOf(']') == -1) - && line.toLowerCase().indexOf(MAILBOX_BASE) == -1) { - } - if (line != null) { - int start = line.toLowerCase().lastIndexOf(MAILBOX_BASE) + MAILBOX_BASE.length(); - int end = line.indexOf('<', start); - alias = line.substring(start, end); - end = line.lastIndexOf(']'); - start = line.lastIndexOf('[', end) + 1; - email = line.substring(start, end); - } - } catch (IOException e) { - // restore cookies on error - httpClient.getState().addCookies(currentCookies); - LOGGER.error("Error parsing options page at " + optionsMethod.getPath()); - } finally { - if (optionsPageReader != null) { - try { - optionsPageReader.close(); - } catch (IOException e) { - LOGGER.error("Error parsing options page at " + optionsMethod.getPath()); + // find email and alias + //noinspection StatementWithEmptyBody + while ((line = optionsPageReader.readLine()) != null + && (line.indexOf('[') == -1 + || line.indexOf('@') == -1 + || line.indexOf(']') == -1) + && !line.toLowerCase().contains(MAILBOX_BASE)) { } + if (line != null) { + int start = line.toLowerCase().lastIndexOf(MAILBOX_BASE) + MAILBOX_BASE.length(); + int end = line.indexOf('<', start); + alias = line.substring(start, end); + end = line.lastIndexOf(']'); + start = line.lastIndexOf('[', end) + 1; + email = line.substring(start, end); + } + } catch (IOException e) { + // restore cookies on error + httpClient.getState().addCookies(currentCookies); + LOGGER.error("Error parsing options page at " + optionsMethod.getPath()); + } finally { + if (optionsPageReader != null) { + try { + optionsPageReader.close(); + } catch (IOException e) { + LOGGER.error("Error parsing options page at " + optionsMethod.getPath()); + } + } + optionsMethod.releaseConnection(); } - optionsMethod.releaseConnection(); } } diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index a516a43e..1cd1bf58 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -752,31 +752,32 @@ public class DavExchangeSession extends ExchangeSession { } protected void checkPublicFolder() { - - Cookie[] currentCookies = httpClient.getState().getCookies(); - // check public folder access - try { - publicFolderUrl = httpClient.getHostConfiguration().getHostURL() + PUBLIC_ROOT; - DavPropertyNameSet davPropertyNameSet = new DavPropertyNameSet(); - davPropertyNameSet.add(Field.getPropertyName("displayname")); - PropFindMethod propFindMethod = new PropFindMethod(publicFolderUrl, davPropertyNameSet, 0); + synchronized (httpClient.getState()) { + Cookie[] currentCookies = httpClient.getState().getCookies(); + // check public folder access try { - DavGatewayHttpClientFacade.executeMethod(httpClient, propFindMethod); - } catch (IOException e) { - // workaround for NTLM authentication only on /public - if (!DavGatewayHttpClientFacade.hasNTLMorNegotiate(httpClient)) { - DavGatewayHttpClientFacade.addNTLM(httpClient); + publicFolderUrl = httpClient.getHostConfiguration().getHostURL() + PUBLIC_ROOT; + DavPropertyNameSet davPropertyNameSet = new DavPropertyNameSet(); + davPropertyNameSet.add(Field.getPropertyName("displayname")); + PropFindMethod propFindMethod = new PropFindMethod(publicFolderUrl, davPropertyNameSet, 0); + try { DavGatewayHttpClientFacade.executeMethod(httpClient, propFindMethod); + } catch (IOException e) { + // workaround for NTLM authentication only on /public + if (!DavGatewayHttpClientFacade.hasNTLMorNegotiate(httpClient)) { + DavGatewayHttpClientFacade.addNTLM(httpClient); + DavGatewayHttpClientFacade.executeMethod(httpClient, propFindMethod); + } } + // update public folder URI + publicFolderUrl = propFindMethod.getURI().getURI(); + } catch (IOException e) { + // restore cookies on error + httpClient.getState().addCookies(currentCookies); + LOGGER.warn("Public folders not available: " + (e.getMessage() == null ? e : e.getMessage())); + // default public folder path + publicFolderUrl = PUBLIC_ROOT; } - // update public folder URI - publicFolderUrl = propFindMethod.getURI().getURI(); - } catch (IOException e) { - // restore cookies on error - httpClient.getState().addCookies(currentCookies); - LOGGER.warn("Public folders not available: " + (e.getMessage() == null ? e : e.getMessage())); - // default public folder path - publicFolderUrl = PUBLIC_ROOT; } } @@ -1099,7 +1100,7 @@ public class DavExchangeSession extends ExchangeSession { // workaround for messages in Sent folder if (!messageHeaders.contains("From:")) { String from = getItemProperty(permanentUrl, "from"); - messageHeaders = "From: "+from+ '\n' +messageHeaders; + messageHeaders = "From: " + from + '\n' + messageHeaders; } result = new ByteArrayInputStream(messageHeaders.getBytes("UTF-8")); } @@ -1358,7 +1359,7 @@ public class DavExchangeSession extends ExchangeSession { @Override public byte[] getEventContent() throws IOException { byte[] result = null; - LOGGER.debug("Get event subject: " + subject + " contentclass: "+contentClass+" href: " + getHref() + " permanentUrl: " + permanentUrl); + LOGGER.debug("Get event subject: " + subject + " contentclass: " + contentClass + " href: " + getHref() + " permanentUrl: " + permanentUrl); // do not try to load tasks MIME body if (!"urn:content-classes:task".equals(contentClass)) { // try to get PR_INTERNET_CONTENT @@ -1706,7 +1707,7 @@ public class DavExchangeSession extends ExchangeSession { // Set contentclass to make ActiveSync happy propertyList.add(Field.createDavProperty("contentclass", contentClass)); // ... but also set PR_INTERNET_CONTENT to preserve custom properties - propertyList.add(Field.createDavProperty("internetContent",IOUtil.encodeBase64AsString(mimeContent))); + propertyList.add(Field.createDavProperty("internetContent", IOUtil.encodeBase64AsString(mimeContent))); PropPatchMethod propPatchMethod = new PropPatchMethod(encodedHref, propertyList); int patchStatus = DavGatewayHttpClientFacade.executeHttpMethod(httpClient, propPatchMethod); if (patchStatus != HttpStatus.SC_MULTI_STATUS) {