Skip to content

Commit 96a3148

Browse files
author
Robert
committed
adding exceptions
1 parent b1d4548 commit 96a3148

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 resource that requires roles
32+
* and {@link io.cdap.http.AuthHandler#hasRoles(HttpRequest, String[])} doesn't accept it.
33+
*/
34+
public class AuthorizationException extends HandlerException {
35+
36+
public AuthorizationException() {
37+
super(HttpResponseStatus.UNAUTHORIZED, "Request doesn't satisfy role requirement.");
38+
}
39+
40+
@Override
41+
public HttpResponse createFailureResponse() {
42+
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN,
43+
Unpooled.copiedBuffer("FORBIDDEN", StandardCharsets.UTF_8));
44+
HttpUtil.setContentLength(response, response.content().readableBytes());
45+
return response;
46+
}
47+
}

0 commit comments

Comments
 (0)