Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / util / ValidateEncoding.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.rest.util;
21
22 import java.io.UnsupportedEncodingException;
23 import java.net.URI;
24
25 import javax.ws.rs.core.MultivaluedMap;
26 import javax.ws.rs.core.UriInfo;
27
28 import org.springframework.web.util.UriUtils;
29
30 /**
31  * The Class ValidateEncoding.
32  */
33 public class ValidateEncoding {
34
35         private final String encoding = "UTF-8";
36         
37         /**
38          * Instantiates a new validate encoding.
39          */
40         private ValidateEncoding() {
41                 
42         }
43         
44         /**
45          * The Class Helper.
46          */
47         private static class Helper {
48                 
49                 /** The Constant INSTANCE. */
50                 private static final ValidateEncoding INSTANCE = new ValidateEncoding();
51         }
52         
53         /**
54          * Gets the single instance of ValidateEncoding.
55          *
56          * @return single instance of ValidateEncoding
57          */
58         public static ValidateEncoding getInstance() {
59                 return Helper.INSTANCE;
60         }       
61         
62         /**
63          * Validate.
64          *
65          * @param uri the uri
66          * @return true, if successful
67          * @throws UnsupportedEncodingException the unsupported encoding exception
68          */
69         public boolean validate(URI uri) throws UnsupportedEncodingException {
70                 boolean result = true;
71                 if (!validatePath(uri.getRawPath())) {
72                         result = false;
73                 }
74                 /*if (!validateQueryParams(uri.getRawQuery())) {
75                         result = false;
76                 } //TODO
77                 */
78                 
79                 return result;
80         }
81         
82         /**
83          * Validate.
84          *
85          * @param info the info
86          * @return true, if successful
87          * @throws UnsupportedEncodingException the unsupported encoding exception
88          */
89         public boolean validate(UriInfo info) throws UnsupportedEncodingException {
90                 boolean result = true;
91                 if (!validatePath(info.getPath(false))) {
92                         result = false;
93                 }
94                 if (!validateQueryParams(info.getQueryParameters(false))) {
95                         result = false;
96                 }
97                 
98                 return result;
99         }
100         
101         /**
102          * Validate path.
103          *
104          * @param path the path
105          * @return true, if successful
106          * @throws UnsupportedEncodingException the unsupported encoding exception
107          */
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)) {
113                                 valid = false;
114                         }
115                 }
116                 
117                 return valid;
118                 
119         }
120         
121         /**
122          * Validate query params.
123          *
124          * @param params the params
125          * @return true, if successful
126          * @throws UnsupportedEncodingException the unsupported encoding exception
127          */
128         private boolean validateQueryParams(MultivaluedMap<String, String> params) throws UnsupportedEncodingException {
129                 boolean valid = true;
130                 
131                 for (String key : params.keySet()) {
132                         if (!this.checkEncoding(key)) {
133                                 valid = false;
134                         }
135                         for (String item : params.get(key)) {
136                                 if (!this.checkEncoding(item)) {
137                                         valid = false;
138                                 }
139                         }
140                 }
141                 return valid;
142         }
143         
144         /**
145          * Check encoding.
146          *
147          * @param segment the segment
148          * @return true, if successful
149          * @throws UnsupportedEncodingException the unsupported encoding exception
150          */
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);
156                 
157                 return result;
158         }
159 }