Search in sources :

Example 6 with ServletDef

use of org.apache.tomcat.util.descriptor.web.ServletDef in project ofbiz-framework by apache.

the class WebAppUtil method getControlServletPath.

/**
 * Returns the control servlet path. The path consists of the web application's mount-point
 * specified in the <code>ofbiz-component.xml</code> file and the servlet mapping specified
 * in the web application's <code>web.xml</code> file.
 *
 * @param webAppInfo
 * @throws IOException
 * @throws SAXException
 */
public static String getControlServletPath(WebappInfo webAppInfo) throws IOException, SAXException {
    Assert.notNull("webAppInfo", webAppInfo);
    String servletMapping = null;
    WebXml webXml = getWebXml(webAppInfo);
    for (ServletDef servletDef : webXml.getServlets().values()) {
        if ("org.apache.ofbiz.webapp.control.ControlServlet".equals(servletDef.getServletClass()) || "org.apache.ofbiz.product.category.SeoControlServlet".equals(servletDef.getServletClass())) {
            String servletName = servletDef.getServletName();
            // Catalina servlet mappings: key = url-pattern, value = servlet-name.
            for (Entry<String, String> entry : webXml.getServletMappings().entrySet()) {
                if (servletName.equals(entry.getValue())) {
                    servletMapping = entry.getKey();
                    break;
                }
            }
            break;
        }
    }
    if (servletMapping == null) {
        throw new IllegalArgumentException("org.apache.ofbiz.webapp.control.ControlServlet mapping not found in " + webAppInfo.getLocation() + webAppFileName);
    }
    servletMapping = servletMapping.replace("*", "");
    String servletPath = webAppInfo.contextRoot.concat(servletMapping);
    return servletPath;
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef)

Example 7 with ServletDef

use of org.apache.tomcat.util.descriptor.web.ServletDef in project tomcat by apache.

the class TestContextConfigAnnotation method testSetupWebXMLNoMapping.

@Test
public void testSetupWebXMLNoMapping() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ServletDef servletDef = new ServletDef();
    servletDef.setServletName("param1");
    servletDef.setServletClass("org.apache.catalina.startup.NoMappingParamServlet");
    servletDef.addInitParameter("foo", "tomcat");
    webxml.addServlet(servletDef);
    webxml.addServletMapping("/param", "param1");
    ContextConfig config = new ContextConfig();
    File pFile = paramClassResource("org/apache/catalina/startup/NoMappingParamServlet");
    Assert.assertTrue(pFile.exists());
    config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
    Assert.assertEquals("tomcat", servletDef.getParameterMap().get("foo"));
    Assert.assertEquals("World!", servletDef.getParameterMap().get("bar"));
    ServletDef servletDef1 = webxml.getServlets().get("param1");
    Assert.assertNotNull(servletDef1);
    Assert.assertEquals(servletDef, servletDef1);
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) HashMap(java.util.HashMap) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 8 with ServletDef

use of org.apache.tomcat.util.descriptor.web.ServletDef in project tomcat by apache.

the class TestContextConfigAnnotation method testOverwriteAnnotation.

@Test
public void testOverwriteAnnotation() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ServletDef servletDef = new ServletDef();
    servletDef.setServletName("param");
    servletDef.setServletClass("org.apache.catalina.startup.ParamServlet");
    servletDef.addInitParameter("foo", "tomcat");
    servletDef.setDescription("Description");
    servletDef.setDisplayName("DisplayName");
    servletDef.setLargeIcon("LargeIcon");
    servletDef.setSmallIcon("SmallIcon");
    servletDef.setAsyncSupported("true");
    servletDef.setLoadOnStartup("1");
    webxml.addServlet(servletDef);
    webxml.addServletMapping("/param", "param");
    ContextConfig config = new ContextConfig();
    File pFile = paramClassResource("org/apache/catalina/startup/ParamServlet");
    Assert.assertTrue(pFile.exists());
    config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
    Assert.assertEquals(servletDef, webxml.getServlets().get("param"));
    Assert.assertEquals("tomcat", servletDef.getParameterMap().get("foo"));
    Assert.assertEquals("param", webxml.getServletMappings().get("/param"));
    // annotation mapping not added s. Servlet Spec 3.0 (Nov 2009)
    // 8.2.3.3.iv page 81
    Assert.assertNull(webxml.getServletMappings().get("/annotation/overwrite"));
    Assert.assertEquals("Description", servletDef.getDescription());
    Assert.assertEquals("DisplayName", servletDef.getDisplayName());
    Assert.assertEquals("LargeIcon", servletDef.getLargeIcon());
    Assert.assertEquals("SmallIcon", servletDef.getSmallIcon());
    Assert.assertEquals(Boolean.TRUE, servletDef.getAsyncSupported());
    Assert.assertEquals(Integer.valueOf(1), servletDef.getLoadOnStartup());
    Assert.assertNull(servletDef.getEnabled());
    Assert.assertNull(servletDef.getJspFile());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) HashMap(java.util.HashMap) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 9 with ServletDef

