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  
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   * Custom Exchange PROPFIND method.
37   * Does not load full DOM in memory.
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              // build namespace map
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                      // property namespace
68                      String namespaceUri = davPropertyName.getNamespace().getURI();
69                      if (!nameSpaceMap.containsKey(namespaceUri)) {
70                          nameSpaceMap.put(namespaceUri, currentChar++);
71                      }
72                  }
73              }
74              // <D:propfind xmlns:D="DAV:"><D:prop><D:displayname/></D:prop></D:propfind>
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 }