001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.devtools.remote.server;
018
019import java.io.IOException;
020
021import org.springframework.http.HttpStatus;
022import org.springframework.http.server.ServerHttpRequest;
023import org.springframework.http.server.ServerHttpResponse;
024import org.springframework.util.Assert;
025
026/**
027 * {@link Handler} that responds with a specific {@link HttpStatus}.
028 *
029 * @author Phillip Webb
030 */
031public class HttpStatusHandler implements Handler {
032
033        private final HttpStatus status;
034
035        /**
036         * Create a new {@link HttpStatusHandler} instance that will respond with a HTTP OK
037         * 200 status.
038         */
039        public HttpStatusHandler() {
040                this(HttpStatus.OK);
041        }
042
043        /**
044         * Create a new {@link HttpStatusHandler} instance that will respond with the
045         * specified status.
046         * @param status the status
047         */
048        public HttpStatusHandler(HttpStatus status) {
049                Assert.notNull(status, "Status must not be null");
050                this.status = status;
051        }
052
053        @Override
054        public void handle(ServerHttpRequest request, ServerHttpResponse response)
055                        throws IOException {
056                response.setStatusCode(this.status);
057        }
058
059}