1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.server.handlers;
20
21 import io.undertow.server.HttpHandler;
22 import io.undertow.server.HttpServerExchange;
23 import org.jboss.logging.Logger;
24
25 /**
26  * A handler which simply sets a response code.
27  *
28  * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
29  */

30 public final class ResponseCodeHandler implements HttpHandler {
31
32     private static final Logger log = Logger.getLogger(ResponseCodeHandler.class);
33     private static final boolean traceEnabled;
34
35     static {
36         traceEnabled = log.isTraceEnabled();
37     }
38
39     /**
40      * A handler which sets a 200 code. This is the default response code, so in most cases
41      * this simply has the result of finishing the request
42      */

43     public static final ResponseCodeHandler HANDLE_200 = new ResponseCodeHandler(200);
44
45     /**
46      * A handler which sets a 403 code.
47      */

48     public static final ResponseCodeHandler HANDLE_403 = new ResponseCodeHandler(403);
49     /**
50      * A handler which sets a 404 code.
51      */

52     public static final ResponseCodeHandler HANDLE_404 = new ResponseCodeHandler(404);
53     /**
54      * A handler which sets a 405 code.
55      */

56     public static final ResponseCodeHandler HANDLE_405 = new ResponseCodeHandler(405);
57     /**
58      * A handler which sets a 406 code.
59      */

60     public static final ResponseCodeHandler HANDLE_406 = new ResponseCodeHandler(406);
61     /**
62      * A handler which sets a 500 code.
63      */

64     public static final ResponseCodeHandler HANDLE_500 = new ResponseCodeHandler(500);
65
66     private final int responseCode;
67
68     /**
69      * Construct a new instance.
70      *
71      * @param responseCode the response code to set
72      */

73     public ResponseCodeHandler(final int responseCode) {
74         this.responseCode = responseCode;
75     }
76
77     @Override
78     public void handleRequest(final HttpServerExchange exchange) throws Exception {
79         exchange.setStatusCode(responseCode);
80         if(traceEnabled) {
81             log.tracef("Setting response code %s for exchange %s", responseCode, exchange);
82         }
83     }
84 }
85