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.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 org.onap.dmaap.dmf.mr.CambriaApiException;
34 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
35 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
36 import ajsc.beans.interceptors.AjscInterceptor;
39 * AJSC Intercepter implementation of ContentLengthFilter
42 public class ContentLengthInterceptor implements AjscInterceptor{
45 private String defLength;
47 private static final EELFLogger log = EELFManager.getInstance().getLogger(ContentLengthInterceptor.class);
51 * Intercepter method to intercept requests before processing
54 public boolean allowOrReject(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse,
55 Map map) throws Exception {
57 log.info("inside Interceptor allowOrReject content length checking before pub/sub");
59 JSONObject jsonObj = null;
60 int requestLength = 0;
61 setDefLength(System.getProperty("maxcontentlength"));
63 // retrieving content length from message header
65 if (null != httpservletrequest.getHeader("Content-Length")) {
66 requestLength = Integer.parseInt(httpservletrequest.getHeader("Content-Length"));
68 // retrieving encoding from message header
69 String transferEncoding = httpservletrequest.getHeader("Transfer-Encoding");
70 // checking for no encoding, chunked and requestLength greater then
72 if (null != transferEncoding && !(transferEncoding.contains("chunked"))
73 && (requestLength > Integer.parseInt(getDefLength()))) {
74 jsonObj = new JSONObject().append("defaultlength", getDefLength())
75 .append("requestlength", requestLength);
76 log.error("message length is greater than default");
77 throw new CambriaApiException(jsonObj);
79 else if (null == transferEncoding && (requestLength > Integer.parseInt(getDefLength())))
81 jsonObj = new JSONObject().append("defaultlength", getDefLength()).append(
82 "requestlength", requestLength);
83 log.error("Request message is not chunked or request length is greater than default length");
84 throw new CambriaApiException(jsonObj);
94 } catch (CambriaApiException | NumberFormatException | JSONException e) {
96 log.info("Exception obj--"+e);
97 log.error("message size is greater then default"+e.getMessage());
98 String messg=e.toString();
100 messg=jsonObj.toString();
102 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_REQUEST_TOO_LONG,
103 DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(), System.getProperty("msg_size_exceeds")
105 log.info(errRes.toString());
108 map.put(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"test");
109 httpservletresponse.setStatus(HttpStatus.SC_REQUEST_TOO_LONG);
110 if(httpservletresponse.getOutputStream()!=null){
111 httpservletresponse.getOutputStream().write(errRes.toString().getBytes());
122 * Get Default Content Length
125 public String getDefLength() {
129 * Set Default Content Length
132 public void setDefLength(String defLength) {
133 this.defLength = defLength;