2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.client;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Optional;
27 import javax.annotation.Priority;
28 import javax.ws.rs.BadRequestException;
29 import javax.ws.rs.ForbiddenException;
30 import javax.ws.rs.InternalServerErrorException;
31 import javax.ws.rs.NotAcceptableException;
32 import javax.ws.rs.NotAllowedException;
33 import javax.ws.rs.NotAuthorizedException;
34 import javax.ws.rs.NotFoundException;
35 import javax.ws.rs.NotSupportedException;
36 import javax.ws.rs.WebApplicationException;
37 import javax.ws.rs.client.ClientRequestContext;
38 import javax.ws.rs.client.ClientResponseContext;
39 import javax.ws.rs.client.ClientResponseFilter;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.ext.Provider;
45 public abstract class ResponseExceptionMapper implements ClientResponseFilter {
48 public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
49 if (responseContext.getStatus() >= 300) {
50 String message = "empty message";
51 if (responseContext.hasEntity()) {
52 Optional<String> result = this.extractMessage(responseContext.getEntityStream());
53 if (result.isPresent()) {
54 message = result.get();
57 Response.Status status = Response.Status.fromStatusCode(responseContext.getStatus());
58 WebApplicationException webAppException;
61 webAppException = new BadRequestException(message);
64 webAppException = new NotAuthorizedException(message);
67 webAppException = new ForbiddenException(message);
70 webAppException = new NotFoundException(message);
72 case METHOD_NOT_ALLOWED:
73 webAppException = new NotAllowedException(message);
76 webAppException = new NotAcceptableException(message);
78 case PRECONDITION_FAILED:
79 webAppException = new PreconditionFailedException(message);
81 case UNSUPPORTED_MEDIA_TYPE:
82 webAppException = new NotSupportedException(message);
84 case INTERNAL_SERVER_ERROR:
85 webAppException = new InternalServerErrorException(message);
87 case SERVICE_UNAVAILABLE:
88 webAppException = new WebApplicationException(message);
91 webAppException = new WebApplicationException(message);
93 throw webAppException;
97 public abstract Optional<String> extractMessage(InputStream stream) throws IOException;