1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
28
29 public class ResolveNamesMethod extends EWSMethod {
30 protected static final AttributeOption RETURN_FULL_CONTACT_DATA = new AttributeOption("ReturnFullContactData", "true");
31
32
33
34
35
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
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
148 responseItem.putIfAbsent("EmailAddress", value);
149 }
150 }
151 }
152 }
153 }
154 }
155
156 }