f981d242a2e7641f91b234a2a633025aa512d061
[JavaForFun] /
1 /**
2  * Copyright 2014 Gustavo Martin Morcuende
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package name.gumartinm.weather.information.httpclient;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24
25 import org.apache.http.HttpEntity;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.HttpStatus;
28 import org.apache.http.client.ClientProtocolException;
29 import org.apache.http.client.ResponseHandler;
30 import org.apache.http.client.methods.HttpGet;
31
32 import android.net.http.AndroidHttpClient;
33
34 public class CustomHTTPClient {
35     private final AndroidHttpClient httpClient;
36
37     public CustomHTTPClient(final AndroidHttpClient httpClient) {
38         this.httpClient = httpClient;
39     }
40
41     public String retrieveDataAsString(final URL url)
42             throws URISyntaxException, ClientProtocolException, IOException {
43
44         final ResponseHandler<String> handler = new ResponseHandler<String>() {
45             @Override
46             public String handleResponse(
47                     final HttpResponse response)
48                             throws UnsupportedEncodingException, IOException {
49
50                 if (response != null) {
51                     final HttpEntity entity = response.getEntity();
52                     if (entity != null) {
53                         try {
54                             final ContentType contentType = ContentType.getOrDefault(entity);
55                             final ByteArrayOutputStream buffer = CustomHTTPClient.this
56                                     .sortResponse(response);
57                             return new String(buffer.toByteArray(), contentType.getCharset());
58                         } finally {
59                             entity.consumeContent();
60                         }
61                     }
62
63                     throw new IOException("There is no entity");
64                 }
65
66                 throw new IOException("There is no response");
67             }
68         };
69
70         final HttpGet httpGet = new HttpGet();
71         httpGet.setURI(url.toURI());
72
73         return this.httpClient.execute(httpGet, handler);
74     }
75
76     public ByteArrayOutputStream retrieveRawData(final URL url)
77             throws URISyntaxException, ClientProtocolException, IOException {
78         final ResponseHandler<ByteArrayOutputStream> handler = new ResponseHandler<ByteArrayOutputStream>() {
79
80             @Override
81             public ByteArrayOutputStream handleResponse(
82                     final HttpResponse response)
83                             throws UnsupportedEncodingException, IOException {
84
85                 if (response != null) {
86                     final HttpEntity entity = response.getEntity();
87                     if (entity != null) {
88                         try {
89                             return CustomHTTPClient.this.sortResponse(response);
90                         } finally {
91                             entity.consumeContent();
92                         }
93                     }
94
95                     throw new IOException("There is no entity");
96                 }
97
98                 throw new IOException("There is no response");
99             }
100         };
101
102         final HttpGet httpGet = new HttpGet();
103         httpGet.setURI(url.toURI());
104
105         return this.httpClient.execute(httpGet, handler);
106     }
107
108     public void close() {
109         this.httpClient.close();
110     }
111
112     private ByteArrayOutputStream sortResponse(final HttpResponse httpResponse) throws IOException {
113
114         if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
115             throw new IOException("Unexpected response code: "
116                     + httpResponse.getStatusLine().getStatusCode());
117         }
118
119         final HttpEntity entity = httpResponse.getEntity();
120         final InputStream inputStream = entity.getContent();
121         try {
122             return this.readInputStream(inputStream);
123         } finally {
124             inputStream.close();
125         }
126
127     }
128
129     private ByteArrayOutputStream readInputStream (final InputStream inputStream) throws IOException {
130         final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
131         final int bufferSize = 1024;
132         final byte[] buffer = new byte[bufferSize];
133
134         int len = 0;
135         while ((len = inputStream.read(buffer)) != -1) {
136             byteBuffer.write(buffer, 0, len);
137         }
138
139         return byteBuffer;
140     }
141 }