Added RegisteredServicesUtils

This commit is contained in:
Katerina Iatropoulou 2020-10-07 14:20:32 +00:00
parent 91c90cc1bb
commit 1f8f7b598d
4 changed files with 90 additions and 20 deletions

View File

@ -15,9 +15,16 @@ public class EditRegisteredService extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String serviceId = request.getParameter("id");
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
//AuthenticationUtils.checkAuthenticationAndRedirect(authentication, response);
String accessToken = authentication.getAccessTokenValue();
String serviceId = request.getParameter("id");
if (serviceId == null || serviceId.isEmpty()) {
request.getSession().setAttribute("message", "No selected service is provided.");
}
ServiceResponse serviceResponse = TokenUtils.getRegisteredService(serviceId, accessToken);
request.getSession().setAttribute("serviceId", serviceResponse.getId());
request.getSession().setAttribute("first_name", serviceResponse.getClientName());

View File

@ -1,30 +1,37 @@
package eu.dnetlib.openaire.usermanagement;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import eu.dnetlib.openaire.user.pojos.RegisteredService;
import eu.dnetlib.openaire.user.utils.VerificationActions;
import eu.dnetlib.openaire.usermanagement.utils.RegisteredServicesUtils;
import eu.dnetlib.openaire.usermanagement.utils.TokenUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
public class RegisterServiceServlet extends HttpServlet {
@Value("${oidc.issuer}")
private String issuer;
private Logger logger = Logger.getLogger(RegisterServiceServlet.class);
public void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
@Autowired
private RegisteredServicesUtils registeredServicesUtils;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
@ -44,17 +51,40 @@ public class RegisterServiceServlet extends HttpServlet {
String email = authentication.getUserInfo().getEmail();
String accessToken = authentication.getAccessTokenValue();
if ( name!= null && !name.isEmpty() && userid != null && !userid.isEmpty() &&
email != null && email.isEmpty()) {
String serverMessage = TokenUtils.registerService(userid, name, description, email, accessToken);
ServiceResponse serviceResponse = new Gson().fromJson(serverMessage, ServiceResponse.class);
ServiceResponse serviceResponse = null;
System.out.println("The created id is " + serviceResponse.getId());
if ( name!= null && !name.isEmpty() && userid != null && !userid.isEmpty() &&
email != null && !email.isEmpty()) {
String serverMessage = TokenUtils.registerService(userid, name, description, email, accessToken);
if (serverMessage == null) {
request.getSession().setAttribute("message", "There was an error registering your service. Please try again later.");
response.sendRedirect("./registeredServices");
}
serviceResponse = new Gson().fromJson(serverMessage, ServiceResponse.class);
}
String aai_id = serviceResponse.getId();
String owner = userid;
RegisteredService registeredService = new RegisteredService(aai_id, owner, name);
try {
System.out.println("I WILL CONTACT THE DB");
registeredServicesUtils.addRegistedService(registeredService);
} catch (SQLException sqle) {
System.out.println("SQLE SKATA " + sqle.getMessage());
logger.error("SQLE SKATA", sqle);
request.getSession().setAttribute("message", "There was an error registering your service. Please try again later.");
response.sendRedirect("/registeredServices");
}
request.getSession().setAttribute("accessToken", authentication.getAccessTokenValue());
request.getSession().setAttribute("refreshToken", authentication.getRefreshTokenValue());
request.getRequestDispatcher("./serviceTokens.jsp").include(request, response);
request.getRequestDispatcher("./registeredServices.jsp").include(request, response);
}
}

View File

@ -0,0 +1,28 @@
package eu.dnetlib.openaire.usermanagement.utils;
import eu.dnetlib.openaire.user.pojos.RegisteredService;
import eu.dnetlib.openaire.user.registeredService.RegisteredServiceDao;
import eu.dnetlib.openaire.user.registeredService.RegisteredServiceSQL;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
@Component
public class RegisteredServicesUtils {
RegisteredServiceDao registeredServiceDao = new RegisteredServiceSQL();
public RegisteredServiceDao getRegisteredServiceDao() {
return registeredServiceDao;
}
public void setRegisteredServiceDao(RegisteredServiceDao registeredServiceDao) {
this.registeredServiceDao = registeredServiceDao;
}
public void addRegistedService(RegisteredService registeredService) throws SQLException {
System.out.println("I AM HEEEEEEEEERE :P");
registeredServiceDao.insertRegisteredService(registeredService);
}
}

View File

@ -41,7 +41,12 @@ public class TokenUtils {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse httpResponse = httpclient.execute(httppost);
return IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
System.out.println("HTTP RESPONSE " + httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
return IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name());
}
return null;
}
public static HttpResponse updateService(String serviceId, String userId, String name, String description, String email, String accessToken) throws IOException {