예제 #1
0
  String getContent(String identifier) throws Exception {
    logger.trace("Searching for '{}' in cache.", identifier);
    ResourceWrapper wrapper = resources.get(identifier);

    // If not in cache yet, load it
    if (wrapper == null) {
      long start = System.currentTimeMillis();
      logger.debug("'{}' not in cache. Loading and processing...", identifier);
      String extension = FilenameUtils.getExtension(identifier);

      Type type = TypeFactory.getType(extension);
      if (type == null) {
        logger.warn("No type available for the specified resource: {}", extension);
        throw new UnknownTypeException(
            "No type available for the specified resource: " + extension);
      }

      String content = processContent(loader.loadResource(identifier), type);
      wrapper = new ResourceWrapper(identifier, type, content);
      resources.put(identifier, wrapper);
      logger.debug("Resource loaded and processed in {} ms.", System.currentTimeMillis() - start);
    } else {
      // If in cache, check last modified
      if (wrapper.getLastLoaded() < loader.lastModified(identifier)) {
        // If last loaded was before last modified, load it again
        wrapper.setContent(loader.loadResource(identifier));
      }
    }

    return wrapper.getContent();
  }
예제 #2
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String[] files = request.getParameterValues("file");
    if (logger.isDebugEnabled()) {
      logger.debug("Resource servlet received request to load files: {}", Arrays.toString(files));
    }

    if (files == null || files.length == 0) {
      logger.warn("No resource specified, 'file' parameter is empty.");
      response.sendError(
          HttpServletResponse.SC_BAD_REQUEST, "No resource specified, 'file' parameter is empty.");
      return;
    }

    try {
      String requestedExtension = FilenameUtils.getExtension(request.getRequestURI());
      logger.trace("Request done with extension {}", requestedExtension);

      String answer = null;
      if ("json".equals(requestedExtension)) {
        /* If extension was .json, then it will return all resources as
         * a JSON object.
         */
        response.setContentType(CONTENT_TYPE_JSON);
        answer = renderContentAsJson(files);
      } else {
        // Any other extension will set content type for the type of the first file requested
        response.setContentType(
            TypeFactory.getType(FilenameUtils.getExtension(files[0])).getContentType());
        answer = renderContentAsText(files);
      }

      logger.trace("--- Sending response to client ---\n{}", answer);
      response.getWriter().write(answer);
    } catch (ResourceNotFoundException ex) {
      response.sendError(
          HttpServletResponse.SC_NOT_FOUND, "Resource not found: " + ex.getMessage());
      logger.warn("Resource not found.", ex);
    } catch (Exception ex) {
      response.sendError(
          HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
          "Error while loading resource: " + ex.getMessage());
      logger.error("Error while loading resoruce.", ex);
    }
  }