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.apps;
12  
13  import jakarta.inject.Inject;
14  import jakarta.servlet.http.HttpServletRequest;
15  import jakarta.servlet.http.HttpServletResponse;
16  
17  import org.apache.catalina.Context;
18  import org.springframework.web.bind.ServletRequestUtils;
19  import org.springframework.web.servlet.ModelAndView;
20  
21  import psiprobe.beans.ResourceResolver;
22  import psiprobe.controllers.AbstractContextHandlerController;
23  import psiprobe.model.Application;
24  import psiprobe.model.stats.StatsCollection;
25  import psiprobe.tools.ApplicationUtils;
26  import psiprobe.tools.SecurityUtils;
27  
28  /**
29   * Retrieves Application model object populated with application information.
30   */
31  public class BaseGetApplicationController extends AbstractContextHandlerController {
32  
33    /** denotes whether extended application information and statistics should be collected. */
34    private boolean extendedInfo;
35  
36    /** The stats collection. */
37    @Inject
38    protected StatsCollection statsCollection;
39  
40    /** The collection period. */
41    private long collectionPeriod;
42  
43    /**
44     * Checks if is extended info.
45     *
46     * @return true, if is extended info
47     */
48    public boolean isExtendedInfo() {
49      return extendedInfo;
50    }
51  
52    /**
53     * Sets the extended info.
54     *
55     * @param extendedInfo the new extended info
56     */
57    public void setExtendedInfo(boolean extendedInfo) {
58      this.extendedInfo = extendedInfo;
59    }
60  
61    /**
62     * Gets the collection period.
63     *
64     * @return the collection period
65     */
66    public long getCollectionPeriod() {
67      return collectionPeriod;
68    }
69  
70    /**
71     * Sets the collection period.
72     *
73     * @param collectionPeriod the new collection period
74     */
75    public void setCollectionPeriod(long collectionPeriod) {
76      this.collectionPeriod = collectionPeriod;
77    }
78  
79    @Override
80    protected ModelAndView handleContext(String contextName, Context context,
81        HttpServletRequest request, HttpServletResponse response) throws Exception {
82  
83      boolean calcSize = ServletRequestUtils.getBooleanParameter(request, "size", false)
84          && SecurityUtils.hasAttributeValueRole(getServletContext());
85  
86      ResourceResolver resourceResolver = containerWrapper.getResourceResolver();
87      Application app = ApplicationUtils.getApplication(context,
88          isExtendedInfo() ? resourceResolver : null, calcSize, containerWrapper);
89  
90      if (isExtendedInfo() && statsCollection != null) {
91        String avgStatisticName = "app.avg_proc_time." + app.getName();
92        app.setAvgTime(statsCollection.getLastValueForStat(avgStatisticName));
93      }
94  
95      return new ModelAndView(getViewName()).addObject("app", app)
96          .addObject("no_resources", !resourceResolver.supportsPrivateResources())
97          .addObject("collectionPeriod", getCollectionPeriod());
98    }
99  
100 }