Search in sources :

Example 1 with ResponseData

use of com.github.ignition.support.http.cache.CachedHttpResponse.ResponseData in project ignition by mttkay.

the class IgnitedHttpRequestBase method handleResponse.

@Override
public IgnitedHttpResponse handleResponse(HttpResponse response) throws IOException {
    int status = response.getStatusLine().getStatusCode();
    if (expectedStatusCodes != null && !expectedStatusCodes.isEmpty() && !expectedStatusCodes.contains(status)) {
        throw new HttpResponseException(status, "Unexpected status code: " + status);
    }
    IgnitedHttpResponse bhttpr = new IgnitedHttpResponseImpl(response);
    HttpResponseCache responseCache = ignitedHttp.getResponseCache();
    if (responseCache != null && bhttpr.getResponseBody() != null) {
        ResponseData responseData = new ResponseData(status, bhttpr.getResponseBodyAsBytes());
        responseCache.put(getRequestUrl(), responseData);
    }
    return bhttpr;
}
Also used : ResponseData(com.github.ignition.support.http.cache.CachedHttpResponse.ResponseData) HttpResponseCache(com.github.ignition.support.http.cache.HttpResponseCache) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 2 with ResponseData

use of com.github.ignition.support.http.cache.CachedHttpResponse.ResponseData in project ignition by mttkay.

the class HttpResponseCache method readValueFromDisk.

@Override
protected ResponseData readValueFromDisk(File file) throws IOException {
    BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file));
    long fileSize = file.length();
    if (fileSize > Integer.MAX_VALUE) {
        throw new IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes");
    }
    // first byte is the status code
    int statusCode = istream.read();
    // the remainder is the response data
    int responseDataLength = (int) fileSize - 1;
    byte[] responseBody = new byte[responseDataLength];
    istream.read(responseBody, 0, responseDataLength);
    istream.close();
    return new ResponseData(statusCode, responseBody);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ResponseData(com.github.ignition.support.http.cache.CachedHttpResponse.ResponseData) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

ResponseData (com.github.ignition.support.http.cache.CachedHttpResponse.ResponseData)2 HttpResponseCache (com.github.ignition.support.http.cache.HttpResponseCache)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 HttpResponseException (org.apache.http.client.HttpResponseException)1