Commit bd5d80bc authored by Alexander Prendetskiy's avatar Alexander Prendetskiy Committed by Matija Obreza

add Bit.ly shortener

parent 154da81e
package org.genesys2.server.service.impl;
import org.genesys2.server.service.UrlShortenerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
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;
@Value("${bitly.group.guid}")
private String groupGuid;
@Value("${bitly.url.shortener}")
private String bitlyUrlShortener;
private RestTemplate restTemplate;
public BitlyURLShortener() {
this.restTemplate = new RestTemplate();
}
@Override
public String shortenUrl(URL url) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization" , accessToken);
Map<String, String> request = new HashMap<String, String>();
request.put("long_url", url.toString());
request.put("group_guid", groupGuid);
HttpEntity<Object> requestEntity = new HttpEntity<Object>(request, headers);
String shortenUrl = null;
try {
LinkedHashMap<String, String> linkedHashMap = restTemplate.postForObject(bitlyUrlShortener, requestEntity, LinkedHashMap.class);
shortenUrl = linkedHashMap.get("id");
} catch (final RestClientException e) {
LOG.error(e.getMessage(), e);
}
return shortenUrl;
}
}
......@@ -56,6 +56,12 @@ db.showSql=false
db.hbm2ddl=false
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# bit.ly shortener
# TODO change access token and guid
bitly.access.token=xx
bitly.group.guid=xx
bitly.url.shortener=https://api-ssl.bitly.com/v4/shorten
google.api.key=AIzaSyBEvPuc8j5ps5GDQ3tdnTJaffKhfOdxFVc
google.url.shortener=https://www.googleapis.com/urlshortener/v1/url
news.menu.abbreviate.length=22
......
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