Search in sources :

Example 11 with LinkedDataFetchingException

use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.

the class WonConversationUtils method getAgreementProtocolState.

public static AgreementProtocolState getAgreementProtocolState(URI connectionUri, LinkedDataSource linkedDataSource) {
    URI atomUri = WonRelativeUriHelper.stripConnectionSuffix(connectionUri);
    // allow each resource to be re-crawled once for each reason
    Set<URI> recrawledForIncompleteness = new HashSet<>();
    Set<URI> recrawledForFailedFetch = new HashSet<>();
    while (true) {
        // we leave the loop either with a runtime exception or with the result
        try {
            Dataset conversationDataset = WonLinkedDataUtils.getConversationAndAtomsDatasetUsingAtomUriAsWebId(connectionUri, linkedDataSource);
            return AgreementProtocolState.of(conversationDataset);
        } catch (IncompleteConversationDataException e) {
            // we may have tried to crawl a conversation dataset of which messages
            // were still in-flight. we allow one re-crawl attempt per exception before
            // we throw the exception on:
            refreshDataForConnection(connectionUri, atomUri, linkedDataSource);
            if (!recrawl(recrawledForIncompleteness, connectionUri, atomUri, linkedDataSource, e.getMissingMessageUri(), e.getReferringMessageUri())) {
                throw e;
            }
        } catch (LinkedDataFetchingException e) {
            // we may have tried to crawl a conversation dataset of which messages
            // were still in-flight. we allow one re-crawl attempt per exception before
            // we throw the exception on:
            refreshDataForConnection(connectionUri, atomUri, linkedDataSource);
            if (!recrawl(recrawledForFailedFetch, connectionUri, atomUri, linkedDataSource, e.getResourceUri())) {
                throw e;
            }
        }
    }
}
Also used : Dataset(org.apache.jena.query.Dataset) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) IncompleteConversationDataException(won.protocol.agreement.IncompleteConversationDataException) URI(java.net.URI) HashSet(java.util.HashSet)

Example 12 with LinkedDataFetchingException

use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.

the class AuthEnabledLinkedDataRestClient method readAccessTokens.

public Set<String> readAccessTokens(URI resourceURI, RestTemplate restTemplate, HttpHeaders requestHeaders) {
    assert resourceURI != null : "resource URI must not be null";
    StopWatch sw = new StopWatch();
    sw.start();
    logger.debug("fetching linked data resource: {}", resourceURI);
    // If a RestClientException is thrown here complaining that it can't read a
    // Model with MIME media type text/html,
    // it was probably the wrong resourceURI
    Set<String> result;
    int statusCode;
    HttpHeaders responseHeaders;
    try {
        HttpEntity entity = new HttpEntity(null, requestHeaders);
        ResponseEntity<Set> response = restTemplate.exchange(resourceURI, HttpMethod.GET, entity, Set.class);
        // RestTemplate will automatically follow redirects on HttpGet calls
        statusCode = response.getStatusCode().value();
        responseHeaders = response.getHeaders();
        if (response.getStatusCode().is4xxClientError()) {
            throw new HttpClientErrorException(response.getStatusCode());
        }
        if (response.getStatusCode().is5xxServerError()) {
            throw new HttpServerErrorException(response.getStatusCode());
        }
        result = response.getBody();
    } catch (RestClientException e) {
        // first, let's see if we can answer the request from loaded ontologies:
        if (e instanceof HttpClientErrorException) {
            throw new LinkedDataFetchingException(resourceURI, MessageFormat.format("Caught a HttpClientErrorException exception trying to obtain token from {0}. Underlying error message is: {1}, response Body: {2}", resourceURI, e.getMessage(), ((HttpClientErrorException) e).getResponseBodyAsString()), e, ((HttpClientErrorException) e).getRawStatusCode());
        }
        if (e instanceof HttpServerErrorException) {
            throw new LinkedDataFetchingException(resourceURI, MessageFormat.format("Caught a HttpServerErrorException exception trying to obtain token from {0}. Underlying error message is: {1}, response Body: {2}", resourceURI, e.getMessage(), ((HttpServerErrorException) e).getResponseBodyAsString()), e, ((HttpServerErrorException) e).getRawStatusCode());
        }
        throw new LinkedDataFetchingException(resourceURI, MessageFormat.format("Caught a clientHandler exception trying to obtain token from {0}. Underlying error message is: {1}", resourceURI, e.getMessage()), e);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("fetched auth token from {}", resourceURI);
    }
    sw.stop();
    logger.debug(LogMarkers.TIMING, "fetching {} took {} millis", resourceURI, sw.getLastTaskTimeMillis());
    return result;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Set(java.util.Set) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) RestClientException(org.springframework.web.client.RestClientException) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) StopWatch(org.springframework.util.StopWatch)

Example 13 with LinkedDataFetchingException

use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.

the class AuthEnabledLinkedDataSource method getDataForResource.

/**
 * Obtains the description of the reseource identified by the given resourceURI,
 * using the specified authToken to authenticate.
 *
 * @param resource
 * @param authToken
 * @return
 */
