update the package name
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / 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 org.onap.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 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;
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                         String messg=e.toString();
99                         if(jsonObj!=null){
100                          messg=jsonObj.toString();
101                         }
102                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_REQUEST_TOO_LONG,
103                                         DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(), System.getProperty("msg_size_exceeds")
104                                                         + messg);
105                         log.info(errRes.toString());
106                         
107                         
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());
112                         }
113                         return false;
114                 }
115
116                 
117                 
118         }
119
120         
121         /**
122          * Get Default Content Length
123          * @return defLength
124          */
125         public String getDefLength() {
126                 return defLength;
127         }
128         /**
129          * Set Default Content Length
130          * @param defLength
131          */
132         public void setDefLength(String defLength) {
133                 this.defLength = defLength;
134         }
135
136         
137
138 }