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.ws.rs.BadRequestException;
28 import javax.ws.rs.ForbiddenException;
29 import javax.ws.rs.InternalServerErrorException;
30 import javax.ws.rs.NotAcceptableException;
31 import javax.ws.rs.NotAllowedException;
32 import javax.ws.rs.NotAuthorizedException;
33 import javax.ws.rs.NotFoundException;
34 import javax.ws.rs.NotSupportedException;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.client.ClientRequestContext;
37 import javax.ws.rs.client.ClientResponseContext;
38 import javax.ws.rs.client.ClientResponseFilter;
39 import javax.ws.rs.core.Response;
41 public abstract class ResponseExceptionMapper implements ClientResponseFilter {
44 public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
45 if (responseContext.getStatus() >= 300) {
46 String message = "empty message";
47 if (responseContext.hasEntity()) {
48 Optional<String> result = this.extractMessage(responseContext.getEntityStream());
49 if (result.isPresent()) {
50 message = result.get();
53 Response.Status status = Response.Status.fromStatusCode(responseContext.getStatus());
54 WebApplicationException webAppException;
57 webAppException = new BadRequestException(message);
60 webAppException = new NotAuthorizedException(message);
63 webAppException = new ForbiddenException(message);
66 webAppException = new NotFoundException(message);
68 case METHOD_NOT_ALLOWED:
69 webAppException = new NotAllowedException(message);
72 webAppException = new NotAcceptableException(message);
74 case PRECONDITION_FAILED:
75 webAppException = new PreconditionFailedException(message);
77 case UNSUPPORTED_MEDIA_TYPE:
78 webAppException = new NotSupportedException(message);
80 case INTERNAL_SERVER_ERROR:
81 webAppException = new InternalServerErrorException(message);
83 case SERVICE_UNAVAILABLE:
84 webAppException = new WebApplicationException(message);
87 webAppException = new WebApplicationException(message);
89 throw webAppException;
93 public abstract Optional<String> extractMessage(InputStream stream) throws IOException;