Search in sources :

Example 6 with LinkedDataFetchingException

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;
}
Also used : DatasetResponseWithStatusCodeAndHeaders(won.protocol.rest.DatasetResponseWithStatusCodeAndHeaders) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) Element(net.sf.ehcache.Element) Duration(ch.qos.logback.core.util.Duration)

Example 7 with LinkedDataFetchingException

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();
    }
}
Also used : Dataset(org.apache.jena.query.Dataset) PublicKey(java.security.PublicKey) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) URI(java.net.URI)

Example 8 with LinkedDataFetchingException

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();
}
Also used : Dataset(org.apache.jena.query.Dataset) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URI(java.net.URI)

Example 9 with LinkedDataFetchingException

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();
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) Arrays(java.util.Arrays) LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AuthenticationThreadLocal(won.protocol.util.AuthenticationThreadLocal) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) KeyStoreException(java.security.KeyStoreException) HashSet(java.util.HashSet) OnExportUserEvent(won.owner.web.events.OnExportUserEvent) Charset(java.nio.charset.Charset) URI(java.net.URI) ZipEntry(java.util.zip.ZipEntry) Dataset(org.apache.jena.query.Dataset) Logger(org.slf4j.Logger) WonLinkedDataUtils(won.protocol.util.linkeddata.WonLinkedDataUtils) MethodHandles(java.lang.invoke.MethodHandles) WonOwnerMailSender(won.owner.web.WonOwnerMailSender) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ApplicationListener(org.springframework.context.ApplicationListener) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) File(java.io.File) Component(org.springframework.stereotype.Component) KeystoreEnabledUserDetails(won.owner.service.impl.KeystoreEnabledUserDetails) User(won.owner.model.User) RDFDataMgr(org.apache.jena.riot.RDFDataMgr) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RDFFormat(org.apache.jena.riot.RDFFormat) Authentication(org.springframework.security.core.Authentication) UserService(won.owner.service.impl.UserService) User(won.owner.model.User) ZipEntry(java.util.zip.ZipEntry) KeystoreEnabledUserDetails(won.owner.service.impl.KeystoreEnabledUserDetails) CertificateException(java.security.cert.CertificateException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Authentication(org.springframework.security.core.Authentication) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) File(java.io.File)

Example 10 with LinkedDataFetchingException

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;
}
Also used : Dataset(org.apache.jena.query.Dataset) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

LinkedDataFetchingException (won.protocol.rest.LinkedDataFetchingException)14 Dataset (org.apache.jena.query.Dataset)10 URI (java.net.URI)6 HashSet (java.util.HashSet)4 ExecutionException (java.util.concurrent.ExecutionException)4 Set (java.util.Set)3 HttpHeaders (org.springframework.http.HttpHeaders)3 DatasetResponseWithStatusCodeAndHeaders (won.protocol.rest.DatasetResponseWithStatusCodeAndHeaders)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 PublicKey (java.security.PublicKey)2 RDFDataMgr (org.apache.jena.riot.RDFDataMgr)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 AuthenticationThreadLocal (won.protocol.util.AuthenticationThreadLocal)2 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)1 Duration (ch.qos.logback.core.util.Duration)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1