use of org.apache.tomcat.util.descriptor.web.ServletDef in project tomcat by apache.

the class TestContextConfigAnnotation method testNoMapping.

@Test
public void testNoMapping() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ContextConfig config = new ContextConfig();
    File pFile = paramClassResource("org/apache/catalina/startup/NoMappingParamServlet");
    Assert.assertTrue(pFile.exists());
    config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
    ServletDef servletDef = webxml.getServlets().get("param1");
    Assert.assertNull(servletDef);
    webxml.addServletMapping("/param", "param1");
    config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
    servletDef = webxml.getServlets().get("param1");
    Assert.assertNull(servletDef);
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) HashMap(java.util.HashMap) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 10 with ServletDef

use of org.apache.tomcat.util.descriptor.web.ServletDef in project tomcat by apache.

the class ContextConfig method processAnnotationWebServlet.

protected void processAnnotationWebServlet(String className, AnnotationEntry ae, WebXml fragment) {
    String servletName = null;
    // must search for name s. Spec Servlet API 3.0 - 8.2.3.3.n.ii page 81
    List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("name".equals(name)) {
            servletName = evp.getValue().stringifyValue();
            break;
        }
    }
    if (servletName == null) {
        // classname is default servletName as annotation has no name!
        servletName = className;
    }
    ServletDef servletDef = fragment.getServlets().get(servletName);
    boolean isWebXMLservletDef;
    if (servletDef == null) {
        servletDef = new ServletDef();
        servletDef.setServletName(servletName);
        servletDef.setServletClass(className);
        isWebXMLservletDef = false;
    } else {
        isWebXMLservletDef = true;
    }
    boolean urlPatternsSet = false;
    String[] urlPatterns = null;
    // List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("value".equals(name) || "urlPatterns".equals(name)) {
            if (urlPatternsSet) {
                throw new IllegalArgumentException(sm.getString("contextConfig.urlPatternValue", "WebServlet", className));
            }
            urlPatternsSet = true;
            urlPatterns = processAnnotationsStringArray(evp.getValue());
        } else if ("description".equals(name)) {
            if (servletDef.getDescription() == null) {
                servletDef.setDescription(evp.getValue().stringifyValue());
            }
        } else if ("displayName".equals(name)) {
            if (servletDef.getDisplayName() == null) {
                servletDef.setDisplayName(evp.getValue().stringifyValue());
            }
        } else if ("largeIcon".equals(name)) {
            if (servletDef.getLargeIcon() == null) {
                servletDef.setLargeIcon(evp.getValue().stringifyValue());
            }
        } else if ("smallIcon".equals(name)) {
            if (servletDef.getSmallIcon() == null) {
                servletDef.setSmallIcon(evp.getValue().stringifyValue());
            }
        } else if ("asyncSupported".equals(name)) {
            if (servletDef.getAsyncSupported() == null) {
                servletDef.setAsyncSupported(evp.getValue().stringifyValue());
            }
        } else if ("loadOnStartup".equals(name)) {
            if (servletDef.getLoadOnStartup() == null) {
                servletDef.setLoadOnStartup(evp.getValue().stringifyValue());
            }
        } else if ("initParams".equals(name)) {
            Map<String, String> initParams = processAnnotationWebInitParams(evp.getValue());
            if (isWebXMLservletDef) {
                Map<String, String> webXMLInitParams = servletDef.getParameterMap();
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    if (webXMLInitParams.get(entry.getKey()) == null) {
                        servletDef.addInitParameter(entry.getKey(), entry.getValue());
                    }
                }
            } else {
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    servletDef.addInitParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    if (!isWebXMLservletDef && urlPatterns != null) {
        fragment.addServlet(servletDef);
    }
    if (urlPatterns != null) {
        if (!fragment.getServletMappings().containsValue(servletName)) {
            for (String urlPattern : urlPatterns) {
                fragment.addServletMapping(urlPattern, servletName);
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) ElementValuePair(org.apache.tomcat.util.bcel.classfile.ElementValuePair) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ServletDef (org.apache.tomcat.util.descriptor.web.ServletDef)10 HashMap (java.util.HashMap)7 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)7 File (java.io.File)5 JavaClassCacheEntry (org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry)5 Test (org.junit.Test)5 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Wrapper (org.apache.catalina.Wrapper)2 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)2 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)1 SessionCookieConfig (jakarta.servlet.SessionCookieConfig)1 Method (java.lang.reflect.Method)1 Entry (java.util.Map.Entry)1 StandardWrapper (org.apache.catalina.core.StandardWrapper)1 AnnotationEntry (org.apache.tomcat.util.bcel.classfile.AnnotationEntry)1 ElementValuePair (org.apache.tomcat.util.bcel.classfile.ElementValuePair)1 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)1 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)1