io.reactivex.netty.protocol.http.server.file.FileErrorResponseMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rx-netty Show documentation
Show all versions of rx-netty Show documentation
rx-netty developed by Netflix
/*
* Copyright 2014 Netflix, Inc.
*
* 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.
*/
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> {
public static final String _404_HTML_TEMPLATE =
"\n" +
"\n" +
"\n" +
" Http Error 404 \n" +
"\n" +
"\n" +
" File not found.
\n" +
"\n" +
"";
private static class ConstantErrorResponseGenerator implements ErrorResponseGenerator {
public final String template;
public ConstantErrorResponseGenerator(String template) {
this.template = template;
}
@Override
public void updateResponse(HttpServerResponse 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 call(Throwable t1) {
if (t1 instanceof HttpError) {
HttpError error = (HttpError)t1;
if (error.getStatus().equals(HttpResponseStatus.NOT_FOUND)) {
return new ConstantErrorResponseGenerator(_404_HTML_TEMPLATE);
}
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy