1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.filter;
24 import java.io.IOException;
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;
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;
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;
47 * Servlet Filter implementation class ContentLengthFilter
49 public class ContentLengthFilter implements Filter {
51 private DefaultLength defaultLength;
53 private FilterConfig filterConfig = null;
54 DMaaPErrorMessages errorMessages = null;
56 private static final EELFLogger log = EELFManager.getInstance().getLogger(ContentLengthFilter.class);
58 * Default constructor.
61 public ContentLengthFilter() {
62 // TODO Auto-generated constructor stub
66 * @see Filter#destroy()
68 public void destroy() {
69 // TODO Auto-generated method stub
73 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
75 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
77 log.info("inside servlet do filter content length checking before pub/sub");
78 HttpServletRequest request = (HttpServletRequest) req;
79 JSONObject jsonObj = null;
80 int requestLength = 0;
82 // retrieving content length from message header
84 if (null != request.getHeader("Content-Length")) {
85 requestLength = Integer.parseInt(request.getHeader("Content-Length"));
87 // retrieving encoding from message header
88 String transferEncoding = request.getHeader("Transfer-Encoding");
89 // checking for no encoding, chunked and requestLength greater then
91 if (null != transferEncoding && !(transferEncoding.contains("chunked"))
92 && (requestLength > Integer.parseInt(defaultLength.getDefaultLength()))) {
93 jsonObj = new JSONObject().append("defaultlength", defaultLength)
94 .append("requestlength", requestLength);
95 log.error("message length is greater than default");
96 throw new CambriaApiException(jsonObj);
97 } else if (null == transferEncoding && (requestLength > Integer.parseInt(defaultLength.getDefaultLength()))) {
98 jsonObj = new JSONObject().append("defaultlength", defaultLength.getDefaultLength()).append(
99 "requestlength", requestLength);
100 log.error("Request message is not chunked or request length is greater than default length");
101 throw new CambriaApiException(jsonObj);
103 chain.doFilter(req, res);
105 } catch (CambriaApiException | NumberFormatException e) {
106 log.error("message size is greater then default", e);
107 if (jsonObj != null) {
108 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_EXPECTATION_FAILED,
109 DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(),
110 errorMessages.getMsgSizeExceeds()
111 + jsonObj.toString());
112 log.info(errRes.toString());
119 * @see Filter#init(FilterConfig)
121 public void init(FilterConfig fConfig) throws ServletException {
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 errMessages = (DMaaPErrorMessages) ctx.getBean("DMaaPErrorMessages");
128 this.errorMessages = errMessages;
129 this.defaultLength = defLength;