View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2011  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.dav;
20  
21  import org.apache.jackrabbit.webdav.header.DepthHeader;
22  import org.apache.jackrabbit.webdav.property.DavPropertyName;
23  import org.apache.jackrabbit.webdav.property.DavPropertyNameIterator;
24  import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
25  import org.apache.log4j.Logger;
26  
27  import java.io.ByteArrayOutputStream;
28  import java.io.IOException;
29  import java.io.OutputStreamWriter;
30  import java.nio.charset.StandardCharsets;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  /**
35   * Custom Exchange PROPFIND method.
36   * Does not load full DOM in memory.
37   */
38  public class ExchangePropFindMethod extends ExchangeDavMethod {
39      protected static final Logger LOGGER = Logger.getLogger(ExchangePropFindMethod.class);
40  
41      protected final DavPropertyNameSet propertyNameSet;
42  
43      public ExchangePropFindMethod(String uri) {
44          this(uri, null, DepthHeader.DEPTH_INFINITY);
45      }
46  
47      public ExchangePropFindMethod(String uri, DavPropertyNameSet propertyNameSet, int depth) {
48          super(uri);
49          this.propertyNameSet = propertyNameSet;
50          DepthHeader dh = new DepthHeader(depth);
51          setRequestHeader(dh.getHeaderName(), dh.getHeaderValue());
52      }
53  
54      protected byte[] generateRequestContent() {
55          try {
56              // build namespace map
57              int currentChar = 'e';
58              final Map<String, Integer> nameSpaceMap = new HashMap<>();
59              nameSpaceMap.put("DAV:", (int) 'D');
60              if (propertyNameSet != null) {
61                  DavPropertyNameIterator propertyNameIterator = propertyNameSet.iterator();
62                  while (propertyNameIterator.hasNext()) {
63                      DavPropertyName davPropertyName = propertyNameIterator.nextPropertyName();
64  
65                      davPropertyName.getName();
66                      // property namespace
67                      String namespaceUri = davPropertyName.getNamespace().getURI();
68                      if (!nameSpaceMap.containsKey(namespaceUri)) {
69                          nameSpaceMap.put(namespaceUri, currentChar++);
70                      }
71                  }
72              }
73              // <D:propfind xmlns:D="DAV:"><D:prop><D:displayname/></D:prop></D:propfind>
74              ByteArrayOutputStream baos = new ByteArrayOutputStream();
75              OutputStreamWriter writer = new OutputStreamWriter(baos, StandardCharsets.UTF_8);
76              writer.write("<D:propfind ");
77              for (Map.Entry<String, Integer> mapEntry : nameSpaceMap.entrySet()) {
78                  writer.write(" xmlns:");
79                  writer.write((char) mapEntry.getValue().intValue());
80                  writer.write("=\"");
81                  writer.write(mapEntry.getKey());
82                  writer.write("\"");
83              }
84              writer.write(">");
85              if (propertyNameSet == null || propertyNameSet.isEmpty()) {
86                  writer.write("<D:allprop/>");
87              } else {
88                  writer.write("<D:prop>");
89                  DavPropertyNameIterator propertyNameIterator = propertyNameSet.iterator();
90                  while (propertyNameIterator.hasNext()) {
91                      DavPropertyName davPropertyName = propertyNameIterator.nextPropertyName();
92                      char nameSpaceChar = (char) nameSpaceMap.get(davPropertyName.getNamespace().getURI()).intValue();
93                      writer.write('<');
94                      writer.write(nameSpaceChar);
95                      writer.write(':');
96                      writer.write(davPropertyName.getName());
97                      writer.write("/>");
98                  }
99                  writer.write("</D:prop>");
100             }
101             writer.write("</D:propfind>");
102             writer.close();
103             return baos.toByteArray();
104         } catch (IOException e) {
105             throw new RuntimeException(e);
106         }
107 
108     }
109 
110     @Override
111     public String getName() {
112         return "PROPFIND";
113     }
114 
115 }