Containerization feature of SO
[so.git] / common / src / main / java / org / onap / so / client / ResponseExceptionMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.StringWriter;
26 import java.util.Optional;
27
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;
38
39 import org.apache.commons.io.IOUtils;
40
41 public abstract class ResponseExceptionMapper {
42
43         public void map(Response response) {
44                 
45                 response.bufferEntity();
46                 if (response.getStatus() >= 300) {
47                         String message = "empty message";
48                         if (response.hasEntity()) {
49                                 StringWriter writer = new StringWriter();
50                                 try {
51                                         IOUtils.copy((InputStream)response.getEntity(), writer, "UTF-8");
52                                 } catch (IOException e) {
53                                         writer.append("failed to read entity stream");
54                                 }
55                                 Optional<String> result = this.extractMessage(writer.toString());
56                                 if (result.isPresent()) {
57                                         message = result.get();
58                                 }
59                         }
60                         Response.Status status = Response.Status.fromStatusCode(response.getStatus());
61                         WebApplicationException webAppException;
62                         switch (status) {
63                         case BAD_REQUEST:
64                                 webAppException = new BadRequestException(message);
65                                 break;
66                         case UNAUTHORIZED:
67                                 webAppException = new NotAuthorizedException(message);
68                                 break;
69                         case FORBIDDEN:
70                                 webAppException = new ForbiddenException(message);
71                                 break;
72                         case NOT_FOUND:
73                                 webAppException = new NotFoundException(message);
74                                 break;
75                         case METHOD_NOT_ALLOWED:
76                                 webAppException = new NotAllowedException(message);
77                                 break;
78                         case NOT_ACCEPTABLE:
79                                 webAppException = new NotAcceptableException(message);
80                                 break;
81                         case PRECONDITION_FAILED:
82                                 webAppException = new PreconditionFailedException(message);
83                                 break;
84                         case UNSUPPORTED_MEDIA_TYPE:
85                                 webAppException = new NotSupportedException(message);
86                                 break;
87                         case INTERNAL_SERVER_ERROR:
88                                 webAppException = new InternalServerErrorException(message);
89                                 break;
90                         case SERVICE_UNAVAILABLE:
91                                 webAppException = new WebApplicationException(message);
92                                 break;
93                         default:
94                                 webAppException = new WebApplicationException(message);
95                         }
96                         throw webAppException;
97                 }
98         }
99         
100         public abstract Optional<String> extractMessage(String entity);
101 }