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 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          ArrayList<NameValuePair> toDelete = new ArrayList<>();
81          for (NameValuePair param: parameters) {
82              if (param.getName().equals(name)) {
83                  toDelete.add(param);
84              }
85          }
86          parameters.removeAll(toDelete);
87      }
88  
89      public ArrayList<NameValuePair> getParameters() {
90          return parameters;
91      }
92  
93      public String getResponseBodyAsString() throws IOException {
94          if (responseBodyAsString == null) {
95              throw new IOException("No response body available");
96          }
97          return responseBodyAsString;
98      }
99  
100     public Header getResponseHeader(String name) {
101         checkResponse();
102         return response.getFirstHeader(name);
103     }
104 
105     /**
106      * Get status code from response.
107      * @return Http status code
108      */
109     public int getStatusCode() {
110         checkResponse();
111         return response.getStatusLine().getStatusCode();
112     }
113 
114     /**
115      * Get reason phrase from response.
116      * @return reason phrase
117      */
118     public String getReasonPhrase() {
119         checkResponse();
120         return response.getStatusLine().getReasonPhrase();
121     }
122 
123     public URI getRedirectLocation() {
124         checkResponse();
125         return HttpClientAdapter.getRedirectLocation(response);
126     }
127 
128     public HttpResponse getHttpResponse() {
129         return response;
130     }
131 
132     /**
133      * Check if response is available.
134      */
135     private void checkResponse() {
136         if (response == null) {
137             throw new IllegalStateException("Should execute request first");
138         }
139     }
140 
141 }