Add new License file
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / dmaap / util / ContentLengthInterceptor.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 com.att.nsa.dmaap.util;
23
24 import java.util.Map;
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;
37
38 /**
39  * AJSC Intercepter implementation of ContentLengthFilter
40  */
41 @Component
42 public class ContentLengthInterceptor implements AjscInterceptor{
43
44         
45         private String defLength;
46         //private Logger log = Logger.getLogger(ContentLengthInterceptor.class.toString());
47         private static final EELFLogger log = EELFManager.getInstance().getLogger(ContentLengthInterceptor.class);
48
49
50         /**
51          * Intercepter method to intercept requests before processing
52          */
53         @Override
54         public boolean allowOrReject(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse,
55                         Map map) throws Exception {
56                                 
57                 log.info("inside Interceptor allowOrReject content length checking before pub/sub");
58                 
59                 JSONObject jsonObj = null;
60                 int requestLength = 0;
61                 setDefLength(System.getProperty("maxcontentlength"));
62                 try {
63                         // retrieving content length from message header
64
65                         if (null != httpservletrequest.getHeader("Content-Length")) {
66                                 requestLength = Integer.parseInt(httpservletrequest.getHeader("Content-Length"));
67                         }
68                         // retrieving encoding from message header
69                         String transferEncoding = httpservletrequest.getHeader("Transfer-Encoding");
70                         // checking for no encoding, chunked and requestLength greater then
71                         // default length
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);
78                                 } 
79                                 else if (null == transferEncoding && (requestLength > Integer.parseInt(getDefLength()))) 
80                                 {
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);
85                                 
86                                 
87                                 } 
88                                 else 
89                                 {
90                                 //chain.doFilter(req, res);
91                                 return true;
92                                 }
93                         
94                 } catch (CambriaApiException | NumberFormatException | JSONException e) {
95                         
96                         log.info("Exception obj--"+e);
97                         log.error("message size is greater then default"+e.getMessage());
98                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_REQUEST_TOO_LONG,
99                                         DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(), System.getProperty("msg_size_exceeds")
100                                                         + jsonObj.toString());
101                         log.info(errRes.toString());
102                         
103                         
104                         map.put(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"test");
105                         httpservletresponse.setStatus(HttpStatus.SC_REQUEST_TOO_LONG);
106                         httpservletresponse.getOutputStream().write(errRes.toString().getBytes());
107                         return false;
108                 }
109
110                 
111                 
112         }
113
114         
115         /**
116          * Get Default Content Length
117          * @return defLength
118          */
119         public String getDefLength() {
120                 return defLength;
121         }
122         /**
123          * Set Default Content Length
124          * @param defLength
125          */
126         public void setDefLength(String defLength) {
127                 this.defLength = defLength;
128         }
129
130         
131
132 }