<commit_before>package ro.isdc.wro.http.handler;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.sun.xml.internal.ws.encoding.ContentType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.isdc.wro.WroRuntimeException;
import ro.isdc.wro.config.Context;
import ro.isdc.wro.http.support.UnauthorizedRequestException;
import ro.isdc.wro.model.group.Inject;
import ro.isdc.wro.model.resource.locator.factory.UriLocatorFactory;

import javax.activation.FileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Provides access to wro resources via a resource proxy.
 *
 * @author <NAME>
 * @created 19 May 2012
 * @since 1.4.7
 */
public class ResourceProxyRequestHandler implements RequestHandler {
  private static final Logger LOG = LoggerFactory.getLogger(ResourceProxyRequestHandler.class);

  public static final String PARAM_RESOURCE_ID = "id";
  public static final String PATH_RESOURCES = "wroResources";
  
  @Inject
  private UriLocatorFactory uriLocatorFactory;

  private FileTypeMap fileTypeMap = FileTypeMap.getDefaultFileTypeMap();

  public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
    final String resourceUri = request.getParameter(PARAM_RESOURCE_ID);
    if(!hasAccessToResource(resourceUri)) {
      accessDeniedResponse(resourceUri, response);
    }

    OutputStream outputStream = response.getOutputStream();
    LOG.debug("locating stream for resourceId: {}", resourceUri);
    final InputStream is = uriLocatorFactory.locate(resourceUri);
    if (is == null) {
      throw new WroRuntimeException("Cannot process request with uri: " + request.getRequestURI());
    }

    int length = IOUtils.copy(is, outputStream);
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(outputStream);

    response.setContentLength(length);
    response.setContentType(getContentType(resourceUri));
  }

  /**
   * {@inheritDoc}
   */
  public boolean accept(HttpServletRequest request) {
    return StringUtils.contains(request.getRequestURI(), PATH_RESOURCES);
  }

  /**
   * {@inheritDoc}
   */
  public boolean isEnabled() {
    return true;
  }

  private void accessDeniedResponse(String resourceUri, HttpServletResponse response) {
    throw new UnauthorizedRequestException("Unauthorized resource request detected! " + resourceUri);
  }

  /**
   * TODO: use new AuthorizedResourcesHolder to check acccess to resourceUri
   * Verifies that the user has access or not to the requested resource
   *
   * @param resourceUri of the resource to be proxied.
   * @return
   */
  private boolean hasAccessToResource(String resourceUri) {
    return true;
  }

  private String getContentType(String resourceUri) {
    if(resourceUri.toLowerCase().endsWith(".css")) {
      return "text/css";
    } else if(resourceUri.toLowerCase().endsWith(".js")) {
      return "application/javascript";
    } else if(resourceUri.toLowerCase().endsWith(".png")) {
      return "image/png";
    } else {
      return fileTypeMap.getContentType(resourceUri);
    }
  }

}<commit_msg>Issue467 - Minor cleanups in imports.<commit_after>package ro.isdc.wro.http.handler;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.isdc.wro.WroRuntimeException;
import ro.isdc.wro.http.support.UnauthorizedRequestException;
import ro.isdc.wro.model.group.Inject;
import ro.isdc.wro.model.resource.locator.factory.UriLocatorFactory;

import javax.activation.FileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Provides access to wro resources via a resource proxy.
 *
 * @author <NAME> Østhus
 * @created 19 May 2012
 * @since 1.4.7
 */
public class ResourceProxyRequestHandler implements RequestHandler {
  private static final Logger LOG = LoggerFactory.getLogger(ResourceProxyRequestHandler.class);

  public static final String PARAM_RESOURCE_ID = "id";
  public static final String PATH_RESOURCES = "wroResources";
  
  @Inject
  private UriLocatorFactory uriLocatorFactory;

  private FileTypeMap fileTypeMap = FileTypeMap.getDefaultFileTypeMap();

  public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
    final String resourceUri = request.getParameter(PARAM_RESOURCE_ID);
    if(!hasAccessToResource(resourceUri)) {
      accessDeniedResponse(resourceUri, response);
    }

    OutputStream outputStream = response.getOutputStream();
    LOG.debug("locating stream for resourceId: {}", resourceUri);
    final InputStream is = uriLocatorFactory.locate(resourceUri);
    if (is == null) {
      throw new WroRuntimeException("Cannot process request with uri: " + request.getRequestURI());
    }

    int length = IOUtils.copy(is, outputStream);

    response.setContentLength(length);
    response.setContentType(getContentType(resourceUri));
    response.setStatus(HttpServletResponse.SC_OK);

    IOUtils.closeQuietly(outputStream);
    IOUtils.closeQuietly(is);
  }

  /**
   * {@inheritDoc}
   */
  public boolean accept(HttpServletRequest request) {
    return StringUtils.contains(request.getRequestURI(), PATH_RESOURCES);
  }

  /**
   * {@inheritDoc}
   */
  public boolean isEnabled() {
    return true;
  }

  private void accessDeniedResponse(String resourceUri, HttpServletResponse response) {
    throw new UnauthorizedRequestException("Unauthorized resource request detected! " + resourceUri);
  }

  /**
   * TODO: use new AuthorizedResourcesHolder to check acccess to resourceUri
   * Verifies that the user has access or not to the requested resource
   *
   * @param resourceUri of the resource to be proxied.
   * @return
   */
  private boolean hasAccessToResource(String resourceUri) {
    return true;
  }

  private String getContentType(String resourceUri) {
    if (resourceUri.toLowerCase().endsWith(".css")) {
      return "text/css";
    } else if (resourceUri.toLowerCase().endsWith(".js")) {
      return "application/javascript";
    } else if (resourceUri.toLowerCase().endsWith(".png")) {
      return "image/png";
    } else {
      return fileTypeMap.getContentType(resourceUri);
    }
  }

}
{'fraction_non_alphanumeric': 0.10152439024390245, 'fraction_numerical': 0.0041158536585365856, 'mean_word_length': 4.481203007518797, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '4673678', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1885, 'n_words': 427}