Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * 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.undertow.server.handlers.error;
 
 import java.io.File;
 import java.util.Set;
 
Handler that serves up a file from disk to serve as an error page.

This handler does not server up and response codes by default, you must configure the response codes it responds to.

Author(s):
Stuart Douglas
 
 public class FileErrorPageHandler implements HttpHandler {
 
     private static final Logger log = Logger.getLogger("io.undertow.server.error.file");
     private volatile HttpHandler next = .;

    
The response codes that this handler will handle. If this is empty then this handler will have no effect.
 
     private volatile Set<IntegerresponseCodes;
 
     private volatile File file;
 
     public FileErrorPageHandler(final File filefinal Integer... responseCodes) {
         this. = file;
         this. = new HashSet<Integer>(Arrays.asList(responseCodes));
     }
 
     @Override
     public void handleRequest(final HttpServerExchange exchangethrows Exception {
         exchange.addDefaultResponseListener(new DefaultResponseListener() {
             @Override
             public boolean handleDefaultResponse(final HttpServerExchange exchange) {
                 Set<Integercodes = ;
                 if (!exchange.isResponseStarted() && codes.contains(exchange.getResponseCode())) {
                     serveFile(exchange);
                     return true;
                 }
                 return false;
             }
         });
 
         .handleRequest(exchange);
     }
 
     private void serveFile(final HttpServerExchange exchange) {
         exchange.dispatch(new Runnable() {
             @Override
             public void run() {
                 final FileChannel fileChannel;
                 try {
                     try {
                         fileChannel = exchange.getConnection().getWorker().getXnio().openFile(.);
                     } catch (FileNotFoundException e) {
                         ..ioException(e);
                         exchange.endExchange();
                         return;
                     }
                 } catch (IOException e) {
                    ..ioException(e);
                    exchange.endExchange();
                    return;
                }
                exchange.getResponseHeaders().put(..length());
                final StreamSinkChannel response = exchange.getResponseChannel();
                exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {
                    @Override
                    public void exchangeEvent(HttpServerExchange exchangeNextListener nextListener) {
                        IoUtils.safeClose(fileChannel);
                        nextListener.proceed();
                    }
                });
                try {
                    .tracef("Serving file %s (blocking)"fileChannel);
                    Channels.transferBlocking(responsefileChannel, 0, .length());
                    .tracef("Finished serving %s, shutting down (blocking)"fileChannel);
                    response.shutdownWrites();
                    .tracef("Finished serving %s, flushing (blocking)"fileChannel);
                    Channels.flushBlocking(response);
                    .tracef("Finished serving %s (complete)"fileChannel);
                    exchange.endExchange();
                } catch (IOException ignored) {
                    .tracef("Failed to serve %s: %s"fileChannelignored);
                    exchange.endExchange();
                    IoUtils.safeClose(response);
                } finally {
                    IoUtils.safeClose(fileChannel);
                }
            }
        });
    }
    public HttpHandler getNext() {
        return ;
    }
    public FileErrorPageHandler setNext(final HttpHandler next) {
        Handlers.handlerNotNull(next);
        this. = next;
        return this;
    }
    public Set<IntegergetResponseCodes() {
        return Collections.unmodifiableSet();
    }
    public FileErrorPageHandler setResponseCodes(final Set<IntegerresponseCodes) {
        if (responseCodes == null) {
            this. = Collections.emptySet();
        } else {
            this. = new HashSet<Integer>(responseCodes);
        }
        return this;
    }
    public FileErrorPageHandler setResponseCodes(final Integer... responseCodes) {
        this. = new HashSet<Integer>(Arrays.asList(responseCodes));
        return this;
    }
    public File getFile() {
        return ;
    }
    public FileErrorPageHandler setFile(final File file) {
        this. = file;
        return this;
    }
New to GrepCode? Check out our FAQ X