public String shortenEncodedUrl(String longUrl) throws IOException {
String prettyUrl = StringUtils.decodeUrl(longUrl);
String path = String.format(urlTemplate, version, "shorten");
URI uri = URI.create(path);
try {
Response response = client.target(uri)
.queryParam("format", "txt")
.queryParam("longUrl", longUrl)
.queryParam("access_token", accessToken)
.request(MediaType.APPLICATION_JSON_TYPE)
.get();
int status = response.getStatus();
String text = response.readEntity(String.class).trim();
if (status == 200) {
return text;
} else {
String msg = String.format("%s: Unable to process request to shortenEncoded %s (%s)", status, prettyUrl, text);
throw new ApiException(HttpStatusCode.findByCode(status), msg);
}
} catch (ApiException ex) {
throw ex;
} catch (Exception ex) {
String msg = String.format("Unexpected exception shortening %s", prettyUrl);
throw new IOException(msg, ex);
}
}