-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up implementation to merge the URIResolver into specific subcla…
…sses of FileRequestHandler. Remove support for cascading file sources. This may be added later. Introduce HttpError to attach an HTTP response code to exceptions
- Loading branch information
Showing
16 changed files
with
251 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
rx-netty/src/main/java/io/reactivex/netty/protocol/http/server/HttpError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.reactivex.netty.protocol.http.server; | ||
|
||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
|
||
/** | ||
* Encapsulate an exception with a specific HTTP response code | ||
* so a proper HTTP error response may be generated. | ||
* | ||
* @author elandau | ||
* | ||
*/ | ||
public class HttpError extends Exception { | ||
private final HttpResponseStatus status; | ||
|
||
public HttpError(HttpResponseStatus status, String message) { | ||
super(message); | ||
this.status = status; | ||
} | ||
public HttpError(HttpResponseStatus status, String message, Throwable t) { | ||
super(message, t); | ||
this.status = status; | ||
} | ||
public HttpError(HttpResponseStatus status, Throwable t) { | ||
super(t); | ||
this.status = status; | ||
} | ||
public HttpError(HttpResponseStatus status) { | ||
this.status = status; | ||
} | ||
public HttpResponseStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public String toString() { | ||
return "" + status.toString() + " : " + super.toString(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../src/main/java/io/reactivex/netty/protocol/http/server/RequestHandlerWithErrorMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.reactivex.netty.protocol.http.server; | ||
|
||
import rx.Observable; | ||
import rx.functions.Func1; | ||
|
||
/** | ||
* Decorator for a {@link RequestHandler} with an accompanying {@link ErrorResponseGenerator}. | ||
* | ||
* @author elandau | ||
* | ||
* @param <I> | ||
* @param <O> | ||
*/ | ||
public class RequestHandlerWithErrorMapper<I, O> implements RequestHandler<I, O> { | ||
private Func1<Throwable, ErrorResponseGenerator<O>> errorMapper; | ||
private RequestHandler<I, O> handler; | ||
|
||
public static <I, O> RequestHandlerWithErrorMapper<I, O> from(RequestHandler<I, O> handler, Func1<Throwable, ErrorResponseGenerator<O>> errorMapper) { | ||
return new RequestHandlerWithErrorMapper<I, O>(handler, errorMapper); | ||
} | ||
|
||
public RequestHandlerWithErrorMapper( | ||
RequestHandler<I, O> handler, | ||
Func1<Throwable, ErrorResponseGenerator<O>> errorMapper) { | ||
this.handler = handler; | ||
this.errorMapper = errorMapper; | ||
} | ||
|
||
@Override | ||
public Observable<Void> handle(HttpServerRequest<I> request, final HttpServerResponse<O> response) { | ||
return handler.handle(request, response) | ||
.onErrorResumeNext(new Func1<Throwable, Observable<Void>>() { | ||
@Override | ||
public Observable<Void> call(Throwable exception) { | ||
// Try to redner the error with the ErrorResponseGenerator | ||
ErrorResponseGenerator<O> generator = errorMapper.call(exception); | ||
if (generator != null) { | ||
generator.updateResponse(response, exception); | ||
return Observable.empty(); | ||
} | ||
// Defer the to default error renderer | ||
return Observable.error(exception); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...y/src/main/java/io/reactivex/netty/protocol/http/server/file/FileErrorResponseMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.reactivex.netty.protocol.http.server.file; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.handler.codec.http.HttpHeaders; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.reactivex.netty.protocol.http.server.ErrorResponseGenerator; | ||
import io.reactivex.netty.protocol.http.server.HttpError; | ||
import io.reactivex.netty.protocol.http.server.HttpServerResponse; | ||
import rx.functions.Func1; | ||
|
||
/** | ||
* Custom error mapper to render 404 errors when serving files. All other errors | ||
* are defered to the default handler | ||
* | ||
* @author elandau | ||
* | ||
*/ | ||
public class FileErrorResponseMapper implements Func1<Throwable, ErrorResponseGenerator<ByteBuf>> { | ||
|
||
public static final String _404_HTML_TEMPLATE = | ||
"<!DOCTYPE html>\n" + | ||
"<html>\n" + | ||
"<head>\n" + | ||
" <title>Http Error 404</title>\n" + | ||
"</head>\n" + | ||
"<body>\n" + | ||
" <h1>File not found.</h1>\n" + | ||
"</body>\n" + | ||
"</html>"; | ||
|
||
private static class ConstantErrorResponseGenerator<O> implements ErrorResponseGenerator<O> { | ||
public final String template; | ||
|
||
public ConstantErrorResponseGenerator(String template) { | ||
this.template = template; | ||
} | ||
|
||
@Override | ||
public void updateResponse(HttpServerResponse<O> response, Throwable t) { | ||
HttpError error = (HttpError)t; | ||
response.setStatus(error.getStatus()); | ||
response.getHeaders().set(HttpHeaders.Names.CONTENT_TYPE, "text/html"); | ||
response.writeString(template); | ||
} | ||
} | ||
|
||
@Override | ||
public ErrorResponseGenerator<ByteBuf> call(Throwable t1) { | ||
if (t1 instanceof HttpError) { | ||
HttpError error = (HttpError)t1; | ||
if (error.getStatus().equals(HttpResponseStatus.NOT_FOUND)) { | ||
return new ConstantErrorResponseGenerator<ByteBuf>(_404_HTML_TEMPLATE); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.
The INSECURE_URI pattern matches ampersand (&) which means that query parameters will not be allowed. In my case I am using the query parameters in a javascript on a static html page. Unfortunately santizeUri is static, otherwise I could have overridden it in a subclass, but now I simply can't use this feature.