2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.rest.impl.validator;
23 import org.apache.commons.codec.binary.Base64;
24 import org.openecomp.sdc.common.api.Constants;
25 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
26 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.MediaType;
30 import java.util.List;
33 public class RequestHeadersValidator {
34 private static Logger log = Logger.getLogger(RequestHeadersValidator.class.getName());
36 public static void validateContentType(HttpServletRequest request, MediaType expectedContentType,
37 Map<String, String> headersMap) throws RestRequestValidationException {
39 log.debug("validateContentType - expected: {}", expectedContentType);
40 if (request == null || expectedContentType == null) {
41 throw new RestRequestValidationException("request or media-type are null");
43 String contentType = request.getHeader(Constants.CONTENT_TYPE_HEADER);
44 if (contentType == null || !contentType.contains(MediaType.APPLICATION_JSON)) {
45 throw new RestRequestValidationException(
46 "Content-Type of requset is different then " + expectedContentType);
48 headersMap.put(Constants.CONTENT_TYPE_HEADER, contentType);
52 public static void validateIdentificationHeaders(HttpServletRequest request, List<String> identificationList,
53 Map<String, String> headersMap) throws RestRequestValidationException {
55 log.debug("validateIdentificationHeaders");
56 for (String requiredHeader : identificationList) {
57 String headerVal = request.getHeader(requiredHeader);
58 if (headerVal != null && !headerVal.isEmpty()) {
59 headersMap.put(requiredHeader, headerVal);
60 log.debug("found header - {} : {}", requiredHeader, headerVal);
62 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","missing identification header: {}", requiredHeader);
63 throw new RestRequestValidationException("missing identification header: " + requiredHeader);
69 public static void validateMd5(byte[] encodedData, HttpServletRequest request, Map<String, String> headersMap)
70 throws RestRequestValidationException {
72 // validate parameters
73 if (encodedData == null || request == null) {
74 throw new RestRequestValidationException("encoded data or request are not valid");
77 // calculate MD5 on the data
78 String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(encodedData);
79 byte[] encodedMd5 = Base64.encodeBase64(md5.getBytes());
81 // read the Content-MD5 header
82 String origMd5 = request.getHeader(Constants.MD5_HEADER);
83 if ((origMd5 == null) || origMd5.isEmpty()) {
84 throw new RestRequestValidationException("missing Content-MD5 header ");
88 if (!origMd5.equals(new String(encodedMd5))) {
89 throw new RestRequestValidationException("uploaded file failed MD5 validation");
91 headersMap.put(Constants.MD5_HEADER, origMd5);