Search in sources :

Example 6 with RestServerConfiguration

use of org.infinispan.rest.configuration.RestServerConfiguration in project infinispan by infinispan.

the class BaseCacheResource method getCacheValue.

CompletionStage<RestResponse> getCacheValue(RestRequest request) throws RestResponseException {
    String cacheName = request.variables().get("cacheName");
    MediaType keyContentType = request.keyContentType();
    AdvancedCache<?, ?> cache = invocationHelper.getRestCacheManager().getCache(cacheName, request);
    MediaType requestedMediaType = negotiateMediaType(cache, invocationHelper.getEncoderRegistry(), request);
    Object key = getKey(request);
    String cacheControl = request.getCacheControlHeader();
    boolean returnBody = request.method() == GET;
    RestCacheManager<Object> restCacheManager = invocationHelper.getRestCacheManager();
    return restCacheManager.getInternalEntry(cacheName, key, keyContentType, requestedMediaType, request).thenApply(entry -> {
        NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
        responseBuilder.status(HttpResponseStatus.NOT_FOUND);
        if (entry instanceof InternalCacheEntry) {
            InternalCacheEntry<Object, Object> ice = (InternalCacheEntry<Object, Object>) entry;
            Long lastMod = CacheOperationsHelper.lastModified(ice);
            Date expires = ice.canExpire() ? new Date(ice.getExpiryTime()) : null;
            OptionalInt minFreshSeconds = CacheOperationsHelper.minFresh(cacheControl);
            if (CacheOperationsHelper.entryFreshEnough(expires, minFreshSeconds)) {
                Metadata meta = ice.getMetadata();
                String etag = calcETAG(ice.getValue());
                String ifNoneMatch = request.getEtagIfNoneMatchHeader();
                String ifMatch = request.getEtagIfMatchHeader();
                String ifUnmodifiedSince = request.getIfUnmodifiedSinceHeader();
                String ifModifiedSince = request.getIfModifiedSinceHeader();
                if (ifNoneMatch != null && ifNoneMatch.equals(etag)) {
                    return responseBuilder.status(HttpResponseStatus.NOT_MODIFIED).build();
                }
                if (ifMatch != null && !ifMatch.equals(etag)) {
                    return responseBuilder.status(HttpResponseStatus.PRECONDITION_FAILED).build();
                }
                if (DateUtils.ifUnmodifiedIsBeforeModificationDate(ifUnmodifiedSince, lastMod)) {
                    return responseBuilder.status(HttpResponseStatus.PRECONDITION_FAILED).build();
                }
                if (DateUtils.isNotModifiedSince(ifModifiedSince, lastMod)) {
                    return responseBuilder.status(HttpResponseStatus.NOT_MODIFIED).build();
                }
                Object value = ice.getValue();
                MediaType configuredMediaType = restCacheManager.getValueConfiguredFormat(cacheName, request);
                writeValue(value, requestedMediaType, configuredMediaType, responseBuilder, returnBody);
                responseBuilder.status(HttpResponseStatus.OK).lastModified(lastMod).eTag(etag).cacheControl(CacheOperationsHelper.calcCacheControl(expires)).expires(expires).timeToLive(meta.lifespan()).maxIdle(meta.maxIdle()).created(ice.getCreated()).lastUsed(ice.getLastUsed());
                List<String> extended = request.parameters().get(EXTENDED_HEADER.getValue());
                RestServerConfiguration restServerConfiguration = invocationHelper.getConfiguration();
                if (extended != null && extended.size() > 0 && CacheOperationsHelper.supportsExtendedHeaders(restServerConfiguration, extended.iterator().next())) {
                    responseBuilder.clusterPrimaryOwner(restCacheManager.getPrimaryOwner(cacheName, key, request)).clusterBackupOwners(restCacheManager.getBackupOwners(cacheName, key, request)).clusterNodeName(restCacheManager.getNodeName()).clusterServerAddress(restCacheManager.getServerAddress());
                }
            }
        }
        return responseBuilder.build();
    });
}
Also used : Metadata(org.infinispan.metadata.Metadata) OptionalInt(java.util.OptionalInt) NettyRestResponse(org.infinispan.rest.NettyRestResponse) Date(java.util.Date) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) MediaType(org.infinispan.commons.dataconversion.MediaType) MediaTypeUtils.negotiateMediaType(org.infinispan.rest.resources.MediaTypeUtils.negotiateMediaType) RestServerConfiguration(org.infinispan.rest.configuration.RestServerConfiguration)

Example 7 with RestServerConfiguration

use of org.infinispan.rest.configuration.RestServerConfiguration in project infinispan by infinispan.

the class ServerConfigurationParserTest method validateConfiguration.

