[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / onboarding-rest-war / src / main / java / org / openecomp / server / interceptors / EmptyOutputOutInterceptor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.server.interceptors;
22
23 import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
24 import org.apache.cxf.interceptor.Fault;
25 import org.apache.cxf.message.Message;
26 import org.apache.cxf.message.MessageContentsList;
27 import org.apache.cxf.phase.Phase;
28
29 import javax.inject.Named;
30 import javax.ws.rs.core.Response;
31
32
33 /**
34  * The type Empty output out interceptor.
35  */
36 @Named
37 public class EmptyOutputOutInterceptor extends AbstractOutDatabindingInterceptor {
38
39   public EmptyOutputOutInterceptor() {
40     // To be executed in post logical phase before marshal phase
41     super(Phase.POST_LOGICAL);
42   }
43
44   /**
45    * Intercepts a message.
46    * Interceptors should NOT invoke handleMessage or handleFault
47    * on the next interceptor - the interceptor chain will
48    * take care of this.
49    *
50    * @param message input message.
51    */
52   public void handleMessage(Message message) throws Fault {
53     //get the message
54     MessageContentsList objs = MessageContentsList.getContentsList(message);
55     if (objs.get(0) instanceof Response) {
56       //check if response is present but entity inside it is null the set a default entity
57       int status = ((Response) objs.get(0)).getStatus();
58       Object entity = ((Response) objs.get(0)).getEntity();
59       // in case of staus 200 and entity is null send InternalEmptyObject in output.
60       if (entity == null && status == 200) {
61         DefaultOutput defaultOutput = new DefaultOutput(status, new InternalEmptyObject());
62         defaultOutput.addMetadata(((Response) objs.get(0)).getMetadata());
63         objs.set(0, defaultOutput);
64       }
65     }
66   }
67 }