re base code
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / onboarding-rest-war / src / main / java / org / openecomp / server / interceptors / EmptyOutputOutInterceptor.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.server.interceptors;
17
18 import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
19 import org.apache.cxf.message.Message;
20 import org.apache.cxf.message.MessageContentsList;
21 import org.apache.cxf.phase.Phase;
22
23 import javax.inject.Named;
24 import javax.ws.rs.core.Response;
25
26
27 /**
28  * The type Empty output out interceptor.
29  */
30 @Named
31 public class EmptyOutputOutInterceptor extends AbstractOutDatabindingInterceptor {
32
33   public EmptyOutputOutInterceptor() {
34     // To be executed in post logical phase before marshal phase
35     super(Phase.POST_LOGICAL);
36   }
37
38   /**
39    * Intercepts a message.
40    * Interceptors should NOT invoke handleMessage or handleFault
41    * on the next interceptor - the interceptor chain will
42    * take care of this.
43    *
44    * @param message input message.
45    */
46   @Override
47   public void handleMessage(Message message) {
48     //get the message
49     MessageContentsList objs = MessageContentsList.getContentsList(message);
50     if (objs.get(0) instanceof Response) {
51       //check if response is present but entity inside it is null the set a default entity
52       int status = ((Response) objs.get(0)).getStatus();
53       Object entity = ((Response) objs.get(0)).getEntity();
54       // in case of staus 200 and entity is null send InternalEmptyObject in output.
55       if (entity == null && status == 200) {
56         DefaultOutput defaultOutput = new DefaultOutput(status, new InternalEmptyObject());
57         defaultOutput.addMetadata(((Response) objs.get(0)).getMetadata());
58         objs.set(0, defaultOutput);
59       }
60     }
61   }
62 }