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