|
12 | 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
13 | 13 | * PURPOSE. See the GNU General Public License for more details |
14 | 14 | * |
15 | | - * @author Juan Gama |
| 15 | + * @author Juan Gama and modified by nesterXneo |
16 | 16 | * @created 2015 |
17 | 17 | */ |
18 | 18 | package org.owasp.benchmark.helpers; |
19 | 19 |
|
20 | 20 | import java.io.IOException; |
| 21 | +import java.sql.Connection; |
| 22 | +import java.sql.PreparedStatement; |
| 23 | +import java.sql.ResultSet; |
| 24 | +import java.sql.SQLException; |
21 | 25 | import java.util.ArrayList; |
22 | 26 | import java.util.List; |
23 | 27 | import javax.servlet.ServletException; |
24 | | -import javax.servlet.http.HttpServletRequest; |
25 | | -import javax.servlet.http.HttpServletResponse; |
26 | 28 | import org.owasp.benchmark.service.pojo.Person; |
27 | 29 | import org.owasp.benchmark.service.pojo.XMLMessage; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
28 | 31 | import org.springframework.http.HttpStatus; |
29 | 32 | import org.springframework.http.ResponseEntity; |
30 | 33 | import org.springframework.web.bind.annotation.GetMapping; |
31 | 34 | import org.springframework.web.bind.annotation.PostMapping; |
32 | 35 | import org.springframework.web.bind.annotation.RequestBody; |
33 | 36 | import org.springframework.web.bind.annotation.RestController; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
34 | 39 |
|
35 | 40 | @RestController |
36 | 41 | public class DataBaseServer { |
37 | 42 |
|
| 43 | + private static final Logger logger = LoggerFactory.getLogger(DataBaseServer.class); |
| 44 | + private static final String NOT_IMPLEMENTED = "Not Implemented."; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private DatabaseService databaseService; |
| 48 | + |
38 | 49 | @GetMapping(value = "/resetdb") |
39 | | - public ResponseEntity<List<XMLMessage>> getOtherOrder( |
40 | | - @RequestBody Person model, HttpServletRequest request, HttpServletResponse response) |
41 | | - throws ServletException, IOException { |
42 | | - ArrayList<XMLMessage> resp = new ArrayList<XMLMessage>(); |
43 | | - resp.add(new XMLMessage("Not Implemented.")); |
44 | | - return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK); |
| 50 | + public ResponseEntity<List<XMLMessage>> resetDatabase(@RequestBody Person model) { |
| 51 | + List<XMLMessage> resp = new ArrayList<>(); |
| 52 | + resp.add(new XMLMessage(NOT_IMPLEMENTED)); |
| 53 | + return new ResponseEntity<>(resp, HttpStatus.OK); |
45 | 54 | } |
46 | 55 |
|
47 | 56 | @PostMapping(value = "/testdb") |
48 | | - public ResponseEntity<List<XMLMessage>> createOrder2( |
49 | | - @RequestBody Person model, HttpServletRequest request, HttpServletResponse response) |
50 | | - throws ServletException, IOException { |
51 | | - List<XMLMessage> resp = new ArrayList<XMLMessage>(); |
52 | | - resp.add(new XMLMessage("Not Implemented.")); |
53 | | - return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK); |
| 57 | + public ResponseEntity<List<XMLMessage>> testDatabase(@RequestBody Person model) { |
| 58 | + List<XMLMessage> resp = new ArrayList<>(); |
| 59 | + resp.add(new XMLMessage(NOT_IMPLEMENTED)); |
| 60 | + return new ResponseEntity<>(resp, HttpStatus.OK); |
54 | 61 | } |
55 | 62 |
|
56 | 63 | @GetMapping(value = "/getall") |
57 | | - public ResponseEntity<List<XMLMessage>> getAll( |
58 | | - HttpServletRequest request, HttpServletResponse response) |
59 | | - throws ServletException, IOException { |
60 | | - List<XMLMessage> resp = new ArrayList<XMLMessage>(); |
| 64 | + public ResponseEntity<List<XMLMessage>> getAll() { |
| 65 | + List<XMLMessage> resp = new ArrayList<>(); |
61 | 66 | String sql = "SELECT * from USERS"; |
62 | | - try { |
63 | | - java.sql.Connection connection = |
64 | | - org.owasp.benchmark.helpers.DatabaseHelper.getSqlConnection(); |
65 | | - java.sql.PreparedStatement statement = connection.prepareStatement(sql); |
66 | | - statement.execute(); |
67 | | - org.owasp.benchmark.helpers.DatabaseHelper.printResults(statement, sql, resp); |
68 | | - } catch (java.sql.SQLException e) { |
69 | | - if (org.owasp.benchmark.helpers.DatabaseHelper.hideSQLErrors) { |
70 | | - e.printStackTrace(); |
71 | | - resp.add(new XMLMessage("Error processing request: " + e.getMessage())); |
72 | | - return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK); |
73 | | - } else throw new ServletException(e); |
| 67 | + |
| 68 | + try (Connection connection = databaseService.getConnection(); |
| 69 | + PreparedStatement statement = connection.prepareStatement(sql); |
| 70 | + ResultSet resultSet = statement.executeQuery()) { |
| 71 | + |
| 72 | + while (resultSet.next()) { |
| 73 | + // Process each row and add to resp |
| 74 | + // This is a placeholder - adjust according to your actual data structure |
| 75 | + resp.add(new XMLMessage(resultSet.getString("username"))); |
| 76 | + } |
| 77 | + |
| 78 | + return new ResponseEntity<>(resp, HttpStatus.OK); |
| 79 | + } catch (SQLException e) { |
| 80 | + logger.error("Database error occurred", e); |
| 81 | + resp.add(new XMLMessage("An error occurred while processing your request.")); |
| 82 | + return new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR); |
74 | 83 | } |
75 | | - return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK); |
76 | | - } |
77 | | - |
78 | | - public static void main(String[] args) { |
79 | | - // This empty main() method is required to be able to start the Database. Otherwise you get |
80 | | - // the error: |
81 | | - |
82 | | - /* |
83 | | - [java] Error: Main method not found in class org.owasp.benchmark.helpers.DataBaseServer, please define the main method as: |
84 | | - [java] public static void main(String[] args) |
85 | | - [java] or a JavaFX application class must extend javafx.application.Application |
86 | | - */ |
87 | 84 | } |
88 | 85 | } |
0 commit comments