Search in sources :

Example 1 with MemoryReplayCache

use of org.apache.wss4j.common.cache.MemoryReplayCache in project cxf by apache.

the class WSS4JUtils method getReplayCache.

/**
 * Get a ReplayCache instance. It first checks to see whether caching has been explicitly
 * enabled or disabled via the booleanKey argument. If it has been set to false then no
 * replay caching is done (for this booleanKey). If it has not been specified, then caching
 * is enabled only if we are not the initiator of the exchange. If it has been specified, then
 * caching is enabled.
 *
 * It tries to get an instance of ReplayCache via the instanceKey argument from a
 * contextual property, and failing that the message exchange. If it can't find any, then it
 * defaults to using an EH-Cache instance and stores that on the message exchange.
 */
public static ReplayCache getReplayCache(SoapMessage message, String booleanKey, String instanceKey) throws WSSecurityException {
    boolean specified = false;
    Object o = message.getContextualProperty(booleanKey);
    if (o != null) {
        if (!PropertyUtils.isTrue(o)) {
            return null;
        }
        specified = true;
    }
    if (!specified && MessageUtils.isRequestor(message)) {
        return null;
    }
    ReplayCache replayCache = (ReplayCache) message.getContextualProperty(instanceKey);
    Endpoint ep = message.getExchange().getEndpoint();
    if (replayCache == null && ep != null && ep.getEndpointInfo() != null) {
        EndpointInfo info = ep.getEndpointInfo();
        synchronized (info) {
            replayCache = (ReplayCache) info.getProperty(instanceKey);
            if (replayCache == null) {
                String cacheKey = instanceKey;
                if (info.getName() != null) {
                    int hashcode = info.getName().toString().hashCode();
                    if (hashcode < 0) {
                        cacheKey += hashcode;
                    } else {
                        cacheKey += "-" + hashcode;
                    }
                }
                if (WSS4JCacheUtil.isEhCacheInstalled()) {
                    Bus bus = message.getExchange().getBus();
                    final Path diskstoreParent;
                    try {
                        diskstoreParent = Files.createTempDirectory("cxf");
                    } catch (IOException ex) {
                        throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, ex);
                    }
                    replayCache = new CXFEHCacheReplayCache(cacheKey, bus, diskstoreParent);
                } else {
                    replayCache = new MemoryReplayCache();
                }
                info.setProperty(instanceKey, replayCache);
            }
        }
    }
    return replayCache;
}
Also used : Path(java.nio.file.Path) Bus(org.apache.cxf.Bus) MemoryReplayCache(org.apache.wss4j.common.cache.MemoryReplayCache) ReplayCache(org.apache.wss4j.common.cache.ReplayCache) CXFEHCacheReplayCache(org.apache.cxf.ws.security.cache.CXFEHCacheReplayCache) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) IOException(java.io.IOException) Endpoint(org.apache.cxf.endpoint.Endpoint) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) CXFEHCacheReplayCache(org.apache.cxf.ws.security.cache.CXFEHCacheReplayCache) MemoryReplayCache(org.apache.wss4j.common.cache.MemoryReplayCache) Endpoint(org.apache.cxf.endpoint.Endpoint)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Bus (org.apache.cxf.Bus)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)1 CXFEHCacheReplayCache (org.apache.cxf.ws.security.cache.CXFEHCacheReplayCache)1 MemoryReplayCache (org.apache.wss4j.common.cache.MemoryReplayCache)1 ReplayCache (org.apache.wss4j.common.cache.ReplayCache)1 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)1