2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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=========================================================
20 package org.onap.aai.rest.util;
22 import java.io.UnsupportedEncodingException;
25 import javax.ws.rs.core.MultivaluedMap;
26 import javax.ws.rs.core.UriInfo;
28 import org.springframework.web.util.UriUtils;
31 * The Class ValidateEncoding.
33 public class ValidateEncoding {
35 private final String encoding = "UTF-8";
38 * Instantiates a new validate encoding.
40 private ValidateEncoding() {
47 private static class Helper {
49 /** The Constant INSTANCE. */
50 private static final ValidateEncoding INSTANCE = new ValidateEncoding();
54 * Gets the single instance of ValidateEncoding.
56 * @return single instance of ValidateEncoding
58 public static ValidateEncoding getInstance() {
59 return Helper.INSTANCE;
66 * @return true, if successful
67 * @throws UnsupportedEncodingException the unsupported encoding exception
69 public boolean validate(URI uri) throws UnsupportedEncodingException {
70 boolean result = true;
71 if (!validatePath(uri.getRawPath())) {
74 /*if (!validateQueryParams(uri.getRawQuery())) {
85 * @param info the info
86 * @return true, if successful
87 * @throws UnsupportedEncodingException the unsupported encoding exception
89 public boolean validate(UriInfo info) throws UnsupportedEncodingException {
90 boolean result = true;
91 if (!validatePath(info.getPath(false))) {
94 if (!validateQueryParams(info.getQueryParameters(false))) {
104 * @param path the path
105 * @return true, if successful
106 * @throws UnsupportedEncodingException the unsupported encoding exception
108 private boolean validatePath(String path) throws UnsupportedEncodingException {
109 String[] segments = path.split("/");
110 boolean valid = true;
111 for (String segment : segments) {
112 if (!this.checkEncoding(segment)) {
122 * Validate query params.
124 * @param params the params
125 * @return true, if successful
126 * @throws UnsupportedEncodingException the unsupported encoding exception
128 private boolean validateQueryParams(MultivaluedMap<String, String> params) throws UnsupportedEncodingException {
129 boolean valid = true;
131 for (String key : params.keySet()) {
132 if (!this.checkEncoding(key)) {
135 for (String item : params.get(key)) {
136 if (!this.checkEncoding(item)) {
147 * @param segment the segment
148 * @return true, if successful
149 * @throws UnsupportedEncodingException the unsupported encoding exception
151 private boolean checkEncoding(String segment) throws UnsupportedEncodingException {
152 boolean result = false;
153 String decode = UriUtils.decode(segment, encoding);
154 String encode = UriUtils.encode(decode, encoding);
155 result = segment.equals(encode);