use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.
the class JournalBasedTokenStateService method getTokenExpiration.
@Override
public long getTokenExpiration(final String tokenId, boolean validate) throws UnknownTokenException {
// Check the in-memory collection first, to avoid file access when possible
try {
// return the expiration from there.
return super.getTokenExpiration(tokenId, validate);
} catch (UnknownTokenException e) {
// It's not in memory
}
if (validate) {
validateToken(tokenId);
}
// If there is no associated state in the in-memory cache, proceed to check the journal
long expiration = 0;
try {
JournalEntry entry = journal.get(tokenId);
if (entry == null) {
throw new UnknownTokenException(tokenId);
}
expiration = Long.parseLong(entry.getExpiration());
super.addToken(tokenId, Long.parseLong(entry.getIssueTime()), expiration, Long.parseLong(entry.getMaxLifetime()));
} catch (IOException e) {
log.failedToLoadJournalEntry(e);
}
return expiration;
}
use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.
the class JournalBasedTokenStateService method getMaxLifetime.
@Override
protected long getMaxLifetime(final String tokenId) {
long result = super.getMaxLifetime(tokenId);
// If there is no result from the in-memory collection, proceed to check the journal
if (result < 1L) {
try {
JournalEntry entry = journal.get(tokenId);
if (entry == null) {
throw new UnknownTokenException(tokenId);
}
result = Long.parseLong(entry.getMaxLifetime());
super.setMaxLifetime(tokenId, Long.parseLong(entry.getIssueTime()), result);
} catch (Exception e) {
log.failedToLoadJournalEntry(e);
}
}
return result;
}
use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.
the class JournalBasedTokenStateService method init.
@Override
public void init(final GatewayConfig config, final Map<String, String> options) throws ServiceLifecycleException {
super.init(config, options);
try {
// Initialize the token state journal
journal = TokenStateJournalFactory.create(config);
// Load any persisted journal entries, and add them to the in-memory collection
List<JournalEntry> entries = journal.get();
for (JournalEntry entry : entries) {
String id = entry.getTokenId();
try {
long issueTime = Long.parseLong(entry.getIssueTime());
long expiration = Long.parseLong(entry.getExpiration());
long maxLifetime = Long.parseLong(entry.getMaxLifetime());
// Add the token state to memory
super.addToken(id, issueTime, expiration, maxLifetime);
} catch (Exception e) {
log.failedToLoadJournalEntry(Tokens.getTokenIDDisplayText(id), e);
}
}
} catch (IOException e) {
throw new ServiceLifecycleException("Failed to load persisted state from the token state journal", e);
}
}
use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.
the class MultiFileTokenStateJournal method get.
@Override
public JournalEntry get(final String tokenId) throws IOException {
JournalEntry result = null;
Path entryFilePath = journalDir.resolve(tokenId + ENTRY_FILE_EXT);
if (Files.exists(entryFilePath)) {
try (FileChannel fileChannel = FileChannel.open(entryFilePath, StandardOpenOption.READ)) {
fileChannel.lock(0L, Long.MAX_VALUE, true);
List<FileJournalEntry> entries = loadJournal(fileChannel);
if (entries.isEmpty()) {
log.journalEntryNotFound(Tokens.getTokenIDDisplayText(tokenId));
} else {
result = entries.get(0);
}
}
} else {
log.journalEntryNotFound(Tokens.getTokenIDDisplayText(tokenId));
}
return result;
}
use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.
the class MultiFileTokenStateJournal method add.
@Override
public void add(final List<JournalEntry> entries) throws IOException {
// Persist each journal entry as an individual file in the journal directory
for (JournalEntry entry : entries) {
final Path entryFile = journalDir.resolve(entry.getTokenId() + ENTRY_FILE_EXT);
log.persistingJournalEntry(getDisplayableJournalFilepath(entry.getTokenId(), entryFile.toString()));
try (FileChannel fileChannel = FileChannel.open(entryFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
fileChannel.lock();
try (OutputStream out = Channels.newOutputStream(fileChannel)) {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
writer.write(entry.toString());
writer.newLine();
writer.flush();
}
log.addedJournalEntry(Tokens.getTokenIDDisplayText(entry.getTokenId()));
} catch (IOException e) {
log.failedToPersistJournalEntry(Tokens.getTokenIDDisplayText(entry.getTokenId()), e);
throw e;
}
}
}
Aggregations