Skip to content

Commit b71a327

Browse files
committed
Add UserNotFoundException
Signed-off-by: sami <[email protected]>
1 parent ae1d1bf commit b71a327

File tree

8 files changed

+56
-11
lines changed

8 files changed

+56
-11
lines changed

server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/DownloadRoutes.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import org.apache.james.jmap.api.model.{Upload, UploadId, UploadNotFoundExceptio
3838
import org.apache.james.jmap.api.upload.UploadService
3939
import org.apache.james.jmap.core.Id.Id
4040
import org.apache.james.jmap.core.{AccountId, Id, ProblemDetails, SessionTranslator}
41-
import org.apache.james.jmap.exceptions.UnauthorizedException
41+
import org.apache.james.jmap.exceptions.{UnauthorizedException, UserNotFoundException}
4242
import org.apache.james.jmap.http.Authenticator
4343
import org.apache.james.jmap.http.rfc8621.InjectionKeys
4444
import org.apache.james.jmap.json.ResponseSerializer
@@ -272,6 +272,8 @@ class DownloadRoutes @Inject()(@Named(InjectionKeys.RFC_8621) val authenticator:
272272
.onErrorResume {
273273
case _: ForbiddenException | _: AccountNotFoundException =>
274274
respondDetails(response, ProblemDetails(status = FORBIDDEN, detail = "You cannot download in others accounts"))
275+
case e: UserNotFoundException =>
276+
respondDetails(e.addHeaders(response), ProblemDetails(status = NOT_FOUND, detail = e.getMessage))
275277
case e: UnauthorizedException =>
276278
LOGGER.warn("Unauthorized", e)
277279
respondDetails(e.addHeaders(response), ProblemDetails(status = UNAUTHORIZED, detail = e.getMessage))

server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/EventSourceRoutes.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import eu.timepit.refined.api.Refined
2828
import eu.timepit.refined.numeric.Positive
2929
import eu.timepit.refined.refineV
3030
import io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE
31+
import io.netty.handler.codec.http.HttpResponseStatus._
3132
import io.netty.handler.codec.http.{HttpMethod, QueryStringDecoder}
3233
import jakarta.inject.{Inject, Named}
3334
import org.apache.james.events.{EventBus, Registration, RegistrationKey}
@@ -37,7 +38,7 @@ import org.apache.james.jmap.api.change.TypeStateFactory
3738
import org.apache.james.jmap.api.model.TypeName
3839
import org.apache.james.jmap.change.{AccountIdRegistrationKey, StateChangeListener}
3940
import org.apache.james.jmap.core.{OutboundMessage, PingMessage, ProblemDetails, StateChange}
40-
import org.apache.james.jmap.exceptions.UnauthorizedException
41+
import org.apache.james.jmap.exceptions.{UnauthorizedException, UserNotFoundException}
4142
import org.apache.james.jmap.http.rfc8621.InjectionKeys
4243
import org.apache.james.jmap.http.{Authenticator, UserProvisioning}
4344
import org.apache.james.jmap.json.{PushSerializer, ResponseSerializer}
@@ -222,6 +223,7 @@ class EventSourceRoutes@Inject() (@Named(InjectionKeys.RFC_8621) val authenticat
222223
}
223224

224225
private def handleConnectionEstablishmentError(throwable: Throwable, response: HttpServerResponse): SMono[Void] = throwable match {
226+
case e: UserNotFoundException => respondDetails(e.addHeaders(response), ProblemDetails(status = NOT_FOUND, detail = e.getMessage))
225227
case e: UnauthorizedException => respondDetails(e.addHeaders(response), ProblemDetails.forThrowable(throwable))
226228
case _ => respondDetails(response, ProblemDetails.forThrowable(throwable))
227229
}

server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/JMAPApiRoutes.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import java.util.stream.Stream
2525

2626
import io.netty.handler.codec.http.HttpHeaderNames.{CONTENT_LENGTH, CONTENT_TYPE}
2727
import io.netty.handler.codec.http.HttpMethod
28-
import io.netty.handler.codec.http.HttpResponseStatus.OK
28+
import io.netty.handler.codec.http.HttpResponseStatus.{NOT_FOUND, OK}
2929
import jakarta.inject.{Inject, Named}
3030
import org.apache.james.jmap.HttpConstants.JSON_CONTENT_TYPE
3131
import org.apache.james.jmap.JMAPUrls.JMAP
3232
import org.apache.james.jmap.core.CapabilityIdentifier.CapabilityIdentifier
3333
import org.apache.james.jmap.core.{ProblemDetails, RequestObject}
34-
import org.apache.james.jmap.exceptions.UnauthorizedException
34+
import org.apache.james.jmap.exceptions.{UnauthorizedException, UserNotFoundException}
3535
import org.apache.james.jmap.http.rfc8621.InjectionKeys
3636
import org.apache.james.jmap.http.{Authenticator, UserProvisioning}
3737
import org.apache.james.jmap.json.ResponseSerializer
@@ -102,6 +102,7 @@ class JMAPApiRoutes @Inject() (@Named(InjectionKeys.RFC_8621) val authenticator:
102102
.`then`()))
103103

104104
private def handleError(throwable: Throwable, response: HttpServerResponse): SMono[Void] = throwable match {
105+
case e: UserNotFoundException => respondDetails(e.addHeaders(response), ProblemDetails(status = NOT_FOUND, detail = e.getMessage))
105106
case e: UnauthorizedException => respondDetails(e.addHeaders(response), ProblemDetails.forThrowable(throwable))
106107
case _ => respondDetails(response, ProblemDetails.forThrowable(throwable))
107108
}

server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/SessionRoutes.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ import java.nio.charset.StandardCharsets
2323
import java.util.stream.Stream
2424

2525
import io.netty.handler.codec.http.HttpHeaderNames.{CONTENT_LENGTH, CONTENT_TYPE}
26-
import io.netty.handler.codec.http.HttpResponseStatus.{BAD_REQUEST, INTERNAL_SERVER_ERROR, OK, UNAUTHORIZED}
26+
import io.netty.handler.codec.http.HttpResponseStatus.{BAD_REQUEST, INTERNAL_SERVER_ERROR, NOT_FOUND, OK, UNAUTHORIZED}
2727
import io.netty.handler.codec.http.{HttpMethod, HttpResponseStatus}
2828
import jakarta.inject.{Inject, Named}
2929
import org.apache.commons.lang3.tuple.Pair
3030
import org.apache.james.core.Username
3131
import org.apache.james.jmap.HttpConstants.{JSON_CONTENT_TYPE, JSON_CONTENT_TYPE_UTF8}
3232
import org.apache.james.jmap.JMAPRoutes.CORS_CONTROL
3333
import org.apache.james.jmap.core.{JmapRfc8621Configuration, ProblemDetails, Session, UrlPrefixes}
34-
import org.apache.james.jmap.exceptions.UnauthorizedException
34+
import org.apache.james.jmap.exceptions.{UnauthorizedException, UserNotFoundException}
3535
import org.apache.james.jmap.http.Authenticator
3636
import org.apache.james.jmap.http.rfc8621.InjectionKeys
3737
import org.apache.james.jmap.json.ResponseSerializer
@@ -110,6 +110,7 @@ class SessionRoutes @Inject()(@Named(InjectionKeys.RFC_8621) val authenticator:
110110

111111
private def errorHandling(throwable: Throwable, response: HttpServerResponse): Mono[Void] =
112112
throwable match {
113+
case e: UserNotFoundException => respondDetails(e.addHeaders(response), ProblemDetails(status = NOT_FOUND, detail = e.getMessage))
113114
case e: UnauthorizedException =>
114115
LOGGER.warn("Unauthorized", e)
115116
respondDetails(e.addHeaders(response),

server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/UploadRoutes.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import java.util.stream
2626
import java.util.stream.Stream
2727

2828
import io.netty.handler.codec.http.HttpHeaderNames.{CONTENT_LENGTH, CONTENT_TYPE}
29-
import io.netty.handler.codec.http.HttpResponseStatus.{BAD_REQUEST, CREATED, FORBIDDEN, INTERNAL_SERVER_ERROR, UNAUTHORIZED}
29+
import io.netty.handler.codec.http.HttpResponseStatus.{BAD_REQUEST, CREATED, FORBIDDEN, INTERNAL_SERVER_ERROR, NOT_FOUND, UNAUTHORIZED}
3030
import io.netty.handler.codec.http.{HttpMethod, HttpResponseStatus}
3131
import jakarta.inject.{Inject, Named}
3232
import org.apache.commons.fileupload.util.LimitedInputStream
@@ -36,7 +36,7 @@ import org.apache.james.jmap.api.model.{UploadId, UploadMetaData}
3636
import org.apache.james.jmap.api.upload.UploadService
3737
import org.apache.james.jmap.core.Id.Id
3838
import org.apache.james.jmap.core.{AccountId, Id, JmapRfc8621Configuration, ProblemDetails, SessionTranslator}
39-
import org.apache.james.jmap.exceptions.UnauthorizedException
39+
import org.apache.james.jmap.exceptions.{UnauthorizedException, UserNotFoundException}
4040
import org.apache.james.jmap.http.Authenticator
4141
import org.apache.james.jmap.http.rfc8621.InjectionKeys
4242
import org.apache.james.jmap.json.{ResponseSerializer, UploadSerializer}
@@ -89,6 +89,7 @@ class UploadRoutes @Inject()(@Named(InjectionKeys.RFC_8621) val authenticator: A
8989
authenticator.authenticate(request))
9090
.flatMap(session => post(request, response, ContentType.of(contentType), session))
9191
.onErrorResume {
92+
case e: UserNotFoundException => respondDetails(e.addHeaders(response), ProblemDetails(status = NOT_FOUND, detail = e.getMessage))
9293
case e: UnauthorizedException =>
9394
LOGGER.warn("Unauthorized", e)
9495
respondDetails(e.addHeaders(response), ProblemDetails(status = UNAUTHORIZED, detail = e.getMessage))

server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/WebSocketRoutes.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import java.util.stream
2525

2626
import io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE
2727
import io.netty.handler.codec.http.HttpMethod
28+
import io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND
2829
import io.netty.handler.codec.http.websocketx.WebSocketFrame
2930
import jakarta.inject.{Inject, Named}
3031
import org.apache.james.core.Username
@@ -35,7 +36,7 @@ import org.apache.james.jmap.api.change.{EmailChangeRepository, MailboxChangeRep
3536
import org.apache.james.jmap.api.model.{AccountId => JavaAccountId}
3637
import org.apache.james.jmap.change._
3738
import org.apache.james.jmap.core._
38-
import org.apache.james.jmap.exceptions.UnauthorizedException
39+
import org.apache.james.jmap.exceptions.{UnauthorizedException, UserNotFoundException}
3940
import org.apache.james.jmap.http.rfc8621.InjectionKeys
4041
import org.apache.james.jmap.http.{Authenticator, UserProvisioning}
4142
import org.apache.james.jmap.json.{PushSerializer, ResponseSerializer}
@@ -170,6 +171,7 @@ class WebSocketRoutes @Inject() (@Named(InjectionKeys.RFC_8621) val authenticato
170171
}
171172

172173
private def handleHttpHandshakeError(throwable: Throwable, response: HttpServerResponse): SMono[Void] = throwable match {
174+
case e: UserNotFoundException => respondDetails(e.addHeaders(response), ProblemDetails(status = NOT_FOUND, detail = e.getMessage))
173175
case e: UnauthorizedException => respondDetails(e.addHeaders(response), ProblemDetails.forThrowable(throwable))
174176
case _ => respondDetails(response, ProblemDetails.forThrowable(throwable))
175177
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/****************************************************************
2+
* Licensed to the Apache Software Foundation (ASF) under one *
3+
* or more contributor license agreements. See the NOTICE file *
4+
* distributed with this work for additional information *
5+
* regarding copyright ownership. The ASF licenses this file *
6+
* to you under the Apache License, Version 2.0 (the *
7+
* "License"); you may not use this file except in compliance *
8+
* with the License. You may obtain a copy of the License at *
9+
* *
10+
* http://www.apache.org/licenses/LICENSE-2.0 *
11+
* *
12+
* Unless required by applicable law or agreed to in writing, *
13+
* software distributed under the License is distributed on an *
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15+
* KIND, either express or implied. See the License for the *
16+
* specific language governing permissions and limitations *
17+
* under the License. *
18+
****************************************************************/
19+
package org.apache.james.jmap.exceptions;
20+
21+
import reactor.netty.http.server.HttpServerResponse;
22+
23+
public class UserNotFoundException extends RuntimeException {
24+
25+
public UserNotFoundException(String message) {
26+
super(message);
27+
}
28+
29+
public UserNotFoundException(String message, Throwable throwable) {
30+
super(message, throwable);
31+
}
32+
33+
public HttpServerResponse addHeaders(HttpServerResponse response) {
34+
return response;
35+
}
36+
}

server/protocols/jmap/src/main/java/org/apache/james/jmap/http/JWTAuthenticationStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.apache.james.core.Username;
2525
import org.apache.james.jmap.exceptions.UnauthorizedException;
26+
import org.apache.james.jmap.exceptions.UserNotFoundException;
2627
import org.apache.james.jwt.JwtTokenVerifier;
2728
import org.apache.james.mailbox.MailboxManager;
2829
import org.apache.james.mailbox.MailboxSession;
@@ -70,9 +71,8 @@ public Mono<MailboxSession> createMailboxSession(HttpServerRequest httpRequest)
7071
throw new UnauthorizedException("Invalid username", e);
7172
}
7273

73-
//TODO: Implement this behavior using an extension-jar instead
7474
if (!usersRepository.contains(username)) {
75-
throw new UnauthorizedException("User does not exist");
75+
throw new UserNotFoundException("User does not exist");
7676
}
7777

7878
return username;

0 commit comments

Comments
 (0)