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