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.http.HttpClientAdapter;
23  import org.apache.http.Consts;
24  import org.apache.http.Header;
25  import org.apache.http.HttpEntity;
26  import org.apache.http.HttpResponse;
27  import org.apache.http.NameValuePair;
28  import org.apache.http.client.ResponseHandler;
29  import org.apache.http.client.entity.UrlEncodedFormEntity;
30  import org.apache.http.client.methods.HttpPost;
31  import org.apache.http.impl.client.BasicResponseHandler;
32  import org.apache.http.message.BasicNameValuePair;
33  
34  import java.io.IOException;
35  import java.net.URI;
36  import java.util.ArrayList;
37  
38  /**
39   * Http post request with a string response handler.
40   */
41  public class PostRequest extends HttpPost implements ResponseHandler<String>, ResponseWrapper {
42      private final ArrayList<NameValuePair> parameters = new ArrayList<>();
43      private String responseBodyAsString = null;
44      private HttpResponse response;
45  
46      public PostRequest(final URI uri) {
47          super(uri);
48      }
49  
50      public PostRequest(final String url) {
51          super(URI.create(url));
52      }
53  
54      public void setRequestHeader(String name, String value) {
55          setHeader(name, value);
56      }
57  
58      @Override
59      public HttpEntity getEntity() {
60          return new UrlEncodedFormEntity(parameters, Consts.UTF_8);
61      }
62  
63      @Override
64      public String handleResponse(HttpResponse response) throws IOException {
65          this.response = response;
66          if (HttpClientAdapter.isRedirect(response)) {
67              return null;
68          } else {
69              responseBodyAsString = new BasicResponseHandler().handleResponse(response);
70              return responseBodyAsString;
71          }
72  
73      }
74  
75      public void setParameter(final String name, final String value) {
76          parameters.add(new BasicNameValuePair(name, value));
77      }
78  
79      public void removeParameter(final String name) {
80          parameters.removeIf(param -> param.getName().equals(name));
81      }
82  
83      public ArrayList<NameValuePair> getParameters() {
84          return parameters;
85      }
86  
87      public String getResponseBodyAsString() throws IOException {
88          checkResponse();
89          if (responseBodyAsString == null) {
90              throw new IOException("No response body available");
91          }
92          return responseBodyAsString;
93      }
94  
95      public Header getResponseHeader(String name) {
96          checkResponse();
97          return response.getFirstHeader(name);
98      }
99  
100     /**
101      * Get status code from response.
102      * @return Http status code
103      */
104     public int getStatusCode() {
105         checkResponse();
106         return response.getStatusLine().getStatusCode();
107     }
108 
109     /**
110      * Get reason phrase from response.
111      * @return reason phrase
112      */
113     public String getReasonPhrase() {
114         checkResponse();
115         return response.getStatusLine().getReasonPhrase();
116     }
117 
118     public URI getRedirectLocation() {
119         checkResponse();
120         return HttpClientAdapter.getRedirectLocation(response);
121     }
122 
123     public HttpResponse getHttpResponse() {
124         return response;
125     }
126 
127     /**
128      * Check if response is available.
129      */
130     private void checkResponse() {
131         if (response == null) {
132             throw new IllegalStateException("Should execute request first");
133         }
134     }
135 
136 }