View Javadoc

1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2009  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.exchange;
20  
21  import davmail.BundleMessage;
22  import davmail.Settings;
23  import davmail.exception.DavMailAuthenticationException;
24  import davmail.exception.DavMailException;
25  import davmail.exception.WebdavNotAvailableException;
26  import davmail.http.DavGatewayHttpClientFacade;
27  import davmail.http.DavGatewayOTPPrompt;
28  import davmail.ui.NotificationDialog;
29  import davmail.util.StringUtil;
30  import org.apache.commons.httpclient.*;
31  import org.apache.commons.httpclient.methods.GetMethod;
32  import org.apache.commons.httpclient.methods.PostMethod;
33  import org.apache.commons.httpclient.params.HttpClientParams;
34  import org.apache.commons.httpclient.util.URIUtil;
35  import org.apache.log4j.Logger;
36  import org.htmlcleaner.CommentNode;
37  import org.htmlcleaner.ContentNode;
38  import org.htmlcleaner.HtmlCleaner;
39  import org.htmlcleaner.TagNode;
40  
41  import javax.imageio.ImageIO;
42  import javax.mail.MessagingException;
43  import javax.mail.internet.*;
44  import javax.mail.util.SharedByteArrayInputStream;
45  import java.awt.image.BufferedImage;
46  import java.io.*;
47  import java.net.ConnectException;
48  import java.net.NoRouteToHostException;
49  import java.net.UnknownHostException;
50  import java.text.ParseException;
51  import java.text.SimpleDateFormat;
52  import java.util.*;
53  
54  /**
55   * Exchange session through Outlook Web Access (DAV)
56   */
57  public abstract class ExchangeSession {
58  
59      protected static final Logger LOGGER = Logger.getLogger("davmail.exchange.ExchangeSession");
60  
61      /**
62       * Reference GMT timezone to format dates
63       */
64      public static final SimpleTimeZone GMT_TIMEZONE = new SimpleTimeZone(0, "GMT");
65  
66      protected static final Set<String> USER_NAME_FIELDS = new HashSet<String>();
67  
68      static {
69          USER_NAME_FIELDS.add("username");
70          USER_NAME_FIELDS.add("txtUserName");
71          USER_NAME_FIELDS.add("userid");
72          USER_NAME_FIELDS.add("SafeWordUser");
73          USER_NAME_FIELDS.add("user_name");
74      }
75  
76      protected static final Set<String> PASSWORD_FIELDS = new HashSet<String>();
77  
78      static {
79          PASSWORD_FIELDS.add("password");
80          PASSWORD_FIELDS.add("txtUserPass");
81          PASSWORD_FIELDS.add("pw");
82          PASSWORD_FIELDS.add("basicPassword");
83      }
84  
85      protected static final Set<String> TOKEN_FIELDS = new HashSet<String>();
86  
87      static {
88          TOKEN_FIELDS.add("SafeWordPassword");
89          TOKEN_FIELDS.add("passcode");
90      }
91  
92      protected static final int FREE_BUSY_INTERVAL = 15;
93  
94      protected static final String PUBLIC_ROOT = "/public/";
95      protected static final String CALENDAR = "calendar";
96      protected static final String TASKS = "tasks";
97      /**
98       * Contacts folder logical name
99       */
100     public static final String CONTACTS = "contacts";
101     protected static final String ADDRESSBOOK = "addressbook";
102     protected static final String INBOX = "INBOX";
103     protected static final String LOWER_CASE_INBOX = "inbox";
104     protected static final String SENT = "Sent";
105     protected static final String SENDMSG = "##DavMailSubmissionURI##";
106     protected static final String DRAFTS = "Drafts";
107     protected static final String TRASH = "Trash";
108     protected static final String JUNK = "Junk";
109     protected static final String UNSENT = "Unsent Messages";
110 
111     static {
112         // Adjust Mime decoder settings
113         System.setProperty("mail.mime.ignoreunknownencoding", "true");
114         System.setProperty("mail.mime.decodetext.strict", "false");
115     }
116 
117     protected String publicFolderUrl;
118 
119     /**
120      * Base user mailboxes path (used to select folder)
121      */
122     protected String mailPath;
123     protected String rootPath;
124     protected String email;
125     protected String alias;
126     /**
127      * Lower case Caldav path to current user mailbox.
128      * /users/<i>email</i>
129      */
130     protected String currentMailboxPath;
131     protected final HttpClient httpClient;
132 
133     protected String userName;
134     /**
135      * A OTP pre-auth page may require a different username.
136      */
137     private String preAuthUsername;
138 
139     protected String serverVersion;
140 
141     protected static final String YYYY_MM_DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss";
142     private static final String YYYYMMDD_T_HHMMSS_Z = "yyyyMMdd'T'HHmmss'Z'";
143     protected static final String YYYY_MM_DD_T_HHMMSS_Z = "yyyy-MM-dd'T'HH:mm:ss'Z'";
144     private static final String YYYY_MM_DD = "yyyy-MM-dd";
145     private static final String YYYY_MM_DD_T_HHMMSS_SSS_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
146 
147     /**
148      * Logon form user name fields.
149      */
150     private final List<String> userNameInputs = new ArrayList<String>();
151     /**
152      * Logon form password field, default is password.
153      */
154     private String passwordInput = null;
155     /**
156      * Tells if, during the login navigation, an OTP pre-auth page has been found.
157      */
158     private boolean otpPreAuthFound = false;
159     /**
160      * Lets the user try again a couple of times to enter the OTP pre-auth key before giving up.
161      */
162     private int otpPreAuthRetries = 0;
163     /**
164      * Maximum number of times the user can try to input again the OTP pre-auth key before giving up.
165      */
166     private static final int MAX_OTP_RETRIES = 3;
167 
168     /**
169      * Create an exchange session for the given URL.
170      * The session is established for given userName and password
171      *
172      * @param url      Exchange url
173      * @param userName user login name
174      * @param password user password
175      * @throws IOException on error
176      */
177     public ExchangeSession(String url, String userName, String password) throws IOException {
178         this.userName = userName;
179         try {
180             httpClient = DavGatewayHttpClientFacade.getInstance(url);
181             // set private connection pool
182             DavGatewayHttpClientFacade.createMultiThreadedHttpConnectionManager(httpClient);
183             boolean isBasicAuthentication = isBasicAuthentication(httpClient, url);
184             // clear cookies created by authentication test
185             httpClient.getState().clearCookies();
186 
187             // The user may have configured an OTP pre-auth username. It is processed
188             // so early because OTP pre-auth may disappear in the Exchange LAN and this
189             // helps the user to not change is account settings in mail client at each network change.
190             if (preAuthUsername == null) {
191                 // Searches for the delimiter in configured username for the pre-auth user. 
192                 // The double-quote is not allowed inside email addresses anyway.
193                 int doubleQuoteIndex = this.userName.indexOf('"');
194                 if (doubleQuoteIndex > 0) {
195                     preAuthUsername = this.userName.substring(0, doubleQuoteIndex);
196                     this.userName = this.userName.substring(doubleQuoteIndex + 1);
197                 } else {
198                     // No doublequote: the pre-auth user is the full username, or it is not used at all.
199                     preAuthUsername = this.userName;
200                 }
201             }
202 
203             DavGatewayHttpClientFacade.setCredentials(httpClient, userName, password);
204 
205             // get webmail root url
206             // providing credentials
207             // manually follow redirect
208             HttpMethod method = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, url);
209 
210             if (!this.isAuthenticated(method)) {
211                 if (isBasicAuthentication) {
212                     int status = method.getStatusCode();
213 
214                     if (status == HttpStatus.SC_UNAUTHORIZED) {
215                         method.releaseConnection();
216                         throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED");
217                     } else if (status != HttpStatus.SC_OK) {
218                         method.releaseConnection();
219                         throw DavGatewayHttpClientFacade.buildHttpException(method);
220                     }
221                     // workaround for basic authentication on /exchange and form based authentication at /owa
222                     if ("/owa/auth/logon.aspx".equals(method.getPath())) {
223                         method = formLogin(httpClient, method, userName, password);
224                     }
225                 } else {
226                     method = formLogin(httpClient, method, userName, password);
227                 }
228             }
229 
230             // avoid 401 roundtrips, only if NTLM is disabled and basic authentication enabled
231             if (isBasicAuthentication && !DavGatewayHttpClientFacade.hasNTLMorNegotiate(httpClient)) {
232                 httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
233             }
234 
235             buildSessionInfo(method);
236 
237         } catch (DavMailAuthenticationException exc) {
238             LOGGER.error(exc.getMessage());
239             throw exc;
240         } catch (ConnectException exc) {
241             BundleMessage message = new BundleMessage("EXCEPTION_CONNECT", exc.getClass().getName(), exc.getMessage());
242             ExchangeSession.LOGGER.error(message);
243             throw new DavMailException("EXCEPTION_DAVMAIL_CONFIGURATION", message);
244         } catch (UnknownHostException exc) {
245             BundleMessage message = new BundleMessage("EXCEPTION_CONNECT", exc.getClass().getName(), exc.getMessage());
246             ExchangeSession.LOGGER.error(message);
247             throw new DavMailException("EXCEPTION_DAVMAIL_CONFIGURATION", message);
248         } catch (WebdavNotAvailableException exc) {
249             throw exc;
250         } catch (IOException exc) {
251             LOGGER.error(BundleMessage.formatLog("EXCEPTION_EXCHANGE_LOGIN_FAILED", exc));
252             throw new DavMailException("EXCEPTION_EXCHANGE_LOGIN_FAILED", exc);
253         }
254         LOGGER.debug("Session " + this + " created");
255     }
256 
257     /**
258      * Format date to exchange search format.
259      *
260      * @param date date object
261      * @return formatted search date
262      */
263     public abstract String formatSearchDate(Date date);
264 
265     /**
266      * Return standard zulu date formatter.
267      *
268      * @return zulu date formatter
269      */
270     public static SimpleDateFormat getZuluDateFormat() {
271         SimpleDateFormat dateFormat = new SimpleDateFormat(YYYYMMDD_T_HHMMSS_Z, Locale.ENGLISH);
272         dateFormat.setTimeZone(GMT_TIMEZONE);
273         return dateFormat;
274     }
275 
276     protected static SimpleDateFormat getVcardBdayFormat() {
277         SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD, Locale.ENGLISH);
278         dateFormat.setTimeZone(GMT_TIMEZONE);
279         return dateFormat;
280     }
281 
282     protected static SimpleDateFormat getExchangeZuluDateFormat() {
283         SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD_T_HHMMSS_Z, Locale.ENGLISH);
284         dateFormat.setTimeZone(GMT_TIMEZONE);
285         return dateFormat;
286     }
287 
288     protected static SimpleDateFormat getExchangeZuluDateFormatMillisecond() {
289         SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD_T_HHMMSS_SSS_Z, Locale.ENGLISH);
290         dateFormat.setTimeZone(GMT_TIMEZONE);
291         return dateFormat;
292     }
293 
294     protected static Date parseDate(String dateString) throws ParseException {
295         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
296         dateFormat.setTimeZone(GMT_TIMEZONE);
297         return dateFormat.parse(dateString);
298     }
299 
300 
301     /**
302      * Test if the session expired.
303      *
304      * @return true this session expired
305      * @throws NoRouteToHostException on error
306      * @throws UnknownHostException   on error
307      */
308     public boolean isExpired() throws NoRouteToHostException, UnknownHostException {
309         boolean isExpired = false;
310         try {
311             getFolder("");
312         } catch (UnknownHostException exc) {
313             throw exc;
314         } catch (NoRouteToHostException exc) {
315             throw exc;
316         } catch (IOException e) {
317             isExpired = true;
318         }
319 
320         return isExpired;
321     }
322 
323     /**
324      * Test authentication mode : form based or basic.
325      *
326      * @param url        exchange base URL
327      * @param httpClient httpClient instance
328      * @return true if basic authentication detected
329      */
330     protected boolean isBasicAuthentication(HttpClient httpClient, String url) {
331         return DavGatewayHttpClientFacade.getHttpStatus(httpClient, url) == HttpStatus.SC_UNAUTHORIZED;
332     }
333 
334     protected String getAbsoluteUri(HttpMethod method, String path) throws URIException {
335         URI uri = method.getURI();
336         if (path != null) {
337             // reset query string
338             uri.setQuery(null);
339             if (path.startsWith("/")) {
340                 // path is absolute, replace method path
341                 uri.setPath(path);
342             } else if (path.startsWith("http://") || path.startsWith("https://")) {
343                 return path;
344             } else {
345                 // relative path, build new path
346                 String currentPath = method.getPath();
347                 int end = currentPath.lastIndexOf('/');
348                 if (end >= 0) {
349                     uri.setPath(currentPath.substring(0, end + 1) + path);
350                 } else {
351                     throw new URIException(uri.getURI());
352                 }
353             }
354         }
355         return uri.getURI();
356     }
357 
358     protected String getScriptBasedFormURL(HttpMethod initmethod, String pathQuery) throws URIException {
359         URI initmethodURI = initmethod.getURI();
360         int queryIndex = pathQuery.indexOf('?');
361         if (queryIndex >= 0) {
362             if (queryIndex > 0) {
363                 // update path
364                 String newPath = pathQuery.substring(0, queryIndex);
365                 if (newPath.startsWith("/")) {
366                     // absolute path
367                     initmethodURI.setPath(newPath);
368                 } else {
369                     String currentPath = initmethodURI.getPath();
370                     int folderIndex = currentPath.lastIndexOf('/');
371                     if (folderIndex >= 0) {
372                         // replace relative path
373                         initmethodURI.setPath(currentPath.substring(0, folderIndex + 1) + newPath);
374                     } else {
375                         // should not happen
376                         initmethodURI.setPath('/' + newPath);
377                     }
378                 }
379             }
380             initmethodURI.setQuery(pathQuery.substring(queryIndex + 1));
381         }
382         return initmethodURI.getURI();
383     }
384 
385     /**
386      * Try to find logon method path from logon form body.
387      *
388      * @param httpClient httpClient instance
389      * @param initmethod form body http method
390      * @return logon method
391      * @throws IOException on error
392      */
393     protected HttpMethod buildLogonMethod(HttpClient httpClient, HttpMethod initmethod) throws IOException {
394 
395         HttpMethod logonMethod = null;
396 
397         // create an instance of HtmlCleaner
398         HtmlCleaner cleaner = new HtmlCleaner();
399 
400         // A OTP token authentication form in a previous page could have username fields with different names
401         userNameInputs.clear();
402 
403         try {
404             TagNode node = cleaner.clean(initmethod.getResponseBodyAsStream());
405             List forms = node.getElementListByName("form", true);
406             TagNode logonForm = null;
407             // select form
408             if (forms.size() == 1) {
409                 logonForm = (TagNode) forms.get(0);
410             } else if (forms.size() > 1) {
411                 for (Object form : forms) {
412                     if ("logonForm".equals(((TagNode) form).getAttributeByName("name"))) {
413                         logonForm = ((TagNode) form);
414                     }
415                 }
416             }
417             if (logonForm != null) {
418                 String logonMethodPath = logonForm.getAttributeByName("action");
419 
420                 // workaround for broken form with empty action
421                 if (logonMethodPath != null && logonMethodPath.length() == 0) {
422                     logonMethodPath = "/owa/auth.owa";
423                 }
424 
425                 logonMethod = new PostMethod(getAbsoluteUri(initmethod, logonMethodPath));
426 
427                 // retrieve lost inputs attached to body
428                 List inputList = node.getElementListByName("input", true);
429 
430                 for (Object input : inputList) {
431                     String type = ((TagNode) input).getAttributeByName("type");
432                     String name = ((TagNode) input).getAttributeByName("name");
433                     String value = ((TagNode) input).getAttributeByName("value");
434                     if ("hidden".equalsIgnoreCase(type) && name != null && value != null) {
435                         ((PostMethod) logonMethod).addParameter(name, value);
436                     }
437                     // custom login form
438                     if (USER_NAME_FIELDS.contains(name)) {
439                         userNameInputs.add(name);
440                     } else if (PASSWORD_FIELDS.contains(name)) {
441                         passwordInput = name;
442                     } else if ("addr".equals(name)) {
443                         // this is not a logon form but a redirect form
444                         HttpMethod newInitMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, logonMethod);
445                         logonMethod = buildLogonMethod(httpClient, newInitMethod);
446                     } else if (TOKEN_FIELDS.contains(name)) {
447                         // one time password, ask it to the user
448                         ((PostMethod) logonMethod).addParameter(name, DavGatewayOTPPrompt.getOneTimePassword());
449                     } else if ("otc".equals(name)) {
450                         // captcha image, get image and ask user
451                         String pinsafeUser = getAliasFromLogin();
452                         if (pinsafeUser == null) {
453                             pinsafeUser = userName;
454                         }
455                         GetMethod getMethod = new GetMethod("/PINsafeISAFilter.dll?username=" + pinsafeUser);
456                         try {
457                             int status = httpClient.executeMethod(getMethod);
458                             if (status != HttpStatus.SC_OK) {
459                                 throw DavGatewayHttpClientFacade.buildHttpException(getMethod);
460                             }
461                             BufferedImage captchaImage = ImageIO.read(getMethod.getResponseBodyAsStream());
462                             ((PostMethod) logonMethod).addParameter(name, DavGatewayOTPPrompt.getCaptchaValue(captchaImage));
463 
464                         } finally {
465                             getMethod.releaseConnection();
466                         }
467                     }
468                 }
469             } else {
470                 List frameList = node.getElementListByName("frame", true);
471                 if (frameList.size() == 1) {
472                     String src = ((TagNode) frameList.get(0)).getAttributeByName("src");
473                     if (src != null) {
474                         LOGGER.debug("Frames detected in form page, try frame content");
475                         initmethod.releaseConnection();
476                         HttpMethod newInitMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, src);
477                         logonMethod = buildLogonMethod(httpClient, newInitMethod);
478                     }
479                 } else {
480                     // another failover for script based logon forms (Exchange 2007)
481                     List scriptList = node.getElementListByName("script", true);
482                     for (Object script : scriptList) {
483                         List contents = ((TagNode) script).getChildren();
484                         for (Object content : contents) {
485                             if (content instanceof CommentNode) {
486                                 String scriptValue = ((CommentNode) content).getCommentedContent();
487                                 String sUrl = StringUtil.getToken(scriptValue, "var a_sUrl = \"", "\"");
488                                 String sLgn = StringUtil.getToken(scriptValue, "var a_sLgnQS = \"", "\"");
489                                 if (sLgn == null) {
490                                     sLgn = StringUtil.getToken(scriptValue, "var a_sLgn = \"", "\"");
491                                 }
492                                 if (sUrl != null && sLgn != null) {
493                                     String src = getScriptBasedFormURL(initmethod, sLgn + sUrl);
494                                     LOGGER.debug("Detected script based logon, redirect to form at " + src);
495                                     HttpMethod newInitMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, src);
496                                     logonMethod = buildLogonMethod(httpClient, newInitMethod);
497                                 }
498 
499                             } else if (content instanceof ContentNode) {
500                                 // Microsoft Forefront Unified Access Gateway redirect
501                                 String scriptValue = ((ContentNode) content).getContent().toString();
502                                 String location = StringUtil.getToken(scriptValue, "window.location.replace(\"", "\"");
503                                 if (location != null) {
504                                     LOGGER.debug("Post logon redirect to: " + location);
505                                     logonMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, location);
506                                 }
507                             }
508                         }
509                     }
510                 }
511             }
512         } catch (IOException e) {
513             LOGGER.error("Error parsing login form at " + initmethod.getURI());
514         } finally {
515             initmethod.releaseConnection();
516         }
517 
518         return logonMethod;
519     }
520 
521     protected HttpMethod postLogonMethod(HttpClient httpClient, HttpMethod logonMethod, String userName, String password) throws IOException {
522 
523         setAuthFormFields(logonMethod, httpClient, password);
524 
525         // add exchange 2010 PBack cookie in compatibility mode
526         httpClient.getState().addCookie(new Cookie(httpClient.getHostConfiguration().getHost(), "PBack", "0", "/", null, false));
527 
528         logonMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, logonMethod);
529 
530         // test form based authentication
531         checkFormLoginQueryString(logonMethod);
532 
533         // workaround for post logon script redirect
534         if (!isAuthenticated(logonMethod)) {
535             // try to get new method from script based redirection
536             logonMethod = buildLogonMethod(httpClient, logonMethod);
537 
538             if (logonMethod != null) {
539                 if (otpPreAuthFound && otpPreAuthRetries < MAX_OTP_RETRIES) {
540                     // A OTP pre-auth page has been found, it is needed to restart the login process.
541                     // This applies to both the case the user entered a good OTP code (the usual login process
542                     // takes place) and the case the user entered a wrong OTP code (another code will be asked to him).
543                     // The user has up to MAX_OTP_RETRIES chances to input a valid OTP key.
544                     return postLogonMethod(httpClient, logonMethod, userName, password);
545                 }
546 
547                 // if logonMethod is not null, try to follow redirection
548                 logonMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, logonMethod);
549                 checkFormLoginQueryString(logonMethod);
550                 // also check cookies
551                 if (!isAuthenticated(logonMethod)) {
552                     throwAuthenticationFailed();
553                 }
554             } else {
555                 // authentication failed
556                 throwAuthenticationFailed();
557             }
558         }
559 
560         // check for language selection form
561         if (logonMethod != null && "/owa/languageselection.aspx".equals(logonMethod.getPath())) {
562             // need to submit form
563             logonMethod = submitLanguageSelectionForm(logonMethod);
564         }
565         return logonMethod;
566     }
567 
568     protected void setAuthFormFields(HttpMethod logonMethod, HttpClient httpClient, String password) throws IllegalArgumentException {
569         String userNameInput;
570         if (userNameInputs.size() == 2) {
571             String userid;
572             // multiple username fields, split userid|username on |
573             int pipeIndex = userName.indexOf('|');
574             if (pipeIndex < 0) {
575                 LOGGER.debug("Multiple user fields detected, please use userid|username as user name in client, except when userid is username");
576                 userid = userName;
577             } else {
578                 userid = userName.substring(0, pipeIndex);
579                 userName = userName.substring(pipeIndex + 1);
580                 // adjust credentials
581                 DavGatewayHttpClientFacade.setCredentials(httpClient, userName, password);
582             }
583             ((PostMethod) logonMethod).removeParameter("userid");
584             ((PostMethod) logonMethod).addParameter("userid", userid);
585 
586             userNameInput = "username";
587         } else if (userNameInputs.size() == 1) {
588             // simple username field
589             userNameInput = userNameInputs.get(0);
590         } else {
591             // should not happen
592             userNameInput = "username";
593         }
594         // make sure username and password fields are empty
595         ((PostMethod) logonMethod).removeParameter(userNameInput);
596         if (passwordInput != null) {
597             ((PostMethod) logonMethod).removeParameter(passwordInput);
598         }
599         ((PostMethod) logonMethod).removeParameter("trusted");
600         ((PostMethod) logonMethod).removeParameter("flags");
601 
602         if (passwordInput == null) {
603             // This is a OTP pre-auth page. A different username may be required.
604             otpPreAuthFound = true;
605             otpPreAuthRetries++;
606             ((PostMethod) logonMethod).addParameter(userNameInput, preAuthUsername);
607         } else {
608             otpPreAuthFound = false;
609             otpPreAuthRetries = 0;
610             // This is a regular Exchange login page
611             ((PostMethod) logonMethod).addParameter(userNameInput, userName);
612             ((PostMethod) logonMethod).addParameter(passwordInput, password);
613             ((PostMethod) logonMethod).addParameter("trusted", "4");
614             ((PostMethod) logonMethod).addParameter("flags", "4");
615         }
616     }
617 
618     protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password) throws IOException {
619         LOGGER.debug("Form based authentication detected");
620 
621         HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod);
622         if (logonMethod == null) {
623             LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", trying default url");
624             logonMethod = new PostMethod("/owa/auth/owaauth.dll");
625         }
626         logonMethod = postLogonMethod(httpClient, logonMethod, userName, password);
627 
628         return logonMethod;
629     }
630 
631     protected HttpMethod submitLanguageSelectionForm(HttpMethod logonMethod) throws IOException {
632         PostMethod postLanguageFormMethod;
633         // create an instance of HtmlCleaner
634         HtmlCleaner cleaner = new HtmlCleaner();
635 
636         try {
637             TagNode node = cleaner.clean(logonMethod.getResponseBodyAsStream());
638             List forms = node.getElementListByName("form", true);
639             TagNode languageForm;
640             // select form
641             if (forms.size() == 1) {
642                 languageForm = (TagNode) forms.get(0);
643             } else {
644                 throw new IOException("Form not found");
645             }
646             String languageMethodPath = languageForm.getAttributeByName("action");
647 
648             postLanguageFormMethod = new PostMethod(getAbsoluteUri(logonMethod, languageMethodPath));
649 
650             List inputList = languageForm.getElementListByName("input", true);
651             for (Object input : inputList) {
652                 String name = ((TagNode) input).getAttributeByName("name");
653                 String value = ((TagNode) input).getAttributeByName("value");
654                 if (name != null && value != null) {
655                     postLanguageFormMethod.addParameter(name, value);
656                 }
657             }
658             List selectList = languageForm.getElementListByName("select", true);
659             for (Object select : selectList) {
660                 String name = ((TagNode) select).getAttributeByName("name");
661                 List optionList = ((TagNode) select).getElementListByName("option", true);
662                 String value = null;
663                 for (Object option : optionList) {
664                     if (((TagNode) option).getAttributeByName("selected") != null) {
665                         value = ((TagNode) option).getAttributeByName("value");
666                         break;
667                     }
668                 }
669                 if (name != null && value != null) {
670                     postLanguageFormMethod.addParameter(name, value);
671                 }
672             }
673         } catch (IOException e) {
674             String errorMessage = "Error parsing language selection form at " + logonMethod.getURI();
675             LOGGER.error(errorMessage);
676             throw new IOException(errorMessage);
677         } finally {
678             logonMethod.releaseConnection();
679         }
680 
681         return DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, postLanguageFormMethod);
682     }
683 
684     /**
685      * Look for session cookies.
686      *
687      * @return true if session cookies are available
688      */
689     protected boolean isAuthenticated(HttpMethod method) {
690         boolean authenticated = false;
691         if (method.getStatusCode() == HttpStatus.SC_OK
692                 && "/ews/services.wsdl".equalsIgnoreCase(method.getPath())) {
693             // direct EWS access returned wsdl
694             authenticated = true;
695         } else {
696             // check cookies
697             for (Cookie cookie : httpClient.getState().getCookies()) {
698                 // Exchange 2003 cookies
699                 if (cookie.getName().startsWith("cadata") || "sessionid".equals(cookie.getName())
700                         // Exchange 2007 cookie
701                         || "UserContext".equals(cookie.getName())
702                         // Direct EWS access
703                         || "exchangecookie".equals(cookie.getName())
704                         ) {
705                     authenticated = true;
706                     break;
707                 }
708             }
709         }
710         return authenticated;
711     }
712 
713     protected void checkFormLoginQueryString(HttpMethod logonMethod) throws DavMailAuthenticationException {
714         String queryString = logonMethod.getQueryString();
715         if (queryString != null && (queryString.contains("reason=2") || queryString.contains("reason=4"))) {
716             logonMethod.releaseConnection();
717             throwAuthenticationFailed();
718         }
719     }
720 
721     protected void throwAuthenticationFailed() throws DavMailAuthenticationException {
722         if (this.userName != null && this.userName.contains("\\")) {
723             throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED");
724         } else {
725             throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED_RETRY");
726         }
727     }
728 
729     protected abstract void buildSessionInfo(HttpMethod method) throws DavMailException;
730 
731     /**
732      * Create message in specified folder.
733      * Will overwrite an existing message with same subject in the same folder
734      *
735      * @param folderPath  Exchange folder path
736      * @param messageName message name
737      * @param properties  message properties (flags)
738      * @param mimeMessage MIME message
739      * @throws IOException when unable to create message
740      */
741     public abstract void createMessage(String folderPath, String messageName, HashMap<String, String> properties, MimeMessage mimeMessage) throws IOException;
742 
743     /**
744      * Update given properties on message.
745      *
746      * @param message    Exchange message
747      * @param properties Webdav properties map
748      * @throws IOException on error
749      */
750     public abstract void updateMessage(Message message, Map<String, String> properties) throws IOException;
751 
752 
753     /**
754      * Delete Exchange message.
755      *
756      * @param message Exchange message
757      * @throws IOException on error
758      */
759     public abstract void deleteMessage(Message message) throws IOException;
760 
761     /**
762      * Get raw MIME message content
763      *
764      * @param message Exchange message
765      * @return message body
766      * @throws IOException on error
767      */
768     protected abstract byte[] getContent(Message message) throws IOException;
769 
770     protected static final Set<String> POP_MESSAGE_ATTRIBUTES = new HashSet<String>();
771 
772     static {
773         POP_MESSAGE_ATTRIBUTES.add("uid");
774         POP_MESSAGE_ATTRIBUTES.add("imapUid");
775         POP_MESSAGE_ATTRIBUTES.add("messageSize");
776     }
777 
778     /**
779      * Return folder message list with id and size only (for POP3 listener).
780      *
781      * @param folderName Exchange folder name
782      * @return folder message list
783      * @throws IOException on error
784      */
785     public MessageList getAllMessageUidAndSize(String folderName) throws IOException {
786         return searchMessages(folderName, POP_MESSAGE_ATTRIBUTES, null);
787     }
788 
789     protected static final Set<String> IMAP_MESSAGE_ATTRIBUTES = new HashSet<String>();
790 
791     static {
792         IMAP_MESSAGE_ATTRIBUTES.add("permanenturl");
793         IMAP_MESSAGE_ATTRIBUTES.add("urlcompname");
794         IMAP_MESSAGE_ATTRIBUTES.add("uid");
795         IMAP_MESSAGE_ATTRIBUTES.add("messageSize");
796         IMAP_MESSAGE_ATTRIBUTES.add("imapUid");
797         IMAP_MESSAGE_ATTRIBUTES.add("junk");
798         IMAP_MESSAGE_ATTRIBUTES.add("flagStatus");
799         IMAP_MESSAGE_ATTRIBUTES.add("messageFlags");
800         IMAP_MESSAGE_ATTRIBUTES.add("lastVerbExecuted");
801         IMAP_MESSAGE_ATTRIBUTES.add("read");
802         IMAP_MESSAGE_ATTRIBUTES.add("deleted");
803         IMAP_MESSAGE_ATTRIBUTES.add("date");
804         IMAP_MESSAGE_ATTRIBUTES.add("lastmodified");
805         // OSX IMAP requests content-class
806         IMAP_MESSAGE_ATTRIBUTES.add("contentclass");
807         IMAP_MESSAGE_ATTRIBUTES.add("keywords");
808     }
809 
810     protected static final Set<String> UID_MESSAGE_ATTRIBUTES = new HashSet<String>();
811 
812     static {
813         UID_MESSAGE_ATTRIBUTES.add("uid");
814     }
815 
816     /**
817      * Get all folder messages.
818      *
819      * @param folderPath Exchange folder name
820      * @return message list
821      * @throws IOException on error
822      */
823     public MessageList searchMessages(String folderPath) throws IOException {
824         return searchMessages(folderPath, IMAP_MESSAGE_ATTRIBUTES, null);
825     }
826 
827     /**
828      * Search folder for messages matching conditions, with attributes needed by IMAP listener.
829      *
830      * @param folderName Exchange folder name
831      * @param condition  search filter
832      * @return message list
833      * @throws IOException on error
834      */
835     public MessageList searchMessages(String folderName, Condition condition) throws IOException {
836         return searchMessages(folderName, IMAP_MESSAGE_ATTRIBUTES, condition);
837     }
838 
839     /**
840      * Search folder for messages matching conditions, with given attributes.
841      *
842      * @param folderName Exchange folder name
843      * @param attributes requested Webdav attributes
844      * @param condition  search filter
845      * @return message list
846      * @throws IOException on error
847      */
848     public abstract MessageList searchMessages(String folderName, Set<String> attributes, Condition condition) throws IOException;
849 
850     /**
851      * Get server version (Exchange2003, Exchange2007 or Exchange2010)
852      *
853      * @return server version
854      */
855     public String getServerVersion() {
856         return serverVersion;
857     }
858 
859     @SuppressWarnings({"JavaDoc"})
860     public enum Operator {
861         Or, And, Not, IsEqualTo,
862         IsGreaterThan, IsGreaterThanOrEqualTo,
863         IsLessThan, IsLessThanOrEqualTo,
864         IsNull, IsTrue, IsFalse,
865         Like, StartsWith, Contains
866     }
867 
868     /**
869      * Exchange search filter.
870      */
871     public static interface Condition {
872         /**
873          * Append condition to buffer.
874          *
875          * @param buffer search filter buffer
876          */
877         void appendTo(StringBuilder buffer);
878 
879         /**
880          * True if condition is empty.
881          *
882          * @return true if condition is empty
883          */
884         boolean isEmpty();
885 
886         /**
887          * Test if the contact matches current condition.
888          *
889          * @param contact Exchange Contact
890          * @return true if contact matches condition
891          */
892         boolean isMatch(ExchangeSession.Contact contact);
893     }
894 
895     /**
896      * Attribute condition.
897      */
898     public abstract static class AttributeCondition implements Condition {
899         protected final String attributeName;
900         protected final Operator operator;
901         protected final String value;
902 
903         protected AttributeCondition(String attributeName, Operator operator, String value) {
904             this.attributeName = attributeName;
905             this.operator = operator;
906             this.value = value;
907         }
908 
909         public boolean isEmpty() {
910             return false;
911         }
912 
913         /**
914          * Get attribute name.
915          *
916          * @return attribute name
917          */
918         public String getAttributeName() {
919             return attributeName;
920         }
921 
922         /**
923          * Condition value.
924          *
925          * @return value
926          */
927         public String getValue() {
928             return value;
929         }
930 
931     }
932 
933     /**
934      * Multiple condition.
935      */
936     public abstract static class MultiCondition implements Condition {
937         protected final Operator operator;
938         protected final List<Condition> conditions;
939 
940         protected MultiCondition(Operator operator, Condition... conditions) {
941             this.operator = operator;
942             this.conditions = new ArrayList<Condition>();
943             for (Condition condition : conditions) {
944                 if (condition != null) {
945                     this.conditions.add(condition);
946                 }
947             }
948         }
949 
950         /**
951          * Conditions list.
952          *
953          * @return conditions
954          */
955         public List<Condition> getConditions() {
956             return conditions;
957         }
958 
959         /**
960          * Condition operator.
961          *
962          * @return operator
963          */
964         public Operator getOperator() {
965             return operator;
966         }
967 
968         /**
969          * Add a new condition.
970          *
971          * @param condition single condition
972          */
973         public void add(Condition condition) {
974             if (condition != null) {
975                 conditions.add(condition);
976             }
977         }
978 
979         public boolean isEmpty() {
980             boolean isEmpty = true;
981             for (Condition condition : conditions) {
982                 if (!condition.isEmpty()) {
983                     isEmpty = false;
984                     break;
985                 }
986             }
987             return isEmpty;
988         }
989 
990         public boolean isMatch(ExchangeSession.Contact contact) {
991             if (operator == Operator.And) {
992                 for (Condition condition : conditions) {
993                     if (!condition.isMatch(contact)) {
994                         return false;
995                     }
996                 }
997                 return true;
998             } else if (operator == Operator.Or) {
999                 for (Condition condition : conditions) {
1000                     if (condition.isMatch(contact)) {
1001                         return true;
1002                     }
1003                 }
1004                 return false;
1005             } else {
1006                 return false;
1007             }
1008         }
1009 
1010     }
1011 
1012     /**
1013      * Not condition.
1014      */
1015     public abstract static class NotCondition implements Condition {
1016         protected final Condition condition;
1017 
1018         protected NotCondition(Condition condition) {
1019             this.condition = condition;
1020         }
1021 
1022         public boolean isEmpty() {
1023             return condition.isEmpty();
1024         }
1025 
1026         public boolean isMatch(ExchangeSession.Contact contact) {
1027             return !condition.isMatch(contact);
1028         }
1029     }
1030 
1031     /**
1032      * Single search filter condition.
1033      */
1034     public abstract static class MonoCondition implements Condition {
1035         protected final String attributeName;
1036         protected final Operator operator;
1037 
1038         protected MonoCondition(String attributeName, Operator operator) {
1039             this.attributeName = attributeName;
1040             this.operator = operator;
1041         }
1042 
1043         public boolean isEmpty() {
1044             return false;
1045         }
1046 
1047         public boolean isMatch(ExchangeSession.Contact contact) {
1048             String actualValue = contact.get(attributeName);
1049             return (operator == Operator.IsNull && actualValue == null) ||
1050                     (operator == Operator.IsFalse && "false".equals(actualValue)) ||
1051                     (operator == Operator.IsTrue && "true".equals(actualValue));
1052         }
1053     }
1054 
1055     /**
1056      * And search filter.
1057      *
1058      * @param condition search conditions
1059      * @return condition
1060      */
1061     public abstract MultiCondition and(Condition... condition);
1062 
1063     /**
1064      * Or search filter.
1065      *
1066      * @param condition search conditions
1067      * @return condition
1068      */
1069     public abstract MultiCondition or(Condition... condition);
1070 
1071     /**
1072      * Not search filter.
1073      *
1074      * @param condition search condition
1075      * @return condition
1076      */
1077     public abstract Condition not(Condition condition);
1078 
1079     /**
1080      * Equals condition.
1081      *
1082      * @param attributeName logical Exchange attribute name
1083      * @param value         attribute value
1084      * @return condition
1085      */
1086     public abstract Condition isEqualTo(String attributeName, String value);
1087 
1088     /**
1089      * Equals condition.
1090      *
1091      * @param attributeName logical Exchange attribute name
1092      * @param value         attribute value
1093      * @return condition
1094      */
1095     public abstract Condition isEqualTo(String attributeName, int value);
1096 
1097     /**
1098      * MIME header equals condition.
1099      *
1100      * @param headerName MIME header name
1101      * @param value      attribute value
1102      * @return condition
1103      */
1104     public abstract Condition headerIsEqualTo(String headerName, String value);
1105 
1106     /**
1107      * Greater than or equals condition.
1108      *
1109      * @param attributeName logical Exchange attribute name
1110      * @param value         attribute value
1111      * @return condition
1112      */
1113     public abstract Condition gte(String attributeName, String value);
1114 
1115     /**
1116      * Greater than condition.
1117      *
1118      * @param attributeName logical Exchange attribute name
1119      * @param value         attribute value
1120      * @return condition
1121      */
1122     public abstract Condition gt(String attributeName, String value);
1123 
1124     /**
1125      * Lower than condition.
1126      *
1127      * @param attributeName logical Exchange attribute name
1128      * @param value         attribute value
1129      * @return condition
1130      */
1131     public abstract Condition lt(String attributeName, String value);
1132 
1133     /**
1134      * Lower than or equals condition.
1135      *
1136      * @param attributeName logical Exchange attribute name
1137      * @param value         attribute value
1138      * @return condition
1139      */
1140     @SuppressWarnings({"UnusedDeclaration"})
1141     public abstract Condition lte(String attributeName, String value);
1142 
1143     /**
1144      * Contains condition.
1145      *
1146      * @param attributeName logical Exchange attribute name
1147      * @param value         attribute value
1148      * @return condition
1149      */
1150     public abstract Condition contains(String attributeName, String value);
1151 
1152     /**
1153      * Starts with condition.
1154      *
1155      * @param attributeName logical Exchange attribute name
1156      * @param value         attribute value
1157      * @return condition
1158      */
1159     public abstract Condition startsWith(String attributeName, String value);
1160 
1161     /**
1162      * Is null condition.
1163      *
1164      * @param attributeName logical Exchange attribute name
1165      * @return condition
1166      */
1167     public abstract Condition isNull(String attributeName);
1168 
1169     /**
1170      * Is true condition.
1171      *
1172      * @param attributeName logical Exchange attribute name
1173      * @return condition
1174      */
1175     public abstract Condition isTrue(String attributeName);
1176 
1177     /**
1178      * Is false condition.
1179      *
1180      * @param attributeName logical Exchange attribute name
1181      * @return condition
1182      */
1183     public abstract Condition isFalse(String attributeName);
1184 
1185     /**
1186      * Search mail and generic folders under given folder.
1187      * Exclude calendar and contacts folders
1188      *
1189      * @param folderName Exchange folder name
1190      * @param recursive  deep search if true
1191      * @return list of folders
1192      * @throws IOException on error
1193      */
1194     public List<Folder> getSubFolders(String folderName, boolean recursive) throws IOException {
1195         Condition folderCondition = null;
1196         if (!Settings.getBooleanProperty("davmail.imapIncludeSpecialFolders", false)) {
1197             folderCondition = or(isEqualTo("folderclass", "IPF.Note"), isNull("folderclass"));
1198         }
1199         List<Folder> results = getSubFolders(folderName, folderCondition,
1200                 recursive);
1201         // need to include base folder in recursive search, except on root
1202         if (recursive && folderName.length() > 0) {
1203             results.add(getFolder(folderName));
1204         }
1205 
1206         return results;
1207     }
1208 
1209     /**
1210      * Search calendar folders under given folder.
1211      *
1212      * @param folderName Exchange folder name
1213      * @param recursive  deep search if true
1214      * @return list of folders
1215      * @throws IOException on error
1216      */
1217     public List<Folder> getSubCalendarFolders(String folderName, boolean recursive) throws IOException {
1218         return getSubFolders(folderName, isEqualTo("folderclass", "IPF.Appointment"), recursive);
1219     }
1220 
1221     /**
1222      * Search folders under given folder matching filter.
1223      *
1224      * @param folderName Exchange folder name
1225      * @param condition  search filter
1226      * @param recursive  deep search if true
1227      * @return list of folders
1228      * @throws IOException on error
1229      */
1230     public abstract List<Folder> getSubFolders(String folderName, Condition condition, boolean recursive) throws IOException;
1231 
1232     /**
1233      * Delete oldest messages in trash.
1234      * keepDelay is the number of days to keep messages in trash before delete
1235      *
1236      * @throws IOException when unable to purge messages
1237      */
1238     public void purgeOldestTrashAndSentMessages() throws IOException {
1239         int keepDelay = Settings.getIntProperty("davmail.keepDelay");
1240         if (keepDelay != 0) {
1241             purgeOldestFolderMessages(TRASH, keepDelay);
1242         }
1243         // this is a new feature, default is : do nothing
1244         int sentKeepDelay = Settings.getIntProperty("davmail.sentKeepDelay");
1245         if (sentKeepDelay != 0) {
1246             purgeOldestFolderMessages(SENT, sentKeepDelay);
1247         }
1248     }
1249 
1250     protected void purgeOldestFolderMessages(String folderPath, int keepDelay) throws IOException {
1251         Calendar cal = Calendar.getInstance();
1252         cal.add(Calendar.DAY_OF_MONTH, -keepDelay);
1253         LOGGER.debug("Delete messages in " + folderPath + " not modified since " + cal.getTime());
1254 
1255         MessageList messages = searchMessages(folderPath, UID_MESSAGE_ATTRIBUTES,
1256                 lt("lastmodified", formatSearchDate(cal.getTime())));
1257 
1258         for (Message message : messages) {
1259             message.delete();
1260         }
1261     }
1262 
1263     protected void convertResentHeader(MimeMessage mimeMessage, String headerName) throws MessagingException {
1264         String[] resentHeader = mimeMessage.getHeader("Resent-" + headerName);
1265         if (resentHeader != null) {
1266             mimeMessage.removeHeader("Resent-" + headerName);
1267             mimeMessage.removeHeader(headerName);
1268             for (String value : resentHeader) {
1269                 mimeMessage.addHeader(headerName, value);
1270             }
1271         }
1272     }
1273 
1274     protected String lastSentMessageId;
1275 
1276     /**
1277      * Send message in reader to recipients.
1278      * Detect visible recipients in message body to determine bcc recipients
1279      *
1280      * @param rcptToRecipients recipients list
1281      * @param mimeMessage      mime message
1282      * @throws IOException        on error
1283      * @throws MessagingException on error
1284      */
1285     public void sendMessage(List<String> rcptToRecipients, MimeMessage mimeMessage) throws IOException, MessagingException {
1286         // detect duplicate send command
1287         String messageId = mimeMessage.getMessageID();
1288         if (lastSentMessageId != null && lastSentMessageId.equals(messageId)) {
1289             LOGGER.debug("Dropping message id " + messageId + ": already sent");
1290             return;
1291         }
1292         lastSentMessageId = messageId;
1293 
1294         convertResentHeader(mimeMessage, "From");
1295         convertResentHeader(mimeMessage, "To");
1296         convertResentHeader(mimeMessage, "Cc");
1297         convertResentHeader(mimeMessage, "Bcc");
1298         convertResentHeader(mimeMessage, "Message-Id");
1299 
1300         // do not allow send as another user on Exchange 2003
1301         if ("Exchange2003".equals(serverVersion) || Settings.getBooleanProperty("davmail.smtpStripFrom", false)) {
1302             mimeMessage.removeHeader("From");
1303         }
1304 
1305         // remove visible recipients from list
1306         Set<String> visibleRecipients = new HashSet<String>();
1307         List<InternetAddress> recipients = getAllRecipients(mimeMessage);
1308         for (InternetAddress address : recipients) {
1309             visibleRecipients.add((address.getAddress().toLowerCase()));
1310         }
1311         for (String recipient : rcptToRecipients) {
1312             if (!visibleRecipients.contains(recipient.toLowerCase())) {
1313                 mimeMessage.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(recipient));
1314             }
1315         }
1316         sendMessage(mimeMessage);
1317 
1318     }
1319 
1320     static final String[] RECIPIENT_HEADERS = {"to", "cc", "bcc"};
1321 
1322     protected List<InternetAddress> getAllRecipients(MimeMessage mimeMessage) throws MessagingException {
1323         List<InternetAddress> recipientList = new ArrayList<InternetAddress>();
1324         for (String recipientHeader : RECIPIENT_HEADERS) {
1325             final String recipientHeaderValue = mimeMessage.getHeader(recipientHeader, ",");
1326             if (recipientHeaderValue != null) {
1327                 // parse headers in non strict mode
1328                 recipientList.addAll(Arrays.asList(InternetAddress.parseHeader(recipientHeaderValue, false)));
1329             }
1330 
1331         }
1332         return recipientList;
1333     }
1334 
1335     /**
1336      * Send Mime message.
1337      *
1338      * @param mimeMessage MIME message
1339      * @throws IOException        on error
1340      * @throws MessagingException on error
1341      */
1342     public abstract void sendMessage(MimeMessage mimeMessage) throws IOException, MessagingException;
1343 
1344     /**
1345      * Get folder object.
1346      * Folder name can be logical names INBOX, Drafts, Trash or calendar,
1347      * or a path relative to user base folder or absolute path.
1348      *
1349      * @param folderPath folder path
1350      * @return Folder object
1351      * @throws IOException on error
1352      */
1353     public ExchangeSession.Folder getFolder(String folderPath) throws IOException {
1354         Folder folder = internalGetFolder(folderPath);
1355         if (isMainCalendar(folderPath)) {
1356             Folder taskFolder = internalGetFolder(TASKS);
1357             folder.ctag += taskFolder.ctag;
1358         }
1359         return folder;
1360     }
1361 
1362     protected abstract Folder internalGetFolder(String folderName) throws IOException;
1363 
1364     /**
1365      * Check folder ctag and reload messages as needed.
1366      *
1367      * @param currentFolder current folder
1368      * @return true if folder changed
1369      * @throws IOException on error
1370      */
1371     public boolean refreshFolder(Folder currentFolder) throws IOException {
1372         Folder newFolder = getFolder(currentFolder.folderPath);
1373         if (currentFolder.ctag == null || !currentFolder.ctag.equals(newFolder.ctag)) {
1374             if (LOGGER.isDebugEnabled()) {
1375                 LOGGER.debug("Contenttag changed on " + currentFolder.folderPath + ' '
1376                         + currentFolder.ctag + " => " + newFolder.ctag + ", reloading messages");
1377             }
1378             currentFolder.hasChildren = newFolder.hasChildren;
1379             currentFolder.noInferiors = newFolder.noInferiors;
1380             currentFolder.unreadCount = newFolder.unreadCount;
1381             currentFolder.ctag = newFolder.ctag;
1382             currentFolder.etag = newFolder.etag;
1383             if (newFolder.uidNext > currentFolder.uidNext) {
1384                 currentFolder.uidNext = newFolder.uidNext;
1385             }
1386             currentFolder.loadMessages();
1387             return true;
1388         } else {
1389             return false;
1390         }
1391     }
1392 
1393     /**
1394      * Create Exchange message folder.
1395      *
1396      * @param folderName logical folder name
1397      * @return status
1398      * @throws IOException on error
1399      */
1400     public int createMessageFolder(String folderName) throws IOException {
1401         return createFolder(folderName, "IPF.Note", null);
1402     }
1403 
1404     /**
1405      * Create Exchange calendar folder.
1406      *
1407      * @param folderName logical folder name
1408      * @param properties folder properties
1409      * @return status
1410      * @throws IOException on error
1411      */
1412     public int createCalendarFolder(String folderName, Map<String, String> properties) throws IOException {
1413         return createFolder(folderName, "IPF.Appointment", properties);
1414     }
1415 
1416     /**
1417      * Create Exchange contact folder.
1418      *
1419      * @param folderName logical folder name
1420      * @param properties folder properties
1421      * @return status
1422      * @throws IOException on error
1423      */
1424     public int createContactFolder(String folderName, Map<String, String> properties) throws IOException {
1425         return createFolder(folderName, "IPF.Contact", properties);
1426     }
1427 
1428     /**
1429      * Create Exchange folder with given folder class.
1430      *
1431      * @param folderName  logical folder name
1432      * @param folderClass folder class
1433      * @param properties  folder properties
1434      * @return status
1435      * @throws IOException on error
1436      */
1437     public abstract int createFolder(String folderName, String folderClass, Map<String, String> properties) throws IOException;
1438 
1439     /**
1440      * Update Exchange folder properties.
1441      *
1442      * @param folderName logical folder name
1443      * @param properties folder properties
1444      * @return status
1445      * @throws IOException on error
1446      */
1447     public abstract int updateFolder(String folderName, Map<String, String> properties) throws IOException;
1448 
1449     /**
1450      * Delete Exchange folder.
1451      *
1452      * @param folderName logical folder name
1453      * @throws IOException on error
1454      */
1455     public abstract void deleteFolder(String folderName) throws IOException;
1456 
1457     /**
1458      * Copy message to target folder
1459      *
1460      * @param message      Exchange message
1461      * @param targetFolder target folder
1462      * @throws IOException on error
1463      */
1464     public abstract void copyMessage(Message message, String targetFolder) throws IOException;
1465 
1466     /**
1467      * Move message to target folder
1468      *
1469      * @param message      Exchange message
1470      * @param targetFolder target folder
1471      * @throws IOException on error
1472      */
1473     public abstract void moveMessage(Message message, String targetFolder) throws IOException;
1474 
1475     /**
1476      * Move folder to target name.
1477      *
1478      * @param folderName current folder name/path
1479      * @param targetName target folder name/path
1480      * @throws IOException on error
1481      */
1482     public abstract void moveFolder(String folderName, String targetName) throws IOException;
1483 
1484     /**
1485      * Move item from source path to target path.
1486      *
1487      * @param sourcePath item source path
1488      * @param targetPath item target path
1489      * @throws IOException on error
1490      */
1491     public abstract void moveItem(String sourcePath, String targetPath) throws IOException;
1492 
1493     protected abstract void moveToTrash(Message message) throws IOException;
1494 
1495     /**
1496      * Convert keyword value to IMAP flag.
1497      *
1498      * @param value keyword value
1499      * @return IMAP flag
1500      */
1501     public String convertKeywordToFlag(String value) {
1502         // first test for keyword in settings
1503         Properties flagSettings = Settings.getSubProperties("davmail.imapFlags");
1504         Enumeration flagSettingsEnum = flagSettings.propertyNames();
1505         while (flagSettingsEnum.hasMoreElements()) {
1506             String key = (String) flagSettingsEnum.nextElement();
1507             if (value.equalsIgnoreCase(flagSettings.getProperty(key))) {
1508                 return key;
1509             }
1510         }
1511 
1512         ResourceBundle flagBundle = ResourceBundle.getBundle("imapflags");
1513         Enumeration<String> flagBundleEnum = flagBundle.getKeys();
1514         while (flagBundleEnum.hasMoreElements()) {
1515             String key = flagBundleEnum.nextElement();
1516             if (value.equalsIgnoreCase(flagBundle.getString(key))) {
1517                 return key;
1518             }
1519         }
1520 
1521         // fall back to raw value
1522         return value;
1523     }
1524 
1525     /**
1526      * Convert IMAP flag to keyword value.
1527      *
1528      * @param value IMAP flag
1529      * @return keyword value
1530      */
1531     public String convertFlagToKeyword(String value) {
1532         // first test for flag in settings
1533         Properties flagSettings = Settings.getSubProperties("davmail.imapFlags");
1534         String flagValue = flagSettings.getProperty(value);
1535         if (flagValue != null) {
1536             return flagValue;
1537         }
1538 
1539         // fall back to predefined flags
1540         ResourceBundle flagBundle = ResourceBundle.getBundle("imapflags");
1541         try {
1542             return flagBundle.getString(value);
1543         } catch (MissingResourceException e) {
1544             // ignore
1545         }
1546 
1547         // fall back to raw value
1548         return value;
1549     }
1550 
1551     /**
1552      * Exchange folder with IMAP properties
1553      */
1554     public class Folder {
1555         /**
1556          * Logical (IMAP) folder path.
1557          */
1558         public String folderPath;
1559 
1560         /**
1561          * Display Name.
1562          */
1563         public String displayName;
1564         /**
1565          * Folder class (PR_CONTAINER_CLASS).
1566          */
1567         public String folderClass;
1568         /**
1569          * Folder message count.
1570          */
1571         public int count;
1572         /**
1573          * Folder unread message count.
1574          */
1575         public int unreadCount;
1576         /**
1577          * true if folder has subfolders (DAV:hassubs).
1578          */
1579         public boolean hasChildren;
1580         /**
1581          * true if folder has no subfolders (DAV:nosubs).
1582          */
1583         public boolean noInferiors;
1584         /**
1585          * Folder content tag (to detect folder content changes).
1586          */
1587         public String ctag;
1588         /**
1589          * Folder etag (to detect folder object changes).
1590          */
1591         public String etag;
1592         /**
1593          * Next IMAP uid
1594          */
1595         public long uidNext;
1596         /**
1597          * recent count
1598          */
1599         public int recent;
1600 
1601         /**
1602          * Folder message list, empty before loadMessages call.
1603          */
1604         public ExchangeSession.MessageList messages;
1605         /**
1606          * Permanent uid (PR_SEARCH_KEY) to IMAP UID map.
1607          */
1608         private final HashMap<String, Long> permanentUrlToImapUidMap = new HashMap<String, Long>();
1609 
1610         /**
1611          * Get IMAP folder flags.
1612          *
1613          * @return folder flags in IMAP format
1614          */
1615         public String getFlags() {
1616             if (noInferiors) {
1617                 return "\\NoInferiors";
1618             } else if (hasChildren) {
1619                 return "\\HasChildren";
1620             } else {
1621                 return "\\HasNoChildren";
1622             }
1623         }
1624 
1625         /**
1626          * Load folder messages.
1627          *
1628          * @throws IOException on error
1629          */
1630         public void loadMessages() throws IOException {
1631             messages = ExchangeSession.this.searchMessages(folderPath, null);
1632             fixUids(messages);
1633             recent = 0;
1634             for (Message message : messages) {
1635                 if (message.recent) {
1636                     recent++;
1637                 }
1638             }
1639             long computedUidNext = 1;
1640             if (!messages.isEmpty()) {
1641                 computedUidNext = messages.get(messages.size() - 1).getImapUid() + 1;
1642             }
1643             if (computedUidNext > uidNext) {
1644                 uidNext = computedUidNext;
1645             }
1646         }
1647 
1648         /**
1649          * Search messages in folder matching query.
1650          *
1651          * @param condition search query
1652          * @return message list
1653          * @throws IOException on error
1654          */
1655         public MessageList searchMessages(Condition condition) throws IOException {
1656             MessageList localMessages = ExchangeSession.this.searchMessages(folderPath, condition);
1657             fixUids(localMessages);
1658             return localMessages;
1659         }
1660 
1661         /**
1662          * Restore previous uids changed by a PROPPATCH (flag change).
1663          *
1664          * @param messages message list
1665          */
1666         protected void fixUids(MessageList messages) {
1667             boolean sortNeeded = false;
1668             for (Message message : messages) {
1669                 if (permanentUrlToImapUidMap.containsKey(message.getPermanentId())) {
1670                     long previousUid = permanentUrlToImapUidMap.get(message.getPermanentId());
1671                     if (message.getImapUid() != previousUid) {
1672                         LOGGER.debug("Restoring IMAP uid " + message.getImapUid() + " -> " + previousUid + " for message " + message.getPermanentId());
1673                         message.setImapUid(previousUid);
1674                         sortNeeded = true;
1675                     }
1676                 } else {
1677                     // add message to uid map
1678                     permanentUrlToImapUidMap.put(message.getPermanentId(), message.getImapUid());
1679                 }
1680             }
1681             if (sortNeeded) {
1682                 Collections.sort(messages);
1683             }
1684         }
1685 
1686         /**
1687          * Folder message count.
1688          *
1689          * @return message count
1690          */
1691         public int count() {
1692             if (messages == null) {
1693                 return count;
1694             } else {
1695                 return messages.size();
1696             }
1697         }
1698 
1699         /**
1700          * Compute IMAP uidnext.
1701          *
1702          * @return max(messageuids)+1
1703          */
1704         public long getUidNext() {
1705             return uidNext;
1706         }
1707 
1708         /**
1709          * Get message at index.
1710          *
1711          * @param index message index
1712          * @return message
1713          */
1714         public Message get(int index) {
1715             return messages.get(index);
1716         }
1717 
1718         /**
1719          * Get current folder messages imap uids and flags
1720          *
1721          * @return imap uid list
1722          */
1723         public TreeMap<Long, String> getImapFlagMap() {
1724             TreeMap<Long, String> imapFlagMap = new TreeMap<Long, String>();
1725             for (ExchangeSession.Message message : messages) {
1726                 imapFlagMap.put(message.getImapUid(), message.getImapFlags());
1727             }
1728             return imapFlagMap;
1729         }
1730 
1731         /**
1732          * Calendar folder flag.
1733          *
1734          * @return true if this is a calendar folder
1735          */
1736         public boolean isCalendar() {
1737             return "IPF.Appointment".equals(folderClass);
1738         }
1739 
1740         /**
1741          * Contact folder flag.
1742          *
1743          * @return true if this is a calendar folder
1744          */
1745         public boolean isContact() {
1746             return "IPF.Contact".equals(folderClass);
1747         }
1748 
1749         /**
1750          * Task folder flag.
1751          *
1752          * @return true if this is a task folder
1753          */
1754         public boolean isTask() {
1755             return "IPF.Task".equals(folderClass);
1756         }
1757 
1758         /**
1759          * drop cached message
1760          */
1761         public void clearCache() {
1762             messages.cachedMimeBody = null;
1763             messages.cachedMimeMessage = null;
1764             messages.cachedMessageImapUid = 0;
1765         }
1766     }
1767 
1768     /**
1769      * Exchange message.
1770      */
1771     public abstract class Message implements Comparable<Message> {
1772         /**
1773          * enclosing message list
1774          */
1775         public MessageList messageList;
1776         /**
1777          * Message url.
1778          */
1779         public String messageUrl;
1780         /**
1781          * Message permanent url (does not change on message move).
1782          */
1783         public String permanentUrl;
1784         /**
1785          * Message uid.
1786          */
1787         public String uid;
1788         /**
1789          * Message content class.
1790          */
1791         public String contentClass;
1792         /**
1793          * Message keywords (categories).
1794          */
1795         public String keywords;
1796         /**
1797          * Message IMAP uid, unique in folder (x0e230003).
1798          */
1799         public long imapUid;
1800         /**
1801          * MAPI message size.
1802          */
1803         public int size;
1804         /**
1805          * Message date (urn:schemas:mailheader:date).
1806          */
1807         public String date;
1808 
1809         /**
1810          * Message flag: read.
1811          */
1812         public boolean read;
1813         /**
1814          * Message flag: deleted.
1815          */
1816         public boolean deleted;
1817         /**
1818          * Message flag: junk.
1819          */
1820         public boolean junk;
1821         /**
1822          * Message flag: flagged.
1823          */
1824         public boolean flagged;
1825         /**
1826          * Message flag: recent.
1827          */
1828         public boolean recent;
1829         /**
1830          * Message flag: draft.
1831          */
1832         public boolean draft;
1833         /**
1834          * Message flag: answered.
1835          */
1836         public boolean answered;
1837         /**
1838          * Message flag: fowarded.
1839          */
1840         public boolean forwarded;
1841 
1842         /**
1843          * Unparsed message content.
1844          */
1845         protected SharedByteArrayInputStream mimeBody;
1846 
1847         /**
1848          * Message content parsed in a MIME message.
1849          */
1850         protected MimeMessage mimeMessage;
1851 
1852         /**
1853          * Get permanent message id.
1854          * permanentUrl over WebDav or IitemId over EWS
1855          *
1856          * @return permanent id
1857          */
1858         public abstract String getPermanentId();
1859 
1860         /**
1861          * IMAP uid , unique in folder (x0e230003)
1862          *
1863          * @return IMAP uid
1864          */
1865         public long getImapUid() {
1866             return imapUid;
1867         }
1868 
1869         /**
1870          * Set IMAP uid.
1871          *
1872          * @param imapUid new uid
1873          */
1874         public void setImapUid(long imapUid) {
1875             this.imapUid = imapUid;
1876         }
1877 
1878         /**
1879          * Exchange uid.
1880          *
1881          * @return uid
1882          */
1883         public String getUid() {
1884             return uid;
1885         }
1886 
1887         /**
1888          * Return message flags in IMAP format.
1889          *
1890          * @return IMAP flags
1891          */
1892         public String getImapFlags() {
1893             StringBuilder buffer = new StringBuilder();
1894             if (read) {
1895                 buffer.append("\\Seen ");
1896             }
1897             if (deleted) {
1898                 buffer.append("\\Deleted ");
1899             }
1900             if (recent) {
1901                 buffer.append("\\Recent ");
1902             }
1903             if (flagged) {
1904                 buffer.append("\\Flagged ");
1905             }
1906             if (junk) {
1907                 buffer.append("Junk ");
1908             }
1909             if (draft) {
1910                 buffer.append("\\Draft ");
1911             }
1912             if (answered) {
1913                 buffer.append("\\Answered ");
1914             }
1915             if (forwarded) {
1916                 buffer.append("$Forwarded ");
1917             }
1918             if (keywords != null) {
1919                 for (String keyword : keywords.split(",")) {
1920                     buffer.append(convertKeywordToFlag(keyword)).append(" ");
1921                 }
1922             }
1923             return buffer.toString().trim();
1924         }
1925 
1926         /**
1927          * Load message content in a Mime message
1928          *
1929          * @throws IOException        on error
1930          * @throws MessagingException on error
1931          */
1932         public void loadMimeMessage() throws IOException, MessagingException {
1933             if (mimeMessage == null) {
1934                 // try to get message content from cache
1935                 if (this.imapUid == messageList.cachedMessageImapUid) {
1936                     mimeBody = messageList.cachedMimeBody;
1937                     mimeMessage = messageList.cachedMimeMessage;
1938                     LOGGER.debug("Got message content for " + imapUid + " from cache");
1939                 } else {
1940                     // load and parse message
1941                     mimeBody = new SharedByteArrayInputStream(getContent(this));
1942                     mimeMessage = new MimeMessage(null, mimeBody);
1943                     mimeBody.reset();
1944                     // workaround for Exchange 2003 ActiveSync bug
1945                     if (mimeMessage.getHeader("MAIL FROM") != null) {
1946                         mimeBody = (SharedByteArrayInputStream) mimeMessage.getRawInputStream();
1947                         mimeMessage = new MimeMessage(null, mimeBody);
1948                         mimeBody.reset();
1949                     }
1950                     LOGGER.debug("Downloaded full message content for IMAP UID " + imapUid + " (" + mimeBody.available() + " bytes)");
1951                 }
1952             }
1953         }
1954 
1955         /**
1956          * Get message content as a Mime message.
1957          *
1958          * @return mime message
1959          * @throws IOException        on error
1960          * @throws MessagingException on error
1961          */
1962         public MimeMessage getMimeMessage() throws IOException, MessagingException {
1963             loadMimeMessage();
1964             return mimeMessage;
1965         }
1966 
1967         public Enumeration getMatchingHeaderLinesFromHeaders(String[] headerNames) throws MessagingException, IOException {
1968             Enumeration result = null;
1969             if (mimeMessage == null) {
1970                 // message not loaded, try to get headers only
1971                 InputStream headers = getMimeHeaders();
1972                 if (headers != null) {
1973                     InternetHeaders internetHeaders = new InternetHeaders(headers);
1974                     if (internetHeaders.getHeader("Subject") == null) {
1975                         // invalid header content
1976                         return null;
1977                     }
1978                     if (headerNames == null) {
1979                         result = internetHeaders.getAllHeaderLines();
1980                     } else {
1981                         result = internetHeaders.getMatchingHeaderLines(headerNames);
1982                     }
1983                 }
1984             }
1985             return result;
1986         }
1987 
1988         public Enumeration getMatchingHeaderLines(String[] headerNames) throws MessagingException, IOException {
1989             Enumeration result = getMatchingHeaderLinesFromHeaders(headerNames);
1990             if (result == null) {
1991                 if (headerNames == null) {
1992                     result = getMimeMessage().getAllHeaderLines();
1993                 } else {
1994                     result = getMimeMessage().getMatchingHeaderLines(headerNames);
1995                 }
1996 
1997             }
1998             return result;
1999         }
2000 
2001         protected abstract InputStream getMimeHeaders();
2002 
2003         /**
2004          * Get message body size.
2005          *
2006          * @return mime message size
2007          * @throws IOException        on error
2008          * @throws MessagingException on error
2009          */
2010         public int getMimeMessageSize() throws IOException, MessagingException {
2011             loadMimeMessage();
2012             mimeBody.reset();
2013             return mimeBody.available();
2014         }
2015 
2016         /**
2017          * Get message body input stream.
2018          *
2019          * @return mime message InputStream
2020          * @throws IOException        on error
2021          * @throws MessagingException on error
2022          */
2023         public InputStream getRawInputStream() throws IOException, MessagingException {
2024             loadMimeMessage();
2025             mimeBody.reset();
2026             return mimeBody;
2027         }
2028 
2029 
2030         /**
2031          * Drop mime message to avoid keeping message content in memory,
2032          * keep a single message in MessageList cache to handle chunked fetch.
2033          */
2034         public void dropMimeMessage() {
2035             // update single message cache
2036             if (mimeMessage != null) {
2037                 messageList.cachedMessageImapUid = imapUid;
2038                 messageList.cachedMimeBody = mimeBody;
2039                 messageList.cachedMimeMessage = mimeMessage;
2040             }
2041             // drop curent message body to save memory
2042             mimeMessage = null;
2043             mimeBody = null;
2044         }
2045 
2046         public boolean isLoaded() {
2047             return mimeMessage != null;
2048         }
2049 
2050         /**
2051          * Delete message.
2052          *
2053          * @throws IOException on error
2054          */
2055         public void delete() throws IOException {
2056             deleteMessage(this);
2057         }
2058 
2059         /**
2060          * Move message to trash, mark message read.
2061          *
2062          * @throws IOException on error
2063          */
2064         public void moveToTrash() throws IOException {
2065             markRead();
2066 
2067             ExchangeSession.this.moveToTrash(this);
2068         }
2069 
2070         /**
2071          * Mark message as read.
2072          *
2073          * @throws IOException on error
2074          */
2075         public void markRead() throws IOException {
2076             HashMap<String, String> properties = new HashMap<String, String>();
2077             properties.put("read", "1");
2078             updateMessage(this, properties);
2079         }
2080 
2081         /**
2082          * Comparator to sort messages by IMAP uid
2083          *
2084          * @param message other message
2085          * @return imapUid comparison result
2086          */
2087         public int compareTo(Message message) {
2088             long compareValue = (imapUid - message.imapUid);
2089             if (compareValue > 0) {
2090                 return 1;
2091             } else if (compareValue < 0) {
2092                 return -1;
2093             } else {
2094                 return 0;
2095             }
2096         }
2097 
2098         /**
2099          * Override equals, compare IMAP uids
2100          *
2101          * @param message other message
2102          * @return true if IMAP uids are equal
2103          */
2104         @Override
2105         public boolean equals(Object message) {
2106             return message instanceof Message && imapUid == ((Message) message).imapUid;
2107         }
2108 
2109         /**
2110          * Override hashCode, return imapUid hashcode.
2111          *
2112          * @return imapUid hashcode
2113          */
2114         @Override
2115         public int hashCode() {
2116             return (int) (imapUid ^ (imapUid >>> 32));
2117         }
2118 
2119         public String removeFlag(String flag) {
2120             if (keywords != null) {
2121                 final String exchangeFlag = convertFlagToKeyword(flag);
2122                 Set<String> keywordSet = new HashSet<String>();
2123                 String[] keywordArray = keywords.split(",");
2124                 for (String value : keywordArray) {
2125                     if (!value.equalsIgnoreCase(exchangeFlag)) {
2126                         keywordSet.add(value);
2127                     }
2128                 }
2129                 keywords = StringUtil.join(keywordSet, ",");
2130             }
2131             return keywords;
2132         }
2133 
2134         public String addFlag(String flag) {
2135             final String exchangeFlag = convertFlagToKeyword(flag);
2136             HashSet<String> keywordSet = new HashSet<String>();
2137             boolean hasFlag = false;
2138             if (keywords != null) {
2139                 String[] keywordArray = keywords.split(",");
2140                 for (String value : keywordArray) {
2141                     keywordSet.add(value);
2142                     if (value.equalsIgnoreCase(exchangeFlag)) {
2143                         hasFlag = true;
2144                     }
2145                 }
2146             }
2147             if (!hasFlag) {
2148                 keywordSet.add(exchangeFlag);
2149             }
2150             keywords = StringUtil.join(keywordSet, ",");
2151             return keywords;
2152         }
2153 
2154         public String setFlags(HashSet<String> flags) {
2155             HashSet<String> keywordSet = new HashSet<String>();
2156             for (String flag : flags) {
2157                 keywordSet.add(convertFlagToKeyword(flag));
2158             }
2159             keywords = StringUtil.join(keywordSet, ",");
2160             return keywords;
2161         }
2162 
2163     }
2164 
2165     /**
2166      * Message list, includes a single messsage cache
2167      */
2168     public static class MessageList extends ArrayList<Message> {
2169         /**
2170          * Cached message content parsed in a MIME message.
2171          */
2172         protected transient MimeMessage cachedMimeMessage;
2173         /**
2174          * Cached message uid.
2175          */
2176         protected transient long cachedMessageImapUid;
2177         /**
2178          * Cached unparsed message
2179          */
2180         protected transient SharedByteArrayInputStream cachedMimeBody;
2181 
2182     }
2183 
2184     /**
2185      * Generic folder item.
2186      */
2187     public abstract static class Item extends HashMap<String, String> {
2188         protected String folderPath;
2189         protected String itemName;
2190         protected String permanentUrl;
2191         /**
2192          * Display name.
2193          */
2194         public String displayName;
2195         /**
2196          * item etag
2197          */
2198         public String etag;
2199         protected String noneMatch;
2200 
2201         /**
2202          * Build item instance.
2203          *
2204          * @param folderPath folder path
2205          * @param itemName   item name class
2206          * @param etag       item etag
2207          * @param noneMatch  none match flag
2208          */
2209         public Item(String folderPath, String itemName, String etag, String noneMatch) {
2210             this.folderPath = folderPath;
2211             this.itemName = itemName;
2212             this.etag = etag;
2213             this.noneMatch = noneMatch;
2214         }
2215 
2216         /**
2217          * Default constructor.
2218          */
2219         protected Item() {
2220         }
2221 
2222         /**
2223          * Return item content type
2224          *
2225          * @return content type
2226          */
2227         public abstract String getContentType();
2228 
2229         /**
2230          * Retrieve item body from Exchange
2231          *
2232          * @return item body
2233          * @throws HttpException on error
2234          */
2235         public abstract String getBody() throws IOException;
2236 
2237         /**
2238          * Get event name (file name part in URL).
2239          *
2240          * @return event name
2241          */
2242         public String getName() {
2243             return itemName;
2244         }
2245 
2246         /**
2247          * Get event etag (last change tag).
2248          *
2249          * @return event etag
2250          */
2251         public String getEtag() {
2252             return etag;
2253         }
2254 
2255         /**
2256          * Set item href.
2257          *
2258          * @param href item href
2259          */
2260         public void setHref(String href) {
2261             int index = href.lastIndexOf('/');
2262             if (index >= 0) {
2263                 folderPath = href.substring(0, index);
2264                 itemName = href.substring(index + 1);
2265             } else {
2266                 throw new IllegalArgumentException(href);
2267             }
2268         }
2269 
2270         /**
2271          * Return item href.
2272          *
2273          * @return item href
2274          */
2275         public String getHref() {
2276             return folderPath + '/' + itemName;
2277         }
2278     }
2279 
2280     /**
2281      * Contact object
2282      */
2283     public abstract class Contact extends Item {
2284 
2285         /**
2286          * @inheritDoc
2287          */
2288         public Contact(String folderPath, String itemName, Map<String, String> properties, String etag, String noneMatch) {
2289             super(folderPath, itemName.endsWith(".vcf") ? itemName.substring(0, itemName.length() - 3) + "EML" : itemName, etag, noneMatch);
2290             this.putAll(properties);
2291         }
2292 
2293         /**
2294          * @inheritDoc
2295          */
2296         protected Contact() {
2297         }
2298 
2299         /**
2300          * Convert EML extension to vcf.
2301          *
2302          * @return item name
2303          */
2304         @Override
2305         public String getName() {
2306             String name = super.getName();
2307             if (name.endsWith(".EML")) {
2308                 name = name.substring(0, name.length() - 3) + "vcf";
2309             }
2310             return name;
2311         }
2312 
2313         /**
2314          * Set contact name
2315          *
2316          * @param name contact name
2317          */
2318         public void setName(String name) {
2319             this.itemName = name;
2320         }
2321 
2322         /**
2323          * Compute vcard uid from name.
2324          *
2325          * @return uid
2326          * @throws URIException on error
2327          */
2328         protected String getUid() throws URIException {
2329             String uid = getName();
2330             int dotIndex = uid.lastIndexOf('.');
2331             if (dotIndex > 0) {
2332                 uid = uid.substring(0, dotIndex);
2333             }
2334             return URIUtil.encodePath(uid);
2335         }
2336 
2337         @Override
2338         public String getContentType() {
2339             return "text/vcard";
2340         }
2341 
2342 
2343         @Override
2344         public String getBody() throws HttpException {
2345             // build RFC 2426 VCard from contact information
2346             VCardWriter writer = new VCardWriter();
2347             writer.startCard();
2348             writer.appendProperty("UID", getUid());
2349             // common name
2350             writer.appendProperty("FN", get("cn"));
2351             // RFC 2426: Family Name, Given Name, Additional Names, Honorific Prefixes, and Honorific Suffixes
2352             writer.appendProperty("N", get("sn"), get("givenName"), get("middlename"), get("personaltitle"), get("namesuffix"));
2353 
2354             writer.appendProperty("TEL;TYPE=cell", get("mobile"));
2355             writer.appendProperty("TEL;TYPE=work", get("telephoneNumber"));
2356             writer.appendProperty("TEL;TYPE=home", get("homePhone"));
2357             writer.appendProperty("TEL;TYPE=fax", get("facsimiletelephonenumber"));
2358             writer.appendProperty("TEL;TYPE=pager", get("pager"));
2359             writer.appendProperty("TEL;TYPE=car", get("othermobile"));
2360             writer.appendProperty("TEL;TYPE=home,fax", get("homefax"));
2361             writer.appendProperty("TEL;TYPE=isdn", get("internationalisdnnumber"));
2362             writer.appendProperty("TEL;TYPE=msg", get("otherTelephone"));
2363 
2364             // The structured type value corresponds, in sequence, to the post office box; the extended address;
2365             // the street address; the locality (e.g., city); the region (e.g., state or province);
2366             // the postal code; the country name
2367             writer.appendProperty("ADR;TYPE=home",
2368                     get("homepostofficebox"), null, get("homeStreet"), get("homeCity"), get("homeState"), get("homePostalCode"), get("homeCountry"));
2369             writer.appendProperty("ADR;TYPE=work",
2370                     get("postofficebox"), get("roomnumber"), get("street"), get("l"), get("st"), get("postalcode"), get("co"));
2371             writer.appendProperty("ADR;TYPE=other",
2372                     get("otherpostofficebox"), null, get("otherstreet"), get("othercity"), get("otherstate"), get("otherpostalcode"), get("othercountry"));
2373 
2374             writer.appendProperty("EMAIL;TYPE=work", get("smtpemail1"));
2375             writer.appendProperty("EMAIL;TYPE=home", get("smtpemail2"));
2376             writer.appendProperty("EMAIL;TYPE=other", get("smtpemail3"));
2377 
2378             writer.appendProperty("ORG", get("o"), get("department"));
2379             writer.appendProperty("URL;TYPE=work", get("businesshomepage"));
2380             writer.appendProperty("URL;TYPE=home", get("personalHomePage"));
2381             writer.appendProperty("TITLE", get("title"));
2382             writer.appendProperty("NOTE", get("description"));
2383 
2384             writer.appendProperty("CUSTOM1", get("extensionattribute1"));
2385             writer.appendProperty("CUSTOM2", get("extensionattribute2"));
2386             writer.appendProperty("CUSTOM3", get("extensionattribute3"));
2387             writer.appendProperty("CUSTOM4", get("extensionattribute4"));
2388 
2389             writer.appendProperty("ROLE", get("profession"));
2390             writer.appendProperty("NICKNAME", get("nickname"));
2391             writer.appendProperty("X-AIM", get("im"));
2392 
2393             writer.appendProperty("BDAY", convertZuluDateToBday(get("bday")));
2394             writer.appendProperty("ANNIVERSARY", convertZuluDateToBday(get("anniversary")));
2395 
2396             String gender = get("gender");
2397             if ("1".equals(gender)) {
2398                 writer.appendProperty("SEX", "2");
2399             } else if ("2".equals(gender)) {
2400                 writer.appendProperty("SEX", "1");
2401             }
2402 
2403             writer.appendProperty("CATEGORIES", get("keywords"));
2404 
2405             writer.appendProperty("FBURL", get("fburl"));
2406 
2407             if ("1".equals(get("private"))) {
2408                 writer.appendProperty("CLASS", "PRIVATE");
2409             }
2410 
2411             writer.appendProperty("X-ASSISTANT", get("secretarycn"));
2412             writer.appendProperty("X-MANAGER", get("manager"));
2413             writer.appendProperty("X-SPOUSE", get("spousecn"));
2414 
2415             writer.appendProperty("REV", get("lastmodified"));
2416 
2417             if ("true".equals(get("haspicture"))) {
2418                 try {
2419                     ContactPhoto contactPhoto = getContactPhoto(this);
2420                     writer.writeLine("PHOTO;BASE64;TYPE=\"" + contactPhoto.contentType + "\";ENCODING=\"b\":");
2421                     writer.writeLine(contactPhoto.content, true);
2422                 } catch (IOException e) {
2423                     LOGGER.warn("Unable to get photo from contact " + this.get("cn"));
2424                 }
2425             }
2426 
2427             writer.endCard();
2428             return writer.toString();
2429         }
2430 
2431     }
2432 
2433     /**
2434      * Calendar event object.
2435      */
2436     public abstract class Event extends Item {
2437         protected String contentClass;
2438         protected String subject;
2439         protected VCalendar vCalendar;
2440 
2441         /**
2442          * @inheritDoc
2443          */
2444         public Event(String folderPath, String itemName, String contentClass, String itemBody, String etag, String noneMatch) throws IOException {
2445             super(folderPath, itemName, etag, noneMatch);
2446             this.contentClass = contentClass;
2447             fixICS(itemBody.getBytes("UTF-8"), false);
2448             // fix task item name
2449             if (vCalendar.isTodo() && this.itemName.endsWith(".ics")) {
2450                 this.itemName = itemName.substring(0, itemName.length() - 3) + "EML";
2451             }
2452         }
2453 
2454         /**
2455          * @inheritDoc
2456          */
2457         protected Event() {
2458         }
2459 
2460         @Override
2461         public String getContentType() {
2462             return "text/calendar;charset=UTF-8";
2463         }
2464 
2465         @Override
2466         public String getBody() throws IOException {
2467             if (vCalendar == null) {
2468                 fixICS(getEventContent(), true);
2469             }
2470             return vCalendar.toString();
2471         }
2472 
2473         protected HttpException buildHttpException(Exception e) {
2474             String message = "Unable to get event " + getName() + " subject: " + subject + " at " + permanentUrl + ": " + e.getMessage();
2475             LOGGER.warn(message);
2476             return new HttpException(message);
2477         }
2478 
2479         /**
2480          * Retrieve item body from Exchange
2481          *
2482          * @return item content
2483          * @throws HttpException on error
2484          */
2485         public abstract byte[] getEventContent() throws IOException;
2486 
2487         protected static final String TEXT_CALENDAR = "text/calendar";
2488         protected static final String APPLICATION_ICS = "application/ics";
2489 
2490         protected boolean isCalendarContentType(String contentType) {
2491             return TEXT_CALENDAR.regionMatches(true, 0, contentType, 0, TEXT_CALENDAR.length()) ||
2492                     APPLICATION_ICS.regionMatches(true, 0, contentType, 0, APPLICATION_ICS.length());
2493         }
2494 
2495         protected MimePart getCalendarMimePart(MimeMultipart multiPart) throws IOException, MessagingException {
2496             MimePart bodyPart = null;
2497             for (int i = 0; i < multiPart.getCount(); i++) {
2498                 String contentType = multiPart.getBodyPart(i).getContentType();
2499                 if (isCalendarContentType(contentType)) {
2500                     bodyPart = (MimePart) multiPart.getBodyPart(i);
2501                     break;
2502                 } else if (contentType.startsWith("multipart")) {
2503                     Object content = multiPart.getBodyPart(i).getContent();
2504                     if (content instanceof MimeMultipart) {
2505                         bodyPart = getCalendarMimePart((MimeMultipart) content);
2506                     }
2507                 }
2508             }
2509 
2510             return bodyPart;
2511         }
2512 
2513         /**
2514          * Load ICS content from MIME message input stream
2515          *
2516          * @param mimeInputStream mime message input stream
2517          * @return mime message ics attachment body
2518          * @throws IOException        on error
2519          * @throws MessagingException on error
2520          */
2521         protected byte[] getICS(InputStream mimeInputStream) throws IOException, MessagingException {
2522             byte[] result;
2523             MimeMessage mimeMessage = new MimeMessage(null, mimeInputStream);
2524             String[] contentClassHeader = mimeMessage.getHeader("Content-class");
2525             // task item, return null
2526             if (contentClassHeader != null && contentClassHeader.length > 0 && "urn:content-classes:task".equals(contentClassHeader[0])) {
2527                 return null;
2528             }
2529             Object mimeBody = mimeMessage.getContent();
2530             MimePart bodyPart = null;
2531             if (mimeBody instanceof MimeMultipart) {
2532                 bodyPart = getCalendarMimePart((MimeMultipart) mimeBody);
2533             } else if (isCalendarContentType(mimeMessage.getContentType())) {
2534                 // no multipart, single body
2535                 bodyPart = mimeMessage;
2536             }
2537 
2538             if (bodyPart != null) {
2539                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
2540                 bodyPart.getDataHandler().writeTo(baos);
2541                 baos.close();
2542                 result = baos.toByteArray();
2543             } else {
2544                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
2545                 mimeMessage.writeTo(baos);
2546                 baos.close();
2547                 throw new DavMailException("EXCEPTION_INVALID_MESSAGE_CONTENT", new String(baos.toByteArray(), "UTF-8"));
2548             }
2549             return result;
2550         }
2551 
2552         protected void fixICS(byte[] icsContent, boolean fromServer) throws IOException {
2553             if (LOGGER.isDebugEnabled() && fromServer) {
2554                 dumpIndex++;
2555                 String icsBody = new String(icsContent, "UTF-8");
2556                 dumpICS(icsBody, fromServer, false);
2557                 LOGGER.debug("Vcalendar body received from server:\n" + icsBody);
2558             }
2559             vCalendar = new VCalendar(icsContent, getEmail(), getVTimezone());
2560             vCalendar.fixVCalendar(fromServer);
2561             if (LOGGER.isDebugEnabled() && !fromServer) {
2562                 String resultString = vCalendar.toString();
2563                 LOGGER.debug("Fixed Vcalendar body to server:\n" + resultString);
2564                 dumpICS(resultString, fromServer, true);
2565             }
2566         }
2567 
2568         protected void dumpICS(String icsBody, boolean fromServer, boolean after) {
2569             String logFileDirectory = Settings.getLogFileDirectory();
2570 
2571             // additional setting to activate ICS dump (not available in GUI)
2572             int dumpMax = Settings.getIntProperty("davmail.dumpICS");
2573             if (dumpMax > 0) {
2574                 if (dumpIndex > dumpMax) {
2575                     // Delete the oldest dump file
2576                     final int oldest = dumpIndex - dumpMax;
2577                     try {
2578                         File[] oldestFiles = (new File(logFileDirectory)).listFiles(new FilenameFilter() {
2579                             public boolean accept(File dir, String name) {
2580                                 if (name.endsWith(".ics")) {
2581                                     int dashIndex = name.indexOf('-');
2582                                     if (dashIndex > 0) {
2583                                         try {
2584                                             int fileIndex = Integer.parseInt(name.substring(0, dashIndex));
2585                                             return fileIndex < oldest;
2586                                         } catch (NumberFormatException nfe) {
2587                                             // ignore
2588                                         }
2589                                     }
2590                                 }
2591                                 return false;
2592                             }
2593                         });
2594                         if (oldestFiles != null) {
2595                             for (File file : oldestFiles) {
2596                                 if (!file.delete()) {
2597                                     LOGGER.warn("Unable to delete " + file.getAbsolutePath());
2598                                 }
2599                             }
2600                         }
2601                     } catch (Exception ex) {
2602                         LOGGER.warn("Error deleting ics dump: " + ex.getMessage());
2603                     }
2604                 }
2605 
2606                 StringBuilder filePath = new StringBuilder();
2607                 filePath.append(logFileDirectory).append('/')
2608                         .append(dumpIndex)
2609                         .append(after ? "-to" : "-from")
2610                         .append((after ^ fromServer) ? "-server" : "-client")
2611                         .append(".ics");
2612                 if ((icsBody != null) && (icsBody.length() > 0)) {
2613                     OutputStreamWriter writer = null;
2614                     try {
2615                         writer = new OutputStreamWriter(new FileOutputStream(filePath.toString()), "UTF-8");
2616                         writer.write(icsBody);
2617                     } catch (IOException e) {
2618                         LOGGER.error(e);
2619                     } finally {
2620                         if (writer != null) {
2621                             try {
2622                                 writer.close();
2623                             } catch (IOException e) {
2624                                 LOGGER.error(e);
2625                             }
2626                         }
2627                     }
2628 
2629 
2630                 }
2631             }
2632 
2633         }
2634 
2635         /**
2636          * Build Mime body for event or event message.
2637          *
2638          * @return mimeContent as byte array or null
2639          * @throws IOException on error
2640          */
2641         public byte[] createMimeContent() throws IOException {
2642             String boundary = UUID.randomUUID().toString();
2643             ByteArrayOutputStream baos = new ByteArrayOutputStream();
2644             MimeOutputStreamWriter writer = new MimeOutputStreamWriter(baos);
2645 
2646             writer.writeHeader("Content-Transfer-Encoding", "7bit");
2647             writer.writeHeader("Content-class", contentClass);
2648             // append date
2649             writer.writeHeader("Date", new Date());
2650 
2651             // Make sure invites have a proper subject line
2652             String vEventSubject = vCalendar.getFirstVeventPropertyValue("SUMMARY");
2653             if (vEventSubject == null) {
2654                 vEventSubject = BundleMessage.format("MEETING_REQUEST");
2655             }
2656 
2657             // Write a part of the message that contains the
2658             // ICS description so that invites contain the description text
2659             String description = vCalendar.getFirstVeventPropertyValue("DESCRIPTION");
2660 
2661             // handle notifications
2662             if ("urn:content-classes:calendarmessage".equals(contentClass)) {
2663                 // need to parse attendees and organizer to build recipients
2664                 VCalendar.Recipients recipients = vCalendar.getRecipients(true);
2665                 String to;
2666                 String cc;
2667                 String notificationSubject;
2668                 if (email.equalsIgnoreCase(recipients.organizer)) {
2669                     // current user is organizer => notify all
2670                     to = recipients.attendees;
2671                     cc = recipients.optionalAttendees;
2672                     notificationSubject = subject;
2673                 } else {
2674                     String status = vCalendar.getAttendeeStatus();
2675                     // notify only organizer
2676                     to = recipients.organizer;
2677                     cc = null;
2678                     notificationSubject = (status != null) ? (BundleMessage.format(status) + vEventSubject) : subject;
2679                     description = "";
2680                 }
2681 
2682                 // Allow end user notification edit
2683                 if (Settings.getBooleanProperty("davmail.caldavEditNotifications")) {
2684                     // create notification edit dialog
2685                     NotificationDialog notificationDialog = new NotificationDialog(to,
2686                             cc, notificationSubject, description);
2687                     if (!notificationDialog.getSendNotification()) {
2688                         LOGGER.debug("Notification canceled by user");
2689                         return null;
2690                     }
2691                     // get description from dialog
2692                     to = notificationDialog.getTo();
2693                     cc = notificationDialog.getCc();
2694                     notificationSubject = notificationDialog.getSubject();
2695                     description = notificationDialog.getBody();
2696                 }
2697 
2698                 // do not send notification if no recipients found
2699                 if ((to == null || to.length() == 0) && (cc == null || cc.length() == 0)) {
2700                     return null;
2701                 }
2702 
2703                 writer.writeHeader("To", to);
2704                 writer.writeHeader("Cc", cc);
2705                 writer.writeHeader("Subject", notificationSubject);
2706 
2707 
2708                 if (LOGGER.isDebugEnabled()) {
2709                     StringBuilder logBuffer = new StringBuilder("Sending notification ");
2710                     if (to != null) {
2711                         logBuffer.append("to: ").append(to);
2712                     }
2713                     if (cc != null) {
2714                         logBuffer.append("cc: ").append(cc);
2715                     }
2716                     LOGGER.debug(logBuffer.toString());
2717                 }
2718             } else {
2719                 // need to parse attendees and organizer to build recipients
2720                 VCalendar.Recipients recipients = vCalendar.getRecipients(false);
2721                 // storing appointment, full recipients header
2722                 if (recipients.attendees != null) {
2723                     writer.writeHeader("To", recipients.attendees);
2724                 } else {
2725                     // use current user as attendee
2726                     writer.writeHeader("To", email);
2727                 }
2728                 writer.writeHeader("Cc", recipients.optionalAttendees);
2729 
2730                 if (recipients.organizer != null) {
2731                     writer.writeHeader("From", recipients.organizer);
2732                 } else {
2733                     writer.writeHeader("From", email);
2734                 }
2735             }
2736             if (vCalendar.getMethod() == null) {
2737                 vCalendar.setPropertyValue("METHOD", "REQUEST");
2738             }
2739             writer.writeHeader("MIME-Version", "1.0");
2740             writer.writeHeader("Content-Type", "multipart/alternative;\r\n" +
2741                     "\tboundary=\"----=_NextPart_" + boundary + '\"');
2742             writer.writeLn();
2743             writer.writeLn("This is a multi-part message in MIME format.");
2744             writer.writeLn();
2745             writer.writeLn("------=_NextPart_" + boundary);
2746 
2747             if (description != null && description.length() > 0) {
2748                 writer.writeHeader("Content-Type", "text/plain;\r\n" +
2749                         "\tcharset=\"utf-8\"");
2750                 writer.writeHeader("content-transfer-encoding", "8bit");
2751                 writer.writeLn();
2752                 writer.flush();
2753                 baos.write(description.getBytes("UTF-8"));
2754                 writer.writeLn();
2755                 writer.writeLn("------=_NextPart_" + boundary);
2756             }
2757             writer.writeHeader("Content-class", contentClass);
2758             writer.writeHeader("Content-Type", "text/calendar;\r\n" +
2759                     "\tmethod=" + vCalendar.getMethod() + ";\r\n" +
2760                     "\tcharset=\"utf-8\""
2761             );
2762             writer.writeHeader("Content-Transfer-Encoding", "8bit");
2763             writer.writeLn();
2764             writer.flush();
2765             baos.write(vCalendar.toString().getBytes("UTF-8"));
2766             writer.writeLn();
2767             writer.writeLn("------=_NextPart_" + boundary + "--");
2768             writer.close();
2769             return baos.toByteArray();
2770         }
2771 
2772         /**
2773          * Create or update item
2774          *
2775          * @return action result
2776          * @throws IOException on error
2777          */
2778         public abstract ItemResult createOrUpdate() throws IOException;
2779 
2780     }
2781 
2782     protected abstract Set<String> getItemProperties();
2783 
2784     /**
2785      * Search contacts in provided folder.
2786      *
2787      * @param folderPath Exchange folder path
2788      * @return list of contacts
2789      * @throws IOException on error
2790      */
2791     public List<ExchangeSession.Contact> getAllContacts(String folderPath) throws IOException {
2792         return searchContacts(folderPath, ExchangeSession.CONTACT_ATTRIBUTES, isEqualTo("outlookmessageclass", "IPM.Contact"), 0);
2793     }
2794 
2795 
2796     /**
2797      * Search contacts in provided folder matching the search query.
2798      *
2799      * @param folderPath Exchange folder path
2800      * @param attributes requested attributes
2801      * @param condition  Exchange search query
2802      * @param maxCount   maximum item count
2803      * @return list of contacts
2804      * @throws IOException on error
2805      */
2806     public abstract List<Contact> searchContacts(String folderPath, Set<String> attributes, Condition condition, int maxCount) throws IOException;
2807 
2808     /**
2809      * Search calendar messages in provided folder.
2810      *
2811      * @param folderPath Exchange folder path
2812      * @return list of calendar messages as Event objects
2813      * @throws IOException on error
2814      */
2815     public abstract List<Event> getEventMessages(String folderPath) throws IOException;
2816 
2817     /**
2818      * Search calendar events in provided folder.
2819      *
2820      * @param folderPath Exchange folder path
2821      * @return list of calendar events
2822      * @throws IOException on error
2823      */
2824     public List<Event> getAllEvents(String folderPath) throws IOException {
2825         List<Event> results = searchEvents(folderPath, getCalendarItemCondition(getPastDelayCondition("dtstart")));
2826 
2827         if (!Settings.getBooleanProperty("davmail.caldavDisableTasks", false) && isMainCalendar(folderPath)) {
2828             // retrieve tasks from main tasks folder
2829             results.addAll(searchTasksOnly(TASKS));
2830         }
2831 
2832         return results;
2833     }
2834 
2835     protected abstract Condition getCalendarItemCondition(Condition dateCondition);
2836 
2837     protected Condition getPastDelayCondition(String attribute) {
2838         int caldavPastDelay = Settings.getIntProperty("davmail.caldavPastDelay");
2839         Condition dateCondition = null;
2840         if (caldavPastDelay != 0) {
2841             Calendar cal = Calendar.getInstance();
2842             cal.add(Calendar.DAY_OF_MONTH, -caldavPastDelay);
2843             dateCondition = gt(attribute, formatSearchDate(cal.getTime()));
2844         }
2845         return dateCondition;
2846     }
2847 
2848     protected Condition getRangeCondition(String timeRangeStart, String timeRangeEnd) throws IOException {
2849         try {
2850             SimpleDateFormat parser = getZuluDateFormat();
2851             ExchangeSession.MultiCondition andCondition = and();
2852             if (timeRangeStart != null) {
2853                 andCondition.add(gt("dtend", formatSearchDate(parser.parse(timeRangeStart))));
2854             }
2855             if (timeRangeEnd != null) {
2856                 andCondition.add(lt("dtstart", formatSearchDate(parser.parse(timeRangeEnd))));
2857             }
2858             return andCondition;
2859         } catch (ParseException e) {
2860             throw new IOException(e + " " + e.getMessage());
2861         }
2862     }
2863 
2864     /**
2865      * Search events between start and end.
2866      *
2867      * @param folderPath     Exchange folder path
2868      * @param timeRangeStart date range start in zulu format
2869      * @param timeRangeEnd   date range start in zulu format
2870      * @return list of calendar events
2871      * @throws IOException on error
2872      */
2873     public List<Event> searchEvents(String folderPath, String timeRangeStart, String timeRangeEnd) throws IOException {
2874         Condition dateCondition = getRangeCondition(timeRangeStart, timeRangeEnd);
2875         Condition condition = getCalendarItemCondition(dateCondition);
2876 
2877         return searchEvents(folderPath, condition);
2878     }
2879 
2880     /**
2881      * Search events between start and end, exclude tasks.
2882      *
2883      * @param folderPath     Exchange folder path
2884      * @param timeRangeStart date range start in zulu format
2885      * @param timeRangeEnd   date range start in zulu format
2886      * @return list of calendar events
2887      * @throws IOException on error
2888      */
2889     public List<Event> searchEventsOnly(String folderPath, String timeRangeStart, String timeRangeEnd) throws IOException {
2890         Condition dateCondition = getRangeCondition(timeRangeStart, timeRangeEnd);
2891         return searchEvents(folderPath, getCalendarItemCondition(dateCondition));
2892     }
2893 
2894     /**
2895      * Search tasks only (VTODO).
2896      *
2897      * @param folderPath Exchange folder path
2898      * @return list of tasks
2899      * @throws IOException on error
2900      */
2901     public List<Event> searchTasksOnly(String folderPath) throws IOException {
2902         return searchEvents(folderPath, and(isEqualTo("outlookmessageclass", "IPM.Task"),
2903                 or(isNull("datecompleted"), getPastDelayCondition("datecompleted"))));
2904     }
2905 
2906     /**
2907      * Search calendar events in provided folder.
2908      *
2909      * @param folderPath Exchange folder path
2910      * @param filter     search filter
2911      * @return list of calendar events
2912      * @throws IOException on error
2913      */
2914     public List<Event> searchEvents(String folderPath, Condition filter) throws IOException {
2915 
2916         Condition privateCondition = null;
2917         if (isSharedFolder(folderPath) && Settings.getBooleanProperty("davmail.excludePrivateEvents", true)) {
2918             LOGGER.debug("Shared or public calendar: exclude private events");
2919             privateCondition = isEqualTo("sensitivity", 0);
2920         }
2921 
2922         return searchEvents(folderPath, getItemProperties(),
2923                 and(filter, privateCondition));
2924     }
2925 
2926     /**
2927      * Search calendar events or messages in provided folder matching the search query.
2928      *
2929      * @param folderPath Exchange folder path
2930      * @param attributes requested attributes
2931      * @param condition  Exchange search query
2932      * @return list of calendar messages as Event objects
2933      * @throws IOException on error
2934      */
2935     public abstract List<Event> searchEvents(String folderPath, Set<String> attributes, Condition condition) throws IOException;
2936 
2937     /**
2938      * convert vcf extension to EML.
2939      *
2940      * @param itemName item name
2941      * @return EML item name
2942      */
2943     protected String convertItemNameToEML(String itemName) {
2944         if (itemName.endsWith(".vcf")) {
2945             return itemName.substring(0, itemName.length() - 3) + "EML";
2946         } else {
2947             return itemName;
2948         }
2949     }
2950 
2951     /**
2952      * Get item named eventName in folder
2953      *
2954      * @param folderPath Exchange folder path
2955      * @param itemName   event name
2956      * @return event object
2957      * @throws IOException on error
2958      */
2959     public abstract Item getItem(String folderPath, String itemName) throws IOException;
2960 
2961     /**
2962      * Contact picture
2963      */
2964     public static class ContactPhoto {
2965         /**
2966          * Contact picture content type (always image/jpeg on read)
2967          */
2968         public String contentType;
2969         /**
2970          * Base64 encoded picture content
2971          */
2972         public String content;
2973     }
2974 
2975     /**
2976      * Retrieve contact photo attached to contact
2977      *
2978      * @param contact address book contact
2979      * @return contact photo
2980      * @throws IOException on error
2981      */
2982     public abstract ContactPhoto getContactPhoto(Contact contact) throws IOException;
2983 
2984 
2985     /**
2986      * Delete event named itemName in folder
2987      *
2988      * @param folderPath Exchange folder path
2989      * @param itemName   item name
2990      * @throws IOException on error
2991      */
2992     public abstract void deleteItem(String folderPath, String itemName) throws IOException;
2993 
2994     /**
2995      * Mark event processed named eventName in folder
2996      *
2997      * @param folderPath Exchange folder path
2998      * @param itemName   item name
2999      * @throws IOException on error
3000      */
3001     public abstract void processItem(String folderPath, String itemName) throws IOException;
3002 
3003 
3004     private static int dumpIndex;
3005 
3006     /**
3007      * Replace iCal4 (Snow Leopard) principal paths with mailto expression
3008      *
3009      * @param value attendee value or ics line
3010      * @return fixed value
3011      */
3012     protected String replaceIcal4Principal(String value) {
3013         if (value != null && value.contains("/principals/__uuids__/")) {
3014             return value.replaceAll("/principals/__uuids__/([^/]*)__AT__([^/]*)/", "mailto:$1@$2");
3015         } else {
3016             return value;
3017         }
3018     }
3019 
3020     /**
3021      * Event result object to hold HTTP status and event etag from an event creation/update.
3022      */
3023     public static class ItemResult {
3024         /**
3025          * HTTP status
3026          */
3027         public int status;
3028         /**
3029          * Event etag from response HTTP header
3030          */
3031         public String etag;
3032     }
3033 
3034     /**
3035      * Build and send the MIME message for the provided ICS event.
3036      *
3037      * @param icsBody event in iCalendar format
3038      * @return HTTP status
3039      * @throws IOException on error
3040      */
3041     public abstract int sendEvent(String icsBody) throws IOException;
3042 
3043     /**
3044      * Create or update item (event or contact) on the Exchange server
3045      *
3046      * @param folderPath Exchange folder path
3047      * @param itemName   event name
3048      * @param itemBody   event body in iCalendar format
3049      * @param etag       previous event etag to detect concurrent updates
3050      * @param noneMatch  if-none-match header value
3051      * @return HTTP response event result (status and etag)
3052      * @throws IOException on error
3053      */
3054     public ItemResult createOrUpdateItem(String folderPath, String itemName, String itemBody, String etag, String noneMatch) throws IOException {
3055         if (itemBody.startsWith("BEGIN:VCALENDAR")) {
3056             return internalCreateOrUpdateEvent(folderPath, itemName, "urn:content-classes:appointment", itemBody, etag, noneMatch);
3057         } else if (itemBody.startsWith("BEGIN:VCARD")) {
3058             return createOrUpdateContact(folderPath, itemName, itemBody, etag, noneMatch);
3059         } else {
3060             throw new IOException(BundleMessage.format("EXCEPTION_INVALID_MESSAGE_CONTENT", itemBody));
3061         }
3062     }
3063 
3064     static final String[] VCARD_N_PROPERTIES = {"sn", "givenName", "middlename", "personaltitle", "namesuffix"};
3065     static final String[] VCARD_ADR_HOME_PROPERTIES = {"homepostofficebox", null, "homeStreet", "homeCity", "homeState", "homePostalCode", "homeCountry"};
3066     static final String[] VCARD_ADR_WORK_PROPERTIES = {"postofficebox", "roomnumber", "street", "l", "st", "postalcode", "co"};
3067     static final String[] VCARD_ADR_OTHER_PROPERTIES = {"otherpostofficebox", null, "otherstreet", "othercity", "otherstate", "otherpostalcode", "othercountry"};
3068     static final String[] VCARD_ORG_PROPERTIES = {"o", "department"};
3069 
3070     protected void convertContactProperties(Map<String, String> properties, String[] contactProperties, List<String> values) {
3071         for (int i = 0; i < values.size() && i < contactProperties.length; i++) {
3072             if (contactProperties[i] != null) {
3073                 properties.put(contactProperties[i], values.get(i));
3074             }
3075         }
3076     }
3077 
3078     protected ItemResult createOrUpdateContact(String folderPath, String itemName, String itemBody, String etag, String noneMatch) throws IOException {
3079         // parse VCARD body to build contact property map
3080         Map<String, String> properties = new HashMap<String, String>();
3081         properties.put("outlookmessageclass", "IPM.Contact");
3082 
3083         VObject vcard = new VObject(new ICSBufferedReader(new StringReader(itemBody)));
3084         for (VProperty property : vcard.getProperties()) {
3085             if ("FN".equals(property.getKey())) {
3086                 properties.put("cn", property.getValue());
3087                 properties.put("subject", property.getValue());
3088                 properties.put("fileas", property.getValue());
3089 
3090             } else if ("N".equals(property.getKey())) {
3091                 convertContactProperties(properties, VCARD_N_PROPERTIES, property.getValues());
3092             } else if ("NICKNAME".equals(property.getKey())) {
3093                 properties.put("nickname", property.getValue());
3094             } else if ("TEL".equals(property.getKey())) {
3095                 if (property.hasParam("TYPE", "cell") || property.hasParam("X-GROUP", "cell")) {
3096                     properties.put("mobile", property.getValue());
3097                 } else if (property.hasParam("TYPE", "work") || property.hasParam("X-GROUP", "work")) {
3098                     properties.put("telephoneNumber", property.getValue());
3099                 } else if (property.hasParam("TYPE", "home") || property.hasParam("X-GROUP", "home")) {
3100                     properties.put("homePhone", property.getValue());
3101                 } else if (property.hasParam("TYPE", "fax")) {
3102                     if (property.hasParam("TYPE", "home")) {
3103                         properties.put("homefax", property.getValue());
3104                     } else {
3105                         properties.put("facsimiletelephonenumber", property.getValue());
3106                     }
3107                 } else if (property.hasParam("TYPE", "pager")) {
3108                     properties.put("pager", property.getValue());
3109                 } else if (property.hasParam("TYPE", "car")) {
3110                     properties.put("othermobile", property.getValue());
3111                 } else {
3112                     properties.put("otherTelephone", property.getValue());
3113                 }
3114             } else if ("ADR".equals(property.getKey())) {
3115                 // address
3116                 if (property.hasParam("TYPE", "home")) {
3117                     convertContactProperties(properties, VCARD_ADR_HOME_PROPERTIES, property.getValues());
3118                 } else if (property.hasParam("TYPE", "work")) {
3119                     convertContactProperties(properties, VCARD_ADR_WORK_PROPERTIES, property.getValues());
3120                     // any other type goes to other address
3121                 } else {
3122                     convertContactProperties(properties, VCARD_ADR_OTHER_PROPERTIES, property.getValues());
3123                 }
3124             } else if ("EMAIL".equals(property.getKey())) {
3125                 if (property.hasParam("TYPE", "home")) {
3126                     properties.put("email2", property.getValue());
3127                     properties.put("smtpemail2", property.getValue());
3128                 } else if (property.hasParam("TYPE", "other")) {
3129                     properties.put("email3", property.getValue());
3130                     properties.put("smtpemail3", property.getValue());
3131                 } else {
3132                     properties.put("email1", property.getValue());
3133                     properties.put("smtpemail1", property.getValue());
3134                 }
3135             } else if ("ORG".equals(property.getKey())) {
3136                 convertContactProperties(properties, VCARD_ORG_PROPERTIES, property.getValues());
3137             } else if ("URL".equals(property.getKey())) {
3138                 if (property.hasParam("TYPE", "work")) {
3139                     properties.put("businesshomepage", property.getValue());
3140                 } else if (property.hasParam("TYPE", "home")) {
3141                     properties.put("personalHomePage", property.getValue());
3142                 } else {
3143                     // default: set personal home page
3144                     properties.put("personalHomePage", property.getValue());
3145                 }
3146             } else if ("TITLE".equals(property.getKey())) {
3147                 properties.put("title", property.getValue());
3148             } else if ("NOTE".equals(property.getKey())) {
3149                 properties.put("description", property.getValue());
3150             } else if ("CUSTOM1".equals(property.getKey())) {
3151                 properties.put("extensionattribute1", property.getValue());
3152             } else if ("CUSTOM2".equals(property.getKey())) {
3153                 properties.put("extensionattribute2", property.getValue());
3154             } else if ("CUSTOM3".equals(property.getKey())) {
3155                 properties.put("extensionattribute3", property.getValue());
3156             } else if ("CUSTOM4".equals(property.getKey())) {
3157                 properties.put("extensionattribute4", property.getValue());
3158             } else if ("ROLE".equals(property.getKey())) {
3159                 properties.put("profession", property.getValue());
3160             } else if ("X-AIM".equals(property.getKey())) {
3161                 properties.put("im", property.getValue());
3162             } else if ("BDAY".equals(property.getKey())) {
3163                 properties.put("bday", convertBDayToZulu(property.getValue()));
3164             } else if ("ANNIVERSARY".equals(property.getKey()) || "X-ANNIVERSARY".equals(property.getKey())) {
3165                 properties.put("anniversary", convertBDayToZulu(property.getValue()));
3166             } else if ("CATEGORIES".equals(property.getKey())) {
3167                 properties.put("keywords", property.getValue());
3168             } else if ("CLASS".equals(property.getKey())) {
3169                 if ("PUBLIC".equals(property.getValue())) {
3170                     properties.put("sensitivity", "0");
3171                     properties.put("private", "false");
3172                 } else {
3173                     properties.put("sensitivity", "2");
3174                     properties.put("private", "true");
3175                 }
3176             } else if ("SEX".equals(property.getKey())) {
3177                 String propertyValue = property.getValue();
3178                 if ("1".equals(propertyValue)) {
3179                     properties.put("gender", "2");
3180                 } else if ("2".equals(propertyValue)) {
3181                     properties.put("gender", "1");
3182                 }
3183             } else if ("FBURL".equals(property.getKey())) {
3184                 properties.put("fburl", property.getValue());
3185             } else if ("X-ASSISTANT".equals(property.getKey())) {
3186                 properties.put("secretarycn", property.getValue());
3187             } else if ("X-MANAGER".equals(property.getKey())) {
3188                 properties.put("manager", property.getValue());
3189             } else if ("X-SPOUSE".equals(property.getKey())) {
3190                 properties.put("spousecn", property.getValue());
3191             } else if ("PHOTO".equals(property.getKey())) {
3192                 properties.put("photo", property.getValue());
3193                 properties.put("haspicture", "true");
3194             }
3195         }
3196         LOGGER.debug("Create or update contact " + itemName + ": " + properties);
3197         // reset missing properties to null
3198         for (String key : CONTACT_ATTRIBUTES) {
3199             if (!"imapUid".equals(key) && !"etag".equals(key) && !"urlcompname".equals(key)
3200                     && !"lastmodified".equals(key) && !"sensitivity".equals(key) &&
3201                     !properties.containsKey(key)) {
3202                 properties.put(key, null);
3203             }
3204         }
3205         return internalCreateOrUpdateContact(folderPath, itemName, properties, etag, noneMatch);
3206     }
3207 
3208     protected String convertZuluDateToBday(String value) {
3209         String result = null;
3210         if (value != null && value.length() > 0) {
3211             try {
3212                 SimpleDateFormat parser = ExchangeSession.getZuluDateFormat();
3213                 Calendar cal = Calendar.getInstance();
3214                 cal.setTime(parser.parse(value));
3215                 cal.add(Calendar.HOUR_OF_DAY, 12);
3216                 result = ExchangeSession.getVcardBdayFormat().format(cal.getTime());
3217             } catch (ParseException e) {
3218                 LOGGER.warn("Invalid date: " + value);
3219             }
3220         }
3221         return result;
3222     }
3223 
3224     protected String convertBDayToZulu(String value) {
3225         String result = null;
3226         if (value != null && value.length() > 0) {
3227             try {
3228                 SimpleDateFormat parser;
3229                 if (value.length() == 10) {
3230                     parser = ExchangeSession.getVcardBdayFormat();
3231                 } else if (value.length() == 15) {
3232                     parser = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);
3233                     parser.setTimeZone(GMT_TIMEZONE);
3234                 } else {
3235                     parser = ExchangeSession.getExchangeZuluDateFormat();
3236                 }
3237                 result = ExchangeSession.getExchangeZuluDateFormatMillisecond().format(parser.parse(value));
3238             } catch (ParseException e) {
3239                 LOGGER.warn("Invalid date: " + value);
3240             }
3241         }
3242 
3243         return result;
3244     }
3245 
3246 
3247     protected abstract ItemResult internalCreateOrUpdateContact(String folderPath, String itemName, Map<String, String> properties, String etag, String noneMatch) throws IOException;
3248 
3249     protected abstract ItemResult internalCreateOrUpdateEvent(String folderPath, String itemName, String contentClass, String icsBody, String etag, String noneMatch) throws IOException;
3250 
3251     /**
3252      * Get current Exchange alias name from login name
3253      *
3254      * @return user name
3255      */
3256     public String getAliasFromLogin() {
3257         // login is email, not alias
3258         if (this.userName.indexOf('@') >= 0) {
3259             return null;
3260         }
3261         String result = this.userName;
3262         // remove domain name
3263         int index = Math.max(result.indexOf('\\'), result.indexOf('/'));
3264         if (index >= 0) {
3265             result = result.substring(index + 1);
3266         }
3267         return result;
3268     }
3269 
3270     protected String getEmailSuffixFromHostname() {
3271         String domain = httpClient.getHostConfiguration().getHost();
3272         int start = domain.lastIndexOf('.', domain.lastIndexOf('.') - 1);
3273         if (start >= 0) {
3274             return '@' + domain.substring(start + 1);
3275         } else {
3276             return '@' + domain;
3277         }
3278     }
3279 
3280     /**
3281      * Test if folderPath is inside user mailbox.
3282      *
3283      * @param folderPath absolute folder path
3284      * @return true if folderPath is a public or shared folder
3285      */
3286     public abstract boolean isSharedFolder(String folderPath);
3287 
3288     /**
3289      * Test if folderPath is main calendar.
3290      *
3291      * @param folderPath absolute folder path
3292      * @return true if folderPath is a public or shared folder
3293      */
3294     public abstract boolean isMainCalendar(String folderPath);
3295 
3296     static final String MAILBOX_BASE = "/cn=";
3297 
3298     protected void getEmailAndAliasFromOptions() {
3299         synchronized (httpClient.getState()) {
3300             Cookie[] currentCookies = httpClient.getState().getCookies();
3301             // get user mail URL from html body
3302             BufferedReader optionsPageReader = null;
3303             GetMethod optionsMethod = new GetMethod("/owa/?ae=Options&t=About");
3304             try {
3305                 DavGatewayHttpClientFacade.executeGetMethod(httpClient, optionsMethod, false);
3306                 optionsPageReader = new BufferedReader(new InputStreamReader(optionsMethod.getResponseBodyAsStream(), "UTF-8"));
3307                 String line;
3308 
3309                 // find email and alias
3310                 //noinspection StatementWithEmptyBody
3311                 while ((line = optionsPageReader.readLine()) != null
3312                         && (line.indexOf('[') == -1
3313                         || line.indexOf('@') == -1
3314                         || line.indexOf(']') == -1
3315                         || !line.toLowerCase().contains(MAILBOX_BASE))) {
3316                 }
3317                 if (line != null) {
3318                     int start = line.toLowerCase().lastIndexOf(MAILBOX_BASE) + MAILBOX_BASE.length();
3319                     int end = line.indexOf('<', start);
3320                     alias = line.substring(start, end);
3321                     end = line.lastIndexOf(']');
3322                     start = line.lastIndexOf('[', end) + 1;
3323                     email = line.substring(start, end);
3324                 }
3325             } catch (IOException e) {
3326                 // restore cookies on error
3327                 httpClient.getState().addCookies(currentCookies);
3328                 LOGGER.error("Error parsing options page at " + optionsMethod.getPath());
3329             } finally {
3330                 if (optionsPageReader != null) {
3331                     try {
3332                         optionsPageReader.close();
3333                     } catch (IOException e) {
3334                         LOGGER.error("Error parsing options page at " + optionsMethod.getPath());
3335                     }
3336                 }
3337                 optionsMethod.releaseConnection();
3338             }
3339         }
3340     }
3341 
3342     /**
3343      * Get current user email
3344      *
3345      * @return user email
3346      */
3347     public String getEmail() {
3348         return email;
3349     }
3350 
3351     /**
3352      * Get current user alias
3353      *
3354      * @return user email
3355      */
3356     public String getAlias() {
3357         return alias;
3358     }
3359 
3360     /**
3361      * Search global address list
3362      *
3363      * @param condition           search filter
3364      * @param returningAttributes returning attributes
3365      * @param sizeLimit           size limit
3366      * @return matching contacts from gal
3367      * @throws IOException on error
3368      */
3369     public abstract Map<String, Contact> galFind(Condition condition, Set<String> returningAttributes, int sizeLimit) throws IOException;
3370 
3371     /**
3372      * Full Contact attribute list
3373      */
3374     public static final Set<String> CONTACT_ATTRIBUTES = new HashSet<String>();
3375 
3376     static {
3377         CONTACT_ATTRIBUTES.add("imapUid");
3378         CONTACT_ATTRIBUTES.add("etag");
3379         CONTACT_ATTRIBUTES.add("urlcompname");
3380 
3381         CONTACT_ATTRIBUTES.add("extensionattribute1");
3382         CONTACT_ATTRIBUTES.add("extensionattribute2");
3383         CONTACT_ATTRIBUTES.add("extensionattribute3");
3384         CONTACT_ATTRIBUTES.add("extensionattribute4");
3385         CONTACT_ATTRIBUTES.add("bday");
3386         CONTACT_ATTRIBUTES.add("anniversary");
3387         CONTACT_ATTRIBUTES.add("businesshomepage");
3388         CONTACT_ATTRIBUTES.add("personalHomePage");
3389         CONTACT_ATTRIBUTES.add("cn");
3390         CONTACT_ATTRIBUTES.add("co");
3391         CONTACT_ATTRIBUTES.add("department");
3392         CONTACT_ATTRIBUTES.add("smtpemail1");
3393         CONTACT_ATTRIBUTES.add("smtpemail2");
3394         CONTACT_ATTRIBUTES.add("smtpemail3");
3395         CONTACT_ATTRIBUTES.add("facsimiletelephonenumber");
3396         CONTACT_ATTRIBUTES.add("givenName");
3397         CONTACT_ATTRIBUTES.add("homeCity");
3398         CONTACT_ATTRIBUTES.add("homeCountry");
3399         CONTACT_ATTRIBUTES.add("homePhone");
3400         CONTACT_ATTRIBUTES.add("homePostalCode");
3401         CONTACT_ATTRIBUTES.add("homeState");
3402         CONTACT_ATTRIBUTES.add("homeStreet");
3403         CONTACT_ATTRIBUTES.add("homepostofficebox");
3404         CONTACT_ATTRIBUTES.add("l");
3405         CONTACT_ATTRIBUTES.add("manager");
3406         CONTACT_ATTRIBUTES.add("mobile");
3407         CONTACT_ATTRIBUTES.add("namesuffix");
3408         CONTACT_ATTRIBUTES.add("nickname");
3409         CONTACT_ATTRIBUTES.add("o");
3410         CONTACT_ATTRIBUTES.add("pager");
3411         CONTACT_ATTRIBUTES.add("personaltitle");
3412         CONTACT_ATTRIBUTES.add("postalcode");
3413         CONTACT_ATTRIBUTES.add("postofficebox");
3414         CONTACT_ATTRIBUTES.add("profession");
3415         CONTACT_ATTRIBUTES.add("roomnumber");
3416         CONTACT_ATTRIBUTES.add("secretarycn");
3417         CONTACT_ATTRIBUTES.add("sn");
3418         CONTACT_ATTRIBUTES.add("spousecn");
3419         CONTACT_ATTRIBUTES.add("st");
3420         CONTACT_ATTRIBUTES.add("street");
3421         CONTACT_ATTRIBUTES.add("telephoneNumber");
3422         CONTACT_ATTRIBUTES.add("title");
3423         CONTACT_ATTRIBUTES.add("description");
3424         CONTACT_ATTRIBUTES.add("im");
3425         CONTACT_ATTRIBUTES.add("middlename");
3426         CONTACT_ATTRIBUTES.add("lastmodified");
3427         CONTACT_ATTRIBUTES.add("otherstreet");
3428         CONTACT_ATTRIBUTES.add("otherstate");
3429         CONTACT_ATTRIBUTES.add("otherpostofficebox");
3430         CONTACT_ATTRIBUTES.add("otherpostalcode");
3431         CONTACT_ATTRIBUTES.add("othercountry");
3432         CONTACT_ATTRIBUTES.add("othercity");
3433         CONTACT_ATTRIBUTES.add("haspicture");
3434         CONTACT_ATTRIBUTES.add("keywords");
3435         CONTACT_ATTRIBUTES.add("othermobile");
3436         CONTACT_ATTRIBUTES.add("otherTelephone");
3437         CONTACT_ATTRIBUTES.add("gender");
3438         CONTACT_ATTRIBUTES.add("private");
3439         CONTACT_ATTRIBUTES.add("sensitivity");
3440         CONTACT_ATTRIBUTES.add("fburl");
3441     }
3442 
3443     /**
3444      * Get freebusy data string from Exchange.
3445      *
3446      * @param attendee attendee email address
3447      * @param start    start date in Exchange zulu format
3448      * @param end      end date in Exchange zulu format
3449      * @param interval freebusy interval in minutes
3450      * @return freebusy data or null
3451      * @throws IOException on error
3452      */
3453     protected abstract String getFreeBusyData(String attendee, String start, String end, int interval) throws IOException;
3454 
3455     /**
3456      * Get freebusy info for attendee between start and end date.
3457      *
3458      * @param attendee       attendee email
3459      * @param startDateValue start date
3460      * @param endDateValue   end date
3461      * @return FreeBusy info
3462      * @throws IOException on error
3463      */
3464     public FreeBusy getFreebusy(String attendee, String startDateValue, String endDateValue) throws IOException {
3465         // replace ical encoded attendee name
3466         attendee = replaceIcal4Principal(attendee);
3467 
3468         // then check that email address is valid to avoid InvalidSmtpAddress error
3469         if (attendee == null || attendee.indexOf('@') < 0 || attendee.charAt(attendee.length() - 1) == '@') {
3470             return null;
3471         }
3472 
3473         if (attendee.startsWith("mailto:") || attendee.startsWith("MAILTO:")) {
3474             attendee = attendee.substring("mailto:".length());
3475         }
3476 
3477         SimpleDateFormat exchangeZuluDateFormat = getExchangeZuluDateFormat();
3478         SimpleDateFormat icalDateFormat = getZuluDateFormat();
3479 
3480         Date startDate;
3481         Date endDate;
3482         try {
3483             if (startDateValue.length() == 8) {
3484                 startDate = parseDate(startDateValue);
3485             } else {
3486                 startDate = icalDateFormat.parse(startDateValue);
3487             }
3488             if (endDateValue.length() == 8) {
3489                 endDate = parseDate(endDateValue);
3490             } else {
3491                 endDate = icalDateFormat.parse(endDateValue);
3492             }
3493         } catch (ParseException e) {
3494             throw new DavMailException("EXCEPTION_INVALID_DATES", e.getMessage());
3495         }
3496 
3497         FreeBusy freeBusy = null;
3498         String fbdata = getFreeBusyData(attendee, exchangeZuluDateFormat.format(startDate), exchangeZuluDateFormat.format(endDate), FREE_BUSY_INTERVAL);
3499         if (fbdata != null) {
3500             freeBusy = new FreeBusy(icalDateFormat, startDate, fbdata);
3501         }
3502 
3503         if (freeBusy != null && freeBusy.knownAttendee) {
3504             return freeBusy;
3505         } else {
3506             return null;
3507         }
3508     }
3509 
3510     /**
3511      * Exchange to iCalendar Free/Busy parser.
3512      * Free time returns 0, Tentative returns 1, Busy returns 2, and Out of Office (OOF) returns 3
3513      */
3514     public static final class FreeBusy {
3515         final SimpleDateFormat icalParser;
3516         boolean knownAttendee = true;
3517         static final HashMap<Character, String> FBTYPES = new HashMap<Character, String>();
3518 
3519         static {
3520             FBTYPES.put('1', "BUSY-TENTATIVE");
3521             FBTYPES.put('2', "BUSY");
3522             FBTYPES.put('3', "BUSY-UNAVAILABLE");
3523         }
3524 
3525         final HashMap<String, StringBuilder> busyMap = new HashMap<String, StringBuilder>();
3526 
3527         StringBuilder getBusyBuffer(char type) {
3528             String fbType = FBTYPES.get(Character.valueOf(type));
3529             StringBuilder buffer = busyMap.get(fbType);
3530             if (buffer == null) {
3531                 buffer = new StringBuilder();
3532                 busyMap.put(fbType, buffer);
3533             }
3534             return buffer;
3535         }
3536 
3537         void startBusy(char type, Calendar currentCal) {
3538             if (type == '4') {
3539                 knownAttendee = false;
3540             } else if (type != '0') {
3541                 StringBuilder busyBuffer = getBusyBuffer(type);
3542                 if (busyBuffer.length() > 0) {
3543                     busyBuffer.append(',');
3544                 }
3545                 busyBuffer.append(icalParser.format(currentCal.getTime()));
3546             }
3547         }
3548 
3549         void endBusy(char type, Calendar currentCal) {
3550             if (type != '0' && type != '4') {
3551                 getBusyBuffer(type).append('/').append(icalParser.format(currentCal.getTime()));
3552             }
3553         }
3554 
3555         FreeBusy(SimpleDateFormat icalParser, Date startDate, String fbdata) {
3556             this.icalParser = icalParser;
3557             if (fbdata.length() > 0) {
3558                 Calendar currentCal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
3559                 currentCal.setTime(startDate);
3560 
3561                 startBusy(fbdata.charAt(0), currentCal);
3562                 for (int i = 1; i < fbdata.length() && knownAttendee; i++) {
3563                     currentCal.add(Calendar.MINUTE, FREE_BUSY_INTERVAL);
3564                     char previousState = fbdata.charAt(i - 1);
3565                     char currentState = fbdata.charAt(i);
3566                     if (previousState != currentState) {
3567                         endBusy(previousState, currentCal);
3568                         startBusy(currentState, currentCal);
3569                     }
3570                 }
3571                 currentCal.add(Calendar.MINUTE, FREE_BUSY_INTERVAL);
3572                 endBusy(fbdata.charAt(fbdata.length() - 1), currentCal);
3573             }
3574         }
3575 
3576         /**
3577          * Append freebusy information to buffer.
3578          *
3579          * @param buffer String buffer
3580          */
3581         public void appendTo(StringBuilder buffer) {
3582             for (Map.Entry<String, StringBuilder> entry : busyMap.entrySet()) {
3583                 buffer.append("FREEBUSY;FBTYPE=").append(entry.getKey())
3584                         .append(':').append(entry.getValue()).append((char) 13).append((char) 10);
3585             }
3586         }
3587     }
3588 
3589     protected VObject vTimezone;
3590 
3591     /**
3592      * Load and return current user OWA timezone.
3593      *
3594      * @return current timezone
3595      */
3596     public VObject getVTimezone() {
3597         if (vTimezone == null) {
3598             // need to load Timezone info from OWA
3599             loadVtimezone();
3600         }
3601         return vTimezone;
3602     }
3603 
3604     protected abstract void loadVtimezone();
3605 
3606     /**
3607      * Return internal HttpClient instance
3608      *
3609      * @return http client
3610      */
3611     public HttpClient getHttpClient() {
3612         return httpClient;
3613     }
3614 }