public Dataset getDataForResource(URI resource, final String authToken) {
    if (resource == null || authToken == null) {
        throw new IllegalArgumentException("resource and authToken must not be null");
    }
    resource = wonMessageUriResolver.toLocalMessageURI(resource, this);
    logger.debug("fetching linked data for URI {} with authToken (protected)", resource);
    Dataset dataset = DatasetFactory.createGeneral();
    try {
        dataset = getLinkedDataRestClient().readResourceData(resource, authToken);
        if (logger.isDebugEnabled()) {
            logger.debug("fetched resource {} with authToken (protected):", resource);
            RDFDataMgr.write(System.out, dataset, Lang.TRIG);
        }
    } catch (LinkedDataFetchingException e) {
        throw e;
    } catch (Exception e) {
        logger.debug(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)

Example 14 with LinkedDataFetchingException

use of won.protocol.rest.LinkedDataFetchingException in project webofneeds by researchstudio-sat.

the class RematchSparqlService method findAtomsForRematching.

public Set<BulkAtomEvent> findAtomsForRematching() {
    logger.debug("searching atoms for rematching");
    StringBuilder builder = new StringBuilder();
    // Selects atomUris using a back-off strategy, each time doubling
    // the time difference to the reference date
    builder.append(" prefix won: <https://w3id.org/won/core#> \n");
    builder.append(" select distinct ?atomUri where {  \n");
    builder.append("    graph won:rematchMetadata { \n");
    builder.append("        ?atomUri won:referenceDate ?rDate ; \n");
    builder.append("                  won:matchAttemptDate ?mDate . \n");
    builder.append("         filter (?mDate >= ?rDate) \n");
    builder.append("         bind (?mDate - ?rDate as ?lastDiff) \n");
    builder.append("         bind (?now - ?rDate as ?diff) \n");
    builder.append("     } \n");
    builder.append("  \n");
    builder.append("     filter(?diff > 2 * ?lastDiff) \n");
    builder.append(" } \n");
    ParameterizedSparqlString pps = new ParameterizedSparqlString();
    pps.setNsPrefix("won", "https://w3id.org/won/core#");
    pps.setCommandText(builder.toString());
    pps.setLiteral("now", System.currentTimeMillis());
    Set<BulkAtomEvent> bulks = new HashSet<>();
    BulkAtomEvent bulkAtomEvent = new BulkAtomEvent();
    bulks.add(bulkAtomEvent);
    try (QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, pps.asQuery())) {
        ResultSet results = qexec.execSelect();
        // load all the atoms into one bulk atom event
        while (results.hasNext()) {
            QuerySolution qs = results.nextSolution();
            String atomUri = qs.get("atomUri").asResource().getURI();
            try {
                if (logger.isDebugEnabled()) {
                    logger.debug("Rematching {}, fetching its data...", atomUri);
                }
                Dataset ds = linkedDataSource.getDataForPublicResource(URI.create(atomUri));
                if (AtomModelWrapper.isAAtom(ds)) {
                    StringWriter sw = new StringWriter();
                    RDFDataMgr.write(sw, ds, RDFFormat.TRIG.getLang());
                    AtomEvent atomEvent = new AtomEvent(atomUri, null, AtomEvent.TYPE.ACTIVE, System.currentTimeMillis(), sw.toString(), RDFFormat.TRIG.getLang(), Cause.SCHEDULED_FOR_REMATCH);
                    bulkAtomEvent.addAtomEvent(atomEvent);
                    if (bulkAtomEvent.getAtomEvents().size() >= MAX_ATOMS_PER_REMATCH_BULK) {
                        bulkAtomEvent = new BulkAtomEvent();
                        bulks.add(bulkAtomEvent);
                    }
                }
            } catch (LinkedDataFetchingException e) {
                if (e.getStatusCode().isPresent()) {
                    HttpStatus status = HttpStatus.valueOf(e.getStatusCode().get());
                    if (status == HttpStatus.GONE) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Rematching {}: got response status {}, removing resource from index", atomUri, status);
                        }
                        // add the event indicating that the atom was deleted
                        bulkAtomEvent.addAtomEvent(new AtomEvent(atomUri, null, TYPE.DELETED, System.currentTimeMillis(), Cause.SCHEDULED_FOR_REMATCH));
                    }
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Rematching {}: error retrieving linked data - not rematching at this time, will try again later", atomUri);
                }
                // do not add event, just directly register a matching attempt
                registerMatchingAttempt(TYPE.ACTIVE, atomUri, Cause.SCHEDULED_FOR_REMATCH);
            }
        }
    }
    logger.debug("atomEvents for rematching: " + bulkAtomEvent.getAtomEvents().size());
    return bulks;
}
Also used : HttpStatus(org.springframework.http.HttpStatus) BulkAtomEvent(won.matcher.service.common.event.BulkAtomEvent) StringWriter(java.io.StringWriter) BulkAtomEvent(won.matcher.service.common.event.BulkAtomEvent) AtomEvent(won.matcher.service.common.event.AtomEvent) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) HashSet(java.util.HashSet)

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