private InputStream dispatcherBasedStreamLocator(final String uri) throws IOException {
   final HttpServletRequest request = context.getRequest();
   final HttpServletResponse response = context.getResponse();
   // The order of stream retrieval is important. We are trying to get the dispatcherStreamLocator
   // in order to handle
   // jsp resources (if such exist). Switching the order would cause jsp to not be interpreted by
   // the container.
   return getDispatcherStreamLocator().getInputStream(request, response, uri);
 }
  /** {@inheritDoc} */
  public InputStream locate(final String uri) throws IOException {
    Validate.notNull(uri, "URI cannot be NULL!");
    LOG.debug("locate resource: {}", uri);

    try {
      if (getWildcardStreamLocator().hasWildcard(uri)) {
        final ServletContext servletContext = context.getServletContext();
        final String fullPath = FilenameUtils.getFullPath(uri);
        final String realPath = servletContext.getRealPath(fullPath);
        if (realPath == null) {
          final String message = "[FAIL] determine realPath for resource: " + uri;
          LOG.debug(message);
          throw new IOException(message);
        }
        return getWildcardStreamLocator().locateStream(uri, new File(realPath));
      }
    } catch (final IOException e) {
      /**
       * This is a special case when no more attempts are required, since the required computation
       * was achieved successfully. This solves the following <a
       * href="http://code.google.com/p/wro4j/issues/detail?id=321">issue</a>.
       *
       * <p>The problem was that in some situations, when the dispatcherStreamLocator was used to
       * locate resources containing wildcard, the following message was printed to the console:
       * <code>SEVERE: Servlet.service() for servlet default threw exception
       * java.io.FileNotFoundException.</code>
       */
      if (e instanceof NoMoreAttemptsIOException) {
        throw e;
      }
      LOG.warn(
          "[FAIL] localize the stream containing wildcard. Original error message: '{}'",
          e.getMessage() + "\".\n Trying to locate the stream without the wildcard.");
    }

    InputStream inputStream = null;
    try {
      if (locatorStrategy.equals(LocatorStrategy.DISPATCHER_FIRST)) {
        inputStream = dispatcherFirstStreamLocator(uri);
      } else {
        inputStream = servletContextFirstStreamLocator(uri);
      }
      validateInputStreamIsNotNull(inputStream, uri);
      return inputStream;
    } catch (final IOException e) {
      LOG.debug("Wrong or empty resource with location: {}", uri);
      throw e;
    }
  }
Exemple #3
0
 /**
  * Will try an asynchronous check if the async configuration is enabled. If async check is not
  * configured, a synchronous check will be performed. The async check assumes that the {@link
  * ResourceWatcherRequestHandler} is enabled.
  *
  * <p>If the async check is not allowed (the request was not passed through {@link WroFilter}) -
  * no check will be performed. This is important for use-cases when wro resource is included using
  * a taglib which performs a wro api call directly, without being invoked through {@link
  * WroFilter}.
  *
  * @return true if the actual check invocation was performed. This is important to decide if the
  *     resource change should be skipped or not.
  */
 public boolean tryAsyncCheck(final CacheKey cacheKey) {
   boolean checkInvoked = false;
   if (context.getConfig().isResourceWatcherAsync()) {
     if (isAsyncCheckAllowed()) {
       LOG.debug("Checking resourceWatcher asynchronously...");
       final Callable<Void> callable = createAsyncCheckCallable(cacheKey);
       submit(callable);
       checkInvoked = true;
     }
   } else {
     LOG.debug("Async check not allowed. Falling back to sync check.");
     check(cacheKey);
     checkInvoked = true;
   }
   return checkInvoked;
 }
Exemple #4
0
 private boolean isGroupChanged(final Group group, final Callback callback) {
   final List<Resource> resources = group.getResources();
   final AtomicBoolean isChanged = new AtomicBoolean(false);
   final List<Future<?>> futures = new ArrayList<Future<?>>();
   final boolean isAsync = context.getConfig().isResourceWatcherAsync();
   try {
     for (final Resource resource : resources) {
       if (isAsync) {
         futures.add(
             executorServiceRef
                 .get()
                 .submit(
                     ContextPropagatingCallable.decorate(
                         new Callable<Void>() {
                           public Void call() throws Exception {
                             checkResourceChange(resource, group, callback, isChanged);
                             return null;
                           }
                         })));
       } else {
         checkResourceChange(resource, group, callback, isChanged);
       }
     }
     if (isAsync) {
       LOG.debug("await async execution");
       // await for all futures to complete before returning the result
       for (final Future<?> future : futures) {
         future.get();
       }
     }
   } catch (final Exception e) {
     LOG.debug("Exception while onResourceChange is invoked", e);
   }
   LOG.debug("group={}, changed={}", group.getName(), isChanged);
   return isChanged.get();
 }
 private InputStream servletContextBasedStreamLocator(final String uri) throws IOException {
   final ServletContext servletContext = context.getServletContext();
   return servletContext.getResourceAsStream(uri);
 }