Merge "Fix for Sonar blocker issues"
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / dmaap / util / ContentLengthInterceptor.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 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  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package com.att.nsa.dmaap.util;
23
24 import java.util.Map;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import org.apache.http.HttpStatus;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 import org.springframework.stereotype.Component;
33 import com.att.nsa.cambria.CambriaApiException;
34 import com.att.nsa.cambria.exception.DMaaPResponseCode;
35 import com.att.nsa.cambria.exception.ErrorResponse;
36 import ajsc.beans.interceptors.AjscInterceptor;
37
38 /**
39  * AJSC Intercepter implementation of ContentLengthFilter
40  */
41 @Component
42 public class ContentLengthInterceptor implements AjscInterceptor{
43
44         
45         private String defLength;
46         private static final EELFLogger log = EELFManager.getInstance().getLogger(ContentLengthInterceptor.class);
47
48
49         /**
50          * Intercepter method to intercept requests before processing
51          */
52         @Override
53         public boolean allowOrReject(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse,
54                         Map map) throws Exception {
55                                 
56                 log.info("inside Interceptor allowOrReject content length checking before pub/sub");
57                 
58                 JSONObject jsonObj = null;
59                 int requestLength = 0;
60                 setDefLength(System.getProperty("maxcontentlength"));
61                 try {
62                         // retrieving content length from message header
63
64                         if (null != httpservletrequest.getHeader("Content-Length")) {
65                                 requestLength = Integer.parseInt(httpservletrequest.getHeader("Content-Length"));
66                         }
67                         // retrieving encoding from message header
68                         String transferEncoding = httpservletrequest.getHeader("Transfer-Encoding");
69                         // checking for no encoding, chunked and requestLength greater then
70                         // default length
71                                 if (null != transferEncoding && !(transferEncoding.contains("chunked"))
72                                                 && (requestLength > Integer.parseInt(getDefLength()))) {
73                                         jsonObj = new JSONObject().append("defaultlength", getDefLength())
74                                                         .append("requestlength", requestLength);
75                                         log.error("message length is greater than default");
76                                         throw new CambriaApiException(jsonObj);
77                                 } 
78                                 else if (null == transferEncoding && (requestLength > Integer.parseInt(getDefLength()))) 
79                                 {
80                                         jsonObj = new JSONObject().append("defaultlength", getDefLength()).append(
81                                                         "requestlength", requestLength);
82                                         log.error("Request message is not chunked or request length is greater than default length");
83                                         throw new CambriaApiException(jsonObj);
84                                 
85                                 
86                                 } 
87                                 else 
88                                 {
89                                 //chain.doFilter(req, res);
90                                 return true;
91                                 }
92                         
93                 } catch (CambriaApiException | NumberFormatException | JSONException e) {
94                         
95                         log.info("Exception obj--"+e);
96                         log.error("message size is greater then default"+e.getMessage());
97                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_REQUEST_TOO_LONG,
98                                         DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(), System.getProperty("msg_size_exceeds")
99                                                         + e.toString());
100                         log.info(errRes.toString());
101                         
102                         
103                         map.put(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"test");
104                         httpservletresponse.setStatus(HttpStatus.SC_REQUEST_TOO_LONG);
105                         httpservletresponse.getOutputStream().write(errRes.toString().getBytes());
106                         return false;
107                 }
108
109                 
110                 
111         }
112
113         
114         /**
115          * Get Default Content Length
116          * @return defLength
117          */
118         public String getDefLength() {
119                 return defLength;
120         }
121         /**
122          * Set Default Content Length
123          * @param defLength
124          */
125         public void setDefLength(String defLength) {
126                 this.defLength = defLength;
127         }
128
129         
130
131 }