Skip to content

Commit

Permalink
Fixed config issue and rejection handler issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vonnagy committed Jan 29, 2015
1 parent e48c60f commit d99712d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait CoreConfig {
// exists within a conf directory under the application context
case null if new File("conf/application.conf").exists => ConfigFactory.load("conf/application.conf")
// Load the default
case null => ConfigFactory.load("container")
case null => ConfigFactory.load()
// If there is a system property for the file then use that
case f => ConfigFactory.parseFile(new File(f))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,20 @@ package com.github.vonnagy.service.container.http.routing.Rejection

import spray.routing.Rejection

case class ServiceUnavailableRejection(message: AnyRef) extends Rejection
/**
* Use this rejection when a service is not available
* @param message
*/
case class ServiceUnavailableRejection(message: String) extends Rejection

/**
* Use this rejection when trying to create an entity that already exists
* @param message
*/
case class DuplicateRejection(message: String) extends Rejection

/**
* Use this rejection when a resource can't be found and you want a custom message
* @param message
*/
case class NotFoundRejection(message: String) extends Rejection
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package com.github.vonnagy.service.container.http.routing

import akka.actor.{Actor, ActorSystem, Props}
import com.github.vonnagy.service.container.http.{DefaultMarshallers, RejectionResponse}
import com.github.vonnagy.service.container.log.ActorLoggingAdapter
import net.liftweb.json.Serialization
import spray.http.StatusCodes._
import spray.http.{ContentType, HttpEntity, HttpResponse, MediaTypes}
import spray.routing._
import spray.util.LoggingContext

import scala.util.control.NonFatal

/**
* Add a set of defined routes
* @param route
Expand Down Expand Up @@ -44,39 +38,10 @@ object RoutedService {
*
* @param routeEndpoints the routes to manage
*/
class RoutedService(val routeEndpoints: Seq[RoutedEndpoints]) extends Actor with HttpServiceBase with DefaultMarshallers with ActorLoggingAdapter {

/**
* Wrap all Exceptions in the [[com.github.vonnagy.service.container.http.RejectionResponse]] class then marshall as json.
*/
implicit val exceptionHandler = ExceptionHandler {
case errors => mapHttpResponse(transformRejection) {
(ExceptionHandler.apply {
case NonFatal(e) => ctx => {
log.error(e.getMessage, e)
ctx.complete(InternalServerError, InternalServerError.defaultMessage)
}
} orElse ExceptionHandler.default)(errors)
}
}

/**
* Wrap all Rejections in the [[com.github.vonnagy.service.container.http.RejectionResponse]] class then marshall as json.
*/
implicit val rejectionHandler = RejectionHandler {
case rejections => mapHttpResponse(transformRejection) {
(RejectionHandler.apply {
case MalformedRequestContentRejection(errorMsg, cause) :: _ =>
complete(UnprocessableEntity, errorMsg)
} orElse RejectionHandler.Default)(rejections)
}
}

private def transformRejection(response: HttpResponse): HttpResponse = {
response.withEntity(HttpEntity(ContentType(MediaTypes.`application/json`),
Serialization.write(RejectionResponse(response.status.intValue, response.status.defaultMessage, response.entity.asString))))
}
class RoutedService(val routeEndpoints: Seq[RoutedEndpoints]) extends Actor
with RoutingHandler with HttpServiceBase {

implicit val fact = context.system
private[routing] var routes = routeEndpoints

// The base handler
Expand Down Expand Up @@ -115,5 +80,4 @@ class RoutedService(val routeEndpoints: Seq[RoutedEndpoints]) extends Actor with
runRoute(route)(exceptionHandler, rejectionHandler, context, RoutingSettings.default, LoggingContext.fromActorRefFactory)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.vonnagy.service.container.http.routing

import akka.actor.ActorRefFactory
import com.github.vonnagy.service.container.http.routing.Rejection.{DuplicateRejection, NotFoundRejection}
import com.github.vonnagy.service.container.http.{DefaultMarshallers, RejectionResponse}
import com.github.vonnagy.service.container.log.LoggingAdapter
import net.liftweb.json.Serialization
import spray.http.StatusCodes._
import spray.http.{ContentType, HttpEntity, HttpResponse, MediaTypes}
import spray.routing._

import scala.util.control.NonFatal

trait RoutingHandler extends HttpServiceBase with DefaultMarshallers with LoggingAdapter {

implicit val fact: ActorRefFactory

/**
* Wrap all Exceptions in the [[com.github.vonnagy.service.container.http.RejectionResponse]] class then marshall as json.
*/
implicit val exceptionHandler = ExceptionHandler {
case errors => mapHttpResponse(transformRejection) {
(ExceptionHandler.apply {
case NonFatal(e) => ctx => {
log.error(e.getMessage, e)
ctx.complete(InternalServerError, InternalServerError.defaultMessage)
}
} orElse ExceptionHandler.default)(errors)
}
}

/**
* Wrap all Rejections in the [[com.github.vonnagy.service.container.http.RejectionResponse]] class then marshall as json.
*/
implicit val rejectionHandler = RejectionHandler {
case rejections => mapHttpResponse(transformRejection) {
// Pre-default rejections
(RejectionHandler.apply {
case NotFoundRejection(errorMsg) :: _ =>
complete(NotFound, errorMsg)
case DuplicateRejection(errorMsg) :: _ =>
complete(BadRequest, errorMsg)
case MalformedRequestContentRejection(errorMsg, cause) :: _ =>
complete(UnprocessableEntity, errorMsg)
} orElse RejectionHandler.Default orElse
// Post-default rejections
RejectionHandler.apply {
case _ :: _ =>
complete(NotFound, "The requested resource could not be found.")
})(rejections)
}
}

private def transformRejection(response: HttpResponse): HttpResponse = {
response.withEntity(HttpEntity(ContentType(MediaTypes.`application/json`),
Serialization.write(RejectionResponse(response.status.intValue, response.status.defaultMessage, response.entity.asString))))
}


}

0 comments on commit d99712d

Please sign in to comment.