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  import java.util.List;
24  
25  /**
26   * Unindexed Field URI
27   */
28  public class UnindexedFieldURI implements FieldURI {
29      protected final String fieldURI;
30      protected final String fieldName;
31  
32      /**
33       * Create unindexed field URI.
34       *
35       * @param fieldURI field name
36       */
37      public UnindexedFieldURI(String fieldURI) {
38          this.fieldURI = fieldURI;
39          int colonIndex = fieldURI.indexOf(':');
40          if (colonIndex < 0) {
41              fieldName = fieldURI;
42          } else {
43              fieldName = fieldURI.substring(colonIndex + 1);
44          }
45      }
46  
47      public void appendTo(StringBuilder buffer) {
48          buffer.append("<t:FieldURI FieldURI=\"").append(fieldURI).append("\"/>");
49      }
50  
51      public void appendValue(StringBuilder buffer, String itemType, String value) {
52          if (fieldURI.startsWith("message") && itemType != null) {
53              itemType = "Message";
54          } else if (fieldURI.startsWith("calendar") && itemType != null) {
55              itemType = "CalendarItem";
56          } else if (fieldURI.startsWith("task") && itemType != null) {
57              itemType = "Task";
58          } else if (fieldURI.startsWith("contacts") && itemType != null) {
59              itemType = "Contact";
60          }
61          if (itemType != null) {
62              appendTo(buffer);
63              buffer.append("<t:");
64              buffer.append(itemType);
65              buffer.append('>');
66          }
67          if ("MeetingTimeZone".equals(fieldName)) {
68              buffer.append("<t:MeetingTimeZone TimeZoneName=\"");
69              buffer.append(StringUtil.xmlEncodeAttribute(value));
70              buffer.append("\"></t:MeetingTimeZone>");
71          } else if ("StartTimeZone".equals(fieldName)) {
72              buffer.append("<t:StartTimeZone Id=\"");
73              buffer.append(StringUtil.xmlEncodeAttribute(value));
74              buffer.append("\"></t:StartTimeZone>");
75          } else if ("EndTimeZone".equals(fieldName)) {
76              buffer.append("<t:EndTimeZone Id=\"");
77              buffer.append(StringUtil.xmlEncodeAttribute(value));
78              buffer.append("\"></t:EndTimeZone>");
79          } else {
80              buffer.append("<t:");
81              buffer.append(fieldName);
82              buffer.append('>');
83              buffer.append(StringUtil.xmlEncodeAttribute(value));
84              buffer.append("</t:");
85              buffer.append(fieldName);
86              buffer.append('>');
87          }
88          if (itemType != null) {
89              buffer.append("</t:");
90              buffer.append(itemType);
91              buffer.append('>');
92          }
93      }
94  
95      public void appendValues(StringBuilder buffer, String itemType, List<String> values) {
96          if (fieldURI.startsWith("message") && itemType != null) {
97              itemType = "Message";
98          } else if (fieldURI.startsWith("calendar") && itemType != null) {
99              itemType = "CalendarItem";
100         } else if (fieldURI.startsWith("task") && itemType != null) {
101             itemType = "Task";
102         } else if (fieldURI.startsWith("contacts") && itemType != null) {
103             itemType = "Contact";
104         } else if (fieldURI.startsWith("distributionlist") && itemType != null) {
105             itemType = "DistributionList";
106         }
107         if (!values.isEmpty()) {
108             if (itemType != null) {
109                 appendTo(buffer);
110                 buffer.append("<t:");
111                 buffer.append(itemType);
112                 buffer.append('>');
113             }
114             buffer.append("<t:");
115             buffer.append(fieldName);
116             buffer.append('>');
117             for (String value : values) {
118                 if ("RequiredAttendees".equals(fieldName) || "OptionalAttendees".equals(fieldName)) {
119                     buffer.append("<t:Attendee><t:Mailbox><t:EmailAddress>");
120                     buffer.append(StringUtil.xmlEncodeAttribute(value));
121                     buffer.append("</t:EmailAddress></t:Mailbox></t:Attendee>");
122                 } else if ("Members".equals(fieldName)) {
123                     if (value.toLowerCase().startsWith("mailto:")) {
124                         buffer.append("<t:Member><t:Mailbox><t:EmailAddress>");
125                         buffer.append(StringUtil.xmlEncodeAttribute(value.substring(7)));
126                         buffer.append("</t:EmailAddress></t:Mailbox></t:Member>");
127                     } else if (value.startsWith("urn:uuid:")){
128                         buffer.append("<t:Member><t:Mailbox><t:MailboxType>PrivateDL</t:MailboxType><t:ItemId Id=\"");
129                         buffer.append(StringUtil.xmlEncodeAttribute(value.substring(9)));
130                         buffer.append("\"/></t:Mailbox></t:Member>");
131                     }
132                 } else {
133                     buffer.append(StringUtil.xmlEncodeAttribute(value));
134                 }
135             }
136 
137             buffer.append("</t:");
138             buffer.append(fieldName);
139             buffer.append('>');
140 
141             if (itemType != null) {
142                 buffer.append("</t:");
143                 buffer.append(itemType);
144                 buffer.append('>');
145             }
146         } else if (itemType != null) {
147             // append field name only to remove values
148             appendTo(buffer);
149         }
150 
151     }
152 
153     public String getResponseName() {
154         return fieldName;
155     }
156 
157 }