<commit_before>package com.airbnb.scheduler.api

import scala.collection.JavaConverters._
import com.google.inject.Inject
import java.net.{HttpURLConnection, URL}
import java.io.{OutputStream, InputStream}
import java.util.concurrent.atomic.AtomicBoolean
import java.util.logging.{Logger, Level}
import javax.inject.Named
import javax.servlet._
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
import com.airbnb.scheduler.jobs.JobScheduler
import com.google.inject.Inject

/**
 * Simple filter that redirects to the leader if applicable.
 * @author <NAME> (<EMAIL>)
 */
class RedirectFilter @Inject()(val jobScheduler: JobScheduler) extends Filter  {
  def init(filterConfig: FilterConfig) {}

  val log = Logger.getLogger(getClass.getName)

  def copy(input: InputStream, output: OutputStream) = {
    val bytes = new Array[Byte](1024)
    Iterator
      .continually(input.read(bytes))
      .takeWhile(-1 !=)
      .foreach(read => output.write(bytes, 0, read))
  }

  def buildUrl(leaderData: String, request: HttpServletRequest) = {
    if (request.getQueryString != null) {
      new URL("http://%s%s?%s".format(leaderData, request.getRequestURI, request.getQueryString))
    } else {
      new URL("http://%s%s".format(leaderData, request.getRequestURI))
    }
  }

  def doFilter(rawRequest: ServletRequest,
               rawResponse: ServletResponse,
               chain: FilterChain) {
    if (rawRequest.isInstanceOf[HttpServletRequest]) {
      val request = rawRequest.asInstanceOf[HttpServletRequest]
      val leaderData = jobScheduler.getLeader
      val response = rawResponse.asInstanceOf[HttpServletResponse]

      if (jobScheduler.isLeader) {
        chain.doFilter(request, response)
      } else {
        try {
          log.info("Proxying request.")

          val method = request.getMethod

          val proxy =
            buildUrl(leaderData, request)
              .openConnection().asInstanceOf[HttpURLConnection]


          val names = request.getHeaderNames
          // getHeaderNames() and getHeaders() are known to return null, see:
          // http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeaders(java.lang.String)
          if (names != null) {
            for (name <- names.asScala) {
              val values = request.getHeaders(name)
              if (values != null) {
                proxy.setRequestProperty(name, values.asScala.mkString(","))
              }
            }
          }

          proxy.setRequestMethod(method)

          method match {
            case "GET" | "HEAD" | "DELETE" =>
              proxy.setDoOutput(false)
            case _ =>
              proxy.setDoOutput(true)
              val proxyOutputStream = proxy.getOutputStream
              copy(request.getInputStream, proxyOutputStream)
              proxyOutputStream.close
          }

          response.setStatus(proxy.getResponseCode())

          val fields = proxy.getHeaderFields
          // getHeaderNames() and getHeaders() are known to return null, see:
          // http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeaders(java.lang.String)
          if (fields != null) {
            for ((name, values) <- fields.asScala) {
              if (name != null && values != null) {
                for (value <- values.asScala) {
                  response.setHeader(name, value)
                }
              }
            }
          }

          val responseOutputStream = response.getOutputStream
          copy(proxy.getInputStream, response.getOutputStream)
          proxy.getInputStream.close
          responseOutputStream.close
        } catch {
          case t: Exception => 
            response.sendError(500)
            log.log(Level.WARNING, "Exception while proxying!", t)
        }<commit_msg>Return 500 only if proxying exception with code 2xx<commit_after>package com.airbnb.scheduler.api

import scala.collection.JavaConverters._
import com.google.inject.Inject
import java.net.{HttpURLConnection, URL}
import java.io.{OutputStream, InputStream}
import java.util.concurrent.atomic.AtomicBoolean
import java.util.logging.{Logger, Level}
import javax.inject.Named
import javax.servlet._
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
import com.airbnb.scheduler.jobs.JobScheduler
import com.google.inject.Inject

/**
 * Simple filter that redirects to the leader if applicable.
 * @author <NAME> (<EMAIL>)
 */
class RedirectFilter @Inject()(val jobScheduler: JobScheduler) extends Filter  {
  def init(filterConfig: FilterConfig) {}

  val log = Logger.getLogger(getClass.getName)

  def copy(input: InputStream, output: OutputStream) = {
    val bytes = new Array[Byte](1024)
    Iterator
      .continually(input.read(bytes))
      .takeWhile(-1 !=)
      .foreach(read => output.write(bytes, 0, read))
  }

  def buildUrl(leaderData: String, request: HttpServletRequest) = {
    if (request.getQueryString != null) {
      new URL("http://%s%s?%s".format(leaderData, request.getRequestURI, request.getQueryString))
    } else {
      new URL("http://%s%s".format(leaderData, request.getRequestURI))
    }
  }

  def doFilter(rawRequest: ServletRequest,
               rawResponse: ServletResponse,
               chain: FilterChain) {
    if (rawRequest.isInstanceOf[HttpServletRequest]) {
      val request = rawRequest.asInstanceOf[HttpServletRequest]
      val leaderData = jobScheduler.getLeader
      val response = rawResponse.asInstanceOf[HttpServletResponse]

      if (jobScheduler.isLeader) {
        chain.doFilter(request, response)
      } else {
        var proxyStatus: Int = 200
        try {
          log.info("Proxying request.")

          val method = request.getMethod

          val proxy =
            buildUrl(leaderData, request)
              .openConnection().asInstanceOf[HttpURLConnection]


          val names = request.getHeaderNames
          // getHeaderNames() and getHeaders() are known to return null, see:
          // http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeaders(java.lang.String)
          if (names != null) {
            for (name <- names.asScala) {
              val values = request.getHeaders(name)
              if (values != null) {
                proxy.setRequestProperty(name, values.asScala.mkString(","))
              }
            }
          }

          proxy.setRequestMethod(method)

          method match {
            case "GET" | "HEAD" | "DELETE" =>
              proxy.setDoOutput(false)
            case _ =>
              proxy.setDoOutput(true)
              val proxyOutputStream = proxy.getOutputStream
              copy(request.getInputStream, proxyOutputStream)
              proxyOutputStream.close
          }

          proxyStatus = proxy.getResponseCode()
          response.setStatus(proxyStatus)

          val fields = proxy.getHeaderFields
          // getHeaderNames() and getHeaders() are known to return null, see:
          // http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeaders(java.lang.String)
          if (fields != null) {
            for ((name, values) <- fields.asScala) {
              if (name != null && values != null) {
                for (value <- values.asScala) {
                  response.setHeader(name, value)
                }
              }
            }
          }

          val responseOutputStream = response.getOutputStream
          copy(proxy.getInputStream, response.getOutputStream)
          proxy.getInputStream.close
          responseOutputStream.close
        } catch {
          case t: Exception => 
            if ((200 to 299) contains proxyStatus) response.sendError(500)
            log.log(Level.WARNING, "Exception while proxying!", t)
        }
{'fraction_non_alphanumeric': 0.10622290232083652, 'fraction_numerical': 0.004463147156337669, 'mean_word_length': 2.3646503646503647, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '979396', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2110, 'n_words': 440}