All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jstuff.integration.servlet.ClassPathResourcesServletContext Maven / Gradle / Ivy

/*
 * Copyright 2010-2022 by Sebastian Thomschke and contributors.
 * SPDX-License-Identifier: EPL-2.0
 */
package net.sf.jstuff.integration.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.ServletContext;

import org.eclipse.jdt.annotation.Nullable;

import net.sf.jstuff.core.logging.Logger;

/**
 * @author Sebastian Thomschke
 */
public class ClassPathResourcesServletContext extends ServletContextWrapper {
   private static final Logger LOG = Logger.create();

   public ClassPathResourcesServletContext(final ServletContext delegate) {
      super(delegate);
   }

   @Override
   public @Nullable ServletContext getContext(final String uripath) {
      final var ctx = delegate.getContext(uripath);
      if (ctx instanceof ClassPathResourcesServletContext)
         return ctx;
      if (ctx == delegate)
         return this;
      if (ctx == null)
         return null;
      return new ClassPathResourcesServletContext(ctx);
   }

   @Override
   public @Nullable URL getResource(final String path) throws MalformedURLException {
      URL resource = delegate.getResource(path);
      if (resource == null) {
         resource = ClassPathResourcesFilter.findResourceInClassPath(path);
      }
      return resource;
   }

   @SuppressWarnings("resource")
   @Override
   public @Nullable InputStream getResourceAsStream(final String path) {
      InputStream stream = delegate.getResourceAsStream(path);
      if (stream == null) {
         final URL resource = ClassPathResourcesFilter.findResourceInClassPath(path);
         if (resource != null) {
            try {
               stream = resource.openStream();
            } catch (final IOException ex) {
               LOG.error(ex, "Failed to open stream of resource [%s]", path);
            }
         }
      }
      return stream;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy