|
| 1 | +/* |
| 2 | + * Copyright © 2017-2019 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cdap.http.internal; |
| 18 | + |
| 19 | +import io.netty.buffer.Unpooled; |
| 20 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 21 | +import io.netty.handler.codec.http.FullHttpResponse; |
| 22 | +import io.netty.handler.codec.http.HttpRequest; |
| 23 | +import io.netty.handler.codec.http.HttpResponse; |
| 24 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 25 | +import io.netty.handler.codec.http.HttpUtil; |
| 26 | +import io.netty.handler.codec.http.HttpVersion; |
| 27 | + |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | + |
| 30 | +/** |
| 31 | + * Exception that gets thrown when a request tries to access a secured resource |
| 32 | + * and {@link io.cdap.http.AuthHandler#isAuthenticated(HttpRequest)} doesn't accept it. |
| 33 | + */ |
| 34 | +public class AuthenticationException extends HandlerException { |
| 35 | + |
| 36 | + public AuthenticationException(String message) { |
| 37 | + super(HttpResponseStatus.UNAUTHORIZED, message); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public HttpResponse createFailureResponse() { |
| 42 | + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED, |
| 43 | + Unpooled.copiedBuffer("UNAUTHORIZED", StandardCharsets.UTF_8)); |
| 44 | + response.headers().add("WWW-Authenticate", getMessage()); |
| 45 | + HttpUtil.setContentLength(response, response.content().readableBytes()); |
| 46 | + return response; |
| 47 | + } |
| 48 | +} |
0 commit comments