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