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 749c65c874277018877d31851f3738e1f3e045db Author: ducnv <[email protected]> AuthorDate: Thu Apr 9 15:37:20 2020 +0700 JAMES-2888: add handle IllegalAgrumentsEception and test --- .../jmap/draft/exceptions/BadRequestException.java | 30 ---------------------- .../apache/james/jmap/routes/JMAPApiRoutes.scala | 18 ++++++++++--- .../james/jmap/routes/JMAPApiRoutesTest.scala | 21 +++++++++++++++ 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/exceptions/BadRequestException.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/exceptions/BadRequestException.java deleted file mode 100644 index 172c8ae..0000000 --- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/exceptions/BadRequestException.java +++ /dev/null @@ -1,30 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.jmap.draft.exceptions; - -public class BadRequestException extends RuntimeException { - - public BadRequestException(String message) { - super(message); - } - - public BadRequestException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala index 8336487..7637fa1 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala @@ -27,6 +27,7 @@ import eu.timepit.refined.auto._ 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 org.apache.http.HttpStatus.SC_BAD_REQUEST import org.apache.james.jmap.HttpConstants.JSON_CONTENT_TYPE import org.apache.james.jmap.JMAPUrls.JMAP import org.apache.james.jmap.json.Serializer @@ -42,8 +43,6 @@ import reactor.core.scheduler.Schedulers import reactor.netty.http.server.{HttpServerRequest, HttpServerResponse} class JMAPApiRoutes extends JMAPRoutes { - override def logger(): Logger = LoggerFactory.getLogger(getClass) - private val coreEcho = new CoreEcho override def routes(): stream.Stream[JMAPRoute] = Stream.of( @@ -59,7 +58,7 @@ class JMAPApiRoutes extends JMAPRoutes { private def post(httpServerRequest: HttpServerRequest, httpServerResponse: HttpServerResponse): Mono[Void] = this.requestAsJsonStream(httpServerRequest) .flatMap(requestObject => this.process(requestObject, httpServerResponse)) - .onErrorResume(throwable => SMono.fromPublisher(handleInternalError(httpServerResponse, throwable))) + .onErrorResume(throwable => handleError(throwable, httpServerResponse)) .subscribeOn(Schedulers.elastic) .asJava() .`then`() @@ -75,7 +74,7 @@ class JMAPApiRoutes extends JMAPRoutes { private def parseRequestObject(inputStream: InputStream): SMono[RequestObject] = new Serializer().deserializeRequestObject(inputStream) match { case JsSuccess(requestObject, _) => SMono.just(requestObject) - case JsError(errors) => SMono.raiseError(new RuntimeException(errors.toString())) + case JsError(_) => SMono.raiseError(new IllegalArgumentException("Invalid RequestObject")) } private def process(requestObject: RequestObject, httpServerResponse: HttpServerResponse): SMono[Void] = @@ -101,4 +100,15 @@ class JMAPApiRoutes extends JMAPRoutes { Arguments(Json.obj("type" -> "Not implemented")), invocation.methodCallId)) } + + private def handleError(throwable: Throwable, httpServerResponse: HttpServerResponse): SMono[Void] = { + if (throwable.isInstanceOf[IllegalArgumentException]) { + return SMono.fromPublisher(httpServerResponse.status(SC_BAD_REQUEST) + .header(CONTENT_TYPE, JSON_CONTENT_TYPE) + .sendString(SMono.fromCallable(() => throwable.getMessage), StandardCharsets.UTF_8) + .`then`()) + } + + SMono.fromPublisher(handleInternalError(httpServerResponse, throwable)) + } } diff --git a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala index 55bf125..b4277ed 100644 --- a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala +++ b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/routes/JMAPApiRoutesTest.scala @@ -56,6 +56,16 @@ class JMAPApiRoutesTest extends AnyFlatSpec with BeforeAndAfter with Matchers { private val RESPONSE_OBJECT: String = new Serializer().serialize(responseObject1).toString() private val RESPONSE_OBJECT_WITH_UNSUPPORTED_METHOD: String = new Serializer().serialize(responseObjectWithUnsupportedMethod).toString() + private val test: String = """ + |{ + | "using": [ "urn:ietf:params:jmap:core"], + | "methodCalls": { + | "arg1": "arg1data", + | "arg2": "arg2data" + | } + |} + |""".stripMargin + var jmapServer: JMAPServer = _ before { @@ -149,4 +159,15 @@ class JMAPApiRoutesTest extends AnyFlatSpec with BeforeAndAfter with Matchers { .then .statusCode(HttpStatus.SC_NOT_FOUND) } + + "RFC-8621 version, POST, with wrong requestObject body" should "return 400 status" in { + RestAssured + .`given`() + .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER) + .body(test) + .when() + .post + .then + .statusCode(HttpStatus.SC_BAD_REQUEST) + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
