
com.floragunn.searchguard.DefaultObjectMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of search-guard-7 Show documentation
Show all versions of search-guard-7 Show documentation
Provide access control related features for Elasticsearch 6
The newest version!
/*
* Copyright 2016-2018 by floragunn GmbH - All rights reserved
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed here is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* This software is free of charge for non-commercial and academic use.
* For commercial use in a production environment you have to obtain a license
* from https://floragunn.com
*
*/
package com.floragunn.searchguard;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import org.elasticsearch.SpecialPermission;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class DefaultObjectMapper {
public static final ObjectMapper objectMapper = new ObjectMapper();
public final static ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());
private static final ObjectMapper defaulOmittingObjectMapper = new ObjectMapper();
static {
objectMapper.setSerializationInclusion(Include.NON_NULL);
//objectMapper.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
objectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
defaulOmittingObjectMapper.setSerializationInclusion(Include.NON_DEFAULT);
defaulOmittingObjectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
YAML_MAPPER.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
}
public static T readTree(JsonNode node, Class clazz) throws IOException {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new SpecialPermission());
}
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
@Override
public T run() throws Exception {
return objectMapper.treeToValue(node, clazz);
}
});
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
}
public static T readValue(String string, Class clazz) throws IOException {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new SpecialPermission());
}
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
@Override
public T run() throws Exception {
return objectMapper.readValue(string, clazz);
}
});
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
}
public static JsonNode readTree(String string) throws IOException {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new SpecialPermission());
}
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
@Override
public JsonNode run() throws Exception {
return objectMapper.readTree(string);
}
});
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
}
public static String writeValueAsString(Object value, boolean omitDefaults) throws JsonProcessingException {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new SpecialPermission());
}
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
@Override
public String run() throws Exception {
return (omitDefaults?defaulOmittingObjectMapper:objectMapper).writeValueAsString(value);
}
});
} catch (final PrivilegedActionException e) {
throw (JsonProcessingException) e.getCause();
}
}
public static T readValue(String string, TypeReference tr) throws IOException {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new SpecialPermission());
}
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
@Override
public T run() throws Exception {
return objectMapper.readValue(string, tr);
}
});
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
}
public static T readValue(String string, JavaType jt) throws IOException {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new SpecialPermission());
}
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
@Override
public T run() throws Exception {
return objectMapper.readValue(string, jt);
}
});
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
}
public static TypeFactory getTypeFactory() {
return objectMapper.getTypeFactory();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy