<commit_before>/**
 * Copyright 2015 Otto (GmbH & Co KG)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.schedoscope.scheduler.rest.server

import java.util.logging.{ Level, LogManager, Logger }

import akka.actor.{ Actor, Props }
import akka.io.IO
import akka.pattern.ask
import akka.routing.SmallestMailboxPool
import akka.util.Timeout
import org.schedoscope.Schedoscope
import org.schedoscope.scheduler.commandline.SchedoscopeCliRepl
import org.schedoscope.scheduler.rest.SchedoscopeJsonDataFormat._
import org.schedoscope.scheduler.service.{ SchedoscopeService, SchedoscopeServiceImpl }
import org.slf4j.bridge.SLF4JBridgeHandler
import spray.can.Http
import spray.http.HttpHeaders.RawHeader
import spray.httpx.SprayJsonSupport.sprayJsonMarshaller
import spray.httpx.marshalling.ToResponseMarshallable.isMarshallable
import spray.routing.Directive.pimpApply
import spray.routing.HttpService

import scala.language.postfixOps

/**
 * Spray actor providing the Schedoscope REST service
 */
class SchedoscopeRestServiceActor(schedoscope: SchedoscopeService) extends Actor with HttpService {
  def actorRefFactory = context

  def receive = runRoute(route)

  val route = get {
    respondWithHeader(RawHeader("Access-Control-Allow-Origin", "*")) {
      parameters("status"?, "filter"?, "dependencies".as[Boolean]?, "typ"?, "mode" ?, "overview".as[Boolean] ?, "all".as[Boolean] ?) { (status, filter, dependencies, typ, mode, overview, all) =>
        {
          path("transformations") {
            complete(schedoscope.transformations(status, filter))
          } ~
            path("queues") {
              complete(schedoscope.queues(typ, filter))
            } ~
            path("commands") {
              complete(schedoscope.commands(status, filter))
            } ~
            path("views" / Rest ?) { viewUrlPath =>
              complete(schedoscope.views(viewUrlPath, status, filter, dependencies, overview, all))
            } ~
            path("materialize" / Rest ?) { viewUrlPath =>
              complete(schedoscope.materialize(viewUrlPath, status, filter, mode))
            } ~
            path("invalidate" / Rest ?) { viewUrlPath =>
              complete(schedoscope.invalidate(viewUrlPath, status, filter, dependencies))<commit_msg>Added better HTTP error responses when passing invalid filters<commit_after>/**
 * Copyright 2015 Otto (GmbH & Co KG)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.schedoscope.scheduler.rest.server

import java.util.logging.{ Level, LogManager, Logger }
import akka.actor.{ Actor, Props }
import akka.io.IO
import akka.pattern.ask
import akka.routing.SmallestMailboxPool
import akka.util.Timeout
import org.schedoscope.Schedoscope
import org.schedoscope.scheduler.commandline.SchedoscopeCliRepl
import org.schedoscope.scheduler.rest.SchedoscopeJsonDataFormat._
import org.schedoscope.scheduler.service.{ SchedoscopeService, SchedoscopeServiceImpl }
import org.slf4j.bridge.SLF4JBridgeHandler
import spray.can.Http
import spray.http.HttpHeaders.RawHeader
import spray.http.StatusCodes._
import spray.httpx.SprayJsonSupport.sprayJsonMarshaller
import spray.httpx.marshalling.ToResponseMarshallable.isMarshallable
import spray.routing.Directive.pimpApply
import spray.routing.HttpService
import spray.routing.ExceptionHandler
import scala.language.postfixOps
import spray.util.LoggingContext

/**
 * Spray actor providing the Schedoscope REST service
 */
class SchedoscopeRestServiceActor(schedoscope: SchedoscopeService) extends Actor with HttpService {

  def actorRefFactory = context

  def receive = runRoute(route)

  implicit def myExceptionHandler(implicit log: LoggingContext) =
    ExceptionHandler {
      case e: IllegalArgumentException =>
        requestUri { uri =>
          log.warning("Invalid scheduling request: {} Problem: {}", uri, e)
          complete(BadRequest, e.getMessage)
        }

      case t: Throwable =>
        requestUri { uri =>
          log.warning("General exception caught while processing scheduling request: {} Problem: {}", uri, t)
          complete(InternalServerError, s"Server encountered exception: ${t}")
        }
    }

  val route = get {
    respondWithHeader(RawHeader("Access-Control-Allow-Origin", "*")) {
      parameters("status"?, "filter"?, "dependencies".as[Boolean]?, "typ"?, "mode" ?, "overview".as[Boolean] ?, "all".as[Boolean] ?) { (status, filter, dependencies, typ, mode, overview, all) =>
        {
          path("transformations") {
            complete(schedoscope.transformations(status, filter))
          } ~
            path("queues") {
              complete(schedoscope.queues(typ, filter))
            } ~
            path("commands") {
              complete(schedoscope.commands(status, filter))
            } ~
            path("views" / Rest ?) { viewUrlPath =>
              complete(schedoscope.views(viewUrlPath, status, filter, dependencies, overview, all))
            } ~
            path("materialize" / Rest ?) { viewUrlPath =>
              complete(schedoscope.materialize(viewUrlPath, status, filter, mode))
            } ~
            path("invalidate" / Rest ?) { viewUrlPath =>
              complete(schedoscope.invalidate(viewUrlPath, status, filter, dependencies))
{'fraction_non_alphanumeric': 0.10237601658427684, 'fraction_numerical': 0.0031892840057407114, 'mean_word_length': 3.5714285714285716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2282581', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1670, 'n_words': 452}