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.onap.so.client;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.StringWriter;
26 import java.util.Optional;
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.core.Response;
39 import org.apache.commons.io.IOUtils;
41 public abstract class ResponseExceptionMapper {
43 public void map(Response response) {
45 response.bufferEntity();
46 if (response.getStatus() >= 300) {
47 String message = "empty message";
48 if (response.hasEntity()) {
49 StringWriter writer = new StringWriter();
51 IOUtils.copy((InputStream)response.getEntity(), writer, "UTF-8");
52 } catch (IOException e) {
53 writer.append("failed to read entity stream");
55 Optional<String> result = this.extractMessage(writer.toString());
56 if (result.isPresent()) {
57 message = result.get();
60 Response.Status status = Response.Status.fromStatusCode(response.getStatus());
61 WebApplicationException webAppException;
64 webAppException = new BadRequestException(message);
67 webAppException = new NotAuthorizedException(message);
70 webAppException = new ForbiddenException(message);
73 webAppException = new NotFoundException(message);
75 case METHOD_NOT_ALLOWED:
76 webAppException = new NotAllowedException(message);
79 webAppException = new NotAcceptableException(message);
81 case PRECONDITION_FAILED:
82 webAppException = new PreconditionFailedException(message);
84 case UNSUPPORTED_MEDIA_TYPE:
85 webAppException = new NotSupportedException(message);
87 case INTERNAL_SERVER_ERROR:
88 webAppException = new InternalServerErrorException(message);
90 case SERVICE_UNAVAILABLE:
91 webAppException = new WebApplicationException(message);
94 webAppException = new WebApplicationException(message);
96 throw webAppException;
100 public abstract Optional<String> extractMessage(String entity);