Skip to content

Commit

Permalink
Returning "404 not found" response by throwing a NotFound exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 26, 2016
1 parent 7e5c9f6 commit f5b8a32
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 5 deletions.
1 change: 1 addition & 0 deletions rapidoid-commons/src/main/resources/rapidoid-classes.txt
Expand Up @@ -369,6 +369,7 @@ org.rapidoid.http.impl.RespImpl
org.rapidoid.http.impl.ResponseRenderer
org.rapidoid.http.impl.RouteImpl
org.rapidoid.http.impl.RouteOptions
org.rapidoid.http.NotFound
org.rapidoid.http.processor.AbstractHttpProcessor
org.rapidoid.http.processor.HttpProcessor
org.rapidoid.http.processor.LoggingHttpProcessor
Expand Down
18 changes: 14 additions & 4 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/FastHttp.java
Expand Up @@ -186,7 +186,7 @@ public void onRequest(Channel channel, boolean isGet, boolean isKeepAlive, BufRa

try {
if (handler != null) {
status = handler.handle(channel, isKeepAlive, req, null);
status = handleIfFound(channel, isKeepAlive, handler, req);
}

if (status == HttpStatus.NOT_FOUND) {
Expand All @@ -206,6 +206,14 @@ public void onRequest(Channel channel, boolean isGet, boolean isKeepAlive, BufRa
}
}

private HttpStatus handleIfFound(Channel channel, boolean isKeepAlive, HttpHandler handler, ReqImpl req) {
try {
return handler.handle(channel, isKeepAlive, req, null);
} catch (NotFound nf) {
return HttpStatus.NOT_FOUND;
}
}

private boolean handleError(Channel channel, boolean isKeepAlive, ReqImpl req, MediaType contentType, Throwable e) {
if (req != null) {
if (!req.isStopped()) {
Expand Down Expand Up @@ -256,7 +264,7 @@ private HttpStatus tryGenericHandlers(Channel channel, boolean isKeepAlive, ReqI
req.routes(routes);

for (HttpHandler handler : routes.genericHandlers()) {
HttpStatus status = handler.handle(channel, isKeepAlive, req, null);
HttpStatus status = handleIfFound(channel, isKeepAlive, handler, req);

if (status != HttpStatus.NOT_FOUND) {
return status;
Expand Down Expand Up @@ -290,10 +298,12 @@ public void notFound(Channel ctx, boolean isKeepAlive, HttpHandler fromHandler,
// a generic handler returned "not found" -> go to the next one
if (i < count - 1) {
// trying with different routes
((ReqImpl) req).routes(route);
ReqImpl reqi = (ReqImpl) req;
reqi.routes(route);

HttpHandler nextHandler = genericHandlers.get(i + 1);
status = nextHandler.handle(ctx, isKeepAlive, req, null);
status = handleIfFound(ctx, isKeepAlive, nextHandler, reqi);

break tryRoutes;
}
}
Expand Down
29 changes: 29 additions & 0 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/NotFound.java
@@ -0,0 +1,29 @@
package org.rapidoid.http;

/*
* #%L
* rapidoid-http-fast
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;

@Authors("Nikolche Mihajlovski")
@Since("5.2.0")
public class NotFound extends RuntimeException {
}
Expand Up @@ -269,7 +269,7 @@ private Object handleError(Req req, Throwable e) {

public void complete(Channel ctx, boolean isKeepAlive, Req req, Object result) {

if (result == null) {
if (result == null || result instanceof NotFound) {
http.notFound(ctx, isKeepAlive, this, req);
return; // not found
}
Expand Down
@@ -0,0 +1,63 @@
package org.rapidoid.http;

/*
* #%L
* rapidoid-integration-tests
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.setup.On;

@Authors("Nikolche Mihajlovski")
@Since("5.2.0")
public class HttpNotFoundTest extends IntegrationTestCommons {

@Test
public void testThrowingNotFound() {
On.get("/foo").json(req -> {
if (req.params().isEmpty()) {
throw new NotFound();
}

return req.params();
});

notFound("/foo");

onlyGet("/foo?x=1");
}

@Test
public void testReturningNullAsNotFound() {
On.post("/bar").json(req -> {
if (req.params().isEmpty()) {
return null;
}

return req.params();
});

notFound("/bar");

onlyPost("/bar?y=abc");
}

}

@@ -0,0 +1,8 @@
HTTP/1.1 200 OK
Connection: keep-alive
Server: Rapidoid
Date: XXXXX GMT
Content-Type: application/json; charset=utf-8
Content-Length: 11

{"y":"abc"}
@@ -0,0 +1,8 @@
HTTP/1.1 200 OK
Connection: keep-alive
Server: Rapidoid
Date: XXXXX GMT
Content-Type: application/json; charset=utf-8
Content-Length: 9

{"x":"1"}

0 comments on commit f5b8a32

Please sign in to comment.