View Javadoc
1   package com.srv4pos.server.api.infrastructure.http;
2   
3   import com.srv4pos.commons.io.IOUtils;
4   
5   import java.io.IOException;
6   import java.io.InputStream;
7   
8   /**
9   * Http response.
10  * <p>User: Kirill, Date: 23.08.13 15:20</p>
11  */
12  public class Response {
13      private InputStream stream;
14      private int responseCode;
15      private String contentEncoding;
16      private String contentType;
17  
18      public Response(InputStream stream, int responseCode, String contentEncoding, String contentType) {
19          this.stream = stream;
20          this.responseCode = responseCode;
21          this.contentEncoding = contentEncoding;
22          this.contentType = contentType;
23      }
24  
25      public String toText()  {
26          if (stream == null) {
27              return null;
28          }
29  
30          try {
31              return IOUtils.toString(stream, contentEncoding == null ? HttpHelper.ENCODING_FOR_BODY : contentEncoding);
32          } catch (IOException e) {
33              throw new RuntimeException(e);
34          }
35      }
36  
37      public byte[] toBytes()  {
38          if (stream == null) {
39              return new byte[0];
40          }
41  
42          try {
43              return IOUtils.toByteArray(stream);
44          } catch (IOException e) {
45              throw new RuntimeException(e);
46          }
47      }
48  
49      public InputStream getStream() {
50          return stream;
51      }
52  
53      public int getResponseCode() {
54          return responseCode;
55      }
56  
57      public String getContentEncoding() {
58          return contentEncoding;
59      }
60  
61      public String getContentType() {
62          return contentType;
63      }
64  }