View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.tomcat.webresources;
17  
18  import java.util.jar.Attributes;
19  import java.util.jar.JarFile;
20  import java.util.jar.Manifest;
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  import org.apache.catalina.Context;
25  import org.apache.catalina.LifecycleException;
26  import org.apache.catalina.WebResource;
27  import org.apache.catalina.webresources.StandardRoot;
28  
29  public class FessWebResourceRoot extends StandardRoot {
30      private static final Logger logger = Logger.getLogger(FessWebResourceRoot.class.getName());
31  
32      public FessWebResourceRoot(final Context context) {
33          super(context);
34      }
35  
36      @Override
37      protected void processWebInfLib() throws LifecycleException {
38          super.processWebInfLib();
39  
40          final WebResource[] possibleJars = listResources("/WEB-INF/plugin", false);
41  
42          for (final WebResource possibleJar : possibleJars) {
43              if (possibleJar.isFile() && possibleJar.getName().endsWith(".jar")) {
44                  try (final JarFile jarFile = new JarFile(possibleJar.getCanonicalPath())) {
45                      final Manifest manifest = jarFile.getManifest();
46                      if (manifest != null && manifest.getEntries() != null) {
47                          final Attributes attributes = manifest.getMainAttributes();
48                          if (attributes != null
49                                  && (attributes.get("Fess-WebAppJar") != null || attributes.getValue("Fess-WebAppJar") != null)) {
50                              createWebResourceSet(ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", possibleJar.getURL(), "/");
51                          }
52                      }
53                  } catch (final Exception e) {
54                      logger.log(Level.WARNING, "Failed to read " + possibleJar, e);
55                  }
56              }
57          }
58      }
59  }