private void validateConfiguration(ServerConfiguration configuration) {
    // Interfaces
    assertEquals(2, configuration.networkInterfaces().size());
    NetworkAddress defaultInterface = configuration.networkInterfaces().get("default").getNetworkAddress();
    assertNotNull(defaultInterface);
    assertTrue(defaultInterface.getAddress().isLoopbackAddress());
    // Socket bindings
    Map<String, SocketBindingConfiguration> socketBindings = configuration.socketBindings();
    assertEquals(5, socketBindings.size());
    assertEquals(11221, socketBindings.get("memcached").port());
    assertEquals(12221, socketBindings.get("memcached-2").port());
    assertEquals(11222, socketBindings.get("default").port());
    assertEquals(11223, socketBindings.get("hotrod").port());
    assertEquals(8080, socketBindings.get("rest").port());
    // Security realms
    Map<String, RealmConfiguration> realms = configuration.security().realms().realms();
    assertEquals(3, realms.size());
    RealmConfiguration realmConfiguration = realms.get("default");
    assertEquals("default", realmConfiguration.name());
    realmConfiguration = realms.get("using-credentials");
    assertEquals("using-credentials", realmConfiguration.name());
    // Data Sources
    Map<String, DataSourceConfiguration> dataSources = configuration.dataSources();
    assertEquals(2, dataSources.size());
    DataSourceConfiguration dataSource = dataSources.get("database");
    assertEquals("jdbc/database", dataSource.jndiName());
    assertEquals("jdbc:h2:tcp://${org.infinispan.test.host.address}:1521/test", dataSource.url());
    assertEquals("test", dataSource.username());
    assertEquals("test", new String(dataSource.password()));
    assertEquals("SELECT 1", dataSource.initialSql());
    assertEquals("org.h2.Driver", dataSource.driver());
    assertEquals(10, dataSource.maxSize());
    assertEquals(1, dataSource.minSize());
    assertEquals(1, dataSource.initialSize());
    assertEquals(1, dataSource.connectionProperties().size());
    assertEquals(10000, dataSource.leakDetection());
    assertEquals(1000, dataSource.backgroundValidation());
    assertEquals(500, dataSource.validateOnAcquisition());
    assertEquals("somevalue", dataSource.connectionProperties().get("someproperty"));
    dataSource = dataSources.get("database-with-credential");
    assertEquals("test", new String(dataSource.password()));
    // Connectors
    List<ProtocolServerConfiguration> connectors = configuration.endpoints().endpoints().get(0).connectors();
    assertEquals(3, connectors.size());
    assertTrue(connectors.get(0) instanceof HotRodServerConfiguration);
    assertTrue(connectors.get(1) instanceof RestServerConfiguration);
    assertTrue(connectors.get(2) instanceof MemcachedServerConfiguration);
    // Ensure endpoints are bound to the interfaces
    SinglePortRouterConfiguration singlePortRouter = configuration.endpoints().endpoints().get(0).singlePortRouter();
    assertEquals(socketBindings.get("default").interfaceConfiguration().getNetworkAddress().getAddress().getHostAddress(), singlePortRouter.host());
    assertEquals(socketBindings.get("default").port(), singlePortRouter.port());
    assertEquals(socketBindings.get("memcached").port(), configuration.endpoints().endpoints().get(0).connectors().get(2).port());
    assertEquals("strongPassword", new String(((Supplier<char[]>) realmProvider(realmConfiguration, LdapRealmConfiguration.class).attributes().attribute(Attribute.CREDENTIAL).get()).get()));
    assertEquals("secret", new String(((Supplier<char[]>) realmConfiguration.serverIdentitiesConfiguration().sslConfiguration().trustStore().attributes().attribute(Attribute.PASSWORD).get()).get()));
    assertEquals("1fdca4ec-c416-47e0-867a-3d471af7050f", new String(((Supplier<char[]>) realmProvider(realmConfiguration, TokenRealmConfiguration.class).oauth2Configuration().attributes().attribute(Attribute.CLIENT_SECRET).get()).get()));
}
Also used : HotRodServerConfiguration(org.infinispan.server.hotrod.configuration.HotRodServerConfiguration) SinglePortRouterConfiguration(org.infinispan.server.router.configuration.SinglePortRouterConfiguration) RealmConfiguration(org.infinispan.server.configuration.security.RealmConfiguration) LdapRealmConfiguration(org.infinispan.server.configuration.security.LdapRealmConfiguration) TokenRealmConfiguration(org.infinispan.server.configuration.security.TokenRealmConfiguration) ProtocolServerConfiguration(org.infinispan.server.core.configuration.ProtocolServerConfiguration) MemcachedServerConfiguration(org.infinispan.server.memcached.configuration.MemcachedServerConfiguration) NetworkAddress(org.infinispan.server.network.NetworkAddress) TokenRealmConfiguration(org.infinispan.server.configuration.security.TokenRealmConfiguration) RestServerConfiguration(org.infinispan.rest.configuration.RestServerConfiguration) ElytronPasswordProviderSupplier(org.infinispan.server.security.ElytronPasswordProviderSupplier) Supplier(java.util.function.Supplier)

Aggregations

RestServerConfiguration (org.infinispan.rest.configuration.RestServerConfiguration)7 ProtocolServerConfiguration (org.infinispan.server.core.configuration.ProtocolServerConfiguration)4 HotRodServerConfiguration (org.infinispan.server.hotrod.configuration.HotRodServerConfiguration)4 Supplier (java.util.function.Supplier)3 EndpointConfiguration (org.infinispan.server.configuration.endpoint.EndpointConfiguration)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ObjectInput (java.io.ObjectInput)2 ObjectOutput (java.io.ObjectOutput)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 PrivilegedActionException (java.security.PrivilegedActionException)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2