com.cloudinary.http5.api.Response Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudinary-http5 Show documentation
Show all versions of cloudinary-http5 Show documentation
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.
The newest version!
package com.cloudinary.http5.api;
import com.cloudinary.api.ApiResponse;
import com.cloudinary.api.RateLimit;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
import java.text.ParseException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Response extends HashMap implements ApiResponse {
private static final long serialVersionUID = -5458609797599845837L;
private final HttpResponse response;
@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)", Pattern.CASE_INSENSITIVE);
private static final String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";
private static final DateFormat RFC1123 = new SimpleDateFormat(RFC1123_PATTERN, Locale.ENGLISH);
public Map rateLimits() throws ParseException {
Header[] headers = this.response.getHeaders();
Map limits = new HashMap<>();
for (Header header : headers) {
Matcher m = RATE_LIMIT_REGEX.matcher(header.getName());
if (m.matches()) {
String limitName = "Api";
RateLimit limit = limits.getOrDefault(limitName, new RateLimit());
if (!m.group(1).isEmpty()) {
limitName = m.group(1);
}
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");
}
}