net.relaysoft.testing.azure.blob.mock.routes.CreateContainer.scala Maven / Gradle / Ivy
package net.relaysoft.testing.azure.blob.mock.routes
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import com.typesafe.scalalogging.LazyLogging
import net.relaysoft.testing.azure.blob.mock.exceptions.{ContainerAlreadyExistsException, InternalErrorException}
import net.relaysoft.testing.azure.blob.mock.models.Container
import net.relaysoft.testing.azure.blob.mock.providers.Provider
import net.relaysoft.testing.azure.blob.mock.utils.HeaderNames
import java.util.UUID
import scala.util.{Failure, Success, Try}
case class CreateContainer()(implicit provider:Provider) extends LazyLogging {
def route(accountName: String, containerName:String, headers: Map[String, String]): Route = put {
parameters(Symbol("restype") ! "container") {
complete {
val headerString = headers.map(_.productIterator.mkString(":")).mkString(",")
logger.debug(s"account=$accountName, container=$containerName, message=Creating new container, " +
s"headers=[$headerString]")
Try(provider.createContainer(containerName)) match {
case Success(container) => HttpResponse(StatusCodes.Created).withHeaders(getResponseHeaders(container, headers))
case Failure(e: ContainerAlreadyExistsException) => HttpResponse(
StatusCodes.Conflict,
entity = HttpEntity(
ContentType(MediaTypes.`application/xml`, HttpCharsets.`UTF-8`),
e.toXML.toString
)
)
case Failure(e) => HttpResponse(
StatusCodes.InternalServerError,
entity = HttpEntity(
ContentType(MediaTypes.`application/xml`, HttpCharsets.`UTF-8`),
InternalErrorException(e).toXML.toString
)
)
}
}
}
}
def getResponseHeaders(container:Container, requestHeaders: Map[String, String]): Seq[RawHeader] = {
val headers = Seq(
RawHeader(HeaderNames.ETAG, container.etag),
RawHeader(HeaderNames.LAST_MODIFIED, container.lastModifiedDate.toRfc1123DateTimeString()),
RawHeader(HeaderNames.DATE, DateTime.now.toRfc1123DateTimeString()),
RawHeader(HeaderNames.X_MS_REQUEST_ID, UUID.randomUUID().toString),
RawHeader(HeaderNames.X_MS_VERSION, requestHeaders.getOrElse(HeaderNames.X_MS_VERSION, "")))
if(requestHeaders.contains(HeaderNames.X_MS_CLIENT_REQUEST_ID)) {
val clientHeaders = Seq(RawHeader(HeaderNames.X_MS_CLIENT_REQUEST_ID,
requestHeaders.getOrElse(HeaderNames.X_MS_CLIENT_REQUEST_ID, "")))
return headers ++ clientHeaders
}
headers
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy