Commit c9489362 authored by Alexander Dolzhenko's avatar Alexander Dolzhenko
Browse files

URL Shortener 2, used URIBuilder

parent 740fbc0e
package org.genesys2.server.service;
public interface UrlShortenerService {
String reduceUrl(String fullUrl);
}
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.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 reduceUrl(String longUrl) {
Map<String, String> request = new HashMap<String, String>();
request.put("longUrl", longUrl);
LinkedHashMap<String, String> shortUrl = restTemplate.postForObject(googleUrlShortener+"?key="+apiKey, request, LinkedHashMap.class);
return shortUrl.get("id");
}
}
......@@ -23,6 +23,8 @@ import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
......@@ -31,6 +33,7 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.genesys2.server.model.elastic.AccessionDetails;
import org.genesys2.server.model.filters.GenesysFilter;
import org.genesys2.server.model.genesys.Accession;
......@@ -38,14 +41,7 @@ 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.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.*;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -53,6 +49,7 @@ import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
......@@ -98,8 +95,17 @@ public class ExplorerController extends BaseController {
@Autowired
private DownloadService downloadService;
@Autowired
private UrlShortenerService urlShortenerService;
private final ObjectMapper mapper = new ObjectMapper();
@Value("${base.url}")
private String baseUrl;
@Value("${browse.url}")
private String browsePath;
/**
* Redirect to /explore/c/{shortName} if parameter 'crop' is provided
*/
......@@ -524,4 +530,18 @@ 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)
@ResponseBody
public String getBriefURL(@RequestParam(value = "filter", required = false, defaultValue = "") String filter) throws IOException, URISyntaxException {
URI longUrl = new URIBuilder(baseUrl)
.setPath(browsePath)
.addParameter("filter", filter)
.build();
String shortUrl = urlShortenerService.reduceUrl(longUrl.toString());
return "{\"shortUrl\":\"" + shortUrl + "\"}";
}
}
......@@ -675,6 +675,7 @@ statistics.phenotypic.stats-text=Of the {0} accessions, {1} accessions ({2,numbe
twitter.tweet-this=Tweet!
twitter.follow-X=Follow @{0}
linkedin.share-this=Share on LinkedIn
share.link=Share Link
welcome.read-more=Read more about Genesys
twitter.latest-on-twitter=Latest on Twitter
......
......@@ -32,6 +32,10 @@ db.showSql=false
db.hbm2ddl=true
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
c3p0.maxPoolSize=5
......
......@@ -8,10 +8,10 @@
</head>
<body>
<cms:informative-h1 title="accession.page.data.title" fancy="${pagedData.number eq 0}" info="accession.page.data.intro" />
<%--Dropdown filters--%>
<div class="main-col-header clearfix">
<div class="nav-header">
<div class="results">
<spring:message code="accessions.number" arguments="${pagedData.totalElements}" />
......@@ -86,10 +86,18 @@
<b class="glyphicon-plus"></b>
</a>
</li>
<li class="message">
<span class="${fn:length(currentFilters) gt 0 ? '' : 'hide'}"><spring:message code="filter.filters-applied" /></span>
<span class="${fn:length(currentFilters) gt 0 ? 'hide' : ''}"><spring:message code="filter.filters-not-applied" /></span>
</li>
<li class="message">
<span class="${fn:length(currentFilters) gt 0 ? '' : 'hide'}"><spring:message code="filter.filters-applied" /></span>
<span class="${fn:length(currentFilters) gt 0 ? 'hide' : ''}"><spring:message code="filter.filters-not-applied" /></span>
</li>
<li class="active dropdown form-horizontal" style="float: right" id="menuShareLink">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<spring:message code="share.link"/>
</a>
<ul class="dropdown-menu">
<input id="shortLink" type="text" value="">
</ul>
</li>
</ul>
<%-- Only render currently present filters --%>
......@@ -388,7 +396,7 @@
// Upload
GenesysFilterUtil.submitJson('<c:url value="/explore" />', jsonData);
});
$("body").on("click", ".remove-filter", function () {
var key = $(this).parents(".filter-block").attr("i-key");
delete jsonData[key];
......@@ -411,7 +419,7 @@
// value = GenesysFilterUtil.setCharAt(jsonVal, jsonVal.indexOf("{"), '{"');
// value = GenesysFilterUtil.setCharAt(value, jsonVal.indexOf(":") + 1, '":');
// }
if (value=="null") value=null;
GenesysFilterUtil.removeValue(value, key, jsonData);
......@@ -423,6 +431,21 @@
$('input[i-key=' + normKey + value + ']').prop('checked', false);
});
$("#menuShareLink").on("click", function () {
$.ajax({
type: 'POST',
url: '/explore/getBriefURL',
data: {
'filter':JSON.stringify(jsonData)
},
success: function(response) {
var inp = $("#shortLink");
inp.val(response.shortUrl);
inp.select();
}
});
});
GenesysFilterUtil.registerAutocomplete("#allfilters",jsonData);
});
</script>
......
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