/** {@inheritDoc} */ @Override protected void doExecute(@Nonnull final ProfileRequestContext profileRequestContext, @Nonnull final ProfileInterceptorContext interceptorContext) { try { final String value = Long.toString(System.currentTimeMillis()); final String context = getStorageContext(); final String key = getStorageKey(); final StorageRecord storageRecord = getStorageService().read(context, key); log.debug("{} Read storage record '{}' with context '{}' and key '{}'", getLogPrefix(), storageRecord, context, key); if (storageRecord == null) { log.debug("{} Creating counter with value '{}'", getLogPrefix(), value); getStorageService().create(context, key, value, null); } else { log.debug("{} Updating counter with value '{}'", getLogPrefix(), value); getStorageService().update(context, key, value, null); } } catch (final IOException e) { log.debug("{} Unable to update counter", getLogPrefix(), e); } }
/** {@inheritDoc} */ @Override public void remove(@Nonnull @NotEmpty final String artifact) throws IOException { log.debug("Removing artifact entry: {}", artifact); if (artifact.length() > artifactStoreKeySize) { throw new IOException("Length of artifact (" + artifact.length() + ") exceeds storage capabilities"); } getStorageService().delete(STORAGE_CONTEXT, artifact); }
StorageCapabilities caps = storage.getCapabilities(); if (context.length() > caps.getContextSize()) { log.error("context {} too long for StorageService (limit {})", context, caps.getContextSize()); StorageRecord entry = storage.read(context, key); if (entry == null) { log.debug("Value '{}' was not a replay, adding to cache with expiration time {}", s, expires); storage.create(context, key, "x", expires); return true; } else {
/** * Store a profile interceptor result. * * @param result the profile interceptor result to be stored * @throws IOException if an error occurs */ protected void store(@Nonnull final ProfileInterceptorResult result) throws IOException { final String context = result.getStorageContext(); final String key = result.getStorageKey(); final String value = result.getStorageValue(); final Long expiration = result.getStorageExpiration(); // Create / update loop until we succeed or exhaust attempts. int attempts = 10; boolean success = false; do { success = storageService.create(context, key, value, expiration); if (!success) { // The record already exists, so we need to overwrite via an update. success = storageService.update(context, key, value, expiration); } } while (!success && attempts-- > 0); if (!success) { log.error("{} Exhausted retry attempts storing result '{}'", getLogPrefix(), result); } }
/** {@inheritDoc} */ @Override public boolean contains(@Nonnull @NotEmpty final String artifact) throws IOException { if (artifact.length() > artifactStoreKeySize) { throw new IOException("Length of artifact (" + artifact.length() + ") exceeds storage capabilities"); } return getStorageService().read(STORAGE_CONTEXT, artifact) != null; }
final int contextSize = storageService.getCapabilities().getContextSize(); final int keySize = storageService.getCapabilities().getKeySize(); sessionList = storageService.read(serviceId, serviceKey); } catch (final IOException e) { log.error("Exception while querying based service ID {} and key {}", serviceId, serviceKey, e); if (storageService.updateWithVersion(sessionList.getVersion(), serviceId, serviceKey, updated, Math.max(sessionList.getExpiration(), spSession.getExpirationInstant() + sessionSlop)) == null) { serviceId, serviceKey); } else if (!storageService.create(serviceId, serviceKey, idpSession.getId() + ',', spSession.getExpirationInstant() + sessionSlop)) { log.debug("Secondary index record appeared, retrying as update");
final StorageRecord record; try { record = storageService.read(context, key); if (record != null) { response.setStatus(HttpServletResponse.SC_OK); g.writeObjectFieldStart("data"); g.writeStringField("type", "records"); g.writeStringField("id", storageService.getId() + '/' + context +'/' + key); g.writeObjectFieldStart("attributes"); g.writeStringField("value", record.getValue()); if (storageService.delete(context, key)) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); } else {
lookupBySPSession(@Nonnull final SPSessionCriterion criterion) throws ResolverException { final int contextSize = storageService.getCapabilities().getContextSize(); final int keySize = storageService.getCapabilities().getKeySize(); sessionList = storageService.read(serviceId, serviceKey); } catch (final IOException e) { log.error("Exception while querying based service ID {} and key {}", serviceId, serviceKey, e); final String writeBackValue = writeBackSessionList.toString(); if (writeBackValue.length() == 0) { storageService.deleteWithVersion(sessionList.getVersion(), serviceId, serviceKey); } else if (!writeBackValue.equals(sessionList.getValue())) { storageService.updateWithVersion(sessionList.getVersion(), serviceId, serviceKey, writeBackValue, sessionList.getExpiration());
/** * Remove a storage key from the index storage record. * * @param keyToRemove storage key to remove from the index storage record * @return true if key removal succeeded, false otherwise * @throws IOException if an error occurs writing to the storage service */ protected boolean removeKeyFromStorageIndex(@Nonnull final String keyToRemove) throws IOException { final StorageRecord storageRecord = getStorageService().read(getStorageContext(), getStorageIndexKey()); log.debug("{} Read storage record '{}' with context '{}' and key '{}'", getLogPrefix(), storageRecord, getStorageContext(), getStorageIndexKey()); if (storageRecord == null) { log.debug("{} No storage record exists with context '{}' and key '{}', nothing to do", getLogPrefix(), getStorageContext(), getStorageIndexKey()); return false; } else { final LinkedHashSet<String> keys = new LinkedHashSet<>(getStorageKeysFromIndex()); if (keys.remove(keyToRemove)) { log.debug("{} Updating storage index by removing key '{}'", getLogPrefix(), keyToRemove); return getStorageService().update(getStorageContext(), storageIndexKey, keys, storageKeysSerializer, null); } else { log.debug("{} Storage key '{}' not indexed, nothing to do", getLogPrefix(), keyToRemove); return false; } } }
if (sessionId.length() > storageService.getCapabilities().getContextSize()) { throw new SessionException("Session IDs are too large for StorageService, check configuration"); if (!storageService.create(sessionId, SESSION_MASTER_KEY, newSession, serializer, newSession.getCreationInstant() + sessionTimeout + sessionSlop)) { throw new SessionException("A duplicate session ID was generated, unable to create session");
/** {@inheritDoc} */ @Override public void put(@Nonnull @NotEmpty final String artifact, @Nonnull @NotEmpty final String relyingPartyId, @Nonnull @NotEmpty final String issuerId, @Nonnull final SAMLObject samlMessage) throws IOException { if (artifact.length() > artifactStoreKeySize) { throw new IOException("Length of artifact (" + artifact.length() + ") exceeds storage capabilities"); } final SAMLArtifactMapEntry artifactEntry = getEntryFactory().newEntry(artifact, issuerId, relyingPartyId, samlMessage); if (log.isDebugEnabled()) { log.debug("Storing new artifact entry '{}' for relying party '{}', expiring after {} seconds", new Object[] {artifact, relyingPartyId, getArtifactLifetime() / 1000}); } final boolean success = getStorageService().create(STORAGE_CONTEXT, artifact, artifactEntry, (StorageSerializer) getEntryFactory(), System.currentTimeMillis() + getArtifactLifetime()); if (!success) { throw new IOException("A duplicate artifact was generated"); } }
if (getStorageService().getCapabilities().getValueSize() >= flowDescriptor.getExpandedStorageThreshold()) { maxStoredRecords = flowDescriptor.getExpandedNumberOfStoredRecords(); final boolean success = getStorageService().delete(getStorageContext(), keyToDelete);
final Long ver = sessionManager.getStorageService().updateWithVersion(version, getId(), StorageBackedSessionManager.SESSION_MASTER_KEY, this, sessionManager.getStorageSerializer(), getLastActivityInstant() + sessionManager.getSessionTimeout() + sessionManager.getSessionSlop()); sessionManager.getStorageService().read(getId(), StorageBackedSessionManager.SESSION_MASTER_KEY); if (record == null) { log.error("Record for session {} has disappeared from backing store", getId());
/** * Get whether the configured {@link StorageService}'s * {@link org.opensaml.storage.StorageCapabilities#getValueSize()} method meets the * value set via {@link #setStorageServiceThreshold(long)}. * * @return true iff the threshold is met */ public boolean storageServiceMeetsThreshold() { ComponentSupport.ifNotInitializedThrowUninitializedComponentException(this); return storageService.getCapabilities().getValueSize() >= storageServiceThreshold; }
/** {@inheritDoc} */ @Override protected void doInitialize() throws ComponentInitializationException { super.doInitialize(); if (null == idStore) { throw new ComponentInitializationException(getLogPrefix() + " No Id store set"); } log.debug("{} using the store '{}'", getLogPrefix(), idStore.getId()); }
/** {@inheritDoc} */ @Override @Nullable public SAMLArtifactMapEntry get(@Nonnull @NotEmpty final String artifact) throws IOException { log.debug("Attempting to retrieve entry for artifact: {}", artifact); if (artifact.length() > artifactStoreKeySize) { throw new IOException("Length of artifact (" + artifact.length() + ") exceeds storage capabilities"); } final StorageRecord record = getStorageService().read(STORAGE_CONTEXT, artifact); if (record == null) { log.debug("No unexpired entry found for artifact: {}", artifact); return null; } log.debug("Found valid entry for artifact: {}", artifact); return (SAMLArtifactMapEntry) record.getValue((StorageSerializer) getEntryFactory(), STORAGE_CONTEXT, artifact); }
/** * Store a profile interceptor result. * * @param result the profile interceptor result to be stored * @throws IOException if an error occurs * @return boolean whether the record was stored successfully */ protected boolean storeResult(@Nonnull final ProfileInterceptorResult result) throws IOException { final String context = result.getStorageContext(); final String key = result.getStorageKey(); final String value = result.getStorageValue(); final Long expiration = result.getStorageExpiration(); // Create / update loop until we succeed or exhaust attempts. int attempts = 10; boolean success = false; do { success = getStorageService().create(context, key, value, expiration); if (!success) { // The record already exists, so we need to overwrite via an update. success = getStorageService().update(context, key, value, expiration); } } while (!success && attempts-- > 0); if (!success) { log.error("{} Exhausted retry attempts storing result '{}'", getLogPrefix(), result); } return success; }
final int contextSize = storageService.getCapabilities().getContextSize(); final int keySize = storageService.getCapabilities().getKeySize(); sessionList = storageService.read(serviceId, serviceKey); } catch (final IOException e) { log.error("Exception while querying based service ID {} and key {}", serviceId, serviceKey, e); if (storageService.updateWithVersion(sessionList.getVersion(), serviceId, serviceKey, updated, sessionList.getExpiration()) == null) { log.debug("Secondary index record disappeared, nothing to do"); storageService.deleteWithVersion(sessionList.getVersion(), serviceId, serviceKey);
/** * Remove a storage key from the index storage record. * * @param keyToRemove storage key to remove from the index storage record * @return true if key removal succeeded, false otherwise * @throws IOException if an error occurs writing to the storage service */ protected boolean removeKeyFromStorageIndex(@Nonnull final String keyToRemove) throws IOException { final StorageRecord storageRecord = getStorageService().read(getStorageContext(), getStorageIndexKey()); log.debug("{} Read storage record '{}' with context '{}' and key '{}'", getLogPrefix(), storageRecord, getStorageContext(), getStorageIndexKey()); if (storageRecord == null) { log.debug("{} No storage record exists with context '{}' and key '{}', nothing to do", getLogPrefix(), getStorageContext(), getStorageIndexKey()); return false; } else { final LinkedHashSet<String> keys = new LinkedHashSet<>(getStorageKeysFromIndex()); if (keys.remove(keyToRemove)) { log.debug("{} Updating storage index by removing key '{}'", getLogPrefix(), keyToRemove); return getStorageService().update(getStorageContext(), storageIndexKey, keys, storageKeysSerializer, null); } else { log.debug("{} Storage key '{}' not indexed, nothing to do", getLogPrefix(), keyToRemove); return false; } } }
/** * Stores the given ticket in the storage service. * * @param ticket Ticket to store * @param <T> Type of ticket. */ protected <T extends Ticket> void store(final T ticket) { final String context = context(ticket.getClass()); try { final String sessionId = ticket.getSessionId(); final long expiry = ticket.getExpirationInstant().getMillis(); log.debug("Storing mapping of {} to {} in context {}", ticket, sessionId, context); if (!storageService.create(context, ticket.getId(), sessionId, expiry)) { throw new RuntimeException("Failed to store ticket " + ticket); } log.debug("Storing {} in context {}", ticket, sessionId); if (!storageService.create(sessionId, ticket.getId(), ticket, serializer(ticket.getClass()), expiry)) { throw new RuntimeException("Failed to store ticket " + ticket); } } catch (final IOException e) { throw new RuntimeException("Failed to store ticket " + ticket, e); } }