View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  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.dav;
20  
21  import davmail.Settings;
22  import davmail.util.StringUtil;
23  import org.apache.jackrabbit.webdav.property.DavPropertyName;
24  import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
25  import org.apache.jackrabbit.webdav.property.PropEntry;
26  import org.apache.jackrabbit.webdav.xml.DomUtil;
27  import org.apache.jackrabbit.webdav.xml.Namespace;
28  import org.apache.jackrabbit.webdav.xml.XmlSerializable;
29  
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * WebDav Field
37   */
38  public class Field {
39  
40      protected static final Map<DistinguishedPropertySetType, String> distinguishedPropertySetMap = new HashMap<>();
41  
42      static {
43          distinguishedPropertySetMap.put(DistinguishedPropertySetType.Meeting, "6ed8da90-450b-101b-98da-00aa003f1305");
44          distinguishedPropertySetMap.put(DistinguishedPropertySetType.Appointment, "00062002-0000-0000-c000-000000000046");
45          distinguishedPropertySetMap.put(DistinguishedPropertySetType.Common, "00062008-0000-0000-c000-000000000046");
46          distinguishedPropertySetMap.put(DistinguishedPropertySetType.PublicStrings, "00020329-0000-0000-c000-000000000046");
47          distinguishedPropertySetMap.put(DistinguishedPropertySetType.Address, "00062004-0000-0000-c000-000000000046");
48          distinguishedPropertySetMap.put(DistinguishedPropertySetType.InternetHeaders, "00020386-0000-0000-c000-000000000046");
49          distinguishedPropertySetMap.put(DistinguishedPropertySetType.UnifiedMessaging, "4442858e-a9e3-4e80-b900-317a210cc15b");
50          distinguishedPropertySetMap.put(DistinguishedPropertySetType.Task, "00062003-0000-0000-c000-000000000046");
51      }
52  
53      protected static final Namespace EMPTY = Namespace.getNamespace("");
54      protected static final Namespace XML = Namespace.getNamespace("xml:");
55      protected static final Namespace DAV = Namespace.getNamespace("DAV:");
56      protected static final Namespace URN_SCHEMAS_HTTPMAIL = Namespace.getNamespace("urn:schemas:httpmail:");
57      protected static final Namespace URN_SCHEMAS_MAILHEADER = Namespace.getNamespace("urn:schemas:mailheader:");
58  
59      protected static final Namespace SCHEMAS_EXCHANGE = Namespace.getNamespace("http://schemas.microsoft.com/exchange/");
60      protected static final Namespace SCHEMAS_MAPI = Namespace.getNamespace("http://schemas.microsoft.com/mapi/");
61      protected static final Namespace SCHEMAS_MAPI_PROPTAG = Namespace.getNamespace("http://schemas.microsoft.com/mapi/proptag/");
62      protected static final Namespace SCHEMAS_MAPI_ID = Namespace.getNamespace("http://schemas.microsoft.com/mapi/id/");
63      protected static final Namespace SCHEMAS_MAPI_STRING = Namespace.getNamespace("http://schemas.microsoft.com/mapi/string/");
64      protected static final Namespace SCHEMAS_REPL = Namespace.getNamespace("http://schemas.microsoft.com/repl/");
65      protected static final Namespace URN_SCHEMAS_CONTACTS = Namespace.getNamespace("urn:schemas:contacts:");
66      protected static final Namespace URN_SCHEMAS_CALENDAR = Namespace.getNamespace("urn:schemas:calendar:");
67  
68      protected static final Namespace SCHEMAS_MAPI_STRING_INTERNET_HEADERS =
69              Namespace.getNamespace(SCHEMAS_MAPI_STRING.getURI() +
70                      '{' + distinguishedPropertySetMap.get(DistinguishedPropertySetType.InternetHeaders) + "}/");
71  
72      protected static final Map<PropertyType, String> propertyTypeMap = new HashMap<>();
73  
74      static {
75          propertyTypeMap.put(PropertyType.Integer, "0003"); // PT_INT
76          propertyTypeMap.put(PropertyType.Boolean, "000b"); // PT_BOOLEAN
77          propertyTypeMap.put(PropertyType.SystemTime, "0040"); // PT_SYSTIME
78          propertyTypeMap.put(PropertyType.String, "001f"); // 001f is PT_UNICODE_STRING, 001E is PT_STRING
79          propertyTypeMap.put(PropertyType.Binary, "0102"); // PT_BINARY
80          propertyTypeMap.put(PropertyType.Double, "0005"); // PT_DOUBLE
81      }
82  
83      @SuppressWarnings({"UnusedDeclaration"})
84      protected enum DistinguishedPropertySetType {
85          Meeting, Appointment, Common, PublicStrings, Address, InternetHeaders, CalendarAssistant, UnifiedMessaging, Task
86      }
87  
88  
89      protected static final Map<String, Field> fieldMap = new HashMap<>();
90  
91      static {
92          // well known folders
93          createField(URN_SCHEMAS_HTTPMAIL, "inbox");
94          createField(URN_SCHEMAS_HTTPMAIL, "deleteditems");
95          createField(URN_SCHEMAS_HTTPMAIL, "sentitems");
96          createField(URN_SCHEMAS_HTTPMAIL, "sendmsg");
97          createField(URN_SCHEMAS_HTTPMAIL, "drafts");
98          createField(URN_SCHEMAS_HTTPMAIL, "calendar");
99          createField(URN_SCHEMAS_HTTPMAIL, "tasks");
100         createField(URN_SCHEMAS_HTTPMAIL, "contacts");
101         createField(URN_SCHEMAS_HTTPMAIL, "outbox");
102 
103 
104         // folder
105         createField("folderclass", SCHEMAS_EXCHANGE, "outlookfolderclass");
106         createField(DAV, "hassubs");
107         createField(DAV, "nosubs");
108         createField("count", DAV, "objectcount");
109         createField(URN_SCHEMAS_HTTPMAIL, "unreadcount");
110         createField(SCHEMAS_REPL, "contenttag");
111 
112         createField("uidNext", 0x6751, PropertyType.Integer);// PR_ARTICLE_NUM_NEXT
113         createField("highestUid", 0x6752, PropertyType.Integer);// PR_IMAP_LAST_ARTICLE_ID
114 
115         createField(DAV, "isfolder");
116 
117         // item uid, do not use as search parameter, see http://support.microsoft.com/kb/320749
118         createField(DAV, "uid"); // based on PR_RECORD_KEY
119 
120         // POP and IMAP message
121         createField("messageSize", 0x0e08, PropertyType.Integer);//PR_MESSAGE_SIZE
122         createField("imapUid", 0x0e23, PropertyType.Integer);//PR_INTERNET_ARTICLE_NUMBER
123         createField("junk", 0x1083, PropertyType.Integer); //PR_SPAMTYPE
124         createField("flagStatus", 0x1090, PropertyType.Integer);//PR_FLAG_STATUS
125         createField("messageFlags", 0x0e07, PropertyType.Integer);//PR_MESSAGE_FLAGS
126         createField("lastVerbExecuted", 0x1081, PropertyType.Integer);//PR_LAST_VERB_EXECUTED
127         createField("iconIndex", 0x1080, PropertyType.Integer);//PR_ICON_INDEX
128         createField(URN_SCHEMAS_HTTPMAIL, "read");
129         //createField("read", 0x0e69, PropertyType.Boolean);//PR_READ
130 
131         if (Settings.getBooleanProperty("davmail.popCommonDeleted", true)) {
132             // deleted flag, see http://microsoft.public.win32.programmer.messaging.narkive.com/w7Mrsrsx/how-to-detect-deleted-imap-messages-using-mapi-outlook-object-model-api
133             createField("deleted", DistinguishedPropertySetType.Common, 0x8570, "deleted", PropertyType.String);
134         } else {
135             createField("deleted", DistinguishedPropertySetType.PublicStrings);
136         }
137 
138         //createField(URN_SCHEMAS_HTTPMAIL, "date");//PR_CLIENT_SUBMIT_TIME, 0x0039
139         createField("date", 0x0e06, PropertyType.SystemTime);//PR_MESSAGE_DELIVERY_TIME
140         createField(URN_SCHEMAS_MAILHEADER, "bcc");//PS_INTERNET_HEADERS/bcc
141         createField(URN_SCHEMAS_HTTPMAIL, "datereceived");//PR_MESSAGE_DELIVERY_TIME, 0x0E06
142 
143         // unused: force message encoding
144         createField("messageFormat", 0x5909, PropertyType.Integer);//PR_MSG_EDITOR_FORMAT EDITOR_FORMAT_PLAINTEXT = 1 EDITOR_FORMAT_HTML = 2
145         createField("mailOverrideFormat", 0x5902, PropertyType.Integer);//PR_INETMAIL_OVERRIDE_FORMAT ENCODING_PREFERENCE = 2 BODY_ENCODING_TEXT_AND_HTML = 1 ENCODING_MIME = 4
146 
147         // IMAP search
148 
149         createField(URN_SCHEMAS_HTTPMAIL, "subject"); // DistinguishedPropertySetType.InternetHeaders/Subject/String
150         //createField("subject", 0x0037, PropertyType.String);//PR_SUBJECT
151         createField("body", 0x1000, PropertyType.String);//PR_BODY
152         createField("messageheaders", 0x007D, PropertyType.String);// PR_TRANSPORT_MESSAGE_HEADERS
153         createField(URN_SCHEMAS_HTTPMAIL, "from");
154         //createField("from", DistinguishedPropertySetType.PublicStrings, 0x001f);//urn:schemas:httpmail:from
155         createField(URN_SCHEMAS_MAILHEADER, "to"); // DistinguishedPropertySetType.InternetHeaders/To/String
156         createField(URN_SCHEMAS_MAILHEADER, "cc"); // DistinguishedPropertySetType.InternetHeaders/To/String
157         createField(URN_SCHEMAS_MAILHEADER, "message-id"); // DistinguishedPropertySetType.InternetHeaders/message-id/String
158         createField(URN_SCHEMAS_MAILHEADER, "htmldescription"); // DistinguishedPropertySetType.InternetHeaders/htmldescription/String
159 
160         createField("lastmodified", DAV, "getlastmodified"); // PR_LAST_MODIFICATION_TIME 0x3008 SystemTime
161 
162         // failover search
163         createField(DAV, "displayname");
164         createField("urlcompname", 0x10f3, PropertyType.String); //PR_URL_COMP_NAME
165 
166         // items
167         createField("etag", DAV, "getetag");
168 
169         // calendar
170         createField(SCHEMAS_EXCHANGE, "permanenturl");
171         createField(URN_SCHEMAS_CALENDAR, "instancetype"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:instancetype/Integer
172         createField(URN_SCHEMAS_CALENDAR, "dtstart"); // 0x10C3 SystemTime
173         createField(URN_SCHEMAS_CALENDAR, "dtend"); // 0x10C4 SystemTime
174 
175         //createField(URN_SCHEMAS_CALENDAR, "prodid"); //  // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:prodid/String
176         createField("calendarversion", URN_SCHEMAS_CALENDAR, "version"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:version/String
177         createField(URN_SCHEMAS_CALENDAR, "method"); //  // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:method/String
178 
179         createField("calendarlastmodified", URN_SCHEMAS_CALENDAR, "lastmodified"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:isorganizer/Boolean
180         createField(URN_SCHEMAS_CALENDAR, "dtstamp"); // PidLidOwnerCriticalChange
181         createField("calendaruid", URN_SCHEMAS_CALENDAR, "uid"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:uid/String
182         createField(URN_SCHEMAS_CALENDAR, "transparent"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:transparent/String
183 
184         createField(URN_SCHEMAS_CALENDAR, "organizer");
185         createField(URN_SCHEMAS_CALENDAR, "created"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:created/SystemTime
186         createField(URN_SCHEMAS_CALENDAR, "alldayevent"); // DistinguishedPropertySetType.Appointment/0x8215 Boolean
187 
188         createField(URN_SCHEMAS_CALENDAR, "rrule"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:rrule/PtypMultipleString
189         createField(URN_SCHEMAS_CALENDAR, "exdate"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:exdate/PtypMultipleTime
190 
191         createField(SCHEMAS_MAPI, "reminderset"); // PidLidReminderSet
192         createField(SCHEMAS_MAPI, "reminderdelta"); // PidLidReminderDelta         
193 
194         // TODO
195         createField(SCHEMAS_MAPI, "allattendeesstring"); // PidLidAllAttendeesString
196         createField(SCHEMAS_MAPI, "required_attendees"); // PidLidRequiredAttendees
197         createField(SCHEMAS_MAPI, "apptendtime"); // PidLidAppointmentEndTime
198         createField(SCHEMAS_MAPI, "apptstateflags"); // PidLidAppointmentStateFlags 1: Meeting, 2: Received, 4: Cancelled
199 
200         createField(URN_SCHEMAS_CALENDAR, "isorganizer"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:isorganizer/Boolean
201         createField(URN_SCHEMAS_CALENDAR, "location"); // DistinguishedPropertySetType.Appointment/0x8208 String
202         createField(URN_SCHEMAS_CALENDAR, "attendeerole"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:attendeerole/Integer
203         createField(URN_SCHEMAS_CALENDAR, "busystatus"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:busystatus/String
204         createField(URN_SCHEMAS_CALENDAR, "exrule"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:exrule/PtypMultipleString
205         createField(URN_SCHEMAS_CALENDAR, "recurrenceidrange"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:recurrenceidrange/String
206         createField(URN_SCHEMAS_CALENDAR, "rdate"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:rdate/PtypMultipleTime        
207         createField(URN_SCHEMAS_CALENDAR, "reminderoffset"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:reminderoffset/Integer
208         createField(URN_SCHEMAS_CALENDAR, "timezone"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:timezone/String
209 
210 
211         createField(SCHEMAS_EXCHANGE, "sensitivity"); // PR_SENSITIVITY 0x0036 Integer
212         createField(URN_SCHEMAS_CALENDAR, "timezoneid"); // DistinguishedPropertySetType.PublicStrings/urn:schemas:calendar:timezoneid/Integer
213         // should use PidLidServerProcessed ?
214         createField("processed", 0x65e8, PropertyType.Boolean);// PR_MESSAGE_PROCESSED
215 
216         createField(DAV, "contentclass");
217         createField("internetContent", 0x6659, PropertyType.Binary);
218 
219         // contact
220 
221         createField(SCHEMAS_EXCHANGE, "outlookmessageclass");
222         createField(URN_SCHEMAS_HTTPMAIL, "subject");
223 
224         createField(URN_SCHEMAS_CONTACTS, "middlename"); // PR_MIDDLE_NAME 0x3A44
225         createField(URN_SCHEMAS_CONTACTS, "fileas"); // urn:schemas:contacts:fileas PS_PUBLIC_STRINGS
226 
227         //createField("id", 0x0ff6, PropertyType.Binary); // PR_INSTANCE_KEY http://support.microsoft.com/kb/320749
228 
229         createField(URN_SCHEMAS_CONTACTS, "homepostaladdress"); // homeAddress DistinguishedPropertySetType.Address/0x0000801A/String
230         createField(URN_SCHEMAS_CONTACTS, "otherpostaladdress"); // otherAddress DistinguishedPropertySetType.Address/0x0000801C/String
231         createField(URN_SCHEMAS_CONTACTS, "mailingaddressid"); // postalAddressId DistinguishedPropertySetType.Address/0x00008022/String
232         createField(URN_SCHEMAS_CONTACTS, "workaddress"); // workAddress DistinguishedPropertySetType.Address/0x0000801B/String
233 
234         createField(URN_SCHEMAS_CONTACTS, "alternaterecipient"); // alternaterecipient DistinguishedPropertySetType.PublicStrings/urn:schemas:contacts:alternaterecipient/String
235 
236         createField(SCHEMAS_EXCHANGE, "extensionattribute1"); // DistinguishedPropertySetType.Address/0x0000804F/String
237         createField(SCHEMAS_EXCHANGE, "extensionattribute2"); // DistinguishedPropertySetType.Address/0x00008050/String
238         createField(SCHEMAS_EXCHANGE, "extensionattribute3"); // DistinguishedPropertySetType.Address/0x00008051/String
239         createField(SCHEMAS_EXCHANGE, "extensionattribute4"); // DistinguishedPropertySetType.Address/0x00008052/String
240 
241         createField(URN_SCHEMAS_CONTACTS, "bday"); // PR_BIRTHDAY 0x3A42 SystemTime
242         createField("anniversary", URN_SCHEMAS_CONTACTS, "weddinganniversary"); // WeddingAnniversary
243         createField(URN_SCHEMAS_CONTACTS, "businesshomepage"); // PR_BUSINESS_HOME_PAGE 0x3A51 String
244         createField(URN_SCHEMAS_CONTACTS, "personalHomePage"); // PR_PERSONAL_HOME_PAGE 0x3A50 String
245         createField(URN_SCHEMAS_CONTACTS, "cn"); // PR_DISPLAY_NAME 0x3001 String
246         createField(URN_SCHEMAS_CONTACTS, "co"); // workAddressCountry DistinguishedPropertySetType.Address/0x00008049/String
247         createField(URN_SCHEMAS_CONTACTS, "department"); // PR_DEPARTMENT_NAME 0x3A18 String
248 
249         // smtp email
250         createField("smtpemail1", DistinguishedPropertySetType.Address, 0x8084, "smtpemail1"); // Email1OriginalDisplayName
251         createField("smtpemail2", DistinguishedPropertySetType.Address, 0x8094, "smtpemail2"); // Email2OriginalDisplayName
252         createField("smtpemail3", DistinguishedPropertySetType.Address, 0x80A4, "smtpemail3"); // Email3OriginalDisplayName
253 
254         // native email
255         createField("email1", DistinguishedPropertySetType.Address, 0x8083, "email1"); // Email1EmailAddress
256         createField("email2", DistinguishedPropertySetType.Address, 0x8093, "email2"); // Email2EmailAddress
257         createField("email3", DistinguishedPropertySetType.Address, 0x80A3, "email3"); // Email3EmailAddress
258 
259         // email type
260         createField("email1type", DistinguishedPropertySetType.Address, 0x8082, "email1type"); // Email1AddressType
261         createField("email2type", DistinguishedPropertySetType.Address, 0x8092, "email2type"); // Email2AddressType
262         createField("email3type", DistinguishedPropertySetType.Address, 0x80A2, "email3type"); // Email3AddressType
263 
264         createField(URN_SCHEMAS_CONTACTS, "facsimiletelephonenumber"); // PR_BUSINESS_FAX_NUMBER 0x3A24 String
265         createField(URN_SCHEMAS_CONTACTS, "givenName"); // PR_GIVEN_NAME 0x3A06 String
266         createField(URN_SCHEMAS_CONTACTS, "homepostofficebox"); // PR_HOME_ADDRESS_POST_OFFICE_BOX 0x3A5E String
267         createField(URN_SCHEMAS_CONTACTS, "homeCity"); // PR_HOME_ADDRESS_CITY 0x3A59 String
268         createField(URN_SCHEMAS_CONTACTS, "homeCountry"); // PR_HOME_ADDRESS_COUNTRY 0x3A5A String
269         createField(URN_SCHEMAS_CONTACTS, "homePhone"); // PR_HOME_TELEPHONE_NUMBER 0x3A09 String
270         createField(URN_SCHEMAS_CONTACTS, "homePostalCode"); // PR_HOME_ADDRESS_POSTAL_CODE 0x3A5B String
271         createField(URN_SCHEMAS_CONTACTS, "homeState"); // PR_HOME_ADDRESS_STATE_OR_PROVINCE 0x3A5C String
272         createField(URN_SCHEMAS_CONTACTS, "homeStreet"); // PR_HOME_ADDRESS_STREET 0x3A5D String
273         createField(URN_SCHEMAS_CONTACTS, "l"); // workAddressCity DistinguishedPropertySetType.Address/0x00008046/String
274         createField(URN_SCHEMAS_CONTACTS, "manager"); // PR_MANAGER_NAME 0x3A4E String
275         createField(URN_SCHEMAS_CONTACTS, "mobile"); // PR_MOBILE_TELEPHONE_NUMBER 0x3A1C String
276         createField(URN_SCHEMAS_CONTACTS, "namesuffix"); // PR_GENERATION 0x3A05 String
277         createField(URN_SCHEMAS_CONTACTS, "nickname"); // PR_NICKNAME 0x3A4F String
278         createField(URN_SCHEMAS_CONTACTS, "o"); // PR_COMPANY_NAME 0x3A16 String
279         createField(URN_SCHEMAS_CONTACTS, "pager"); // PR_PAGER_TELEPHONE_NUMBER 0x3A21 String
280         createField(URN_SCHEMAS_CONTACTS, "personaltitle"); // PR_DISPLAY_NAME_PREFIX 0x3A45 String
281         createField(URN_SCHEMAS_CONTACTS, "postalcode"); // workAddressPostalCode DistinguishedPropertySetType.Address/0x00008048/String
282         createField(URN_SCHEMAS_CONTACTS, "postofficebox"); // workAddressPostOfficeBox DistinguishedPropertySetType.Address/0x0000804A/String
283         createField(URN_SCHEMAS_CONTACTS, "profession"); // PR_PROFESSION 0x3A46 String
284         createField(URN_SCHEMAS_CONTACTS, "roomnumber"); // PR_OFFICE_LOCATION 0x3A19 String
285         createField(URN_SCHEMAS_CONTACTS, "secretarycn"); // PR_ASSISTANT 0x3A30 String
286         createField(URN_SCHEMAS_CONTACTS, "sn"); // PR_SURNAME 0x3A11 String
287         createField(URN_SCHEMAS_CONTACTS, "spousecn"); // PR_SPOUSE_NAME 0x3A48 String
288         createField(URN_SCHEMAS_CONTACTS, "st"); // workAddressState DistinguishedPropertySetType.Address/0x00008047/String
289         createField(URN_SCHEMAS_CONTACTS, "street"); // workAddressStreet DistinguishedPropertySetType.Address/0x00008045/String
290         createField(URN_SCHEMAS_CONTACTS, "telephoneNumber"); // PR_BUSINESS_TELEPHONE_NUMBER 0x3A08 String
291         createField(URN_SCHEMAS_CONTACTS, "title"); // PR_TITLE 0x3A17 String
292         createField("description", URN_SCHEMAS_HTTPMAIL, "textdescription"); // PR_BODY 0x1000 String
293         createField("im", SCHEMAS_MAPI, "InstMsg"); // InstantMessagingAddress DistinguishedPropertySetType.Address/0x00008062/String
294         createField(URN_SCHEMAS_CONTACTS, "othermobile"); // PR_CAR_TELEPHONE_NUMBER 0x3A1E String
295         createField(URN_SCHEMAS_CONTACTS, "internationalisdnnumber"); // PR_ISDN_NUMBER 0x3A2D String        
296 
297         createField(URN_SCHEMAS_CONTACTS, "otherTelephone"); // PR_OTHER_TELEPHONE_NUMBER 0x3A21 String
298         createField(URN_SCHEMAS_CONTACTS, "homefax"); // PR_HOME_FAX_NUMBER 0x3A25 String
299 
300         createField(URN_SCHEMAS_CONTACTS, "otherstreet"); // PR_OTHER_ADDRESS_STREET 0x3A63 String
301         createField(URN_SCHEMAS_CONTACTS, "otherstate"); // PR_OTHER_ADDRESS_STATE_OR_PROVINCE 0x3A62 String
302         createField(URN_SCHEMAS_CONTACTS, "otherpostofficebox"); // PR_OTHER_ADDRESS_POST_OFFICE_BOX 0x3A64 String
303         createField(URN_SCHEMAS_CONTACTS, "otherpostalcode"); // PR_OTHER_ADDRESS_POSTAL_CODE 0x3A61 String
304         createField(URN_SCHEMAS_CONTACTS, "othercountry"); // PR_OTHER_ADDRESS_COUNTRY 0x3A60 String
305         createField(URN_SCHEMAS_CONTACTS, "othercity"); // PR_OTHER_ADDRESS_CITY 0x3A5F String
306 
307         createField(URN_SCHEMAS_CONTACTS, "gender"); // PR_GENDER 0x3A4D Integer16
308 
309         createField("keywords", SCHEMAS_EXCHANGE, "keywords-utf8", PropertyType.StringArray); // PS_PUBLIC_STRINGS Keywords String
310         //createField("keywords", DistinguishedPropertySetType.PublicStrings, "Keywords", ); // PS_PUBLIC_STRINGS Keywords String
311 
312         // contact private flags
313         createField("private", DistinguishedPropertySetType.Common, 0x8506, "private", PropertyType.Boolean); // True/False
314         createField("sensitivity", 0x0036, PropertyType.Integer); // PR_SENSITIVITY SENSITIVITY_PRIVATE = 2, SENSITIVITY_PERSONAL = 1, SENSITIVITY_NONE = 0
315 
316         createField("haspicture", DistinguishedPropertySetType.Address, 0x8015, "haspicture", PropertyType.Boolean); // True/False
317 
318         createField(URN_SCHEMAS_CALENDAR, "fburl"); // freeBusyLocation
319 
320         // OWA settings
321         createField("messageclass", 0x001a, PropertyType.String);
322         createField("roamingxmlstream", 0x7c08, PropertyType.Binary);
323         createField("roamingdictionary", 0x7c07, PropertyType.Binary);
324 
325         createField(DAV, "ishidden");
326 
327         // attachment content
328         createField("attachDataBinary", 0x3701, PropertyType.Binary);
329 
330         createField("attachmentContactPhoto", 0x7FFF, PropertyType.Boolean); // PR_ATTACHMENT_CONTACTPHOTO
331         createField("renderingPosition", 0x370B, PropertyType.Integer);// PR_RENDERING_POSITION
332         //createField("attachFilename", 0x3704, PropertyType.String); // PR_ATTACH_FILENAME
333         createField("attachExtension", 0x3703, PropertyType.String); // PR_ATTACH_EXTENSION
334 
335         createField("xmozlastack", DistinguishedPropertySetType.PublicStrings);
336         createField("xmozsnoozetime", DistinguishedPropertySetType.PublicStrings);
337         createField("xmozsendinvitations", DistinguishedPropertySetType.PublicStrings);
338 
339         // task
340         createField(URN_SCHEMAS_MAILHEADER, "importance");//PS_INTERNET_HEADERS/importance
341         createField("percentcomplete", DistinguishedPropertySetType.Task, 0x8102, "percentcomplete", PropertyType.Double);
342         createField("taskstatus", DistinguishedPropertySetType.Task, 0x8101, "taskstatus", PropertyType.Integer);
343         createField("startdate", DistinguishedPropertySetType.Task, 0x8104, "startdate", PropertyType.SystemTime);
344         createField("duedate", DistinguishedPropertySetType.Task, 0x8105, "duedate", PropertyType.SystemTime);
345         createField("datecompleted", DistinguishedPropertySetType.Task, 0x810F, "datecompleted", PropertyType.SystemTime);
346         createField("iscomplete", DistinguishedPropertySetType.Task, 0x811C, "iscomplete", PropertyType.Boolean);
347 
348 
349         createField("commonstart", DistinguishedPropertySetType.Common, 0x8516, "commonstart", PropertyType.SystemTime);
350         createField("commonend", DistinguishedPropertySetType.Common, 0x8517, "commonend", PropertyType.SystemTime);
351     }
352 
353     protected static String toHexString(int propertyTag) {
354         StringBuilder hexValue = new StringBuilder(Integer.toHexString(propertyTag));
355         while (hexValue.length() < 4) {
356             hexValue.insert(0, '0');
357         }
358         return hexValue.toString();
359     }
360 
361     protected static void createField(String alias, int propertyTag, PropertyType propertyType) {
362         String name = 'x' + toHexString(propertyTag) + propertyTypeMap.get(propertyType);
363         Field field;
364         if (propertyType == PropertyType.Binary) {
365             field = new Field(alias, SCHEMAS_MAPI_PROPTAG, name, propertyType, null, "bin.base64", name);
366         } else {
367             field = new Field(alias, SCHEMAS_MAPI_PROPTAG, name, propertyType);
368         }
369         fieldMap.put(field.alias, field);
370     }
371 
372     protected static void createField(String alias,@SuppressWarnings("SameParameterValue") DistinguishedPropertySetType propertySetType) {
373         Field field = new Field(Namespace.getNamespace(SCHEMAS_MAPI_STRING.getURI() +
374                 '{' + distinguishedPropertySetMap.get(propertySetType) + "}/"), alias);
375         fieldMap.put(field.alias, field);
376     }
377 
378     protected static void createField(String alias, @SuppressWarnings("SameParameterValue") DistinguishedPropertySetType propertySetType, int propertyTag, String responseAlias) {
379         createField(alias, propertySetType, propertyTag, responseAlias, null);
380     }
381 
382     protected static void createField(String alias, DistinguishedPropertySetType propertySetType, int propertyTag, String responseAlias, PropertyType propertyType) {
383         String name;
384         String updateAlias;
385         if (propertySetType == DistinguishedPropertySetType.Address) {
386             // Address namespace expects integer names
387             name = String.valueOf(propertyTag);
388             updateAlias = "_x0030_x" + toHexString(propertyTag);
389         } else if (propertySetType == DistinguishedPropertySetType.Task) {
390             name = "0x" + toHexString(propertyTag);
391             updateAlias = "0x0000" + toHexString(propertyTag);
392         } else {
393             // Common namespace expects hex names
394             name = "0x" + toHexString(propertyTag);
395             updateAlias = "_x0030_x" + toHexString(propertyTag);
396         }
397         Field field = new Field(alias, Namespace.getNamespace(SCHEMAS_MAPI_ID.getURI() +
398                 '{' + distinguishedPropertySetMap.get(propertySetType) + "}/"), name, propertyType, responseAlias, null, updateAlias);
399         fieldMap.put(field.alias, field);
400     }
401 
402     protected static void createField(Namespace namespace, String name) {
403         Field field = new Field(namespace, name);
404         fieldMap.put(field.alias, field);
405     }
406 
407     protected static void createField(String alias, Namespace namespace, String name) {
408         Field field = new Field(alias, namespace, name, null);
409         fieldMap.put(field.alias, field);
410     }
411 
412     @SuppressWarnings("SameParameterValue")
413     protected static void createField(String alias, Namespace namespace, String name, PropertyType propertyType) {
414         Field field = new Field(alias, namespace, name, propertyType);
415         fieldMap.put(field.alias, field);
416     }
417 
418     private final DavPropertyName davPropertyName;
419     protected final String alias;
420     protected final String uri;
421     protected final String requestPropertyString;
422     protected final DavPropertyName responsePropertyName;
423     protected final DavPropertyName updatePropertyName;
424     protected final String cast;
425     protected final boolean isIntValue;
426     protected final boolean isMultivalued;
427     protected final boolean isBooleanValue;
428     protected final boolean isFloatValue;
429     protected final boolean isDateValue;
430 
431     /**
432      * Create field for namespace and name, use name as alias.
433      *
434      * @param namespace Exchange namespace
435      * @param name      Exchange name
436      */
437     protected Field(Namespace namespace, String name) {
438         this(name, namespace, name, null);
439     }
440 
441     /**
442      * Create field for namespace and name of type propertyType.
443      *
444      * @param alias        logical name in DavMail
445      * @param namespace    Exchange namespace
446      * @param name         Exchange name
447      * @param propertyType property type
448      */
449     protected Field(String alias, Namespace namespace, String name, PropertyType propertyType) {
450         this(alias, namespace, name, propertyType, null, null, name);
451     }
452 
453     /**
454      * Create field for namespace and name of type propertyType.
455      *
456      * @param alias         logical name in DavMail
457      * @param namespace     Exchange namespace
458      * @param name          Exchange name
459      * @param propertyType  property type
460      * @param responseAlias property name in SEARCH response (as responsealias in request)
461      * @param cast          response cast type (e.g. bin.base64)
462      * @param updateAlias   some properties use a different alias in PROPPATCH requests
463      */
464     protected Field(String alias, Namespace namespace, String name, PropertyType propertyType, String responseAlias, String cast, String updateAlias) {
465         this.alias = alias;
466 
467         // property name in PROPFIND requests
468         davPropertyName = DavPropertyName.create(name, namespace);
469         // property name in PROPPATCH requests
470         updatePropertyName = DavPropertyName.create(updateAlias, namespace);
471 
472         // a few type based flags
473         isMultivalued = propertyType != null && propertyType.toString().endsWith("Array");
474         isIntValue = propertyType == PropertyType.Long || propertyType == PropertyType.Integer || propertyType == PropertyType.Short;
475         isBooleanValue = propertyType == PropertyType.Boolean;
476         isFloatValue = propertyType == PropertyType.Float || propertyType == PropertyType.Double;
477         isDateValue = propertyType == PropertyType.SystemTime;
478 
479         this.uri = namespace.getURI() + name;
480         if (responseAlias == null) {
481             this.requestPropertyString = '"' + uri + '"';
482             this.responsePropertyName = davPropertyName;
483         } else {
484             this.requestPropertyString = '"' + uri + "\" as " + responseAlias;
485             this.responsePropertyName = DavPropertyName.create(responseAlias, EMPTY);
486         }
487         this.cast = cast;
488     }
489 
490     /**
491      * Property uri.
492      *
493      * @return uri
494      */
495     public String getUri() {
496         return uri;
497     }
498 
499     /**
500      * Integer value property type.
501      *
502      * @return true if the field value is integer
503      */
504     public boolean isIntValue() {
505         return isIntValue;
506     }
507 
508     /**
509      * Get Field by alias.
510      *
511      * @param alias field alias
512      * @return field
513      */
514     public static Field get(String alias) {
515         Field field = fieldMap.get(alias);
516         if (field == null) {
517             throw new IllegalArgumentException("Unknown field: " + alias);
518         }
519         return field;
520     }
521 
522     /**
523      * Get Mime header field.
524      *
525      * @param headerName header name
526      * @return field object
527      */
528     public static Field getHeader(String headerName) {
529         return new Field(SCHEMAS_MAPI_STRING_INTERNET_HEADERS, headerName);
530     }
531 
532     /**
533      * Create DavProperty object for field alias and value.
534      *
535      * @param alias DavMail field alias
536      * @param value field value
537      * @return DavProperty with value or DavPropertyName for null values
538      */
539     public static PropEntry createDavProperty(String alias, String value) {
540         Field field = Field.get(alias);
541         if (value == null) {
542             // return DavPropertyName to remove property
543             return field.updatePropertyName;
544         } else if (field.isMultivalued) {
545             // multivalued field, split values separated by \n
546             List<XmlSerializable> valueList = new ArrayList<>();
547             String[] values = value.split(",");
548             for (final String singleValue : values) {
549                 valueList.add(document -> DomUtil.createElement(document, "v", XML, singleValue));
550             }
551 
552             return new DefaultDavProperty<>(field.updatePropertyName, valueList);
553         } else if (field.isBooleanValue && !"haspicture".equals(alias)) {
554             if ("true".equals(value)) {
555                 return new DefaultDavProperty<>(field.updatePropertyName, "1");
556             } else if ("false".equals(value)) {
557                 return new DefaultDavProperty<>(field.updatePropertyName, "0");
558             } else {
559                 throw new RuntimeException("Invalid value for " + field.alias + ": " + value);
560             }
561         } else {
562             return new DefaultDavProperty<>(field.updatePropertyName, value);
563         }
564     }
565 
566     /**
567      * Create property value object for field and value.
568      *
569      * @param alias field alias
570      * @param value field value
571      * @return property value object
572      * @see davmail.http.request.ExchangePropPatchRequest
573      */
574     public static PropertyValue createPropertyValue(String alias, String value) {
575         Field field = Field.get(alias);
576         DavPropertyName davPropertyName = field.davPropertyName;
577         if (value == null) {
578             // return DavPropertyName to remove property
579             return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName());
580         } else if (field.isMultivalued) {
581             StringBuilder buffer = new StringBuilder();
582             // multivalued field, split values separated by \n
583             String[] values = value.split("\n");
584             for (final String singleValue : values) {
585                 buffer.append("<v>");
586                 buffer.append(StringUtil.xmlEncode(singleValue));
587                 buffer.append("</v>");
588             }
589             return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), buffer.toString());
590         } else if (field.isBooleanValue) {
591             if ("true".equals(value)) {
592                 return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), "1", "boolean");
593             } else if ("false".equals(value)) {
594                 return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), "0", "boolean");
595             } else {
596                 throw new RuntimeException("Invalid value for " + field.alias + ": " + value);
597             }
598         } else if (field.isFloatValue) {
599             return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), StringUtil.xmlEncode(value), "float");
600         } else if (field.isIntValue) {
601             return new PropertyValue(field.updatePropertyName.getNamespace().getURI(), field.updatePropertyName.getName(), StringUtil.xmlEncode(value), "int");
602         } else if (field.isDateValue) {
603             return new PropertyValue(field.updatePropertyName.getNamespace().getURI(), field.updatePropertyName.getName(), StringUtil.xmlEncode(value), "dateTime.tz");
604         } else {
605             return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), StringUtil.xmlEncode(value));
606         }
607     }
608 
609     /**
610      * SEARCH request property name for alias
611      *
612      * @param alias field alias
613      * @return request property string
614      */
615     public static String getRequestPropertyString(String alias) {
616         return Field.get(alias).requestPropertyString;
617     }
618 
619     /**
620      * PROPFIND request property name
621      *
622      * @param alias field alias
623      * @return request property name
624      */
625     public static DavPropertyName getPropertyName(String alias) {
626         return Field.get(alias).davPropertyName;
627     }
628 
629     /**
630      * SEARCH response property name
631      *
632      * @param alias field alias
633      * @return response property name
634      */
635     public static DavPropertyName getResponsePropertyName(String alias) {
636         return Field.get(alias).responsePropertyName;
637     }
638 }