re base code
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / rest / impl / validator / RequestHeadersValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.rest.impl.validator;
22
23 import org.apache.commons.codec.binary.Base64;
24 import org.openecomp.sdc.common.api.Constants;
25 import org.openecomp.sdc.common.log.wrappers.Logger;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.MediaType;
29 import java.util.List;
30 import java.util.Map;
31
32 public class RequestHeadersValidator {
33         private static Logger log = Logger.getLogger(RequestHeadersValidator.class.getName());
34
35         public static void validateContentType(HttpServletRequest request, MediaType expectedContentType,
36                         Map<String, String> headersMap) throws RestRequestValidationException {
37
38                 log.debug("validateContentType - expected: {}", expectedContentType);
39                 if (request == null || expectedContentType == null) {
40                         throw new RestRequestValidationException("request or media-type are null");
41                 }
42                 String contentType = request.getHeader(Constants.CONTENT_TYPE_HEADER);
43                 if (contentType == null || !contentType.contains(MediaType.APPLICATION_JSON)) {
44                         throw new RestRequestValidationException(
45                                         "Content-Type of requset is different then " + expectedContentType);
46                 } else {
47                         headersMap.put(Constants.CONTENT_TYPE_HEADER, contentType);
48                 }
49         }
50
51         public static void validateIdentificationHeaders(HttpServletRequest request, List<String> identificationList,
52                         Map<String, String> headersMap) throws RestRequestValidationException {
53
54                 log.debug("validateIdentificationHeaders");
55                 for (String requiredHeader : identificationList) {
56                         String headerVal = request.getHeader(requiredHeader);
57                         if (headerVal != null && !headerVal.isEmpty()) {
58                                 headersMap.put(requiredHeader, headerVal);
59                                 log.debug("found header - {} : {}", requiredHeader, headerVal);
60                         } else {
61                                 log.error("missing identification header: {}", requiredHeader);
62                                 throw new RestRequestValidationException("missing identification header: " + requiredHeader);
63                         }
64                 }
65
66         }
67
68         public static void validateMd5(byte[] encodedData, HttpServletRequest request, Map<String, String> headersMap)
69                         throws RestRequestValidationException {
70
71                 // validate parameters
72                 if (encodedData == null || request == null) {
73                         throw new RestRequestValidationException("encoded data or request are not valid");
74                 }
75
76                 // calculate MD5 on the data
77                 String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(encodedData);
78                 byte[] encodedMd5 = Base64.encodeBase64(md5.getBytes());
79
80                 // read the Content-MD5 header
81                 String origMd5 = request.getHeader(Constants.MD5_HEADER);
82                 if ((origMd5 == null) || origMd5.isEmpty()) {
83                         throw new RestRequestValidationException("missing Content-MD5 header ");
84                 }
85
86                 // verify MD5 value
87                 if (!origMd5.equals(new String(encodedMd5))) {
88                         throw new RestRequestValidationException("uploaded file failed MD5 validation");
89                 }
90                 headersMap.put(Constants.MD5_HEADER, origMd5);
91         }
92
93 }