Skip to content

Commit

Permalink
[SHIBUI-570]
Browse files Browse the repository at this point in the history
Added simple caching of fetchMetdata and unmarshallMetadata.
  • Loading branch information
Bill Smith committed Sep 7, 2018
1 parent dcdac35 commit 8cdbef5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public ResponseEntity<?> create(@RequestBody MetadataResolver newResolver) throw
MetadataResolver persistedResolver = resolverRepository.save(newResolver);
positionOrderContainerService.appendPositionOrderForNew(persistedResolver);

//TODO: currently, the update call might explode, but the save works.. in which case, the UI never gets
// an valid response. This operation is not atomic. Should we return an error here?
updateChainingMetadataResolver(persistedResolver);

return ResponseEntity.created(getResourceUriFor(persistedResolver)).body(persistedResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,35 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.lucene.index.IndexWriter;
import org.joda.time.DateTime;
import org.opensaml.core.xml.XMLObject;
import org.opensaml.core.xml.io.UnmarshallingException;
import org.opensaml.saml.metadata.resolver.impl.FileBackedHTTPMetadataResolver;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.io.InputStream;
import java.time.Instant;

import static edu.internet2.tier.shibboleth.admin.util.DurationUtility.toMillis;

/**
* @author Bill Smith (wsmith@unicon.net)
*/
public class OpenSamlFileBackedHTTPMetadataResolver extends FileBackedHTTPMetadataResolver {

private static final long MILLISECONDS_IN_ONE_SECOND = 1000;

private IndexWriter indexWriter;
private FileBackedHttpMetadataResolver sourceResolver;

private OpenSamlMetadataResolverDelegate delegate;

private byte[] cachedMetadataBytes;
private Instant metadataLastFetchedAt;
boolean shouldRefreshMetadata;
XMLObject cachedMetadata;

public OpenSamlFileBackedHTTPMetadataResolver(ParserPool parserPool,
IndexWriter indexWriter,
FileBackedHttpMetadataResolver sourceResolver) throws ResolverException {
Expand Down Expand Up @@ -67,4 +81,31 @@ protected void initMetadataResolver() throws ComponentInitializationException {
this.sourceResolver.getResourceId(),
indexWriter);
}

@Override
protected byte[] fetchMetadata() throws ResolverException {
if (metadataLastFetchedAt == null || shouldRefreshMetadata()) {
this.cachedMetadataBytes = super.fetchMetadata();
this.metadataLastFetchedAt = Instant.now();
}
return cachedMetadataBytes;
}

private boolean shouldRefreshMetadata() {
if ((Instant.now().getEpochSecond() - metadataLastFetchedAt.getEpochSecond()) > (this.getMinRefreshDelay() / MILLISECONDS_IN_ONE_SECOND)) {
shouldRefreshMetadata = true;
}
return shouldRefreshMetadata;
}

@Override
protected XMLObject unmarshallMetadata(@Nonnull final InputStream metadataInput)
throws UnmarshallingException {
//TODO: This should probably be based on something other than minRefreshDelay
if (cachedMetadata == null || shouldRefreshMetadata) {
this.cachedMetadata = super.unmarshallMetadata(metadataInput);
this.shouldRefreshMetadata = false;
}
return this.cachedMetadata;
}
}

0 comments on commit 8cdbef5

Please sign in to comment.