Commit 969a589a authored by Matija Obreza's avatar Matija Obreza

bit.ly short links with https:// prefix

parent 0604344d
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.service.impl;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.genesys2.server.service.UrlShortenerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -12,53 +33,53 @@ import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.net.URL;
import java.util.*;
@Component("bitlyURLShortener")
public class BitlyURLShortener implements UrlShortenerService {
private static final Logger LOG = LoggerFactory.getLogger(BitlyURLShortener.class);
@Value("${bitly.access.token}")
private String accessToken;
private static final Logger LOG = LoggerFactory.getLogger(BitlyURLShortener.class);
@Value("${bitly.group.guid}")
private String groupGuid;
@Value("${bitly.access.token}")
private String accessToken;
@Value("${bitly.url.shortener}")
private String bitlyUrlShortener;
@Value("${bitly.group.guid}")
private String groupGuid;
private RestTemplate restTemplate;
@Value("${bitly.url.shortener}")
private String bitlyUrlShortener;
public BitlyURLShortener() {
this.restTemplate = new RestTemplate();
}
private RestTemplate restTemplate;
@Override
public String shortenUrl(URL url) {
LOG.info("Shortening {}", url);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization" , accessToken);
public BitlyURLShortener() {
this.restTemplate = new RestTemplate();
}
Map<String, String> request = new HashMap<String, String>();
request.put("long_url", url.toString());
request.put("group_guid", groupGuid);
@Override
public String shortenUrl(URL url) {
LOG.info("Shortening {}", url);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", accessToken);
HttpEntity<Object> requestEntity = new HttpEntity<Object>(request, headers);
Map<String, String> request = new HashMap<String, String>();
request.put("long_url", url.toString());
request.put("group_guid", groupGuid);
String shortenUrl = null;
HttpEntity<Object> requestEntity = new HttpEntity<Object>(request, headers);
try {
LinkedHashMap<String, String> linkedHashMap = restTemplate.postForObject(bitlyUrlShortener, requestEntity, LinkedHashMap.class);
shortenUrl = linkedHashMap.get("id");
} catch (final HttpStatusCodeException e) {
LOG.error("bit.ly HTTP error {} {}", e.getResponseBodyAsString(), e.getMessage());
} catch (final RestClientException e) {
LOG.error(e.getMessage(), e);
}
try {
LinkedHashMap<String, Object> linkedHashMap = restTemplate.postForObject(bitlyUrlShortener, requestEntity, LinkedHashMap.class);
if (LOG.isDebugEnabled()) {
for (String key : linkedHashMap.keySet()) {
LOG.debug("{}: {}", key, linkedHashMap.get(key));
}
}
return "https://" + (String) linkedHashMap.get("id");
} catch (final HttpStatusCodeException e) {
LOG.error("bit.ly HTTP error {} {}", e.getResponseBodyAsString(), e.getMessage());
} catch (final RestClientException e) {
LOG.error(e.getMessage(), e);
}
return shortenUrl;
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment