use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.
the class CachingLinkedDataSource method fetchAndCacheIfAppropriate.
private DatasetResponseWithStatusCodeAndHeaders fetchAndCacheIfAppropriate(final URI resource, final URI requesterWebID, final LinkedDataCacheEntry linkedDataCacheEntry, final HttpHeaders headers) {
DatasetResponseWithStatusCodeAndHeaders responseData = fetchWithEtagValidation(resource, requesterWebID, linkedDataCacheEntry, headers);
Date expires = parseCacheControlMaxAgeValue(resource, responseData);
if (responseData.getDataset() == null) {
throw new LinkedDataFetchingException(resource, "Could not load dataset for URI " + resource + " and requesterWebID " + requesterWebID);
}
if (expires == null) {
expires = parseExpiresHeader(resource, responseData);
if (expires != null && expires.getTime() == 0) {
// Don't cache.
if (logger.isDebugEnabled()) {
logger.debug("Fetched {}. Will not be cached due to invalid 'Expires' header sent by server", resource);
}
return responseData;
}
}
EnumSet<CacheControlFlag> cacheControlFlags = parseCacheControlHeaderFlags(resource, responseData);
if (cacheControlFlags.contains(CacheControlFlag.NO_STORE) || cacheControlFlags.contains(CacheControlFlag.NO_CACHE)) {
// we are not allowed to cache the result
// make sure it's not in the cache from a previous request
cache.remove(makeCacheKey(resource, requesterWebID));
if (logger.isDebugEnabled()) {
logger.debug("Fetched {}. Will not be cached due to Cache-Control headers sent by server", resource);
}
return responseData;
}
Date responseDate = parseDateHeader(resource, responseData);
if (responseDate != null && expires != null) {
// old way of saying don't cache: Date header >= Expires header
if (responseDate.equals(expires) || responseDate.after(expires)) {
// make sure it's not in the cache from a previous request
if (logger.isDebugEnabled()) {
logger.debug("Fetched {}. Will not be cached due to Expires/Date header combination sent by server", resource);
}
cache.remove(makeCacheKey(resource, requesterWebID));
return responseData;
}
}
// if we don't get a new etag, see if we have a 304 code - then we can use th
// old etag
String etag = responseData.getResponseHeaders().getFirst(HttpHeaders.ETAG);
if (etag == null && responseData.getStatusCode() == HttpStatus.NOT_MODIFIED.value() && linkedDataCacheEntry != null) {
etag = linkedDataCacheEntry.getEtag();
}
// cache the result
LinkedDataCacheEntry entry = new LinkedDataCacheEntry(etag, expires, writeDatasetToByteArray(responseData.getDataset()), cacheControlFlags, responseData.getResponseHeaders(), responseData.getStatusCode());
this.cache.put(new Element(makeCacheKey(resource, requesterWebID), entry));
if (logger.isDebugEnabled()) {
logger.debug("Fetched and cached {}, will {}", resource, expires == null ? "never expire" : "expire in " + new Duration(expires.getTime() - new Date().getTime()).toString());
logger.debug("cache size: {} elements, in-memory size: {} bytes", cache.getSize(), cache.calculateInMemorySize());
}
return responseData;
}
use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.
the class DefaultWebIdKeyLoader method loadKeyRemotely.
public Set<PublicKey> loadKeyRemotely(String refKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
try {
URI keyUri = URI.create(refKey);
Dataset atomDataset = linkedDataSource.getDataForResource(keyUri, URI.create(cryptographyService.getDefaultPrivateKeyAlias()));
Set<PublicKey> resolvedKeys = wonKeysReaderWriter.readKeyFromAtom(keyUri, atomDataset, refKey);
return resolvedKeys;
} catch (LinkedDataFetchingException e) {
logger.info("Error fetching public key for uri {}: {}", refKey, e.getMessage());
return Collections.emptySet();
}
}
use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.
the class WonLinkedDataUtils method getConnectionURIForSocketAndTargetSocket.
public static Optional<URI> getConnectionURIForSocketAndTargetSocket(URI socket, URI targetSocket, LinkedDataSource linkedDataSource, URI requesterWebId) {
Dataset ds = linkedDataSource.getDataForResource(socket, requesterWebId);
Optional<URI> atomUri = WonRdfUtils.SocketUtils.getAtomOfSocket(ds, socket);
if (!atomUri.isPresent()) {
return Optional.empty();
}
Optional<URI> connectionContainer = WonRdfUtils.AtomUtils.getConnectionContainerOfAtom(ds, atomUri.get());
if (!connectionContainer.isPresent()) {
return Optional.empty();
}
Optional<Dataset> connConnData;
try {
connConnData = Optional.ofNullable(linkedDataSource.getDataForResource(URI.create(connectionContainer.get().toString() + "?socket=" + URLEncoder.encode(socket.toString(), "UTF-8") + "&targetSocket=" + URLEncoder.encode(targetSocket.toString(), "UTF-8")), requesterWebId));
} catch (UnsupportedEncodingException e) {
throw new LinkedDataFetchingException(connectionContainer.get(), "Error building request for connection by socket " + socket.toString() + " and targetSocket " + targetSocket.toString());
}
if (!connConnData.isPresent()) {
return Optional.empty();
}
Iterator<URI> it = WonRdfUtils.AtomUtils.getConnections(connConnData.get(), connectionContainer.get());
return it.hasNext() ? Optional.of(it.next()) : Optional.empty();
}
use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.
the class ExportListener method onApplicationEvent.
@Override
public void onApplicationEvent(OnExportUserEvent onExportUserEvent) {
Authentication authentication = onExportUserEvent.getAuthentication();
KeystoreEnabledUserDetails userDetails = ((KeystoreEnabledUserDetails) authentication.getPrincipal());
String password = onExportUserEvent.getKeyStorePassword();
User user = userService.getByUsername(userDetails.getUsername());
String responseMail = onExportUserEvent.getResponseEmail();
File tmpFile = null;
try {
tmpFile = File.createTempFile("won", null);
tmpFile.deleteOnExit();
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(tmpFile), Charset.forName("UTF-8"));
ZipEntry atomsEntry = new ZipEntry("export.nq");
zip.putNextEntry(atomsEntry);
user.getUserAtoms().stream().parallel().map(userAtom -> fetchAtomData(authentication, userAtom.getUri())).forEach(dataset -> {
RDFDataMgr.write(zip, dataset, RDFFormat.NQUADS_UTF8);
});
zip.closeEntry();
ZipEntry keystoreEntry = new ZipEntry("keystore.jks");
zip.putNextEntry(keystoreEntry);
if (password != null && !password.isEmpty()) {
ByteArrayOutputStream tmpStream = new ByteArrayOutputStream();
userDetails.getKeyStore().store(tmpStream, password.toCharArray());
tmpStream.writeTo(zip);
} else {
zip.write("You need to supply a keyStorePassword to get your keystore for security reasons".getBytes());
}
zip.closeEntry();
zip.close();
emailSender.sendExportMessage(onExportUserEvent.getResponseEmail(), tmpFile);
} catch (LinkedDataFetchingException e) {
logger.warn(e.getMessage());
emailSender.sendExportFailedMessage(responseMail);
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
emailSender.sendExportFailedMessage(responseMail);
throw new RuntimeException(e);
} catch (Exception e) {
emailSender.sendExportFailedMessage(responseMail);
throw e;
} finally {
if (tmpFile != null) {
tmpFile.delete();
}
}
}
use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.
the class LinkedDataSourceBase method getDataForPublicResource.
@Override
public Dataset getDataForPublicResource(URI resource) {
if (resource == null) {
throw new IllegalArgumentException("resource must not be null");
}
resource = wonMessageUriResolver.toLocalMessageURI(resource, this);
logger.debug("fetching linked data for URI {}", resource);
Dataset dataset = DatasetFactory.createGeneral();
try {
dataset = linkedDataRestClient.readResourceData(resource);
if (logger.isDebugEnabled()) {
logger.debug("fetched resource {}:", resource);
RDFDataMgr.write(System.out, dataset, Lang.TRIG);
}
} catch (LinkedDataFetchingException e) {
throw e;
} catch (Exception e) {
logger.info(String.format("Couldn't fetch resource %s", resource), e);
}
return dataset;
}
Aggregations