All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudinary.http42.api.Response Maven / Gradle / Ivy

Go to download

Cloudinary is a cloud service that offers a solution to a web application's entire image management pipeline. Upload images to the cloud. Automatically perform smart image resizing, cropping and conversion without installing any complex software. Integrate Facebook or Twitter profile image extraction in a snap, in any dimension and style to match your website’s graphics requirements. Images are seamlessly delivered through a fast CDN, and much much more. This Java library allows to easily integrate with Cloudinary in Java applications.

There is a newer version: 1.39.0
Show newest version
package com.cloudinary.http42.api;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

import com.cloudinary.api.ApiResponse;
import com.cloudinary.api.RateLimit;
import com.cloudinary.utils.StringUtils;

@SuppressWarnings("rawtypes")
public class Response extends HashMap implements ApiResponse {
    private static final long serialVersionUID = -5458609797599845837L;
    private HttpResponse response = null;

    @SuppressWarnings("unchecked")
    public Response(HttpResponse response, Map result) {
        super(result);
        this.response = response;
    }

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

    private static final Pattern RATE_LIMIT_REGEX = Pattern
            .compile("X-Feature(\\w*)RateLimit(-Limit|-Reset|-Remaining)");
    private static final String RFC1123_PATTERN = "EEE, dd MMM yyyyy HH:mm:ss z";
    private static final DateFormat RFC1123 = new SimpleDateFormat(
            RFC1123_PATTERN);

    public Map rateLimits() throws ParseException {
        Header[] headers = this.response.getAllHeaders();
        Map limits = new HashMap();
        for (Header header : headers) {
            Matcher m = RATE_LIMIT_REGEX.matcher(header.getName());
            if (m.matches()) {
                String limitName = "Api";
                RateLimit limit = null;
                if (!StringUtils.isEmpty(m.group(1))) {
                    limitName = m.group(1);
                }
                limit = limits.get(limitName);
                if (limit == null) {
                    limit = new RateLimit();
                }
                if (m.group(2).equalsIgnoreCase("-limit")) {
                    limit.setLimit(Long.parseLong(header.getValue()));
                } else if (m.group(2).equalsIgnoreCase("-remaining")) {
                    limit.setRemaining(Long.parseLong(header.getValue()));
                } else if (m.group(2).equalsIgnoreCase("-reset")) {
                    limit.setReset(RFC1123.parse(header.getValue()));
                }
                limits.put(limitName, limit);
            }
        }
        return limits;
    }

    public RateLimit apiRateLimit() throws ParseException {
        return rateLimits().get("Api");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy