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 jakarta.inject.Inject;
14  import jakarta.servlet.http.HttpServletRequest;
15  
16  import java.util.Locale;
17  
18  import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.springframework.web.servlet.mvc.AbstractController;
22  
23  import psiprobe.beans.ContainerWrapperBean;
24  
25  /**
26   * Base class for controllers requiring access to ContainerWrapperBean.
27   */
28  public abstract class AbstractTomcatContainerController extends AbstractController {
29  
30    /** The logger. */
31    // We are hiding this as we use better logger and don't care to use springs jcl variation
32    @SuppressWarnings("HidingField")
33    protected final Logger logger = LoggerFactory.getLogger(getClass());
34  
35    /** The container wrapper. */
36    @Inject
37    private ContainerWrapperBean containerWrapper;
38  
39    /** The view name. */
40    private String viewName;
41  
42    /** Part of HTTP content type header. */
43    private static final String MULTIPART = "multipart/";
44  
45    /** Constant for HTTP POST method. */
46    private static final String POST_METHOD = "POST";
47  
48    /**
49     * Gets the container wrapper.
50     *
51     * @return the container wrapper
52     */
53    public ContainerWrapperBean getContainerWrapper() {
54      return containerWrapper;
55    }
56  
57    /**
58     * Sets the container wrapper.
59     *
60     * @param containerWrapper the new container wrapper
61     */
62    public void setContainerWrapper(ContainerWrapperBean containerWrapper) {
63      this.containerWrapper = containerWrapper;
64    }
65  
66    /**
67     * Gets the view name.
68     *
69     * @return the view name
70     */
71    public String getViewName() {
72      return viewName;
73    }
74  
75    /**
76     * Sets the view name.
77     *
78     * @param viewName the new view name
79     */
80    public void setViewName(String viewName) {
81      this.viewName = viewName;
82    }
83  
84    /**
85     * Utility method that determines whether the request contains multipart content. Borrowed and
86     * modified from tomcat as they removed it in 9.0.88 and 10.1.x lines.
87     *
88     * @param request The request context to be evaluated. Must be non-null.
89     *
90     * @return {@code true} if the request is multipart; {@code false} otherwise.
91     */
92    public boolean isMultipartContent(final HttpServletRequest request) {
93      if (!POST_METHOD.equalsIgnoreCase(request.getMethod())) {
94        return false;
95      }
96      final String contentType = new ServletRequestContext(request).getContentType();
97      if (contentType == null) {
98        return false;
99      }
100     return contentType.toLowerCase(Locale.ENGLISH).startsWith(MULTIPART);
101   }
102 
103 }