Add additional checks of input parameters from html forms
[proteocache.git] / server / compbio / controllers / IPDataController.java
1 package compbio.controllers;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RequestMethod;
9 import org.springframework.web.bind.annotation.RequestParam;
10
11 import compbio.cassandra.DataBase;
12 import compbio.statistic.CassandraRequester;
13
14 /**
15  * @author Alexander Sherstnev
16  * @author Natasha Sherstneva
17  */
18 @Controller
19 public class IPDataController {
20
21         @RequestMapping(value = "/ip/stat", method = RequestMethod.GET)
22         public String initStatisticsForm(Map<String, Object> model) {
23                 model.put("value", 5);
24                 return "queryIPStatistics";
25         }
26
27         @RequestMapping(value = "/ip", method = RequestMethod.GET)
28         public String initOneIPForm(Map<String, Object> model) {
29                 model.put("value", "127.0.0.1");
30                 return "queryIP";
31         }
32         
33         @RequestMapping(value = "/ip/stat/querycounter", method = RequestMethod.GET)
34         public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map<String, Object> model) {
35
36                 if (counter.equals("")) {
37                         model.put("error", "The value must not be empty");
38                         model.put("value", counter);
39                         return "queryIPStatistics";
40                 }
41         
42                 int realcounter;
43                 try {
44                         realcounter = Integer.parseInt(counter.trim());
45                 } catch (NumberFormatException e) {
46                         model.put("error", "The value must be an integer number");
47                         model.put("value", counter);
48                         return "queryIPStatistics";
49                 }
50
51                 if (realcounter < 1) {
52                         model.put("error", "The value must be greater than 0");
53                         model.put("value", counter);
54                         return "queryIPStatistics";
55                 }
56
57                 final long startTime = System.currentTimeMillis();
58                 CassandraRequester cr = new CassandraRequester();
59                 List<DataBase> r = cr.readIpByCounter(realcounter);
60                 model.put("results", r);
61                 model.put("njobs", 0);
62                 if (null != r) {
63                         model.put("njobs", r.size());
64                 }
65                 final long endTime = System.currentTimeMillis();
66                 model.put("timeExecution", (endTime - startTime));
67                 model.put("counter", realcounter);
68                 return "reportIPstatistics";
69         }
70
71         @RequestMapping(value = "/ip/query", method = RequestMethod.GET)
72         public String findIP(@RequestParam("ip") String ip, Map<String, Object> model) {
73                 final long startTime = System.currentTimeMillis();
74                 CassandraRequester cr = new CassandraRequester();
75                 List<DataBase> r = cr.readIp(ip);
76                 model.put("results", r);
77                 model.put("njobs", 0);
78                 if (null != r) {
79                         model.put("njobs", r.size());
80                 }
81                 final long endTime = System.currentTimeMillis();
82                 model.put("timeExecution", (endTime - startTime));
83                 model.put("ip", ip);
84                 return "reportIP";
85         }
86 }