使用哈希函数进行乐观锁定

时间:2016-04-03 21:16:43

标签: spray spray-routing

下面的代码似乎没有错误地完成,但是堆栈中的内容更低了.span.server.HttpServerConnection想要' If-Match'标题具有特定格式。
我觉得我错过了一个拒绝处理程序?或者我必须关闭一些东西吗?

任何想法?

api 22:29:56.251 | WARN  | root-actor-system-akka.actor.default-dispatcher-4 | akka://root-actor-system/user/IO-HTTP/listener-0/2 | spray.can.server.HttpServerConnection | Illegal request header: Illegal 'If-Match' header: Invalid input 's', expected $timesIf$minusMatch (line 1, pos 1): somehashfunctionvalue

路线:

override def putObjectRoute = (path("api" / "models" / Segment / "objects") & authenticatedJsonRoute) {
    (modeledSystemName, tenantName, authInfo) =>
      put {
        entity(as[ModeledObject]) { obje =>
          etag(obje, hashfunction) {
            onComplete(service.updateObject(modeledSystemName, tenantName, obje)) {
              case Success(_) => complete(NoContent)
              case Failure(ex) =>
                log.error(ex.getMessage)
                complete(InternalServerError, s"An error occurred: ${ex.getMessage}")
            }
          }
        }

指令:

trait OCCETag {

  def etag[T](entity: T, f: (String, T) => Boolean): Directive0 = optionalHeaderValueByName("If-Match").flatMap {
    case Some(etag) => mapInnerRoute { route =>
      val e = entity
      ctx => {
        def complete304() = ctx.complete(HttpResponse(NotModified))
        def complete412() = ctx.complete(PreconditionFailed)
        if (f(etag, e)) pass else complete412()
      }
    }
    case None => reject(MissingHeaderRejection("If-Match"))
  }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我改变了传递给读者路线(ctx)。基本上,传递不会将上下文传递给指令。