This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit cfc6b0dbd3da81b06e767c04c498c0d71e0e4176
Author: Anton Lazarev <[email protected]>
AuthorDate: Fri Oct 16 18:42:20 2020 -0400

    JAMES-2891 redirect to JMAP session resource from /.well-known/jmap
---
 .../scala/org/apache/james/jmap/http/SessionRoutes.scala    | 13 ++++++++++++-
 .../org/apache/james/jmap/http/SessionRoutesTest.scala      |  9 +++++++++
 .../src/main/java/org/apache/james/jmap/JMAPRoutes.java     |  5 +++++
 3 files changed, 26 insertions(+), 1 deletion(-)

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 c087ed9..10f1565 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
@@ -29,7 +29,7 @@ import 
org.apache.james.jmap.HttpConstants.JSON_CONTENT_TYPE_UTF8
 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.http.SessionRoutes.{JMAP_SESSION, 
WELL_KNOWN_JMAP, LOGGER}
 import org.apache.james.jmap.http.rfc8621.InjectionKeys
 import org.apache.james.jmap.json.ResponseSerializer
 import org.apache.james.jmap.model.Session
@@ -43,6 +43,7 @@ import reactor.netty.http.server.HttpServerResponse
 
 object SessionRoutes {
   private val JMAP_SESSION: String = "/jmap/session"
+  private val WELL_KNOWN_JMAP: String = "/.well-known/jmap"
   private val LOGGER = LoggerFactory.getLogger(classOf[SessionRoutes])
 }
 
@@ -58,6 +59,8 @@ class SessionRoutes @Inject() (@Named(InjectionKeys.RFC_8621) 
val authenticator:
       .subscribeOn(Schedulers.elastic())
       .asJava()
 
+  private val redirectToSession: JMAPRoute.Action = 
JMAPRoutes.redirectTo(JMAP_SESSION)
+
   override def routes: Stream[JMAPRoute] =
     Stream.of(
       JMAPRoute.builder()
@@ -67,6 +70,14 @@ class SessionRoutes @Inject() 
(@Named(InjectionKeys.RFC_8621) val authenticator:
       JMAPRoute.builder()
         .endpoint(new Endpoint(HttpMethod.OPTIONS, JMAP_SESSION))
         .action(CORS_CONTROL)
+        .noCorsHeaders,
+      JMAPRoute.builder()
+        .endpoint(new Endpoint(HttpMethod.GET, WELL_KNOWN_JMAP))
+        .action(redirectToSession)
+        .corsHeaders,
+      JMAPRoute.builder()
+        .endpoint(new Endpoint(HttpMethod.OPTIONS, WELL_KNOWN_JMAP))
+        .action(CORS_CONTROL)
         .noCorsHeaders)
 
   private def sendRespond(session: Session, resp: HttpServerResponse) =
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 dc7895a..a1a9e15 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
@@ -99,6 +99,15 @@ class SessionRoutesTest extends AnyFlatSpec with 
BeforeAndAfter with Matchers {
       .statusCode(HttpStatus.SC_OK)
   }
 
+  "get .well-known/jmap" should "redirect" in {
+    RestAssured.`given`()
+      .basePath(".well-known/jmap")
+      .get
+    .`then`
+      .statusCode(308)
+      .header("Location", "/jmap/session")
+  }
+
   "get" should "return correct session" in {
     val sessionJson = RestAssured.`with`()
         .get
diff --git 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPRoutes.java 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPRoutes.java
index 2908216..8c91027 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPRoutes.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPRoutes.java
@@ -21,6 +21,7 @@ package org.apache.james.jmap;
 
 import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
 import static 
io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
+import static 
io.netty.handler.codec.http.HttpResponseStatus.PERMANENT_REDIRECT;
 import static io.netty.handler.codec.http.HttpResponseStatus.UNAUTHORIZED;
 
 import java.util.stream.Stream;
@@ -42,6 +43,10 @@ public interface JMAPRoutes {
             .header("Access-Control-Allow-Headers", "Content-Type, 
Authorization, Accept"));
     }
 
+    static JMAPRoute.Action redirectTo(String location) {
+        return (req, res) -> res.status(PERMANENT_REDIRECT).header("Location", 
location).send();
+    }
+
     default Mono<Void> handleInternalError(HttpServerResponse response, Logger 
logger, Throwable e) {
         logger.error("Internal server error", e);
         return response.status(INTERNAL_SERVER_ERROR).send();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to