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.nsa.dmaap.util;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import org.apache.http.HttpStatus;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 import org.springframework.stereotype.Component;
33 import com.att.nsa.cambria.CambriaApiException;
34 import com.att.nsa.cambria.exception.DMaaPResponseCode;
35 import com.att.nsa.cambria.exception.ErrorResponse;
36 import ajsc.beans.interceptors.AjscInterceptor;
39 * AJSC Intercepter implementation of ContentLengthFilter
42 public class ContentLengthInterceptor implements AjscInterceptor{
45 private String defLength;
46 private static final EELFLogger log = EELFManager.getInstance().getLogger(ContentLengthInterceptor.class);
50 * Intercepter method to intercept requests before processing
53 public boolean allowOrReject(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse,
54 Map map) throws Exception {
56 log.info("inside Interceptor allowOrReject content length checking before pub/sub");
58 JSONObject jsonObj = null;
59 int requestLength = 0;
60 setDefLength(System.getProperty("maxcontentlength"));
62 // retrieving content length from message header
64 if (null != httpservletrequest.getHeader("Content-Length")) {
65 requestLength = Integer.parseInt(httpservletrequest.getHeader("Content-Length"));
67 // retrieving encoding from message header
68 String transferEncoding = httpservletrequest.getHeader("Transfer-Encoding");
69 // checking for no encoding, chunked and requestLength greater then
71 if (null != transferEncoding && !(transferEncoding.contains("chunked"))
72 && (requestLength > Integer.parseInt(getDefLength()))) {
73 jsonObj = new JSONObject().append("defaultlength", getDefLength())
74 .append("requestlength", requestLength);
75 log.error("message length is greater than default");
76 throw new CambriaApiException(jsonObj);
78 else if (null == transferEncoding && (requestLength > Integer.parseInt(getDefLength())))
80 jsonObj = new JSONObject().append("defaultlength", getDefLength()).append(
81 "requestlength", requestLength);
82 log.error("Request message is not chunked or request length is greater than default length");
83 throw new CambriaApiException(jsonObj);
89 //chain.doFilter(req, res);
93 } catch (CambriaApiException | NumberFormatException | JSONException e) {
95 log.info("Exception obj--"+e);
96 log.error("message size is greater then default"+e.getMessage());
97 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_REQUEST_TOO_LONG,
98 DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(), System.getProperty("msg_size_exceeds")
100 log.info(errRes.toString());
103 map.put(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"test");
104 httpservletresponse.setStatus(HttpStatus.SC_REQUEST_TOO_LONG);
105 httpservletresponse.getOutputStream().write(errRes.toString().getBytes());
115 * Get Default Content Length
118 public String getDefLength() {
122 * Set Default Content Length
125 public void setDefLength(String defLength) {
126 this.defLength = defLength;