Commit 57a0e4e6 authored by Matija Obreza's avatar Matija Obreza
Browse files

URL Shortener cleanup, parameter check, license info

parent c9489362
/**
* 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;
import java.net.URL;
public interface UrlShortenerService {
String reduceUrl(String fullUrl);
String shortenUrl(URL url);
}
/**
* 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;
......@@ -9,6 +25,7 @@ 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
......@@ -32,10 +49,10 @@ public class GoogleURLShortener implements UrlShortenerService {
}
@Override
public String reduceUrl(String longUrl) {
public String shortenUrl(URL url) {
Map<String, String> request = new HashMap<String, String>();
request.put("longUrl", longUrl);
request.put("longUrl", url.toString());
LinkedHashMap<String, String> shortUrl = restTemplate.postForObject(googleUrlShortener+"?key="+apiKey, request, LinkedHashMap.class);
return shortUrl.get("id");
......
/**
* Copyright 2014 Global Crop Diversity Trust
* 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.
......@@ -41,7 +41,15 @@ import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.genesys.ParameterCategory;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.service.*;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.MappingService;
import org.genesys2.server.service.TraitService;
import org.genesys2.server.service.UrlShortenerService;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -103,9 +111,6 @@ public class ExplorerController extends BaseController {
@Value("${base.url}")
private String baseUrl;
@Value("${browse.url}")
private String browsePath;
/**
* Redirect to /explore/c/{shortName} if parameter 'crop' is provided
*/
......@@ -530,18 +535,22 @@ public class ExplorerController extends BaseController {
model.addAttribute("statsSampStat", elasticService.termStatisticsAuto(appliedFilters, FilterConstants.SAMPSTAT, 30));
}
@RequestMapping(value = "/explore/getBriefURL", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "/explore/shorten-url", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getBriefURL(@RequestParam(value = "filter", required = false, defaultValue = "") String filter) throws IOException, URISyntaxException {
public Object getBriefURL(@RequestParam(value = "filter", required = false, defaultValue = "") String jsonFilter,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) throws IOException, URISyntaxException {
AppliedFilters appliedFilters = mapper.readValue(jsonFilter, AppliedFilters.class);
URI longUrl = new URIBuilder(baseUrl)
.setPath(browsePath)
.addParameter("filter", filter)
URI longUrl = new URIBuilder(baseUrl).setPath("/explore").addParameter("filter", appliedFilters.toString()).addParameter("page", Integer.toString(page))
.build();
String shortUrl = urlShortenerService.reduceUrl(longUrl.toString());
final String shortenedUrl = urlShortenerService.shortenUrl(longUrl.toURL());
return "{\"shortUrl\":\"" + shortUrl + "\"}";
return new Object() {
@SuppressWarnings("unused")
public String shortUrl = shortenedUrl;
};
}
}
......@@ -34,7 +34,6 @@ hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
google.api.key=AIzaSyBEvPuc8j5ps5GDQ3tdnTJaffKhfOdxFVc
google.url.shortener=https://www.googleapis.com/urlshortener/v1/url
browse.url=/explore
c3p0.acquireIncrement=1
c3p0.minPoolSize=1
......
......@@ -434,9 +434,9 @@
$("#menuShareLink").on("click", function () {
$.ajax({
type: 'POST',
url: '/explore/getBriefURL',
url: '/explore/shorten-url',
data: {
'filter':JSON.stringify(jsonData)
'page': ${pagedData.number + 1}, 'filter': JSON.stringify(jsonData)
},
success: function(response) {
var inp = $("#shortLink");
......
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