Commit 0604344d authored by Matija Obreza's avatar Matija Obreza

Merge branch 'url-shortener' into 'master'

Switching to bit.ly URL shortener

See merge request genesys-pgr/genesys-server!391
parents 214dfe95 d08d69f5
......@@ -124,7 +124,6 @@ public class ExplorerController extends BaseController implements InitializingBe
private DownloadService downloadService;
@Autowired
@Qualifier("googleURLShortener")
private UrlShortenerService urlShortenerService;
@Autowired
......
......@@ -8,6 +8,7 @@ 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.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
......@@ -36,7 +37,7 @@ public class BitlyURLShortener implements UrlShortenerService {
@Override
public String shortenUrl(URL url) {
LOG.info("Shortening {}", url);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization" , accessToken);
......@@ -52,6 +53,8 @@ public class BitlyURLShortener implements UrlShortenerService {
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);
}
......
/**
* Copyright 2015 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 org.genesys2.server.service.UrlShortenerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.social.support.ClientHttpRequestFactorySelector;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.net.URL;
import java.util.*;
@Component
@SuppressWarnings("unchecked")
public class GoogleURLShortener implements UrlShortenerService {
@Value("${google.api.key}")
private String apiKey;
@Value("${google.url.shortener}")
private String googleUrlShortener;
private RestTemplate restTemplate;
public GoogleURLShortener() {
restTemplate = new RestTemplate(ClientHttpRequestFactorySelector.getRequestFactory());
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
}
@Override
public String shortenUrl(URL url) {
Map<String, String> request = new HashMap<String, String>();
request.put("longUrl", url.toString());
LinkedHashMap<String, String> shortUrl = restTemplate.postForObject(googleUrlShortener+"?key="+apiKey, request, LinkedHashMap.class);
return shortUrl.get("id");
}
}
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