AT&T 1712 and 1802 release code
[so.git] / common / 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.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;
40
41 public abstract class ResponseExceptionMapper implements ClientResponseFilter {
42
43         @Override
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();
51                                 }
52                         }
53                         Response.Status status = Response.Status.fromStatusCode(responseContext.getStatus());
54                         WebApplicationException webAppException;
55                         switch (status) {
56                         case BAD_REQUEST:
57                                 webAppException = new BadRequestException(message);
58                                 break;
59                         case UNAUTHORIZED:
60                                 webAppException = new NotAuthorizedException(message);
61                                 break;
62                         case FORBIDDEN:
63                                 webAppException = new ForbiddenException(message);
64                                 break;
65                         case NOT_FOUND:
66                                 webAppException = new NotFoundException(message);
67                                 break;
68                         case METHOD_NOT_ALLOWED:
69                                 webAppException = new NotAllowedException(message);
70                                 break;
71                         case NOT_ACCEPTABLE:
72                                 webAppException = new NotAcceptableException(message);
73                                 break;
74                         case PRECONDITION_FAILED:
75                                 webAppException = new PreconditionFailedException(message);
76                                 break;
77                         case UNSUPPORTED_MEDIA_TYPE:
78                                 webAppException = new NotSupportedException(message);
79                                 break;
80                         case INTERNAL_SERVER_ERROR:
81                                 webAppException = new InternalServerErrorException(message);
82                                 break;
83                         case SERVICE_UNAVAILABLE:
84                                 webAppException = new WebApplicationException(message);
85                                 break;
86                         default:
87                                 webAppException = new WebApplicationException(message);
88                         }
89                         throw webAppException;
90                 }
91         }
92         
93         public abstract Optional<String> extractMessage(InputStream stream) throws IOException;
94 }