blob: 5bc1baf58159e2e5e6c2976f5fcae8f596b39d52 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
*
*/
package org.apache.ranger.common;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ranger.common.view.ViewBaseBean;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class JSONUtil {
@Autowired
RESTErrorUtil restErrorUtil;
public File writeJsonToFile(ViewBaseBean viewBean, String fileName)
throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
if (fileName.length() < 3) {
fileName = "file_" + fileName;
}
File file = File.createTempFile(fileName, ".json");
objectMapper.defaultPrettyPrintingWriter().writeValue(file, viewBean);
return file;
}
public Map<String, String> jsonToMap(String jsonStr) {
if (jsonStr == null || jsonStr.isEmpty()) {
return new HashMap<String, String>();
}
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, String> tempObject = mapper.readValue(jsonStr,
new TypeReference<Map<String, String>>() {
});
return tempObject;
} catch (JsonParseException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (JsonMappingException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (IOException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
}
}
public String readMapToString(Map<?, ?> map) {
ObjectMapper mapper = new ObjectMapper();
String jsonString = null;
try {
jsonString = mapper.writeValueAsString(map);
} catch (JsonParseException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (JsonMappingException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (IOException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
}
return jsonString;
}
public String readListToString(List<?> list) {
ObjectMapper mapper = new ObjectMapper();
String jsonString = null;
try {
jsonString = mapper.writeValueAsString(list);
} catch (JsonParseException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (JsonMappingException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (IOException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
}
return jsonString;
}
public String writeObjectAsString(Serializable obj) {
ObjectMapper mapper = new ObjectMapper();
String jsonStr;
try {
jsonStr = mapper.writeValueAsString(obj);
return jsonStr;
} catch (JsonParseException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (JsonMappingException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (IOException e) {
throw restErrorUtil.createRESTException(
"Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
}
}
public <T> T writeJsonToJavaObject(String json, Class<T> tClass) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, tClass);
} catch (JsonParseException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (JsonMappingException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
} catch (IOException e) {
throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(),
MessageEnums.INVALID_INPUT_DATA);
}
}
}