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.ews;
20  
21  import davmail.util.StringUtil;
22  
23  /**
24   * Extended MAPI property.
25   */
26  public class ExtendedFieldURI implements FieldURI {
27  
28      @SuppressWarnings({"UnusedDeclaration"})
29      protected enum PropertyType {
30          ApplicationTime, ApplicationTimeArray, Binary, BinaryArray, Boolean, CLSID, CLSIDArray, Currency, CurrencyArray,
31          Double, DoubleArray, Error, Float, FloatArray, Integer, IntegerArray, Long, LongArray, Null, Object,
32          ObjectArray, Short, ShortArray, SystemTime, SystemTimeArray, String, StringArray
33      }
34  
35      @SuppressWarnings({"UnusedDeclaration"})
36      public enum DistinguishedPropertySetType {
37          Meeting, Appointment, Common, PublicStrings, Address, InternetHeaders, CalendarAssistant, UnifiedMessaging, Task
38      }
39  
40  
41      protected String propertyTag;
42      protected DistinguishedPropertySetType distinguishedPropertySetId;
43      protected String propertyName;
44      protected int propertyId;
45      protected final PropertyType propertyType;
46  
47      private String graphId;
48  
49      /**
50       * Create extended field uri.
51       *
52       * @param intPropertyTag property tag as int
53       * @param propertyType   property type
54       */
55      public ExtendedFieldURI(int intPropertyTag, PropertyType propertyType) {
56          this.propertyTag = "0x" + Integer.toHexString(intPropertyTag);
57          this.propertyType = propertyType;
58      }
59  
60      /**
61       * Create extended field uri.
62       *
63       * @param distinguishedPropertySetId distinguished property set id
64       * @param propertyId                 property id
65       * @param propertyType               property type
66       */
67      public ExtendedFieldURI(DistinguishedPropertySetType distinguishedPropertySetId, int propertyId, PropertyType propertyType) {
68          this.distinguishedPropertySetId = distinguishedPropertySetId;
69          this.propertyId = propertyId;
70          this.propertyType = propertyType;
71      }
72  
73      /**
74       * Create extended field uri.
75       *
76       * @param distinguishedPropertySetId distinguished property set id
77       * @param propertyId                 property id
78       * @param propertyType               property type
79       * @param graphId                    graphId
80       */
81      public ExtendedFieldURI(DistinguishedPropertySetType distinguishedPropertySetId, int propertyId, PropertyType propertyType, String graphId) {
82          this.distinguishedPropertySetId = distinguishedPropertySetId;
83          this.propertyId = propertyId;
84          this.propertyType = propertyType;
85          this.graphId = graphId;
86      }
87  
88      /**
89       * Create extended field uri.
90       *
91       * @param distinguishedPropertySetId distinguished property set id
92       * @param propertyName               property name
93       */
94      public ExtendedFieldURI(DistinguishedPropertySetType distinguishedPropertySetId, String propertyName) {
95          this.distinguishedPropertySetId = distinguishedPropertySetId;
96          this.propertyName = propertyName;
97          this.propertyType = PropertyType.String;
98      }
99  
100     /**
101      * Create extended field uri.
102      *
103      * @param distinguishedPropertySetId distinguished property set id
104      * @param propertyName               property name
105      * @param propertyType               property type
106      */
107     public ExtendedFieldURI(DistinguishedPropertySetType distinguishedPropertySetId, String propertyName, PropertyType propertyType) {
108         this.distinguishedPropertySetId = distinguishedPropertySetId;
109         this.propertyName = propertyName;
110         this.propertyType = propertyType;
111     }
112 
113     public ExtendedFieldURI(int intPropertyTag, PropertyType propertyType, String graphId) {
114         this(intPropertyTag, propertyType);
115         this.graphId = graphId;
116     }
117 
118     public void appendTo(StringBuilder buffer) {
119         buffer.append("<t:ExtendedFieldURI");
120         if (propertyTag != null) {
121             buffer.append(" PropertyTag=\"").append(propertyTag).append('"');
122         }
123         if (distinguishedPropertySetId != null) {
124             buffer.append(" DistinguishedPropertySetId=\"").append(distinguishedPropertySetId).append('"');
125         }
126         if (propertyName != null) {
127             buffer.append(" PropertyName=\"").append(propertyName).append('"');
128         }
129         if (propertyId != 0) {
130             buffer.append(" PropertyId=\"").append(propertyId).append('"');
131         }
132         if (propertyType != null) {
133             buffer.append(" PropertyType=\"").append(propertyType.toString()).append('"');
134         }
135         buffer.append("/>");
136     }
137 
138     public void appendValue(StringBuilder buffer, String itemType, String value) {
139         if (itemType != null) {
140             appendTo(buffer);
141             buffer.append("<t:");
142             buffer.append(itemType);
143             buffer.append('>');
144         }
145         buffer.append("<t:ExtendedProperty>");
146         appendTo(buffer);
147         if (propertyType == PropertyType.StringArray) {
148             buffer.append("<t:Values>");
149             String[] values = value.split(",");
150             for (final String singleValue : values) {
151                 buffer.append("<t:Value>");
152                 buffer.append(StringUtil.xmlEncode(singleValue));
153                 buffer.append("</t:Value>");
154 
155             }
156             buffer.append("</t:Values>");
157         } else {
158             buffer.append("<t:Value>");
159             if ("0x10f3".equals(propertyTag)) {
160                 buffer.append(StringUtil.xmlEncode(StringUtil.encodeUrlcompname(value)));
161             } else {
162                 buffer.append(StringUtil.xmlEncode(value));
163             }
164             buffer.append("</t:Value>");
165         }
166         buffer.append("</t:ExtendedProperty>");
167         if (itemType != null) {
168             buffer.append("</t:");
169             buffer.append(itemType);
170             buffer.append('>');
171         }
172     }
173 
174     /**
175      * Field name in EWS response.
176      *
177      * @return field name in response
178      */
179     public String getResponseName() {
180         if (propertyTag != null) {
181             return propertyTag;
182         } else if (propertyName != null) {
183             return propertyName;
184         } else {
185             return String.valueOf(propertyId);
186         }
187     }
188 
189     @Override
190     public String getGraphId() {
191         if (graphId != null) {
192             return graphId;
193         }
194         // PropertyId values may only be in one of the following formats:
195         // 'MapiPropertyType namespaceGuid Name propertyName', 'MapiPropertyType namespaceGuid Id propertyId' or 'MapiPropertyType propertyTag'.
196 
197         String namespaceGuid = null;
198 
199         if (distinguishedPropertySetId == DistinguishedPropertySetType.PublicStrings) {
200             namespaceGuid = "{00020329-0000-0000-c000-000000000046}";
201         }
202         if (distinguishedPropertySetId == DistinguishedPropertySetType.InternetHeaders) {
203             namespaceGuid = "{00020386-0000-0000-c000-000000000046}";
204         }
205         if (distinguishedPropertySetId == DistinguishedPropertySetType.Common) {
206             namespaceGuid = "{00062008-0000-0000-c000-000000000046}";
207         }
208         if (distinguishedPropertySetId == DistinguishedPropertySetType.Address) {
209             namespaceGuid = "{00062004-0000-0000-c000-000000000046}";
210         }
211         if (distinguishedPropertySetId == DistinguishedPropertySetType.Task) {
212             namespaceGuid = "{00062003-0000-0000-c000-000000000046}";
213         }
214 
215         StringBuilder buffer = new StringBuilder();
216         if (namespaceGuid != null) {
217             buffer.append(propertyType.name()).append(" ").append(namespaceGuid);
218             if (propertyName != null) {
219                 buffer.append(" Name ").append(propertyName);
220             } else {
221                 buffer.append(" Id ").append("0x").append(Integer.toHexString(propertyId));
222             }
223         } else if (propertyTag != null) {
224             buffer.append(propertyType.name()).append(" ").append(propertyTag);
225         } else {
226             throw new IllegalStateException("Unsupported graph property for graph " + getResponseName());
227         }
228         return buffer.toString();
229     }
230 
231     @Override
232     public boolean isMultiValued() {
233         return propertyType == PropertyType.StringArray;
234     }
235 
236     public boolean isNumber() {
237         return propertyType == PropertyType.Short || propertyType == PropertyType.Integer || propertyType == PropertyType.Long || propertyType == PropertyType.Double;
238     }
239 
240     @Override
241     public boolean isBoolean() {
242         return propertyType == PropertyType.Boolean;
243     }
244 
245 }
246