Commit c83665e6 authored by igoshin's avatar igoshin Committed by Matija Obreza
Browse files

Genesys Web client API

parent fc58a4b0
......@@ -16,28 +16,14 @@
package org.genesys2.server.model.oauth;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.util.StringUtils;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name = "oauth_client_details")
public class OAuthClientDetails implements ClientDetails {
......@@ -74,6 +60,9 @@ public class OAuthClientDetails implements ClientDetails {
@Column(name = "additional_information")
private String additionalInformation;
@Column(name = "clientType")
private String clientType;
public OAuthClientDetails() {
}
......@@ -116,7 +105,15 @@ public class OAuthClientDetails implements ClientDetails {
}
}
@Override
public String getClientType() {
return clientType;
}
public void setClientType(String clientType) {
this.clientType = clientType;
}
@Override
public String getClientId() {
return clientId;
}
......
......@@ -54,6 +54,6 @@ public interface TokenVerificationService {
*
*/
private static final long serialVersionUID = -1127854381492707753L;
}
}
......@@ -47,8 +47,8 @@ public class TokenVerificationServiceImpl implements TokenVerificationService {
return token;
}
@Override
@Transactional
@Override
@Transactional
public void cancel(String tokenUuid) throws NoSuchVerificationTokenException {
final VerificationToken verificationToken = verificationTokenRepository.findByUuid(tokenUuid);
if (verificationToken == null) {
......
......@@ -16,18 +16,10 @@
package org.genesys2.server.servlet.controller;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jhlabs.image.MapColorsFilter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys2.server.model.genesys.Accession;
......@@ -35,14 +27,8 @@ 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.GenesysFilterService;
import org.genesys2.server.service.*;
import org.genesys2.server.service.GenesysFilterService.GenesysFilter;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.MappingService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.TraitService;
import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -52,16 +38,18 @@ import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jhlabs.image.MapColorsFilter;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
@Controller
public class ExplorerController extends BaseController {
......@@ -377,4 +365,32 @@ public class ExplorerController extends BaseController {
throw e;
}
}
@RequestMapping(value = "/explore/acc", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object getAcc(@RequestBody JsonData jsonData) throws IOException {
Crop crop = cropService.getCrop(jsonData.crop);
if (crop == null) {
throw new ResourceNotFoundException("No crop " + jsonData.crop);
}
String filter = "{\"crop\":[\"" + crop.getShortName() + "\"]}";
ObjectNode jsonTree = null;
jsonTree = (ObjectNode) mapper.readTree(filter);
_logger.debug(jsonTree.toString());
Page<Accession> accessions = filterService.listAccessions(jsonTree, new PageRequest(jsonData.startAt - 1, jsonData.maxRecords, new Sort("acceNumb")));
return accessions;
}
public static class JsonData {
public String crop;
public Integer startAt;
public Integer maxRecords;
public String otherOptions;
}
}
......@@ -19,19 +19,20 @@ package org.genesys2.server.servlet.controller;
import org.genesys2.server.exception.NotUniqueUserException;
import org.genesys2.server.exception.UserException;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.service.OAuth2ClientDetailsService;
import org.genesys2.server.service.UserService;
import org.genesys2.server.servlet.model.UserList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* User REST-controller. It produces and consumes JSON. For "USER" role all
......@@ -44,8 +45,10 @@ public class UsersRestController extends BaseController {
@Autowired
protected UserService userService;
@Autowired
private OAuth2ClientDetailsService clientDetailsService;
@RequestMapping(value = "/users", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
@RequestMapping(value = "/users", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public Object getUsers(@RequestParam(value = "startRow", required = false, defaultValue = "0") Integer startRow,
@RequestParam(value = "pageSize", required = false, defaultValue = "0") Integer pageSize) throws UserException {
......@@ -83,4 +86,30 @@ public class UsersRestController extends BaseController {
public void removeUser(@RequestParam("id") Long userId) throws UserException {
userService.removeUserById(userId);
}
@RequestMapping(value = "/get_widget")
@PreAuthorize("isAuthenticated()")
public String getWidget(Model model,@RequestParam(value = "clientId",required = false) String clientId) {
if (!clientId.equals("")){
ClientDetails clientDetails= clientDetailsService.loadClientByClientId(clientId);
String script =
"<script>(function(d, s, id) {\n" +
"var js, gjs = d.getElementsByTagName(s)[0];\n" +
"if (d.getElementById(id)) return;\n" +
"js = d.createElement(s); js.id = id;\n" +
// "js.src = '/api/genesys-api.js?client_id="+clientDetails.getClientId()+"&client_secret="+clientDetails.getClientSecret()+"';\n" +
"js.src = 'https://www.genesys-pgr.org/api/genesys-api.js?client_id="+clientDetails.getClientId()+"&client_secret="+clientDetails.getClientSecret()+"';\n" +
"gjs.parentNode.insertBefore(js, gjs);\n" +
"}(document, 'script', 'genesys-api'));</script>";
model.addAttribute("client", clientDetails);
model.addAttribute("script", script);
}
List<ClientDetails> clientDetailses = clientDetailsService.listClientDetails();
model.addAttribute("clientDetails",clientDetailses);
return "/user/widget";
}
}
package org.genesys2.server.servlet.filter;
import org.genesys2.server.service.OAuth2ClientDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component("apiFilter")
public class ApiFilter extends OncePerRequestFilter {
@Autowired
private OAuth2ClientDetailsService clientDetailsService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String clientId = request.getParameter("client_id");
String clientSecret = request.getParameter("client_secret");
ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
if (clientDetails!=null&&clientDetails.getClientSecret().equals(clientSecret)) {
filterChain.doFilter(request, response);
} else {
//Wrong token
response.sendRedirect("/api/error.js");
}
}
}
......@@ -83,6 +83,9 @@
<a href="<c:url value="/profile/${user.uuid}/vetted-user"/> "class="btn btn-default"/>Vetted user</a>
</c:if>
</security:authorize>
<security:authorize access="isAuthenticated()">
<a href="#" data-toggle="modal" data-target="#myModal" class="btn btn-default get_widget" id="get_widget">Get Widget</a>
</security:authorize>
</div>
<h3><spring:message code="team.user-teams" /></h3>
......@@ -98,6 +101,13 @@
</c:forEach>
</ul>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content"></div>
</div>
</div>
<!--End modal-->
<security:authorize access="isAuthenticated() && principal.user.id == #user.id">
<h4><spring:message code="team.create-new-team" /></h4>
<form id="new-team-form" class="form-horizontal">
......@@ -118,6 +128,8 @@
<script type="text/javascript">
jQuery(document).ready(function() {
<security:authorize access="principal.user.id == #user.id">
$("#new-team-form input[type=submit]").on("click", function(e) {
e.preventDefault();
......
<%@ page contentType=" ISO-8859-1ISO-8859-1;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Your Plugin Code</h4>
</div>
<div class="modal-body" style="overflow: auto;height: 3--px">
<c:if test="${script ne null}">
<textarea id="area" style="min-width: 535px;min-height: 200px">${script}</textarea>
</c:if>
<label for="clients">Select client details</label>
<select name="clients" id="clientselector" class="get_widget form-control" >
<c:forEach items="${clientDetails}" var="detail">
<option class="get_widget" ${detail.clientId== client.clientId?'selected':''}>${detail.clientId}</option>
</c:forEach>
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal"><spring:message code="filter.apply"/></button>
</div>
......@@ -135,6 +135,16 @@
<filter-name>envVarFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<description>Filter API</description>
<filter-name>apiFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>apiFilter</filter-name>
<url-pattern>/api/genesys-api.js</url-pattern>
</filter-mapping>
<filter>
<filter-name>sitemesh</filter-name>
......@@ -144,6 +154,7 @@
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>
......
alert("Wrong token for genesys-api.js")
\ No newline at end of file
GenesysPrg = {
Accession: function (data) {
var json={crop: data.crop, startAt: data.startAt, maxRecords: data.maxRecords, other_option: data.other_option};
$.ajax('/explore/acc', {
dataType: 'json',
type: 'post',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(json),
success: function (accessions) {
data.success(accessions);
},
error: function (errorAsync) {
data.error(errorAsync);
}
});
}
}
var data = {
crop: this.crop,
startAt: this.startAt,
maxRecords: this.maxRecords,
other_option: this.other_option,
success: new Function(),
error: new Function()
}
......@@ -323,7 +323,22 @@ GenesysFilterUtil = {
};
jQuery(document).ready(function() {
var sc=$("#selcounter");
$("body").on("click", ".get_widget", function () {
var clientId = $(this).val();
$.ajax("/get_widget", {
type: 'GET',
data: 'clientId=' + clientId,
success: function (data) {
$(".modal-content").empty();
$(".modal-content").append(data);
},
error: function (error) {
console.log(error);
}
});
});
var sc=$("#selcounter");
$.ajax({
url: "/sel/json/selection",
......
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