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