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      protected 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      /**
48       * Create extended field uri.
49       *
50       * @param intPropertyTag property tag as int
51       * @param propertyType   property type
52       */
53      public ExtendedFieldURI(int intPropertyTag, PropertyType propertyType) {
54          this.propertyTag = "0x" + Integer.toHexString(intPropertyTag);
55          this.propertyType = propertyType;
56      }
57  
58      /**
59       * Create extended field uri.
60       *
61       * @param distinguishedPropertySetId distinguished property set id
62       * @param propertyId                 property id
63       * @param propertyType               property type
64       */
65      public ExtendedFieldURI(DistinguishedPropertySetType distinguishedPropertySetId, int propertyId, PropertyType propertyType) {
66          this.distinguishedPropertySetId = distinguishedPropertySetId;
67          this.propertyId = propertyId;
68          this.propertyType = propertyType;
69      }
70  
71      /**
72       * Create extended field uri.
73       *
74       * @param distinguishedPropertySetId distinguished property set id
75       * @param propertyName               property name
76       */
77      public ExtendedFieldURI(DistinguishedPropertySetType distinguishedPropertySetId, String propertyName) {
78          this.distinguishedPropertySetId = distinguishedPropertySetId;
79          this.propertyName = propertyName;
80          this.propertyType = PropertyType.String;
81      }
82  
83      /**
84       * Create extended field uri.
85       *
86       * @param distinguishedPropertySetId distinguished property set id
87       * @param propertyName               property name
88       * @param propertyType               property type
89       */
90      public ExtendedFieldURI(DistinguishedPropertySetType distinguishedPropertySetId, String propertyName, PropertyType propertyType) {
91          this.distinguishedPropertySetId = distinguishedPropertySetId;
92          this.propertyName = propertyName;
93          this.propertyType = propertyType;
94      }
95  
96      public void appendTo(StringBuilder buffer) {
97          buffer.append("<t:ExtendedFieldURI");
98          if (propertyTag != null) {
99              buffer.append(" PropertyTag=\"").append(propertyTag).append('"');
100         }
101         if (distinguishedPropertySetId != null) {
102             buffer.append(" DistinguishedPropertySetId=\"").append(distinguishedPropertySetId).append('"');
103         }
104         if (propertyName != null) {
105             buffer.append(" PropertyName=\"").append(propertyName).append('"');
106         }
107         if (propertyId != 0) {
108             buffer.append(" PropertyId=\"").append(propertyId).append('"');
109         }
110         if (propertyType != null) {
111             buffer.append(" PropertyType=\"").append(propertyType.toString()).append('"');
112         }
113         buffer.append("/>");
114     }
115 
116     public void appendValue(StringBuilder buffer, String itemType, String value) {
117         if (itemType != null) {
118             appendTo(buffer);
119             buffer.append("<t:");
120             buffer.append(itemType);
121             buffer.append('>');
122         }
123         buffer.append("<t:ExtendedProperty>");
124         appendTo(buffer);
125         if (propertyType == PropertyType.StringArray) {
126             buffer.append("<t:Values>");
127             String[] values = value.split(",");
128             for (final String singleValue : values) {
129                 buffer.append("<t:Value>");
130                 buffer.append(StringUtil.xmlEncode(singleValue));
131                 buffer.append("</t:Value>");
132 
133             }
134             buffer.append("</t:Values>");
135         } else {
136             buffer.append("<t:Value>");
137             if ("0x10f3".equals(propertyTag)) {
138                 buffer.append(StringUtil.xmlEncode(StringUtil.encodeUrlcompname(value)));
139             } else {
140                 buffer.append(StringUtil.xmlEncode(value));
141             }
142             buffer.append("</t:Value>");
143         }
144         buffer.append("</t:ExtendedProperty>");
145         if (itemType != null) {
146             buffer.append("</t:");
147             buffer.append(itemType);
148             buffer.append('>');
149         }
150     }
151 
152     /**
153      * Field name in EWS response.
154      *
155      * @return field name in response
156      */
157     public String getResponseName() {
158         if (propertyTag != null) {
159             return propertyTag;
160         } else if (propertyName != null) {
161             return propertyName;
162         } else {
163             return String.valueOf(propertyId);
164         }
165     }
166 
167 }
168