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;
12  
13  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpServletResponse;
15  
16  import org.apache.catalina.Context;
17  import org.springframework.web.bind.ServletRequestUtils;
18  import org.springframework.web.servlet.ModelAndView;
19  
20  /**
21   * Base class for all controllers requiring "webapp" request parameter.
22   */
23  public abstract class AbstractContextHandlerController extends AbstractTomcatContainerController {
24  
25    @Override
26    protected ModelAndView handleRequestInternal(HttpServletRequest request,
27        HttpServletResponse response) throws Exception {
28  
29      String contextName = ServletRequestUtils.getStringParameter(request, "webapp");
30      Context context = null;
31      if (contextName != null) {
32        contextName = getContainerWrapper().getTomcatContainer().formatContextName(contextName);
33        context = getContainerWrapper().getTomcatContainer().findContext(contextName);
34      }
35  
36      if (context != null || isContextOptional()) {
37        return handleContext(contextName, context, request, response);
38      }
39      if (contextName != null) {
40        request.setAttribute("errorMessage", getMessageSourceAccessor()
41            .getMessage("probe.src.contextDoesntExist", new Object[] {contextName}));
42      }
43  
44      return new ModelAndView("errors/paramerror");
45    }
46  
47    /**
48     * Checks if is context optional.
49     *
50     * @return true, if is context optional
51     */
52    protected boolean isContextOptional() {
53      return false;
54    }
55  
56    /**
57     * Handle context.
58     *
59     * @param contextName the context name
60     * @param context the context
61     * @param request the request
62     * @param response the response
63     *
64     * @return the model and view
65     *
66     * @throws Exception the exception
67     */
68    protected abstract ModelAndView handleContext(String contextName, Context context,
69        HttpServletRequest request, HttpServletResponse response) throws Exception;
70  }