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(DistinguishedPropertySetType distinguishedPropertySetId, String propertyName, PropertyType propertyType, String graphId) {
114         this.distinguishedPropertySetId = distinguishedPropertySetId;
115         this.propertyName = propertyName;
116         this.propertyType = propertyType;
117         this.graphId = graphId;
118     }
119 
120     public ExtendedFieldURI(int intPropertyTag, PropertyType propertyType, String graphId) {
121         this(intPropertyTag, propertyType);
122         this.graphId = graphId;
123     }
124 
125     public void appendTo(StringBuilder buffer) {
126         buffer.append("<t:ExtendedFieldURI");
127         if (propertyTag != null) {
128             buffer.append(" PropertyTag=\"").append(propertyTag).append('"');
129         }
130         if (distinguishedPropertySetId != null) {
131             buffer.append(" DistinguishedPropertySetId=\"").append(distinguishedPropertySetId).append('"');
132         }
133         if (propertyName != null) {
134             buffer.append(" PropertyName=\"").append(propertyName).append('"');
135         }
136         if (propertyId != 0) {
137             buffer.append(" PropertyId=\"").append(propertyId).append('"');
138         }
139         if (propertyType != null) {
140             buffer.append(" PropertyType=\"").append(propertyType.toString()).append('"');
141         }
142         buffer.append("/>");
143     }
144 
145     public void appendValue(StringBuilder buffer, String itemType, String value) {
146         if (itemType != null) {
147             appendTo(buffer);
148             buffer.append("<t:");
149             buffer.append(itemType);
150             buffer.append('>');
151         }
152         buffer.append("<t:ExtendedProperty>");
153         appendTo(buffer);
154         if (propertyType == PropertyType.StringArray) {
155             buffer.append("<t:Values>");
156             String[] values = value.split(",");
157             for (final String singleValue : values) {
158                 buffer.append("<t:Value>");
159                 buffer.append(StringUtil.xmlEncode(singleValue));
160                 buffer.append("</t:Value>");
161 
162             }
163             buffer.append("</t:Values>");
164         } else {
165             buffer.append("<t:Value>");
166             if ("0x10f3".equals(propertyTag)) {
167                 buffer.append(StringUtil.xmlEncode(StringUtil.encodeUrlcompname(value)));
168             } else {
169                 buffer.append(StringUtil.xmlEncode(value));
170             }
171             buffer.append("</t:Value>");
172         }
173         buffer.append("</t:ExtendedProperty>");
174         if (itemType != null) {
175             buffer.append("</t:");
176             buffer.append(itemType);
177             buffer.append('>');
178         }
179     }
180 
181     /**
182      * Field name in EWS response.
183      *
184      * @return field name in response
185      */
186     public String getResponseName() {
187         if (propertyTag != null) {
188             return propertyTag;
189         } else if (propertyName != null) {
190             return propertyName;
191         } else {
192             return String.valueOf(propertyId);
193         }
194     }
195 
196     @Override
197     public String getGraphId() {
198         if (graphId != null) {
199             return graphId;
200         }
201         // PropertyId values may only be in one of the following formats:
202         // 'MapiPropertyType namespaceGuid Name propertyName', 'MapiPropertyType namespaceGuid Id propertyId' or 'MapiPropertyType propertyTag'.
203 
204         String namespaceGuid = null;
205 
206         if (distinguishedPropertySetId == DistinguishedPropertySetType.PublicStrings) {
207             namespaceGuid = "{00020329-0000-0000-c000-000000000046}";
208         }
209         if (distinguishedPropertySetId == DistinguishedPropertySetType.InternetHeaders) {
210             namespaceGuid = "{00020386-0000-0000-c000-000000000046}";
211         }
212         if (distinguishedPropertySetId == DistinguishedPropertySetType.Common) {
213             namespaceGuid = "{00062008-0000-0000-c000-000000000046}";
214         }
215         if (distinguishedPropertySetId == DistinguishedPropertySetType.Address) {
216             namespaceGuid = "{00062004-0000-0000-c000-000000000046}";
217         }
218         if (distinguishedPropertySetId == DistinguishedPropertySetType.Task) {
219             namespaceGuid = "{00062003-0000-0000-c000-000000000046}";
220         }
221 
222         StringBuilder buffer = new StringBuilder();
223         if (namespaceGuid != null) {
224             buffer.append(propertyType.name()).append(" ").append(namespaceGuid);
225             if (propertyName != null) {
226                 buffer.append(" Name ").append(propertyName);
227             } else {
228                 buffer.append(" Id ").append("0x").append(Integer.toHexString(propertyId));
229             }
230         } else if (propertyTag != null) {
231             buffer.append(propertyType.name()).append(" ").append(propertyTag);
232         } else {
233             throw new IllegalStateException("Unsupported graph property for graph " + getResponseName());
234         }
235         return buffer.toString();
236     }
237 
238     @Override
239     public boolean isMultiValued() {
240         return propertyType == PropertyType.StringArray;
241     }
242 
243     public boolean isNumber() {
244         return propertyType == PropertyType.Short || propertyType == PropertyType.Integer || propertyType == PropertyType.Long || propertyType == PropertyType.Double;
245     }
246 
247     public boolean isBinary() {
248         return propertyType == PropertyType.Binary;
249     }
250 
251     @Override
252     public boolean isBoolean() {
253         return propertyType == PropertyType.Boolean;
254     }
255 
256 }
257