<commit_before>/*
 * Copyright (c) 2008. All rights reserved.
 */
package ro.isdc.wro.manager;

import java.beans.PropertyChangeListener;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ro.isdc.wro.WroRuntimeException;
import ro.isdc.wro.cache.CacheEntry;
import ro.isdc.wro.cache.CacheStrategy;
import ro.isdc.wro.cache.ContentHashEntry;
import ro.isdc.wro.config.Context;
import ro.isdc.wro.config.WroConfigurationChangeListener;
import ro.isdc.wro.http.HttpHeader;
import ro.isdc.wro.http.UnauthorizedRequestException;
import ro.isdc.wro.model.WroModel;
import ro.isdc.wro.model.factory.WroModelFactory;
import ro.isdc.wro.model.group.Group;
import ro.isdc.wro.model.group.GroupExtractor;
import ro.isdc.wro.model.group.processor.GroupsProcessor;
import ro.isdc.wro.model.resource.ResourceType;
import ro.isdc.wro.model.resource.locator.UriLocator;
import ro.isdc.wro.model.resource.processor.impl.css.CssUrlRewritingProcessor;
import ro.isdc.wro.util.StopWatch;
import ro.isdc.wro.util.WroUtil;


/**
 * Contains all the factories used by optimizer in order to perform the logic.
 *
 * @author <NAME>
 * @created Created on Oct 30, 2008
 */
