1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package davmail.http.request;
21
22 import org.apache.jackrabbit.webdav.header.DepthHeader;
23 import org.apache.jackrabbit.webdav.property.DavPropertyName;
24 import org.apache.jackrabbit.webdav.property.DavPropertyNameIterator;
25 import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
26 import org.apache.log4j.Logger;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.OutputStreamWriter;
31 import java.nio.charset.StandardCharsets;
32 import java.util.HashMap;
33 import java.util.Map;
34
35
36
37
38
39 public class ExchangePropFindRequest extends ExchangeDavRequest {
40 protected static final Logger LOGGER = Logger.getLogger(ExchangePropFindRequest.class);
41
42 protected final DavPropertyNameSet propertyNameSet;
43
44 public ExchangePropFindRequest(String uri) {
45 this(uri, null, DepthHeader.DEPTH_INFINITY);
46 }
47
48 public ExchangePropFindRequest(String uri, DavPropertyNameSet propertyNameSet, int depth) {
49 super(uri);
50 this.propertyNameSet = propertyNameSet;
51 DepthHeader dh = new DepthHeader(depth);
52 setHeader(dh.getHeaderName(), dh.getHeaderValue());
53 }
54
55 protected byte[] generateRequestContent() {
56 try {
57
58 int currentChar = 'e';
59 final Map<String, Integer> nameSpaceMap = new HashMap<>();
60 nameSpaceMap.put("DAV:", (int) 'D');
61 if (propertyNameSet != null) {
62 DavPropertyNameIterator propertyNameIterator = propertyNameSet.iterator();
63 while (propertyNameIterator.hasNext()) {
64 DavPropertyName davPropertyName = propertyNameIterator.nextPropertyName();
65
66 davPropertyName.getName();
67
68 String namespaceUri = davPropertyName.getNamespace().getURI();
69 if (!nameSpaceMap.containsKey(namespaceUri)) {
70 nameSpaceMap.put(namespaceUri, currentChar++);
71 }
72 }
73 }
74
75 ByteArrayOutputStream baos = new ByteArrayOutputStream();
76 try (OutputStreamWriter writer = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
77 writer.write("<D:propfind ");
78 for (Map.Entry<String, Integer> mapEntry : nameSpaceMap.entrySet()) {
79 writer.write(" xmlns:");
80 writer.write((char) mapEntry.getValue().intValue());
81 writer.write("=\"");
82 writer.write(mapEntry.getKey());
83 writer.write("\"");
84 }
85 writer.write(">");
86 if (propertyNameSet == null || propertyNameSet.isEmpty()) {
87 writer.write("<D:allprop/>");
88 } else {
89 writer.write("<D:prop>");
90 DavPropertyNameIterator propertyNameIterator = propertyNameSet.iterator();
91 while (propertyNameIterator.hasNext()) {
92 DavPropertyName davPropertyName = propertyNameIterator.nextPropertyName();
93 char nameSpaceChar = (char) nameSpaceMap.get(davPropertyName.getNamespace().getURI()).intValue();
94 writer.write('<');
95 writer.write(nameSpaceChar);
96 writer.write(':');
97 writer.write(davPropertyName.getName());
98 writer.write("/>");
99 }
100 writer.write("</D:prop>");
101 }
102 writer.write("</D:propfind>");
103 }
104 return baos.toByteArray();
105 } catch (IOException e) {
106 throw new RuntimeException(e);
107 }
108
109 }
110
111 @Override
112 public String getMethod() {
113 return "PROPFIND";
114 }
115
116 }