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