use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.
the class AbstractConfigurationSerializerTest method configurationSerializationTest.
@Test(dataProvider = "configurationFiles")
public void configurationSerializationTest(Parameter parameter) throws IOException {
URL url = FileLookupFactory.newInstance().lookupFileLocation(parameter.config.toString(), Thread.currentThread().getContextClassLoader());
ConfigurationBuilderHolder holderBefore = parameter.registry.parse(url);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Map<String, Configuration> configurations = new HashMap<>();
for (Map.Entry<String, ConfigurationBuilder> configuration : holderBefore.getNamedConfigurationBuilders().entrySet()) {
configurations.put(configuration.getKey(), configuration.getValue().build());
}
try (ConfigurationWriter writer = ConfigurationWriter.to(baos).withType(parameter.mediaType).build()) {
parameter.registry.serialize(writer, holderBefore.getGlobalConfigurationBuilder().build(), configurations);
}
log.debug(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ConfigurationBuilderHolder holderAfter = parameter.registry.parse(bais, null, parameter.mediaType);
GlobalConfiguration globalConfigurationBefore = holderBefore.getGlobalConfigurationBuilder().build();
GlobalConfiguration globalConfigurationAfter = holderAfter.getGlobalConfigurationBuilder().build();
assertEquals(globalConfigurationBefore.security().securityCacheTimeout(), globalConfigurationAfter.security().securityCacheTimeout());
assertEquals(globalConfigurationBefore.security().securityCacheSize(), globalConfigurationAfter.security().securityCacheSize());
compareAttributeSets("Global", globalConfigurationBefore.globalState().attributes(), globalConfigurationAfter.globalState().attributes(), "localConfigurationStorage");
compareAttributeSets("Global", globalConfigurationBefore.jmx().attributes(), globalConfigurationAfter.jmx().attributes(), org.infinispan.configuration.parsing.Attribute.MBEAN_SERVER_LOOKUP.toString());
compareAttributeSets("Global", globalConfigurationBefore.security().authorization().attributes(), globalConfigurationAfter.security().authorization().attributes());
compareAttributeSets("Global", globalConfigurationBefore.serialization().attributes(), globalConfigurationAfter.serialization().attributes(), "marshaller", "classResolver", "advancedExternalizer", "contextInitializers");
compareAttributeSets("Global", globalConfigurationBefore.transport().attributes(), globalConfigurationAfter.transport().attributes(), "transport", "properties");
compareExtraGlobalConfiguration(globalConfigurationBefore, globalConfigurationAfter);
for (String name : holderBefore.getNamedConfigurationBuilders().keySet()) {
Configuration configurationBefore = holderBefore.getNamedConfigurationBuilders().get(name).build();
assertTrue(name, holderAfter.getNamedConfigurationBuilders().containsKey(name));
Configuration configurationAfter = holderAfter.getNamedConfigurationBuilders().get(name).build();
compareConfigurations(name, configurationBefore, configurationAfter);
}
}
use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.
the class Server method parseConfiguration.
private void parseConfiguration(List<Path> configurationFiles) {
ParserRegistry parser = new ParserRegistry(classLoader, false, properties);
try {
// load the defaults first
URL defaults = this.getClass().getClassLoader().getResource(SERVER_DEFAULTS);
defaultsHolder = parser.parse(defaults);
// Set a default audit logger
defaultsHolder.getGlobalConfigurationBuilder().security().authorization().auditLogger(new LoggingAuditLogger());
// base the global configuration to the default
configurationBuilderHolder = new ConfigurationBuilderHolder(classLoader);
GlobalConfigurationBuilder global = configurationBuilderHolder.getGlobalConfigurationBuilder();
global.read(defaultsHolder.getGlobalConfigurationBuilder().build());
// Copy all default templates
for (Map.Entry<String, ConfigurationBuilder> entry : defaultsHolder.getNamedConfigurationBuilders().entrySet()) {
configurationBuilderHolder.newConfigurationBuilder(entry.getKey()).read(entry.getValue().build());
}
// then load the user configurations
for (Path configurationFile : configurationFiles) {
if (!configurationFile.isAbsolute()) {
configurationFile = serverConf.toPath().resolve(configurationFile);
}
parser.parse(configurationFile.toUri().toURL(), configurationBuilderHolder);
}
if (log.isDebugEnabled()) {
StringBuilderWriter sw = new StringBuilderWriter();
try (ConfigurationWriter w = ConfigurationWriter.to(sw).build()) {
Map<String, Configuration> configs = configurationBuilderHolder.getNamedConfigurationBuilders().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().build()));
parser.serialize(w, global.build(), configs);
}
log.debugf("Actual configuration: %s", sw);
}
// Amend the named caches configurations with the defaults
for (Map.Entry<String, ConfigurationBuilder> entry : configurationBuilderHolder.getNamedConfigurationBuilders().entrySet()) {
Configuration cfg = entry.getValue().build();
ConfigurationBuilder defaultCfg = defaultsHolder.getNamedConfigurationBuilders().get("org.infinispan." + cfg.clustering().cacheMode().name());
ConfigurationBuilder rebased = new ConfigurationBuilder().read(defaultCfg.build());
rebased.read(cfg);
entry.setValue(rebased);
}
// Process the server configuration
ServerConfigurationBuilder serverBuilder = global.module(ServerConfigurationBuilder.class);
// Set up transport security
TransportSecurityConfiguration transportSecurityConfiguration = serverBuilder.security().transport().create();
if (transportSecurityConfiguration.securityRealm() != null) {
String securityRealm = transportSecurityConfiguration.securityRealm();
Supplier<SSLContext> serverSSLContextSupplier = serverBuilder.serverSSLContextSupplier(securityRealm);
Supplier<SSLContext> clientSSLContextSupplier = serverBuilder.clientSSLContextSupplier(securityRealm);
NamedSocketFactory namedSocketFactory = new NamedSocketFactory(() -> clientSSLContextSupplier.get().getSocketFactory(), () -> serverSSLContextSupplier.get().getServerSocketFactory());
global.transport().addProperty(JGroupsTransport.SOCKET_FACTORY, namedSocketFactory);
Server.log.sslTransport(securityRealm);
}
// Set the operation handler on all endpoints
ServerAdminOperationsHandler adminOperationsHandler = new ServerAdminOperationsHandler(defaultsHolder);
ServerConfigurationBuilder serverConfigurationBuilder = global.module(ServerConfigurationBuilder.class);
for (EndpointConfigurationBuilder endpoint : serverConfigurationBuilder.endpoints().endpoints().values()) {
for (ProtocolServerConfigurationBuilder<?, ?> connector : endpoint.connectors()) {
connector.adminOperationsHandler(adminOperationsHandler);
}
}
configurationBuilderHolder.validate();
} catch (IOException e) {
throw new CacheConfigurationException(e);
}
}
use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.
the class CacheResourceV2 method getCacheConfig.
private CompletionStage<RestResponse> getCacheConfig(RestRequest request) {
NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
String cacheName = request.variables().get("cacheName");
MediaType accept = negotiateMediaType(request, APPLICATION_JSON, APPLICATION_XML, APPLICATION_YAML);
responseBuilder.contentType(accept);
if (!invocationHelper.getRestCacheManager().getInstance().getCacheConfigurationNames().contains(cacheName)) {
responseBuilder.status(NOT_FOUND).build();
}
Cache<?, ?> cache = invocationHelper.getRestCacheManager().getCache(cacheName, request);
if (cache == null)
return notFoundResponseFuture();
Configuration cacheConfiguration = SecurityActions.getCacheConfiguration(cache.getAdvancedCache());
ByteArrayOutputStream entity = new ByteArrayOutputStream();
try (ConfigurationWriter writer = ConfigurationWriter.to(entity).withType(accept).prettyPrint(false).build()) {
parserRegistry.serialize(writer, cacheName, cacheConfiguration);
} catch (Exception e) {
return CompletableFuture.completedFuture(responseBuilder.status(INTERNAL_SERVER_ERROR).entity(Util.getRootCause(e)).build());
}
responseBuilder.entity(entity);
return CompletableFuture.completedFuture(responseBuilder.status(OK).build());
}
use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.
the class CacheResourceV2 method convert.
private CompletionStage<RestResponse> convert(RestRequest request, MediaType toType) {
NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
String contents = request.contents().asString();
if (contents == null || contents.isEmpty()) {
responseBuilder.status(HttpResponseStatus.BAD_REQUEST);
return CompletableFuture.completedFuture(responseBuilder.build());
}
return CompletableFuture.supplyAsync(() -> {
ParserRegistry parserRegistry = invocationHelper.getParserRegistry();
ConfigurationBuilderHolder builderHolder = parserRegistry.parse(contents, request.contentType());
Map.Entry<String, ConfigurationBuilder> entry = builderHolder.getNamedConfigurationBuilders().entrySet().iterator().next();
Configuration configuration = entry.getValue().build();
StringBuilderWriter out = new StringBuilderWriter();
try (ConfigurationWriter writer = ConfigurationWriter.to(out).withType(toType).clearTextSecrets(true).prettyPrint(true).build()) {
parserRegistry.serialize(writer, entry.getKey(), configuration);
}
return responseBuilder.contentType(toType).entity(out.toString()).build();
}, invocationHelper.getExecutor());
}
use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.
the class ContainerResource method getConfig.
private CompletionStage<RestResponse> getConfig(RestRequest request) {
NettyRestResponse.Builder responseBuilder = checkCacheManager(request);
if (responseBuilder.getHttpStatus() == NOT_FOUND)
return completedFuture(responseBuilder.build());
EmbeddedCacheManager embeddedCacheManager = invocationHelper.getRestCacheManager().getInstance().withSubject(request.getSubject());
GlobalConfiguration globalConfiguration = SecurityActions.getCacheManagerConfiguration(embeddedCacheManager);
MediaType format = MediaTypeUtils.negotiateMediaType(request, APPLICATION_JSON, APPLICATION_XML);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ConfigurationWriter writer = ConfigurationWriter.to(baos).withType(format).build()) {
parserRegistry.serialize(writer, globalConfiguration, emptyMap());
}
responseBuilder.contentType(format);
responseBuilder.entity(baos.toByteArray());
} catch (Exception e) {
responseBuilder.status(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
return completedFuture(responseBuilder.build());
}
Aggregations