View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2012  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 davmail.util.StringUtil;
22  import org.apache.log4j.Logger;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.OutputStreamWriter;
27  import java.nio.charset.StandardCharsets;
28  
29  /**
30   * Custom Exchange PROPFIND method.
31   * Does not load full DOM in memory.
32   */
33  public class ExchangeSearchMethod extends ExchangeDavMethod {
34      protected static final Logger LOGGER = Logger.getLogger(ExchangeSearchMethod.class);
35  
36      protected final String searchRequest;
37  
38      /**
39       * Create search method.
40       *
41       * @param uri           method uri
42       * @param searchRequest Exchange search request
43       */
44      public ExchangeSearchMethod(String uri, String searchRequest) {
45          super(uri);
46          this.searchRequest = searchRequest;
47      }
48  
49      protected byte[] generateRequestContent() {
50          try {
51              ByteArrayOutputStream baos = new ByteArrayOutputStream();
52              try (OutputStreamWriter writer = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
53                  writer.write("<?xml version=\"1.0\"?>\n");
54                  writer.write("<d:searchrequest xmlns:d=\"DAV:\">\n");
55                  writer.write("        <d:sql>");
56                  writer.write(StringUtil.xmlEncode(searchRequest));
57                  writer.write("</d:sql>\n");
58                  writer.write("</d:searchrequest>");
59              }
60              return baos.toByteArray();
61          } catch (IOException e) {
62              throw new RuntimeException(e);
63          }
64  
65      }
66  
67      @Override
68      public String getName() {
69          return "SEARCH";
70      }
71  
72  }