44c305ba7ccb8d02d5cf8acfff6e4441f099c14a
[JavaForFun] /
1 package name.gumartinm.weather.information.httpclient;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.UnsupportedEncodingException;
7 import java.net.URISyntaxException;
8 import java.net.URL;
9
10 import org.apache.http.HttpEntity;
11 import org.apache.http.HttpResponse;
12 import org.apache.http.HttpStatus;
13 import org.apache.http.client.ClientProtocolException;
14 import org.apache.http.client.ResponseHandler;
15 import org.apache.http.client.methods.HttpGet;
16
17 import android.net.http.AndroidHttpClient;
18
19 public class CustomHTTPClient {
20     private final AndroidHttpClient httpClient;
21
22     public CustomHTTPClient(final AndroidHttpClient httpClient) {
23         this.httpClient = httpClient;
24     }
25
26     public String retrieveDataAsString(final URL url)
27             throws URISyntaxException, ClientProtocolException, IOException {
28
29         final ResponseHandler<String> handler = new ResponseHandler<String>() {
30             @Override
31             public String handleResponse(
32                     final HttpResponse response)
33                             throws UnsupportedEncodingException, IOException {
34
35                 if (response != null) {
36                     final HttpEntity entity = response.getEntity();
37                     if (entity != null) {
38                         try {
39                             final ContentType contentType = ContentType.getOrDefault(entity);
40                             final ByteArrayOutputStream buffer = CustomHTTPClient.this
41                                     .sortResponse(response);
42                             return new String(buffer.toByteArray(), contentType.getCharset());
43                         } finally {
44                             entity.consumeContent();
45                         }
46                     }
47
48                     throw new IOException("There is no entity");
49                 }
50
51                 throw new IOException("There is no response");
52             }
53         };
54
55         final HttpGet httpGet = new HttpGet();
56         httpGet.setURI(url.toURI());
57
58         return this.httpClient.execute(httpGet, handler);
59     }
60
61     public ByteArrayOutputStream retrieveRawData(final URL url)
62             throws URISyntaxException, ClientProtocolException, IOException {
63         final ResponseHandler<ByteArrayOutputStream> handler = new ResponseHandler<ByteArrayOutputStream>() {
64
65             @Override
66             public ByteArrayOutputStream handleResponse(
67                     final HttpResponse response)
68                             throws UnsupportedEncodingException, IOException {
69
70                 if (response != null) {
71                     final HttpEntity entity = response.getEntity();
72                     if (entity != null) {
73                         try {
74                             return CustomHTTPClient.this.sortResponse(response);
75                         } finally {
76                             entity.consumeContent();
77                         }
78                     }
79
80                     throw new IOException("There is no entity");
81                 }
82
83                 throw new IOException("There is no response");
84             }
85         };
86
87         final HttpGet httpGet = new HttpGet();
88         httpGet.setURI(url.toURI());
89
90         return this.httpClient.execute(httpGet, handler);
91     }
92
93     public void close() {
94         this.httpClient.close();
95     }
96
97     private ByteArrayOutputStream sortResponse(final HttpResponse httpResponse) throws IOException {
98
99         if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
100             throw new IOException("Unexpected response code: "
101                     + httpResponse.getStatusLine().getStatusCode());
102         }
103
104         final HttpEntity entity = httpResponse.getEntity();
105         final InputStream inputStream = entity.getContent();
106         try {
107             return this.readInputStream(inputStream);
108         } finally {
109             inputStream.close();
110         }
111
112     }
113
114     private ByteArrayOutputStream readInputStream (final InputStream inputStream) throws IOException {
115         final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
116         final int bufferSize = 1024;
117         final byte[] buffer = new byte[bufferSize];
118
119         int len = 0;
120         while ((len = inputStream.read(buffer)) != -1) {
121             byteBuffer.write(buffer, 0, len);
122         }
123
124         return byteBuffer;
125     }
126 }