Skip to content

Commit

Permalink
Optimise the creation of servlet base object for v2
Browse files Browse the repository at this point in the history
Base object creation has been moved from the request context to the
servlet context to optimise the execution.
  • Loading branch information
fmarco76 committed Jul 22, 2024
1 parent ae45377 commit 96a341e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ public class AuditServletBase {
public static final Logger logger = LoggerFactory.getLogger(AuditServletBase.class);

private CMSEngine engine;
private String userName;

public AuditServletBase(CMSEngine engine, String username) {
public AuditServletBase(CMSEngine engine) {
this.engine = engine;
this.userName = username;
}

public AuditConfig createAuditConfig() throws EBaseException{
Expand Down Expand Up @@ -114,13 +112,13 @@ public AuditConfig createAuditConfig(Map<String, String> auditParams)
return auditConfig;
}

public AuditConfig updateAuditConfig(AuditConfig auditConfig) {
public AuditConfig updateAuditConfig(AuditConfig auditConfig, String user) {
Map<String, String> auditModParams = new HashMap<>();

if (auditConfig == null) {
BadRequestException e = new BadRequestException("Missing audit configuration");
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;
}

Expand Down Expand Up @@ -165,7 +163,7 @@ public AuditConfig updateAuditConfig(AuditConfig auditConfig) {
if (currentValue == null) {
PKIException e = new PKIException("Unable to add event: " + name);
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;
}

Expand All @@ -174,7 +172,7 @@ public AuditConfig updateAuditConfig(AuditConfig auditConfig) {
if (!"mandatory".equals(currentValue)) {
PKIException e = new PKIException("Unable to add mandatory event: " + name);
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;
}
continue;
Expand All @@ -184,7 +182,7 @@ public AuditConfig updateAuditConfig(AuditConfig auditConfig) {
if ("mandatory".equals(currentValue)) {
PKIException e = new PKIException("Unable to remove mandatory event: " + name);
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;
}

Expand All @@ -197,7 +195,7 @@ public AuditConfig updateAuditConfig(AuditConfig auditConfig) {
} else {
PKIException e = new PKIException("Invalid event configuration: " + name + "=" + value);
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;
}
}
Expand All @@ -210,33 +208,33 @@ public AuditConfig updateAuditConfig(AuditConfig auditConfig) {
if (!eventConfigs.containsKey(name)) {
PKIException e = new PKIException("Unable to remove event: " + name);
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;
}
}

cs.commit(true);

auditConfig = createAuditConfig(auditModParams);
auditTPSConfigSignedAudit(ILogger.SUCCESS, auditModParams);
auditTPSConfigSignedAudit(ILogger.SUCCESS, auditModParams, user);

return auditConfig;

} catch (PKIException e) {
logger.error("Unable to update audit configuration: " + e.getMessage(), e);
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;

} catch (Exception e) {
logger.error("Unable to update audit configuration: " + e.getMessage(), e);
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw new PKIException(e.getMessage());
}
}

public AuditConfig changeAuditStatus(String action) {
public AuditConfig changeAuditStatus(String action, String user) {
Map<String, String> auditModParams = new HashMap<>();

logger.debug("AuditServletBase.changeAuditStatus()");
Expand All @@ -257,25 +255,25 @@ public AuditConfig changeAuditStatus(String action) {
} else {
BadRequestException e = new BadRequestException("Invalid action " + action);
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;
}

cs.commit(true);

AuditConfig auditConfig = createAuditConfig();
auditTPSConfigSignedAudit(ILogger.SUCCESS, auditModParams);
auditTPSConfigSignedAudit(ILogger.SUCCESS, auditModParams, user);

return auditConfig;

} catch (PKIException e) {
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw e;

} catch (Exception e) {
auditModParams.put("Info", e.toString());
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams);
auditTPSConfigSignedAudit(ILogger.FAILURE, auditModParams, user);
throw new PKIException(e.getMessage());
} }

Expand Down Expand Up @@ -361,12 +359,12 @@ public File getCurrentLogFile() {
/*
* in case of failure, "info" should be in the params
*/
private void auditTPSConfigSignedAudit(String status, Map<String, String> params) {
private void auditTPSConfigSignedAudit(String status, Map<String, String> params, String user) {

Auditor auditor = engine.getAuditor();

auditor.log(new ConfigSignedAuditEvent(
userName,
user,
status,
auditor.getParamString(params)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void login(HttpServletRequest request, HttpServletResponse response) thro
PrintWriter out = response.getWriter();
out.println(account.toJSON());
}

@WebAction(method = HttpMethod.GET, paths = {"logout"})
public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.PrintWriter;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
Expand All @@ -30,13 +31,20 @@ public class AuditServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
public static final Logger logger = LoggerFactory.getLogger(AuditServlet.class);

private AuditServletBase auditServletBase;

@Override
public void init() throws ServletException {
super.init();
auditServletBase = new AuditServletBase(getEngine());
}

@WebAction(method = HttpMethod.GET, paths = {""})
public void getAuditConfig(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("AuditServlet.getAuditConfig(): session: {}", session.getId());
PrintWriter out = response.getWriter();
AuditServletBase auditServlet = new AuditServletBase(getEngine(), request.getUserPrincipal().getName());
AuditConfig auditConfig = auditServlet.createAuditConfig();
AuditConfig auditConfig = auditServletBase.createAuditConfig();
out.println(auditConfig.toJSON());
}

Expand All @@ -47,8 +55,7 @@ public void updateAuditConfig(HttpServletRequest request, HttpServletResponse re
PrintWriter out = response.getWriter();
String requestData = request.getReader().lines().collect(Collectors.joining());
AuditConfig auditConfig = JSONSerializer.fromJSON(requestData, AuditConfig.class);
AuditServletBase auditServlet = new AuditServletBase(getEngine(), request.getUserPrincipal().getName());
AuditConfig auditConfigNew = auditServlet.updateAuditConfig(auditConfig);
AuditConfig auditConfigNew = auditServletBase.updateAuditConfig(auditConfig, request.getUserPrincipal().getName());
out.println(auditConfigNew.toJSON());
}

Expand All @@ -58,8 +65,7 @@ public void changeAuditStatus(HttpServletRequest request, HttpServletResponse re
logger.debug("AuditServlet.changeAuditStatus(): session: {}", session.getId());
PrintWriter out = response.getWriter();
String action = request.getParameter("action");
AuditServletBase auditServlet = new AuditServletBase(getEngine(), request.getUserPrincipal().getName());
AuditConfig auditConfigNew = auditServlet.changeAuditStatus(action);
AuditConfig auditConfigNew = auditServletBase.changeAuditStatus(action, request.getUserPrincipal().getName());
out.println(auditConfigNew.toJSON());
}

Expand All @@ -68,8 +74,7 @@ public void findAuditFiles(HttpServletRequest request, HttpServletResponse respo
HttpSession session = request.getSession();
logger.debug("AuditServlet.changeAuditStatus(): session: {}", session.getId());
PrintWriter out = response.getWriter();
AuditServletBase auditServlet = new AuditServletBase(getEngine(), request.getUserPrincipal().getName());
AuditFileCollection auditConfigNew = auditServlet.findAuditFiles();
AuditFileCollection auditConfigNew = auditServletBase.findAuditFiles();
out.println(auditConfigNew.toJSON());
}

Expand All @@ -79,8 +84,7 @@ public void getAuditFile(HttpServletRequest request, HttpServletResponse respons
logger.debug("AuditServlet.getAuditFile(): session: {}", session.getId());
String[] pathElement = request.getPathInfo().substring(1).split("/");
String fileName = pathElement[1];
AuditServletBase auditServlet = new AuditServletBase(getEngine(), request.getUserPrincipal().getName());
File auditFile = auditServlet.getAuditFile(fileName);
File auditFile = auditServletBase.getAuditFile(fileName);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setContentLengthLong(auditFile.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
Expand All @@ -26,13 +27,20 @@ public class JobServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
public static final Logger logger = LoggerFactory.getLogger(JobServlet.class);

private JobServletBase jobServletBase;

@Override
public void init() throws ServletException {
super.init();
jobServletBase = new JobServletBase(getEngine());
}

@WebAction(method = HttpMethod.GET, paths = {""})
public void getJobs(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("JobServlet.getJobs(): session: {}", session.getId());
PrintWriter out = response.getWriter();
JobServletBase jobServlet = new JobServletBase(getEngine());
JobCollection jobs = jobServlet.findJobs(request.getUserPrincipal());
JobCollection jobs = jobServletBase.findJobs(request.getUserPrincipal());
out.println(jobs.toJSON());
}

Expand All @@ -42,8 +50,7 @@ public void getJob(HttpServletRequest request, HttpServletResponse response) thr
logger.debug("JobServlet.getJob(): session: {}", session.getId());
String[] pathElement = request.getPathInfo().substring(1).split("/");
String jobId = pathElement[0];
JobServletBase jobServlet = new JobServletBase(getEngine());
JobInfo job = jobServlet.getJob(jobId, request.getUserPrincipal());
JobInfo job = jobServletBase.getJob(jobId, request.getUserPrincipal());
PrintWriter out = response.getWriter();
out.println(job.toJSON());
}
Expand All @@ -54,7 +61,6 @@ public void postJobStart(HttpServletRequest request, HttpServletResponse respons
logger.debug("JobServlet.postJobStart(): session: {}", session.getId());
String[] pathElement = request.getPathInfo().substring(1).split("/");
String jobId = pathElement[0];
JobServletBase jobServlet = new JobServletBase(getEngine());
jobServlet.startJob(jobId, request.getUserPrincipal());
jobServletBase.startJob(jobId, request.getUserPrincipal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
Expand All @@ -28,6 +29,14 @@ public class SelfTestServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
public static final Logger logger = LoggerFactory.getLogger(SelfTestServlet.class);

private SelfTestServletBase selfTestServletBase;

@Override
public void init() throws ServletException {
super.init();
selfTestServletBase = new SelfTestServletBase(getEngine());
}

@WebAction(method = HttpMethod.GET, paths = {""})
public void findTests(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
Expand All @@ -36,8 +45,7 @@ public void findTests(HttpServletRequest request, HttpServletResponse response)
int size = request.getParameter("size") == null ?
PKIServlet.DEFAULT_SIZE : Integer.parseInt(request.getParameter("size"));
int start = request.getParameter("start") == null ? 0 : Integer.parseInt(request.getParameter("start"));
SelfTestServletBase selfTestServlet = new SelfTestServletBase(getEngine());
SelfTestCollection tests = selfTestServlet.findSelfTests(filter, start, size);
SelfTestCollection tests = selfTestServletBase.findSelfTests(filter, start, size);
PrintWriter out = response.getWriter();
out.println(tests.toJSON());
}
Expand All @@ -48,8 +56,7 @@ public void getTest(HttpServletRequest request, HttpServletResponse response) th
logger.debug("SelfTestServlet.getTest(): session: {}", session.getId());
String[] pathElement = request.getPathInfo().substring(1).split("/");
String selfTestId = pathElement[0];
SelfTestServletBase selfTestServlet = new SelfTestServletBase(getEngine());
SelfTestData test = selfTestServlet.getSelfTest(selfTestId);
SelfTestData test = selfTestServletBase.getSelfTest(selfTestId);
PrintWriter out = response.getWriter();
out.println(test.toJSON());
}
Expand All @@ -59,17 +66,15 @@ public void executeTests(HttpServletRequest request, HttpServletResponse respons
HttpSession session = request.getSession();
logger.debug("SelfTestServlet.executeTests(): session: {}", session.getId());
String action = request.getParameter("action");
SelfTestServletBase selfTestServlet = new SelfTestServletBase(getEngine());
selfTestServlet.executeSelfTests(action);
selfTestServletBase.executeSelfTests(action);
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}

@WebAction(method = HttpMethod.POST, paths = {"run"})
public void runTests(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("SelfTestServlet.runTests(): session: {}", session.getId());
SelfTestServletBase selfTestServlet = new SelfTestServletBase(getEngine());
SelfTestResults results = selfTestServlet.runSelfTests();
SelfTestResults results = selfTestServletBase.runSelfTests();
PrintWriter out = response.getWriter();
out.println(results.toJSON());

Expand All @@ -81,8 +86,7 @@ public void runTest(HttpServletRequest request, HttpServletResponse response) th
logger.debug("SelfTestServlet.runTest(): session: {}", session.getId());
String[] pathElement = request.getPathInfo().substring(1).split("/");
String testId = pathElement[0];
SelfTestServletBase selfTestServlet = new SelfTestServletBase(getEngine());
SelfTestResult result = selfTestServlet.runSelfTest(testId);
SelfTestResult result = selfTestServletBase.runSelfTest(testId);
PrintWriter out = response.getWriter();
out.println(result.toJSON());
}
Expand Down
Loading

0 comments on commit 96a341e

Please sign in to comment.