summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/MozResponse.java
blob: 3f76f929f033b747c91b49cdf13394eb73e637e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.sync.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;

import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.NonArrayJSONException;
import org.mozilla.gecko.sync.NonObjectJSONException;

import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.HttpEntity;
import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.HttpStatus;
import ch.boye.httpclientandroidlib.impl.cookie.DateParseException;
import ch.boye.httpclientandroidlib.impl.cookie.DateUtils;

public class MozResponse {
  private static final String LOG_TAG = "MozResponse";

  private static final String HEADER_RETRY_AFTER = "retry-after";

  protected HttpResponse response;
  private String body = null;

  public HttpResponse httpResponse() {
    return this.response;
  }

  public int getStatusCode() {
    return this.response.getStatusLine().getStatusCode();
  }

  public boolean wasSuccessful() {
    return this.getStatusCode() == 200;
  }

  public boolean isInvalidAuthentication() {
    return this.getStatusCode() == HttpStatus.SC_UNAUTHORIZED;
  }

  /**
   * Fetch the content type of the HTTP response body.
   *
   * @return a <code>Header</code> instance, or <code>null</code> if there was
   *         no body or no valid Content-Type.
   */
  public Header getContentType() {
    HttpEntity entity = this.response.getEntity();
    if (entity == null) {
      return null;
    }
    return entity.getContentType();
  }

  private static boolean missingHeader(String value) {
    return value == null ||
           value.trim().length() == 0;
  }

  public String body() throws IllegalStateException, IOException {
    if (body != null) {
      return body;
    }
    final HttpEntity entity = this.response.getEntity();
    if (entity == null) {
      body = null;
      return null;
    }

    InputStreamReader is = new InputStreamReader(entity.getContent());
    // Oh, Java, you are so evil.
    body = new Scanner(is).useDelimiter("\\A").next();
    return body;
  }

  /**
   * Return the body as a <b>non-null</b> <code>ExtendedJSONObject</code>.
   *
   * @return A non-null <code>ExtendedJSONObject</code>.
   *
   * @throws IllegalStateException
   * @throws IOException
   * @throws NonObjectJSONException
   */
  public ExtendedJSONObject jsonObjectBody() throws IllegalStateException, IOException, NonObjectJSONException {
    if (body != null) {
      // Do it from the cached String.
      return new ExtendedJSONObject(body);
    }

    HttpEntity entity = this.response.getEntity();
    if (entity == null) {
      throw new IOException("no entity");
    }

    InputStream content = entity.getContent();
    try {
      Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8"));
      return new ExtendedJSONObject(in);
    } finally {
      content.close();
    }
  }

  public JSONArray jsonArrayBody() throws NonArrayJSONException, IOException {
    final JSONParser parser = new JSONParser();
    try {
      if (body != null) {
        // Do it from the cached String.
        return (JSONArray) parser.parse(body);
      }

      final HttpEntity entity = this.response.getEntity();
      if (entity == null) {
        throw new IOException("no entity");
      }

      final InputStream content = entity.getContent();
      final Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8"));
      try {
        return (JSONArray) parser.parse(in);
      } finally {
        in.close();
      }
    } catch (ClassCastException | ParseException e) {
      NonArrayJSONException exception = new NonArrayJSONException("value must be a json array");
      exception.initCause(e);
      throw exception;
    }
  }

  protected boolean hasHeader(String h) {
    return this.response.containsHeader(h);
  }

  public MozResponse(HttpResponse res) {
    response = res;
  }

  protected String getNonMissingHeader(String h) {
    if (!this.hasHeader(h)) {
      return null;
    }

    final Header header = this.response.getFirstHeader(h);
    final String value = header.getValue();
    if (missingHeader(value)) {
      Logger.warn(LOG_TAG, h + " header present but empty.");
      return null;
    }
    return value;
  }

  protected long getLongHeader(String h) throws NumberFormatException {
    final String value = getNonMissingHeader(h);
    if (value == null) {
      return -1L;
    }
    return Long.parseLong(value, 10);
  }

  protected int getIntegerHeader(String h) throws NumberFormatException {
    final String value = getNonMissingHeader(h);
    if (value == null) {
      return -1;
    }
    return Integer.parseInt(value, 10);
  }

  /**
   * @return A number of seconds, or -1 if the 'Retry-After' header was not present.
   */
  public int retryAfterInSeconds() throws NumberFormatException {
    final String retryAfter = getNonMissingHeader(HEADER_RETRY_AFTER);
    if (retryAfter == null) {
      return -1;
    }

    try {
      return Integer.parseInt(retryAfter, 10);
    } catch (NumberFormatException e) {
      // Fall through to try date format.
    }

    try {
      final long then = DateUtils.parseDate(retryAfter).getTime();
      final long now  = System.currentTimeMillis();
      return (int)((then - now) / 1000);     // Convert milliseconds to seconds.
    } catch (DateParseException e) {
      Logger.warn(LOG_TAG, "Retry-After header neither integer nor date: " + retryAfter);
      return -1;
    }
  }

  /**
   * @return A number of seconds, or -1 if the 'Backoff' header was not
   *         present.
   */
  public int backoffInSeconds() throws NumberFormatException {
    return this.getIntegerHeader("backoff");
  }

  public void logResponseBody(final String logTag) {
    if (!Logger.LOG_PERSONAL_INFORMATION) {
      return;
    }
    try {
      Logger.pii(logTag, "Response body: " + body());
    } catch (Throwable e) {
      Logger.debug(logTag, "No response body.");
    }
  }
}