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 com.att.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 com.att.dmf.mr.CambriaApiException;
40 import com.att.dmf.mr.exception.DMaaPErrorMessages;
41 import com.att.dmf.mr.exception.DMaaPResponseCode;
42 import com.att.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 // 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;
84 // retrieving content length from message header
86 if (null != request.getHeader("Content-Length")) {
87 requestLength = Integer.parseInt(request.getHeader("Content-Length"));
89 // retrieving encoding from message header
90 String transferEncoding = request.getHeader("Transfer-Encoding");
91 // checking for no encoding, chunked and requestLength greater then
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);
105 chain.doFilter(req, res);
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());
119 * @see Filter#init(FilterConfig)
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;