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.logs;
12  
13  import jakarta.inject.Inject;
14  import jakarta.servlet.http.HttpServletRequest;
15  import jakarta.servlet.http.HttpServletResponse;
16  
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  import org.springframework.web.bind.ServletRequestUtils;
20  import org.springframework.web.servlet.ModelAndView;
21  import org.springframework.web.servlet.mvc.ParameterizableViewController;
22  
23  import psiprobe.beans.LogResolverBean;
24  import psiprobe.tools.logging.LogDestination;
25  
26  /**
27   * The Class AbstractLogHandlerController.
28   */
29  public abstract class AbstractLogHandlerController extends ParameterizableViewController {
30  
31    /** The Constant logger. */
32    private static final Logger logger = LoggerFactory.getLogger(AbstractLogHandlerController.class);
33  
34    /** The log resolver. */
35    @Inject
36    private LogResolverBean logResolver;
37  
38    /**
39     * Gets the log resolver.
40     *
41     * @return the log resolver
42     */
43    public LogResolverBean getLogResolver() {
44      return logResolver;
45    }
46  
47    /**
48     * Sets the log resolver.
49     *
50     * @param logResolver the new log resolver
51     */
52    public void setLogResolver(LogResolverBean logResolver) {
53      this.logResolver = logResolver;
54    }
55  
56    @Override
57    protected ModelAndView handleRequestInternal(HttpServletRequest request,
58        HttpServletResponse response) throws Exception {
59  
60      String logType = ServletRequestUtils.getStringParameter(request, "logType");
61      String webapp = ServletRequestUtils.getStringParameter(request, "webapp");
62      boolean context = ServletRequestUtils.getBooleanParameter(request, "context", false);
63      boolean root = ServletRequestUtils.getBooleanParameter(request, "root", false);
64      String logName = ServletRequestUtils.getStringParameter(request, "logName");
65      String logIndex = ServletRequestUtils.getStringParameter(request, "logIndex");
66  
67      LogDestination dest =
68          logResolver.getLogDestination(logType, webapp, context, root, logName, logIndex);
69  
70      ModelAndView modelAndView = null;
71      boolean logFound = false;
72      if (dest != null) {
73        if (dest.getFile() != null && dest.getFile().exists()) {
74          modelAndView = handleLogFile(request, response, dest);
75          logFound = true;
76        } else {
77          logger.error("{}: file not found", dest.getFile());
78        }
79      } else {
80        logger.error("{}{} log{} not found", logType, root ? " root" : "",
81            root ? "" : " '" + logName + "'");
82      }
83      if (!logFound) {
84        response.sendError(404);
85      }
86      return modelAndView;
87    }
88  
89    /**
90     * Handle log file.
91     *
92     * @param request the request
93     * @param response the response
94     * @param logDest the log dest
95     *
96     * @return the model and view
97     *
98     * @throws Exception the exception
99     */
100   protected abstract ModelAndView handleLogFile(HttpServletRequest request,
101       HttpServletResponse response, LogDestination logDest) throws Exception;
102 
103 }