From 025c55d49c191f24d0e06c5cbb69a9894c50af22 Mon Sep 17 00:00:00 2001 From: Sasha Sherstnev Date: Mon, 9 Dec 2013 08:40:28 +0000 Subject: [PATCH] 1. Move query jsps into a separate directory 2. Re-design getting Principal in controllers --- server/compbio/controllers/BasicController.java | 21 ++++++++++ .../controllers/DailyStatisticsController.java | 19 +++++---- ...dateController.java => DatabaseController.java} | 12 +++--- .../controllers/DocumentationController.java | 25 +++++------- server/compbio/controllers/IPDataController.java | 29 +++++++------- server/compbio/controllers/JobController.java | 22 +++++------ server/compbio/controllers/MainController.java | 25 +++--------- server/compbio/controllers/SequenceController.java | 41 ++++++++++---------- server/compbio/controllers/UserController.java | 17 ++++++-- server/compbio/statistic/CassandraRequester.java | 6 +-- webapp/view/fragments/mainmenu.jsp | 2 +- webapp/view/{queryIP.jsp => query/IP.jsp} | 6 +-- .../IPStatistics.jsp} | 6 +-- webapp/view/{queryJobLog.jsp => query/JobLog.jsp} | 6 +-- .../JobStatistics.jsp} | 6 +-- .../JobTimeExecution.jsp} | 6 +-- .../{queryRemoveJobs.jsp => query/RemoveJobs.jsp} | 6 +-- .../Sequence.jsp} | 10 ++--- .../SequenceCounts.jsp} | 6 +-- webapp/view/reportJobLog.jsp | 4 +- webapp/view/reportProteinSequences.jsp | 6 +-- webapp/view/reportProteinSequencesCounter.jsp | 2 +- 22 files changed, 147 insertions(+), 136 deletions(-) create mode 100644 server/compbio/controllers/BasicController.java rename server/compbio/controllers/{DBUpdateController.java => DatabaseController.java} (90%) rename webapp/view/{queryIP.jsp => query/IP.jsp} (84%) rename webapp/view/{queryIPStatistics.jsp => query/IPStatistics.jsp} (91%) rename webapp/view/{queryJobLog.jsp => query/JobLog.jsp} (84%) rename webapp/view/{queryJobStatistics.jsp => query/JobStatistics.jsp} (88%) rename webapp/view/{queryTimeExecution.jsp => query/JobTimeExecution.jsp} (89%) rename webapp/view/{queryRemoveJobs.jsp => query/RemoveJobs.jsp} (92%) rename webapp/view/{queryProteinSequence.jsp => query/Sequence.jsp} (80%) rename webapp/view/{queryProteinSequenceCounter.jsp => query/SequenceCounts.jsp} (91%) diff --git a/server/compbio/controllers/BasicController.java b/server/compbio/controllers/BasicController.java new file mode 100644 index 0000000..d4be834 --- /dev/null +++ b/server/compbio/controllers/BasicController.java @@ -0,0 +1,21 @@ +package compbio.controllers; + +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +/* +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +*/ + +public class BasicController { + + protected String getPrincipalName() { + Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + if (principal instanceof UserDetails) { + return ((UserDetails) principal).getUsername(); + } + return principal.toString(); + } +} diff --git a/server/compbio/controllers/DailyStatisticsController.java b/server/compbio/controllers/DailyStatisticsController.java index d508c82..d6a0c1e 100644 --- a/server/compbio/controllers/DailyStatisticsController.java +++ b/server/compbio/controllers/DailyStatisticsController.java @@ -1,6 +1,5 @@ package compbio.controllers; -import java.security.Principal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -23,11 +22,11 @@ import compbio.statistic.StatisticsProt; * @author Natasha Sherstneva */ @Controller -public class DailyStatisticsController { +public class DailyStatisticsController extends BasicController { @RequestMapping(value = "/stat/jobs/query", method = RequestMethod.GET) - public String initFindForm(Map model, Principal principal) { - model.put("username", principal.getName()); + public String initFindForm(Map model) { + model.put("username", getPrincipalName()); Calendar cal = Calendar.getInstance(); String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE); cal.add(Calendar.DATE, -3); @@ -36,13 +35,13 @@ public class DailyStatisticsController { model.put("date1", date1); model.put("date2", date2); - return "queryJobStatistics"; + return "query/JobStatistics"; } @RequestMapping(value = "/stat/jobsdaily/results", method = RequestMethod.GET) public String findJobsInPeriod(@RequestParam("date1") String date1, @RequestParam("date2") String date2, - @RequestParam("option") String option, Map model, Principal principal) { - model.put("username", principal.getName()); + @RequestParam("option") String option, Map model) { + model.put("username", getPrincipalName()); final long startTime = System.currentTimeMillis(); CassandraRequester cr = new CassandraRequester(); @@ -63,9 +62,9 @@ public class DailyStatisticsController { } @RequestMapping(value = "/stat/jobsoneday/results", method = RequestMethod.GET) - public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("status") String status, Map model, - Principal principal) throws ParseException { - model.put("username", principal.getName()); + public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("status") String status, Map model) + throws ParseException { + model.put("username", getPrincipalName()); final long startTime = System.currentTimeMillis(); String realdate; diff --git a/server/compbio/controllers/DBUpdateController.java b/server/compbio/controllers/DatabaseController.java similarity index 90% rename from server/compbio/controllers/DBUpdateController.java rename to server/compbio/controllers/DatabaseController.java index a0e9e41..ee0c9b9 100644 --- a/server/compbio/controllers/DBUpdateController.java +++ b/server/compbio/controllers/DatabaseController.java @@ -15,10 +15,11 @@ import compbio.cassandra.CassandraRemover; * @author Natasha Sherstneva */ @Controller -public class DBUpdateController { +public class DatabaseController extends BasicController { @RequestMapping(value = "/database/remove", method = RequestMethod.GET) public String initRemoveForm(Map model) { + model.put("username", getPrincipalName()); Calendar cal = Calendar.getInstance(); String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE); cal.add(Calendar.DATE, -3); @@ -30,13 +31,14 @@ public class DBUpdateController { model.put("date2", date2); model.put("sampleip", "127.0.0.1"); model.put("sampleseq", "ATA"); - return "queryRemoveJobs"; + return "query/RemoveJobs"; } @RequestMapping(value = "/database/state", method = RequestMethod.GET) public String initDBStatRequest(Map model) { + model.put("username", getPrincipalName()); // return "queryDBState"; - return "support/Notpermitted"; + return "support/Denied"; } @RequestMapping(value = "/database/remove/goreal", method = RequestMethod.GET) @@ -44,7 +46,7 @@ public class DBUpdateController { @RequestParam("byDate") String flagDate, @RequestParam("date1") String date1, @RequestParam("date2") String date2, @RequestParam("byIp") String flagIp, @RequestParam("ip") String ip, @RequestParam("bySequence") String flagSeq, @RequestParam("seq") String seq, Map model) { - + model.put("username", getPrincipalName()); CassandraRemover cr = new CassandraRemover(); int numberRemover = 0; if (flagId != null) @@ -62,7 +64,7 @@ public class DBUpdateController { @RequestMapping(value = "/database/remove/go", method = RequestMethod.GET) public String findIPwithCounter(@RequestParam("id") String jobId, @RequestParam("date1") String date1, @RequestParam("date2") String date2, @RequestParam("ip") String ip, @RequestParam("seq") String seq, Map model) { - + model.put("username", getPrincipalName()); /* * CassandraRemover cr = new CassandraRemover(); int numberRemover = 0; * if (flagId != null) numberRemover = cr.RemoveJobById(jobId); if diff --git a/server/compbio/controllers/DocumentationController.java b/server/compbio/controllers/DocumentationController.java index 9e7de42..07c72b9 100644 --- a/server/compbio/controllers/DocumentationController.java +++ b/server/compbio/controllers/DocumentationController.java @@ -1,6 +1,5 @@ package compbio.controllers; -import java.security.Principal; import java.util.Map; import org.springframework.stereotype.Controller; @@ -14,36 +13,30 @@ import org.springframework.web.bind.annotation.RequestMethod; * @author Natasha Sherstneva */ @Controller -public class DocumentationController { +public class DocumentationController extends BasicController { @RequestMapping(value = "/help/overview", method = RequestMethod.GET) - public String formOverviewPage(Map model, Principal principal) { - model.put("username", getPrincipleName(principal)); + public String formOverviewPage(Map model) { + model.put("username", getPrincipalName()); return "help/Overview"; } @RequestMapping(value = "/help/howto", method = RequestMethod.GET) - public String formHowtoPage(Map model, Principal principal) { - model.put("username", getPrincipleName(principal)); + public String formHowtoPage(Map model) { + model.put("username", getPrincipalName()); return "support/Notimplemented"; } @RequestMapping(value = "/help/doc", method = RequestMethod.GET) - public String formDocPage(Map model, Principal principal) { - model.put("username", getPrincipleName(principal)); + public String formDocPage(Map model) { + model.put("username", getPrincipalName()); return "support/Notimplemented"; } @RequestMapping(value = "/help/javadoc", method = RequestMethod.GET) - public String formJavadoc(Map model, Principal principal) { - model.put("username", getPrincipleName(principal)); + public String formJavadoc(Map model) { + model.put("username", getPrincipalName()); return "support/Notimplemented"; } - private String getPrincipleName(Principal principal) { - if (null != principal.getName()) { - return principal.getName(); - } - return "unknown"; - } } diff --git a/server/compbio/controllers/IPDataController.java b/server/compbio/controllers/IPDataController.java index a7cd7f0..7c17a7f 100644 --- a/server/compbio/controllers/IPDataController.java +++ b/server/compbio/controllers/IPDataController.java @@ -1,6 +1,5 @@ package compbio.controllers; -import java.security.Principal; import java.util.List; import java.util.Map; @@ -18,29 +17,29 @@ import compbio.statistic.CassandraRequester; * @author Natasha Sherstneva */ @Controller -public class IPDataController { +public class IPDataController extends BasicController { @RequestMapping(value = "/admin/ip/counts/query", method = RequestMethod.GET) - public String initStatisticsForm(Map model, Principal principal) { - model.put("username", principal.getName()); + public String initStatisticsForm(Map model) { + model.put("username", "name:" + getPrincipalName()); model.put("value", 5); - return "queryIPStatistics"; + return "query/IPStatistics"; } @RequestMapping(value = "/admin/ip/query", method = RequestMethod.GET) - public String initOneIPForm(Map model, Principal principal) { - model.put("username", principal.getName()); + public String initOneIPForm(Map model) { + model.put("username", getPrincipalName()); model.put("value", "127.0.0.1"); - return "queryIP"; + return "query/IP"; } @RequestMapping(value = "/admin/ip/counts/results", method = RequestMethod.GET) - public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map model, Principal principal) { - model.put("username", principal.getName()); + public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map model) { + model.put("username", getPrincipalName()); if (counter.equals("")) { model.put("error", "The value must not be empty"); model.put("value", counter); - return "queryIPStatistics"; + return "query/IPStatistics"; } int realcounter; @@ -49,13 +48,13 @@ public class IPDataController { } catch (NumberFormatException e) { model.put("error", "The value must be an integer number"); model.put("value", counter); - return "queryIPStatistics"; + return "query/IPStatistics"; } if (realcounter < 1) { model.put("error", "The value must be greater than 0"); model.put("value", counter); - return "queryIPStatistics"; + return "query/IPStatistics"; } final long startTime = System.currentTimeMillis(); @@ -73,8 +72,8 @@ public class IPDataController { } @RequestMapping(value = "/admin/ip/results", method = RequestMethod.GET) - public String findIP(@RequestParam("ip") String ip, Map model, Principal principal) { - model.put("username", principal.getName()); + public String findIP(@RequestParam("ip") String ip, Map model) { + model.put("username", getPrincipalName()); final long startTime = System.currentTimeMillis(); CassandraRequester cr = new CassandraRequester(); UserBean r = cr.readIp(ip); diff --git a/server/compbio/controllers/JobController.java b/server/compbio/controllers/JobController.java index 6bfe4ee..94045f9 100644 --- a/server/compbio/controllers/JobController.java +++ b/server/compbio/controllers/JobController.java @@ -19,11 +19,11 @@ import compbio.statistic.StatisticsProt; * @author Natasha Sherstneva */ @Controller -public class JobController { +public class JobController extends BasicController { @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET) - public String initFormExecTime(Map model, Principal principal) { - model.put("username", principal.getName()); + public String initFormExecTime(Map model) { + model.put("username", getPrincipalName()); Calendar cal = Calendar.getInstance(); String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE); cal.add(Calendar.DATE, -3); @@ -31,20 +31,20 @@ public class JobController { model.put("date1", date1); model.put("date2", date2); - return "queryTimeExecution"; + return "query/JobTimeExecution"; } @RequestMapping(value = "/job/query", method = RequestMethod.GET) - public String initFindForm(Map model, Principal principal) { - model.put("username", principal.getName()); + public String initFindForm(Map model) { + model.put("username", getPrincipalName()); model.put("value", "jp_NzBOJKo"); - return "queryJobLog"; + return "query/JobLog"; } @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET) public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2, - @RequestParam(value = "option", required = false) String option, Map model, Principal principal) { - model.put("username", principal.getName()); + @RequestParam(value = "option", required = false) String option, Map model) { + model.put("username", getPrincipalName()); final long startTime = System.currentTimeMillis(); CassandraRequester sp = new CassandraRequester(); @@ -65,8 +65,8 @@ public class JobController { } @RequestMapping(value = "/job/results", method = RequestMethod.GET) - public String findJob(@RequestParam("IdJob") String jobid, Map model, Principal principal) { - model.put("username", principal.getName()); + public String findJob(@RequestParam("IdJob") String jobid, Map model) { + model.put("username", getPrincipalName()); final long startTime = System.currentTimeMillis(); CassandraRequester cr = new CassandraRequester(); model.put("result", cr.readJobLog(jobid)); diff --git a/server/compbio/controllers/MainController.java b/server/compbio/controllers/MainController.java index f918c24..4aeb1cd 100644 --- a/server/compbio/controllers/MainController.java +++ b/server/compbio/controllers/MainController.java @@ -1,21 +1,15 @@ package compbio.controllers; -import java.security.Principal; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller -public class MainController { +public class MainController extends BasicController { @RequestMapping(value = "/index", method = RequestMethod.GET) - public String printPublicHome(ModelMap model, Principal principal) { - String name = ""; - // if (null != name ) { - // name = principal.getName(); - // } - model.addAttribute("username", name); + public String printPublicHome(ModelMap model) { model.addAttribute("message", "Spring Security Custom Form example"); return "public"; } @@ -37,21 +31,14 @@ public class MainController { } @RequestMapping(value = "/denied", method = RequestMethod.GET) - public String denied(ModelMap model, Principal principal) { - String name = "unknown"; - if (null != principal.getName()) - name = principal.getName(); - model.put("username", name); + public String denied(ModelMap model) { + model.put("username", getPrincipalName()); return "support/Denied"; } @RequestMapping(value = "/home", method = RequestMethod.GET) - public String printHome(ModelMap model, Principal principal) { - String name = "unknown"; - if (null != name) { - name = principal.getName(); - } - model.addAttribute("username", name); + public String printHome(ModelMap model ) { + model.addAttribute("username", getPrincipalName()); model.addAttribute("message", "Spring Security Custom Form example"); return "home"; } diff --git a/server/compbio/controllers/SequenceController.java b/server/compbio/controllers/SequenceController.java index 6440d74..df4c987 100644 --- a/server/compbio/controllers/SequenceController.java +++ b/server/compbio/controllers/SequenceController.java @@ -1,6 +1,5 @@ package compbio.controllers; -import java.security.Principal; import java.util.List; import java.util.Map; import java.util.regex.Pattern; @@ -26,7 +25,7 @@ import compbio.statistic.CassandraRequester; */ @Controller @RequestMapping("/sequence") -public class SequenceController { +public class SequenceController extends BasicController { /** * pattern for NON-protein alphabet symbols @@ -34,23 +33,23 @@ public class SequenceController { private final Pattern NONPROTEIN = Pattern.compile("[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE); @RequestMapping(value = "query", method = RequestMethod.GET) - public String formSequenceQuery(Map model, Principal principal) { - model.put("username", principal.getName()); + public String formSequenceQuery(Map model) { + model.put("username", getPrincipalName()); model.put("value", "AAAAA"); - return "queryProteinSequence"; + return "query/Sequence"; } @RequestMapping(value = "counts/query", method = RequestMethod.GET) - public String formCounterQuery(Map model, Principal principal) { - model.put("username", principal.getName()); + public String formCounterQuery(Map model) { + model.put("username", getPrincipalName()); model.put("value", 5); - return "queryProteinSequenceCounter"; + return "query/SequenceCounts"; } @RequestMapping(value = "sequence/results", method = RequestMethod.GET) - public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("protein") String flag, Map model, - Principal principal) { - model.put("username", principal.getName()); + public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("searchtype") String searchtype, + Map model) { + model.put("username", getPrincipalName()); final long startTime = System.currentTimeMillis(); // input checks @@ -58,24 +57,24 @@ public class SequenceController { if (trimmedsequence.equalsIgnoreCase("")) { model.put("error", "The sequence cann't be empty"); model.put("value", sequence); - return "queryProteinSequence"; + return "query/Sequence"; } if (NONPROTEIN.matcher(trimmedsequence).find()) { model.put("error", "The sequence contains symbols not from the standard protein alphabet"); model.put("value", sequence); - return "queryProteinSequence"; + return "query/Sequence"; } model.put("njobs", 0); model.put("prot", trimmedsequence); - model.put("flag", flag); + model.put("searchtype", searchtype); if (0 < trimmedsequence.length()) { CassandraRequester cr = new CassandraRequester(); - List r = cr.readProteins(trimmedsequence, flag); + List r = cr.readProteins(trimmedsequence, searchtype); model.put("results", r); if (null != r) { - if (flag.equals("whole")) + if (searchtype.equals("whole")) model.put("njobs", r.get(0).getJobid().size()); else model.put("njobs", r.size()); @@ -87,14 +86,14 @@ public class SequenceController { } @RequestMapping(value = "counts/results", method = RequestMethod.GET) - public String countSequences(@RequestParam("counterJob") String counter, Map model, Principal principal) { - model.put("username", principal.getName()); + public String countSequences(@RequestParam("counterJob") String counter, Map model) { + model.put("username", getPrincipalName()); final long startTime = System.currentTimeMillis(); if (counter.equals("")) { model.put("error", "The value must not be empty"); model.put("value", counter); - return "queryIPStatistics"; + return "query/SequenceCounts"; } int realcounter; @@ -103,13 +102,13 @@ public class SequenceController { } catch (NumberFormatException e) { model.put("error", "The value must be an integer number"); model.put("value", counter); - return "queryIPStatistics"; + return "query/SequenceCounts"; } if (realcounter < 1) { model.put("error", "The value must be greater than 0"); model.put("value", counter); - return "queryIPStatistics"; + return "query/SequenceCounts"; } CassandraRequester cr = new CassandraRequester(); diff --git a/server/compbio/controllers/UserController.java b/server/compbio/controllers/UserController.java index 513f5bb..7dbfdf4 100644 --- a/server/compbio/controllers/UserController.java +++ b/server/compbio/controllers/UserController.java @@ -1,6 +1,5 @@ package compbio.controllers; -import java.security.Principal; import java.util.Date; import java.util.regex.Pattern; @@ -28,13 +27,25 @@ public class UserController { // JavaMailSender mailSender; private final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"); - @RequestMapping(value = "/register/query", method = RequestMethod.GET) - public String printPublicHome(ModelMap model) { + @RequestMapping(value = "/register/query", method = RequestMethod.POST) + public String RegisterForm(ModelMap model) { User user = new User(); model.addAttribute(user); return "Register"; } + @RequestMapping(value = "/register/edit/query", method = RequestMethod.GET) + public String AccountForm(ModelMap model) { + User user = new User(); + user.setFullName("Sasha Sherstnev"); + user.setEmail("admin@admin.com"); + user.setOrganisation("UoD"); + user.setPosition("cleaner"); + user.setUpdateByEmail(true); + model.addAttribute("u", user); + return "Edit"; + } + @RequestMapping(value = "/register/do", method = RequestMethod.POST) public String addUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) { diff --git a/server/compbio/statistic/CassandraRequester.java b/server/compbio/statistic/CassandraRequester.java index e95291a..1bb0050 100755 --- a/server/compbio/statistic/CassandraRequester.java +++ b/server/compbio/statistic/CassandraRequester.java @@ -184,16 +184,16 @@ public class CassandraRequester { /* * query: protein sequence */ - public List readProteins(String protIn, String flag) { + public List readProteins(String protIn, String searchtype) { List result; - if (flag.equals("whole")) + if (searchtype.equals("whole")) result = db.ReadWholeSequence(protIn); else result = db.ReadPartOfSequence(protIn); if (result == null) return null; - if (flag.equals("part")) { + if (searchtype.equals("partial")) { for (ProteinBean entry : result) { entry.setSubProt(CreateSubprot(entry.getSequence(), protIn)); } diff --git a/webapp/view/fragments/mainmenu.jsp b/webapp/view/fragments/mainmenu.jsp index 1379729..9b3316c 100644 --- a/webapp/view/fragments/mainmenu.jsp +++ b/webapp/view/fragments/mainmenu.jsp @@ -39,7 +39,7 @@