This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 91223ceb60122e4705746146fb9a1b9fb26becc7 Author: Tran Tien Duc <[email protected]> AuthorDate: Fri Apr 10 10:32:04 2020 +0700 JAMES-2891 JMAP Version compliance --- .../org/apache/james/jmap/http/SessionRoutes.scala | 34 ++++++++++++++-------- .../apache/james/jmap/http/SessionRoutesTest.scala | 16 +++++----- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/http/SessionRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/http/SessionRoutes.scala index 901ef64..f262793 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/http/SessionRoutes.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/http/SessionRoutes.scala @@ -19,24 +19,26 @@ package org.apache.james.jmap.http -import java.util.function.BiFunction +import java.util.stream.Stream import io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE +import io.netty.handler.codec.http.HttpMethod import io.netty.handler.codec.http.HttpResponseStatus.OK import javax.inject.Inject import org.apache.james.jmap.HttpConstants.JSON_CONTENT_TYPE_UTF8 -import org.apache.james.jmap.JMAPRoutes +import org.apache.james.jmap.JMAPRoutes.CORS_CONTROL +import org.apache.james.jmap.JMAPUrls.AUTHENTICATION import org.apache.james.jmap.exceptions.UnauthorizedException import org.apache.james.jmap.http.SessionRoutes.{JMAP_SESSION, LOGGER} import org.apache.james.jmap.json.Serializer import org.apache.james.jmap.model.Session -import org.reactivestreams.Publisher -import org.slf4j.{Logger, LoggerFactory} +import org.apache.james.jmap.{Endpoint, JMAPRoute, JMAPRoutes} +import org.slf4j.LoggerFactory import play.api.libs.json.Json import reactor.core.publisher.Mono import reactor.core.scala.publisher.SMono import reactor.core.scheduler.Schedulers -import reactor.netty.http.server.{HttpServerRequest, HttpServerResponse, HttpServerRoutes} +import reactor.netty.http.server.HttpServerResponse object SessionRoutes { private val JMAP_SESSION = "/jmap/session" @@ -44,23 +46,31 @@ object SessionRoutes { } @Inject -class SessionRoutes(val authFilter: Authenticator, +class SessionRoutes(val authenticator: Authenticator, val sessionSupplier: SessionSupplier = new SessionSupplier(), val serializer: Serializer = new Serializer()) extends JMAPRoutes { - private val generateSession: BiFunction[HttpServerRequest, HttpServerResponse, Publisher[Void]] = - (request, response) => SMono.fromPublisher(authFilter.authenticate(request)) + private val generateSession: JMAPRoute.Action = + (request, response) => SMono.fromPublisher(authenticator.authenticate(request)) .map(_.getUser) .flatMap(sessionSupplier.generate) .flatMap(session => sendRespond(session, response)) .onErrorResume(throwable => SMono.fromPublisher(errorHandling(throwable, response))) .subscribeOn(Schedulers.elastic()) + .asJava() - override def define(builder: HttpServerRoutes): HttpServerRoutes = { - builder.get(JMAP_SESSION, generateSession) - } + override def routes: Stream[JMAPRoute] = + Stream.of( + JMAPRoute.builder() + .endpoint(new Endpoint(HttpMethod.GET, JMAP_SESSION)) + .action(generateSession) + .corsHeaders, + JMAPRoute.builder() + .endpoint(new Endpoint(HttpMethod.OPTIONS, AUTHENTICATION)) + .action(CORS_CONTROL) + .noCorsHeaders) - private def sendRespond(session: Session, resp: HttpServerResponse): SMono[Void] = + private def sendRespond(session: Session, resp: HttpServerResponse) = SMono.fromPublisher(resp.header(CONTENT_TYPE, JSON_CONTENT_TYPE_UTF8) .status(OK) .sendString(SMono.fromCallable(() => Json.stringify(serializer.serialize(session)))) diff --git a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/http/SessionRoutesTest.scala b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/http/SessionRoutesTest.scala index a7b3e9c..df01af4 100644 --- a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/http/SessionRoutesTest.scala +++ b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/http/SessionRoutesTest.scala @@ -21,6 +21,7 @@ package org.apache.james.jmap.http import java.nio.charset.StandardCharsets +import io.netty.handler.codec.http.HttpHeaderNames.ACCEPT import io.restassured.RestAssured import io.restassured.builder.RequestSpecBuilder import io.restassured.config.EncoderConfig.encoderConfig @@ -29,7 +30,7 @@ import io.restassured.http.ContentType import org.apache.http.HttpStatus import org.apache.james.core.Username import org.apache.james.jmap.http.SessionRoutesTest.{BOB, TEST_CONFIGURATION} -import org.apache.james.jmap.{JMAPConfiguration, JMAPRoutes, JMAPServer} +import org.apache.james.jmap._ import org.apache.james.mailbox.MailboxSession import org.mockito.ArgumentMatchers.any import org.mockito.Mockito._ @@ -50,7 +51,6 @@ object SessionRoutesTest { class SessionRoutesTest extends AnyFlatSpec with BeforeAndAfter with Matchers { var jmapServer: JMAPServer = _ - var sessionSupplier: SessionSupplier = _ before { val mockedSession = mock(classOf[MailboxSession]) @@ -61,18 +61,18 @@ class SessionRoutesTest extends AnyFlatSpec with BeforeAndAfter with Matchers { when(mockedAuthFilter.authenticate(any())) .thenReturn(Mono.just(mockedSession)) - sessionSupplier = spy(new SessionSupplier()) - val jmapRoutes: Set[JMAPRoutes] = Set(new SessionRoutes( - sessionSupplier = sessionSupplier, - authFilter = mockedAuthFilter)) + val sessionRoutes = new SessionRoutes( + sessionSupplier = new SessionSupplier(), + authenticator = mockedAuthFilter) jmapServer = new JMAPServer( TEST_CONFIGURATION, - jmapRoutes.asJava) + Set(new JMAPRoutesHandler(Version.RFC8621, sessionRoutes)).asJava, + new VersionParser(Set(Version.RFC8621).asJava)) jmapServer.start() RestAssured.requestSpecification = new RequestSpecBuilder() .setContentType(ContentType.JSON) - .setAccept(ContentType.JSON) + .addHeader(ACCEPT.toString, s"application/json; jmapVersion=${Version.RFC8621.asString}") .setConfig(newConfig.encoderConfig(encoderConfig.defaultContentCharset(StandardCharsets.UTF_8))) .setPort(jmapServer.getPort.getValue) .setBasePath(SessionRoutesTest.JMAP_SESSION) --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
