diff --git a/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesRoutes.scala b/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesRoutes.scala index f1870270a1..f4c17f9785 100644 --- a/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesRoutes.scala +++ b/delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesRoutes.scala @@ -175,14 +175,13 @@ final class ResourcesRoutes( // Fetch a resource original source (pathPrefix("source") & get & pathEndOrSingleSlash & idSegmentRef(id) & varyAcceptHeaders) { id => authorizeFor(ref, Read).apply { - (parameter("annotate".as[Boolean].withDefault(false))) { annotate => + parameter("annotate".as[Boolean].withDefault(false)) { annotate => implicit val source: Printer = sourcePrinter if (annotate) { emit( resources .fetch(id, ref, schemaOpt) .flatMap(asSourceWithMetadata) - .rejectWhen(wrongJsonOrNotFound) ) } else { val sourceIO = resources.fetch(id, ref, schemaOpt).map(_.value.source) diff --git a/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/FilesRoutes.scala b/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/FilesRoutes.scala index 17c8f1b81f..d41f74e3cb 100644 --- a/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/FilesRoutes.scala +++ b/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/FilesRoutes.scala @@ -186,15 +186,11 @@ final class FilesRoutes( def fetch(id: IdSegmentRef, ref: ProjectRef)(implicit caller: Caller): Route = (headerValueByType(Accept) & varyAcceptHeaders) { case accept if accept.mediaRanges.exists(metadataMediaRanges.contains) => - emit(fetchMetadata(id, ref).rejectWhen(unauthorizedOrNotFound)) + emit(fetchMetadata(id, ref).rejectOn[FileNotFound]) case _ => - emit(files.fetchContent(id, ref).rejectWhen(unauthorizedOrNotFound)) + emit(files.fetchContent(id, ref).rejectOn[FileNotFound]) } - private val unauthorizedOrNotFound: PartialFunction[FileRejection, Boolean] = { - case _: AuthorizationFailed | _: FileNotFound => true - } - def fetchMetadata(id: IdSegmentRef, ref: ProjectRef)(implicit caller: Caller): IO[FileRejection, FileResource] = aclCheck.authorizeForOr(ref, Read)(AuthorizationFailed(ref, Read)) >> files.fetch(id, ref) } diff --git a/delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/directives/DeltaDirectives.scala b/delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/directives/DeltaDirectives.scala index 4755d974af..231fc1a0b6 100644 --- a/delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/directives/DeltaDirectives.scala +++ b/delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/directives/DeltaDirectives.scala @@ -110,13 +110,6 @@ trait DeltaDirectives extends UriDirectives { } } - /** Injects a `Vary: Accept,Accept-Encoding` into the response */ - def varyAcceptHeaders: Directive0 = - vary(Set(Accept.name, `Accept-Encoding`.name)) - - private def vary(headers: Set[String]): Directive0 = - respondWithHeader(RawHeader("Vary", headers.mkString(","))) - /** * If the `Accept` header is set to `text/html`, redirect to the matching resource page in fusion if the feature is * enabled @@ -186,4 +179,19 @@ trait DeltaDirectives extends UriDirectives { /** The URI of fusion's main login page */ def fusionLoginUri(implicit config: FusionConfig): UIO[Uri] = UIO.pure { config.base / "login" } + + /** Injects a `Vary: Accept,Accept-Encoding` into the response */ + def varyAcceptHeaders: Directive0 = + vary(Set(Accept.name, `Accept-Encoding`.name)) + + private def vary(headers: Set[String]): Directive0 = + respondWithHeader(RawHeader("Vary", headers.mkString(","))) + + private def respondWithHeader(responseHeader: HttpHeader): Directive0 = + mapSuccessResponse(r => r.withHeaders(r.headers :+ responseHeader)) + + private def mapSuccessResponse(f: HttpResponse => HttpResponse): Directive0 = + mapRouteResultPF { + case RouteResult.Complete(response) if response.status.isSuccess => RouteResult.Complete(f(response)) + } }