public class WroManager
    implements WroConfigurationChangeListener, CacheChangeCallbackAware {
  /**
   * Logger for this class.
   */
  private static final Logger LOG = LoggerFactory.getLogger(WroManager.class);
  /**
   * wro API mapping path. If request uri contains this, exposed API method will be invoked.
   */
  public static final String PATH_API = "wroAPI";
  /**
   * API - reload cache method call
   */
  public static final String API_RELOAD_CACHE = "reloadCache";
  /**
   * API - reload model method call
   */
  public static final String API_RELOAD_MODEL = "reloadModel";
  /**
   * ResourcesModel factory.
   */
  private WroModelFactory modelFactory;

  /**
   * GroupExtractor.
   */
  private GroupExtractor groupExtractor;

  /**
   * Groups processor.
   */
  private GroupsProcessor groupsProcessor;

  /**
   * A cacheStrategy used for caching processed results. <GroupName, processed result>.
   */
  private CacheStrategy<CacheEntry, ContentHashEntry> cacheStrategy;
  /**
   * A callback to be notified about the cache change.
   */
  private PropertyChangeListener cacheChangeCallback;
  /**
   * Scheduled executors service, used to update the output result.
   */
  private ScheduledExecutorService scheduler;


  /**
   * Perform processing of the uri.
   *
   * @param request
   *          {@link HttpServletRequest} to process.
   * @param response HttpServletResponse where to write the result content.
   * @throws IOException when any IO related problem occurs.
   */
  public final void process(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    //TODO return true if the request was processed in order to continue filter chain processing
    LOG.debug("processing: " + request.getRequestURI());
    validate();
    InputStream is = null;
    // create model
    final WroModel model = modelFactory.getInstance();
    //TODO move API related checks into separate class and determine filter mapping for better mapping
    if (isApiRequest(request)) {
      if (request.getRequestURI().contains(API_RELOAD_CACHE)) {
        Context.getConfig().reloadCache();
        response.setContentType("application/json");
        response.getWriter().write("{response: 'OK'}");
        response.getWriter().flush();
        response.getWriter().close();
        return;
      }
      if (request.getRequestURI().contains(API_RELOAD_MODEL)) {
        Context.getConfig().reloadModel();
        response.setContentType("application/json");
        response.getWriter().write("{response: 'OK'}");
        response.getWriter().flush();
        response.getWriter().close();
        return;
      }
    } else if (isProxyResourceRequest(request)) {
      is = locateInputeStream(request);<commit_msg>add comment<commit_after>/*
 * Copyright (c) 2008. All rights reserved.
 */
package ro.isdc.wro.manager;

import java.beans.PropertyChangeListener;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ro.isdc.wro.WroRuntimeException;
import ro.isdc.wro.cache.CacheEntry;
import ro.isdc.wro.cache.CacheStrategy;
import ro.isdc.wro.cache.ContentHashEntry;
import ro.isdc.wro.config.Context;
import ro.isdc.wro.config.WroConfigurationChangeListener;
import ro.isdc.wro.http.HttpHeader;
import ro.isdc.wro.http.UnauthorizedRequestException;
import ro.isdc.wro.model.WroModel;
import ro.isdc.wro.model.factory.WroModelFactory;
import ro.isdc.wro.model.group.Group;
import ro.isdc.wro.model.group.GroupExtractor;
import ro.isdc.wro.model.group.processor.GroupsProcessor;
import ro.isdc.wro.model.resource.ResourceType;
import ro.isdc.wro.model.resource.locator.UriLocator;
import ro.isdc.wro.model.resource.processor.impl.css.CssUrlRewritingProcessor;
import ro.isdc.wro.util.StopWatch;
import ro.isdc.wro.util.WroUtil;


/**
 * Contains all the factories used by optimizer in order to perform the logic.
 *
 * @author <NAME>
 * @created Created on Oct 30, 2008
 */
public class WroManager
    implements WroConfigurationChangeListener, CacheChangeCallbackAware {
  /**
   * Logger for this class.
   */
  private static final Logger LOG = LoggerFactory.getLogger(WroManager.class);
  /**
   * wro API mapping path. If request uri contains this, exposed API method will be invoked.
   */
  public static final String PATH_API = "wroAPI";
  /**
   * API - reload cache method call
   */
  public static final String API_RELOAD_CACHE = "reloadCache";
  /**
   * API - reload model method call
   */
  public static final String API_RELOAD_MODEL = "reloadModel";
  /**
   * ResourcesModel factory.
   */
  private WroModelFactory modelFactory;

  /**
   * GroupExtractor.
   */
  private GroupExtractor groupExtractor;

  /**
   * Groups processor.
   */
  private GroupsProcessor groupsProcessor;

  /**
   * A cacheStrategy used for caching processed results. <GroupName, processed result>.
   */
  private CacheStrategy<CacheEntry, ContentHashEntry> cacheStrategy;
  /**
   * A callback to be notified about the cache change.
   */
  private PropertyChangeListener cacheChangeCallback;
  /**
   * Scheduled executors service, used to update the output result.
   */
  private ScheduledExecutorService scheduler;


  /**
   * Perform processing of the uri.
   *
   * @param request
   *          {@link HttpServletRequest} to process.
   * @param response HttpServletResponse where to write the result content.
   * @throws IOException when any IO related problem occurs.
   */
  public final void process(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    //TODO return true if the request was processed in order to continue filter chain processing
    LOG.debug("processing: " + request.getRequestURI());
    validate();
    InputStream is = null;
    // create model
    final WroModel model = modelFactory.getInstance();
    //TODO move API related checks into separate class and determine filter mapping for better mapping
    if (isApiRequest(request)) {
      //must be case insensitive
      if (request.getRequestURI().contains(API_RELOAD_CACHE)) {
        Context.getConfig().reloadCache();
        response.setContentType("application/json");
        response.getWriter().write("{response: 'OK'}");
        response.getWriter().flush();
        response.getWriter().close();
        return;
      }
      if (request.getRequestURI().contains(API_RELOAD_MODEL)) {
        Context.getConfig().reloadModel();
        response.setContentType("application/json");
        response.getWriter().write("{response: 'OK'}");
        response.getWriter().flush();
        response.getWriter().close();
        return;
      }
    } else if (isProxyResourceRequest(request)) {
      is = locateInputeStream(request);
{'fraction_non_alphanumeric': 0.10581538118093538, 'fraction_numerical': 0.0026788704096439337, 'mean_word_length': 3.869565217391304, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6645765', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2578, 'n_words': 642}