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 org.springframework.beans.factory.annotation.Value;
18  import org.springframework.stereotype.Controller;
19  import org.springframework.web.bind.annotation.RequestMapping;
20  import org.springframework.web.servlet.ModelAndView;
21  import org.springframework.web.servlet.mvc.ParameterizableViewController;
22  
23  import psiprobe.model.sql.DataSourceTestInfo;
24  import psiprobe.tools.TimeExpression;
25  
26  /**
27   * Displays a view that allows for a database connectivity testing. Supplies default values to input
28   * fields of the view.
29   */
30  @Controller
31  public class DataSourceTestController extends ParameterizableViewController {
32  
33    /** The max rows. */
34    private int maxRows;
35  
36    /** The rows per page. */
37    private int rowsPerPage;
38  
39    /** The history size. */
40    private int historySize;
41  
42    /** The replace pattern. */
43    private String replacePattern;
44  
45    /** The collection period. */
46    private long collectionPeriod;
47  
48    @RequestMapping(path = "/sql/datasourcetest.htm")
49    @Override
50    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
51        throws Exception {
52      return super.handleRequest(request, response);
53    }
54  
55    @Override
56    protected ModelAndView handleRequestInternal(HttpServletRequest request,
57        HttpServletResponse response) throws Exception {
58  
59      HttpSession sess = request.getSession(false);
60  
61      DataSourceTestInfo sessData = null;
62  
63      if (sess != null) {
64        sessData = (DataSourceTestInfo) sess.getAttribute(DataSourceTestInfo.DS_TEST_SESS_ATTR);
65      }
66  
67      String referer = request.getHeader("Referer");
68      String backUrl;
69      if (referer != null) {
70        backUrl = referer.replaceAll(replacePattern, "");
71      } else {
72        backUrl = null;
73      }
74  
75      return new ModelAndView(getViewName())
76          .addObject("maxRows",
77              String.valueOf(sessData == null ? getMaxRows() : sessData.getMaxRows()))
78          .addObject("rowsPerPage",
79              String.valueOf(sessData == null ? getRowsPerPage() : sessData.getRowsPerPage()))
80          .addObject("historySize",
81              String.valueOf(sessData == null ? getHistorySize() : sessData.getHistorySize()))
82          .addObject("backURL", backUrl).addObject("collectionPeriod", getCollectionPeriod());
83    }
84  
85    /**
86     * Gets the collection period.
87     *
88     * @return the collection period
89     */
90    public long getCollectionPeriod() {
91      return collectionPeriod;
92    }
93  
94    /**
95     * Sets the collection period.
96     *
97     * @param collectionPeriod the new collection period
98     */
99    public void setCollectionPeriod(long collectionPeriod) {
100     this.collectionPeriod = collectionPeriod;
101   }
102 
103   /**
104    * Sets the collection period using expression.
105    *
106    * @param collectionPeriod the new collection period using expression
107    */
108   @Value("${psiprobe.beans.stats.collectors.connector.period}")
109   public void setCollectionPeriod(String collectionPeriod) {
110     this.collectionPeriod = TimeExpression.inSeconds(collectionPeriod);
111   }
112 
113   /**
114    * Gets the max rows.
115    *
116    * @return the max rows
117    */
118   public int getMaxRows() {
119     return maxRows;
120   }
121 
122   /**
123    * Sets the max rows.
124    *
125    * @param maxRows the new max rows
126    */
127   @Value("1000")
128   public void setMaxRows(int maxRows) {
129     this.maxRows = maxRows;
130   }
131 
132   /**
133    * Gets the rows per page.
134    *
135    * @return the rows per page
136    */
137   public int getRowsPerPage() {
138     return rowsPerPage;
139   }
140 
141   /**
142    * Sets the rows per page.
143    *
144    * @param rowsPerPage the new rows per page
145    */
146   @Value("50")
147   public void setRowsPerPage(int rowsPerPage) {
148     this.rowsPerPage = rowsPerPage;
149   }
150 
151   /**
152    * Gets the history size.
153    *
154    * @return the history size
155    */
156   public int getHistorySize() {
157     return historySize;
158   }
159 
160   /**
161    * Sets the history size.
162    *
163    * @param historySize the new history size
164    */
165   @Value("30")
166   public void setHistorySize(int historySize) {
167     this.historySize = historySize;
168   }
169 
170   /**
171    * Gets the replace pattern.
172    *
173    * @return the replace pattern
174    */
175   public String getReplacePattern() {
176     return replacePattern;
177   }
178 
179   /**
180    * Sets the replace pattern.
181    *
182    * @param replacePattern the new replace pattern
183    */
184   @Value("^http(s)?://[a-zA-Z\\-\\.0-9]+(:[0-9]+)?")
185   public void setReplacePattern(String replacePattern) {
186     this.replacePattern = replacePattern;
187   }
188 
189   @Value("datasourcetest")
190   @Override
191   public void setViewName(String viewName) {
192     super.setViewName(viewName);
193   }
194 
195 }