7f90e8005d2ce447e491b040360d147f00e3fecb
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / messagerouter / msgrtr / nsa / filter / ContentLengthFilter.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 org.onap.dmaap.messagerouter.msgrtr.nsa.filter;
23
24 import java.io.IOException;
25
26 import javax.servlet.Filter;
27 import javax.servlet.FilterChain;
28 import javax.servlet.FilterConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
33
34 import org.apache.http.HttpStatus;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.json.JSONObject;
38 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.CambriaApiException;
39 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.exception.DMaaPErrorMessages;
40 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.exception.DMaaPResponseCode;
41 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.exception.ErrorResponse;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.web.context.support.WebApplicationContextUtils;
44
45 /**
46  * Servlet Filter implementation class ContentLengthFilter
47  */
48 public class ContentLengthFilter implements Filter {
49
50         private DefaultLength defaultLength;
51
52         private FilterConfig filterConfig = null;
53         DMaaPErrorMessages errorMessages = null;
54         //private Logger log = Logger.getLogger(ContentLengthFilter.class.toString());
55         private static final EELFLogger log = EELFManager.getInstance().getLogger(ContentLengthFilter.class);
56         /**
57          * Default constructor.
58          */
59
60         public ContentLengthFilter() {
61                 // TODO Auto-generated constructor stub
62         }
63
64         /**
65          * @see Filter#destroy()
66          */
67         public void destroy() {
68                 // TODO Auto-generated method stub
69         }
70
71         /**
72          * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
73          */
74         public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
75                         ServletException {
76                 // TODO Auto-generated method stub
77                 // place your code here
78                 log.info("inside servlet do filter content length checking before pub/sub");
79                 HttpServletRequest request = (HttpServletRequest) req;
80                 JSONObject jsonObj = null;
81                 int requestLength = 0;
82                 try {
83                         // retrieving content length from message header
84
85                         if (null != request.getHeader("Content-Length")) {
86                                 requestLength = Integer.parseInt(request.getHeader("Content-Length"));
87                         }
88                         // retrieving encoding from message header
89                         String transferEncoding = request.getHeader("Transfer-Encoding");
90                         // checking for no encoding, chunked and requestLength greater then
91                         // default length
92                         if (null != transferEncoding && !(transferEncoding.contains("chunked"))
93                                         && (requestLength > Integer.parseInt(defaultLength.getDefaultLength()))) {
94                                 jsonObj = new JSONObject().append("defaultlength", defaultLength)
95                                                 .append("requestlength", requestLength);
96                                 log.error("message length is greater than default");
97                                 throw new CambriaApiException(jsonObj);
98                         } else if (null == transferEncoding && (requestLength > Integer.parseInt(defaultLength.getDefaultLength()))) {
99                                 jsonObj = new JSONObject().append("defaultlength", defaultLength.getDefaultLength()).append(
100                                                 "requestlength", requestLength);
101                                 log.error("Request message is not chunked or request length is greater than default length");
102                                 throw new CambriaApiException(jsonObj);
103                         } else {
104                                 chain.doFilter(req, res);
105                         }
106                 } catch (CambriaApiException | NumberFormatException e) {
107                         log.error("message size is greater then default");
108                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_EXPECTATION_FAILED,
109                                         DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(), errorMessages.getMsgSizeExceeds()
110                                                         + jsonObj.toString());
111                         log.info(errRes.toString());
112                         // throw new CambriaApiException(errRes);
113                 }
114
115         }
116
117         /**
118          * @see Filter#init(FilterConfig)
119          */
120         public void init(FilterConfig fConfig) throws ServletException {
121                 // TODO Auto-generated method stub
122                 this.filterConfig = fConfig;
123                 log.info("Filter Content Length Initialize");
124                 ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(fConfig
125                                 .getServletContext());
126                 DefaultLength defLength = (DefaultLength) ctx.getBean("defLength");
127                 DMaaPErrorMessages errorMessages = (DMaaPErrorMessages) ctx.getBean("DMaaPErrorMessages");
128                 this.errorMessages = errorMessages;
129                 this.defaultLength = defLength;
130
131         }
132
133 }