View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.sql;
12  
13  import jakarta.servlet.http.HttpServletRequest;
14  import jakarta.servlet.http.HttpServletResponse;
15  import jakarta.servlet.http.HttpSession;
16  
17  import java.nio.charset.StandardCharsets;
18  import java.util.List;
19  
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.springframework.stereotype.Controller;
23  import org.springframework.web.bind.ServletRequestUtils;
24  import org.springframework.web.bind.annotation.RequestMapping;
25  import org.springframework.web.servlet.ModelAndView;
26  import org.springframework.web.servlet.mvc.AbstractController;
27  
28  import psiprobe.model.sql.DataSourceTestInfo;
29  
30  /**
31   * Retrieves a single query from a history list.
32   */
33  @Controller
34  public class QueryHistoryItemController extends AbstractController {
35  
36    /** The Constant logger. */
37    private static final Logger logger = LoggerFactory.getLogger(QueryHistoryItemController.class);
38  
39    @RequestMapping(path = "/sql/queryHistoryItem.ajax")
40    @Override
41    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
42        throws Exception {
43      return super.handleRequest(request, response);
44    }
45  
46    @Override
47    protected ModelAndView handleRequestInternal(HttpServletRequest request,
48        HttpServletResponse response) throws Exception {
49  
50      int sqlId = ServletRequestUtils.getIntParameter(request, "sqlId", -1);
51  
52      HttpSession sess = request.getSession(false);
53  
54      if (sess != null) {
55        DataSourceTestInfo sessData =
56            (DataSourceTestInfo) sess.getAttribute(DataSourceTestInfo.DS_TEST_SESS_ATTR);
57  
58        if (sessData != null) {
59          List<String> queryHistory = sessData.getQueryHistory();
60  
61          if (queryHistory != null) {
62            try {
63              String sql = queryHistory.get(sqlId);
64              response.setCharacterEncoding(StandardCharsets.UTF_8.name());
65              response.getWriter().print(sql);
66            } catch (IndexOutOfBoundsException e) {
67              logger.error("Cannot find a query history entry for history item id = {}", sqlId);
68              logger.trace("", e);
69            }
70          }
71        }
72      }
73  
74      return null;
75    }
76  
77  }