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.policy.utils;
23 import java.io.IOException;
24 import java.util.Base64;
25 import java.util.StringTokenizer;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.JsonMappingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
32 public class PolicyUtils {
34 public static String objectToJsonString(Object o) throws JsonProcessingException{
35 ObjectMapper mapper = new ObjectMapper();
36 return mapper.writeValueAsString(o);
39 public static <T> T jsonStringToObject(String jsonString, Class<T> className) throws JsonParseException, JsonMappingException, IOException{
40 ObjectMapper mapper = new ObjectMapper();
41 T t = mapper.readValue(jsonString, className);
45 public static String[] decodeBasicEncoding(String encodedValue) throws Exception{
46 if(encodedValue!=null && encodedValue.contains("Basic ")){
47 String encodedUserPassword = encodedValue.replaceFirst("Basic" + " ", "");
48 String usernameAndPassword = null;
49 byte[] decodedBytes = Base64.getDecoder().decode(encodedUserPassword);
50 usernameAndPassword = new String(decodedBytes, "UTF-8");
51 StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
52 String username = tokenizer.nextToken();
53 String password = tokenizer.nextToken();
54 return new String[]{username, password};