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.exchange.XMLStreamUtil;
22  
23  import javax.xml.stream.XMLStreamException;
24  import javax.xml.stream.XMLStreamReader;
25  
26  /**
27   * Resolve Names method.
28   */
29  public class ResolveNamesMethod extends EWSMethod {
30      protected static final AttributeOption RETURN_FULL_CONTACT_DATA = new AttributeOption("ReturnFullContactData", "true");
31  
32      /**
33       * Build Resolve Names method
34       *
35       * @param value search value
36       */
37      public ResolveNamesMethod(String value) {
38          super("Contact", "ResolveNames", "ResolutionSet");
39          addMethodOption(SearchScope.ActiveDirectory);
40          addMethodOption(RETURN_FULL_CONTACT_DATA);
41          unresolvedEntry = new ElementOption("m:UnresolvedEntry", value);
42      }
43  
44      @Override
45      protected Item handleItem(XMLStreamReader reader) throws XMLStreamException {
46          Item responseItem = new Item();
47          responseItem.type = "Contact";
48          // skip to Contact
49          while (reader.hasNext() && !XMLStreamUtil.isStartTag(reader, "Resolution")) {
50              reader.next();
51          }
52          while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "Resolution")) {
53              reader.next();
54              if (XMLStreamUtil.isStartTag(reader)) {
55                  String tagLocalName = reader.getLocalName();
56                  if ("Mailbox".equals(tagLocalName)) {
57                      handleMailbox(reader, responseItem);
58                  } else if ("Contact".equals(tagLocalName)) {
59                      handleContact(reader, responseItem);
60                  }
61              }
62          }
63          return responseItem;
64      }
65  
66      protected void handleMailbox(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
67          while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "Mailbox")) {
68              reader.next();
69              if (XMLStreamUtil.isStartTag(reader)) {
70                  String tagLocalName = reader.getLocalName();
71                  if ("Name".equals(tagLocalName)) {
72                      responseItem.put(tagLocalName, XMLStreamUtil.getElementText(reader));
73                  } else if ("EmailAddress".equals(tagLocalName)) {
74                      responseItem.put(tagLocalName, XMLStreamUtil.getElementText(reader));
75                  }
76              }
77          }
78      }
79  
80      protected void handleContact(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
81          while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "Contact")) {
82              reader.next();
83              if (XMLStreamUtil.isStartTag(reader)) {
84                  String tagLocalName = reader.getLocalName();
85                  if ("EmailAddresses".equals(tagLocalName)) {
86                      handleEmailAddresses(reader, responseItem);
87                  } else if ("PhysicalAddresses".equals(tagLocalName)) {
88                      handlePhysicalAddresses(reader, responseItem);
89                  } else if ("PhoneNumbers".equals(tagLocalName)) {
90                      handlePhoneNumbers(reader, responseItem);
91                  } else {
92                      responseItem.put(tagLocalName, XMLStreamUtil.getElementText(reader));
93                  }
94              }
95          }
96      }
97  
98      protected void handlePhysicalAddress(XMLStreamReader reader, Item responseItem, String addressType) throws XMLStreamException {
99          while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "Entry")) {
100             reader.next();
101             if (XMLStreamUtil.isStartTag(reader)) {
102                 String tagLocalName = reader.getLocalName();
103                 String value = XMLStreamUtil.getElementText(reader);
104                 responseItem.put(addressType + tagLocalName, value);
105             }
106         }
107     }
108 
109     protected void handlePhysicalAddresses(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
110         while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "PhysicalAddresses")) {
111             reader.next();
112             if (XMLStreamUtil.isStartTag(reader)) {
113                 String tagLocalName = reader.getLocalName();
114                 if ("Entry".equals(tagLocalName)) {
115                     String key = getAttributeValue(reader, "Key");
116                     handlePhysicalAddress(reader, responseItem, key);
117                 }
118             }
119         }
120     }
121 
122     protected void handlePhoneNumbers(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
123         while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "PhoneNumbers")) {
124             reader.next();
125             if (XMLStreamUtil.isStartTag(reader)) {
126                 String tagLocalName = reader.getLocalName();
127                 if ("Entry".equals(tagLocalName)) {
128                     String key = getAttributeValue(reader, "Key");
129                     String value = XMLStreamUtil.getElementText(reader);
130                     responseItem.put(key, value);
131                 }
132             }
133         }
134     }
135 
136     @Override
137     protected void handleEmailAddresses(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
138         while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "EmailAddresses")) {
139             reader.next();
140             if (XMLStreamUtil.isStartTag(reader)) {
141                 String tagLocalName = reader.getLocalName();
142                 if ("Entry".equals(tagLocalName)) {
143                     String value = XMLStreamUtil.getElementText(reader);
144                     if (value != null) {
145                         if (value.startsWith("smtp:") || value.startsWith("SMTP:")) {
146                             value = value.substring(5);
147                             // get smtp address only if not already available through Mailbox info
148                             responseItem.putIfAbsent("EmailAddress", value);
149                         }
150                     }
151                 }
152             }
153         }
154     }
155 
156 }