e0e3e936beb1e95f517f8496d09be696337127da
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / 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.openecomp.mso.client;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Optional;
26
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;
42
43 @Provider
44 @Priority(value = 1)
45 public abstract class ResponseExceptionMapper implements ClientResponseFilter {
46
47         @Override
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();
55                                 }
56                         }
57                         Response.Status status = Response.Status.fromStatusCode(responseContext.getStatus());
58                         WebApplicationException webAppException;
59                         switch (status) {
60                         case BAD_REQUEST:
61                                 webAppException = new BadRequestException(message);
62                                 break;
63                         case UNAUTHORIZED:
64                                 webAppException = new NotAuthorizedException(message);
65                                 break;
66                         case FORBIDDEN:
67                                 webAppException = new ForbiddenException(message);
68                                 break;
69                         case NOT_FOUND:
70                                 webAppException = new NotFoundException(message);
71                                 break;
72                         case METHOD_NOT_ALLOWED:
73                                 webAppException = new NotAllowedException(message);
74                                 break;
75                         case NOT_ACCEPTABLE:
76                                 webAppException = new NotAcceptableException(message);
77                                 break;
78                         case PRECONDITION_FAILED:
79                                 webAppException = new PreconditionFailedException(message);
80                                 break;
81                         case UNSUPPORTED_MEDIA_TYPE:
82                                 webAppException = new NotSupportedException(message);
83                                 break;
84                         case INTERNAL_SERVER_ERROR:
85                                 webAppException = new InternalServerErrorException(message);
86                                 break;
87                         case SERVICE_UNAVAILABLE:
88                                 webAppException = new WebApplicationException(message);
89                                 break;
90                         default:
91                                 webAppException = new WebApplicationException(message);
92                         }
93                         throw webAppException;
94                 }
95         }
96         
97         public abstract Optional<String> extractMessage(InputStream stream) throws IOException;
98 }