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.deploy;
12  
13  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpServletResponse;
15  
16  import org.springframework.beans.factory.annotation.Value;
17  import org.springframework.security.core.Authentication;
18  import org.springframework.security.core.context.SecurityContextHolder;
19  import org.springframework.stereotype.Controller;
20  import org.springframework.web.bind.ServletRequestUtils;
21  import org.springframework.web.bind.annotation.RequestMapping;
22  import org.springframework.web.servlet.ModelAndView;
23  import org.springframework.web.servlet.view.InternalResourceView;
24  
25  import psiprobe.controllers.AbstractTomcatContainerController;
26  
27  /**
28   * Forces Tomcat to install a pre-configured context name.
29   */
30  @Controller
31  public class DeployContextController extends AbstractTomcatContainerController {
32  
33    @RequestMapping(path = "/adm/deploycontext.htm")
34    @Override
35    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
36        throws Exception {
37      return super.handleRequest(request, response);
38    }
39  
40    @Override
41    public ModelAndView handleRequestInternal(HttpServletRequest request,
42        HttpServletResponse response) throws Exception {
43  
44      String contextName = ServletRequestUtils.getStringParameter(request, "context");
45  
46      if (contextName != null) {
47        try {
48          if (getContainerWrapper().getTomcatContainer().installContext(contextName)) {
49            request.setAttribute("successMessage", getMessageSourceAccessor()
50                .getMessage("probe.src.deploy.context.success", new Object[] {contextName}));
51            // Logging action
52            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
53            // get username logger
54            String name = auth.getName();
55            logger.info(getMessageSourceAccessor().getMessage("probe.src.log.deploycontext"), name,
56                contextName);
57          } else {
58            request.setAttribute("errorMessage", getMessageSourceAccessor()
59                .getMessage("probe.src.deploy.context.failure", new Object[] {contextName}));
60          }
61        } catch (Exception e) {
62          request.setAttribute("errorMessage", e.getMessage());
63          logger.trace("", e);
64        }
65      }
66  
67      return new ModelAndView(new InternalResourceView(getViewName()));
68    }
69  
70    @Value("/adm/deploy.htm")
71    @Override
72    public void setViewName(String viewName) {
73      super.setViewName(viewName);
74    }
75  
76  }