Search in sources :

Example 1 with AtomState

use of won.protocol.model.AtomState in project webofneeds by researchstudio-sat.

the class RestAtomController method getAllAtomsOfUser.

/**
 * returns a List containing atoms belonging to the user
 *
 * @return JSON List of atom objects
 */
@ResponseBody
@RequestMapping(value = { "/", "" }, produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public Map<URI, AtomPojo> getAllAtomsOfUser(@RequestParam(value = "state", required = false) AtomState state) {
    User user = getCurrentUser();
    Set<UserAtom> userAtoms = user.getUserAtoms();
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return userAtoms.parallelStream().map(userAtom -> {
        try {
            AuthenticationThreadLocal.setAuthentication(authentication);
            return new AtomPojo(linkedDataSourceOnBehalfOfAtom.getDataForResource(userAtom.getUri(), userAtom.getUri()));
        } catch (Exception e) {
            // we catch all exceptions here as we want to be more robust against
            // unforseen error conditions down the stack
            logger.debug("Could not retrieve atom<" + userAtom.getUri() + "> cause: " + e.getMessage());
            return null;
        } finally {
            // be sure to remove the principal from the threadlocal
            AuthenticationThreadLocal.remove();
        }
    }).filter(Objects::nonNull).collect(Collectors.toMap(AtomPojo::getUri, atomPojo -> atomPojo));
}
Also used : DraftRepository(won.owner.repository.DraftRepository) StringUtils(org.apache.commons.lang.StringUtils) LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) java.util(java.util) UserAtom(won.owner.model.UserAtom) Coordinate(won.protocol.model.Coordinate) AuthenticationThreadLocal(won.protocol.util.AuthenticationThreadLocal) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) UserRepository(won.owner.repository.UserRepository) Controller(org.springframework.stereotype.Controller) AtomPojo(won.owner.pojo.AtomPojo) Propagation(org.springframework.transaction.annotation.Propagation) CreateDraftPojo(won.owner.pojo.CreateDraftPojo) Qualifier(org.springframework.beans.factory.annotation.Qualifier) URI(java.net.URI) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) Logger(org.slf4j.Logger) WonLinkedDataUtils(won.protocol.util.linkeddata.WonLinkedDataUtils) MediaType(org.springframework.http.MediaType) MethodHandles(java.lang.invoke.MethodHandles) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Collectors(java.util.stream.Collectors) AtomState(won.protocol.model.AtomState) LDPContainerPage(won.protocol.util.linkeddata.LDPContainerPage) HttpStatus(org.springframework.http.HttpStatus) User(won.owner.model.User) WONUserDetailService(won.owner.service.impl.WONUserDetailService) DateTimeFormatter(java.time.format.DateTimeFormatter) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ResponseEntity(org.springframework.http.ResponseEntity) Draft(won.owner.model.Draft) Authentication(org.springframework.security.core.Authentication) Transactional(org.springframework.transaction.annotation.Transactional) User(won.owner.model.User) UserAtom(won.owner.model.UserAtom) Authentication(org.springframework.security.core.Authentication) AtomPojo(won.owner.pojo.AtomPojo) URISyntaxException(java.net.URISyntaxException) AccessDeniedException(org.springframework.security.access.AccessDeniedException)

Example 2 with AtomState

use of won.protocol.model.AtomState in project webofneeds by researchstudio-sat.

the class LinkedDataWebController method listAtomURIs.

@RequestMapping(value = "${uri.path.data}/atom", method = RequestMethod.GET, produces = { "application/ld+json", "application/trig", "application/n-quads" })
public ResponseEntity<Dataset> listAtomURIs(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "p", required = false) Integer page, // assumes newest-first
@RequestParam(value = "resumebefore", required = false) String resumeBefore, // assumes newest-first
@RequestParam(value = "resumeafter", required = false) String resumeAfter, // this
@RequestParam(value = "modifiedafter", required = false) String modifiedAfter, @RequestParam(value = "createdafter", required = false) String createdAfter, @RequestParam(value = "filterBySocketTypeUri", required = false) String filterBySocketTypeUriString, @RequestParam(value = "filterByAtomTypeUri", required = false) String filterByAtomTypeUriString, @RequestParam(value = "state", required = false) String state) throws IOException, ParseException {
    logger.debug("listAtomURIs() for page " + page + " called");
    Dataset rdfDataset;
    HttpHeaders headers = new HttpHeaders();
    Integer preferedSize = getPreferredSize(request);
    String passableQuery = getPassableQueryMap("state", state, "modifiedafter", modifiedAfter, "createdafter", createdAfter, "filterBySocketTypeUri", filterBySocketTypeUriString, "filterByAtomTypeUri", filterByAtomTypeUriString);
    AtomState atomState = getAtomState(state);
    URI filterBySocketTypeUri = getURIOrNull(filterBySocketTypeUriString);
    URI filterByAtomTypeUri = getURIOrNull(filterByAtomTypeUriString);
    if (preferedSize == null && modifiedAfter == null && createdAfter == null) {
        rdfDataset = linkedDataService.listAtomURIs(atomState, filterBySocketTypeUri, filterByAtomTypeUri);
    } else if (page == null && resumeBefore == null && resumeAfter == null && modifiedAfter == null && createdAfter == null) {
        // return latest atoms
        AtomInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listPagedAtomURIs(1, preferedSize, atomState);
        rdfDataset = resource.getContent();
        addPagedResourceInSequenceHeader(headers, URI.create(this.atomResourceURIPrefix), resource, passableQuery);
    } else if (page != null) {
        AtomInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listPagedAtomURIs(page, preferedSize, atomState);
        rdfDataset = resource.getContent();
        addPagedResourceInSequenceHeader(headers, URI.create(this.atomResourceURIPrefix), resource, passableQuery);
    } else if (resumeBefore != null) {
        // resumebefore means we are paging and we are interested in atoms newer than
        // the reference
        URI referenceAtom;
        try {
            referenceAtom = new URI(resumeBefore);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("resumeBefore must be a full, valid atom URI");
        }
        AtomInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listPagedAtomURIsAfter(referenceAtom, preferedSize, atomState);
        rdfDataset = resource.getContent();
        addPagedResourceInSequenceHeader(headers, URI.create(this.atomResourceURIPrefix), resource, passableQuery);
    } else if (resumeAfter != null) {
        // resumebefore means we are paging and we are interested in atoms older than
        // the reference
        URI referenceAtom;
        try {
            referenceAtom = new URI(resumeAfter);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("resumeAfter must be a full, valid atom URI");
        }
        AtomInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listPagedAtomURIsBefore(referenceAtom, preferedSize, atomState);
        rdfDataset = resource.getContent();
        addPagedResourceInSequenceHeader(headers, URI.create(this.atomResourceURIPrefix), resource, passableQuery);
    } else if (createdAfter != null) {
        // do not support paging for now
        rdfDataset = linkedDataService.listAtomURIsCreatedAfter(new DateParameter(createdAfter).getDate(), atomState, filterBySocketTypeUri, filterByAtomTypeUri);
    } else {
        // modifiedafter != null && createdafter == null
        // do not support paging for now
        rdfDataset = linkedDataService.listAtomURIsModifiedAfter(new DateParameter(modifiedAfter).getDate(), atomState, filterBySocketTypeUri, filterByAtomTypeUri);
    }
    addLocationHeaderIfNecessary(headers, URI.create(request.getRequestURI()), URI.create(this.atomResourceURIPrefix));
    addMutableResourceHeaders(headers);
    addCORSHeader(headers);
    return new ResponseEntity<>(rdfDataset, headers, HttpStatus.OK);
}
Also used : AtomInformationService(won.node.service.persistence.AtomInformationService) HttpHeaders(org.springframework.http.HttpHeaders) AtomState(won.protocol.model.AtomState) Dataset(org.apache.jena.query.Dataset) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ResponseEntity(org.springframework.http.ResponseEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with AtomState

use of won.protocol.model.AtomState in project webofneeds by researchstudio-sat.

the class WorkerCrawlerActor method crawlUri.

private void crawlUri(CrawlUriMessage uriMsg) {
    Dataset ds = null;
    List<String> etags = null;
    Lock lock = null;
    try {
        // check if resource is already downloaded
        if (uriMsg instanceof ResourceCrawlUriMessage) {
            ResourceCrawlUriMessage resMsg = ((ResourceCrawlUriMessage) uriMsg);
            if (resMsg.getSerializedResource() != null && resMsg.getSerializationFormat() != null) {
                // TODO: this should be optimized, why deserialize the resource here when we
                // just want to save it in the RDF
                // store? How to insert this serialized resource into the SPARQL endpoint?
                ds = SparqlService.deserializeDataset(resMsg.getSerializedResource(), resMsg.getSerializationFormat());
            }
        }
        // download resource if not already downloaded
        if (ds == null) {
            // use ETag/If-None-Match Headers to make the process more efficient
            HttpHeaders httpHeaders = new HttpHeaders();
            if (uriMsg.getResourceETagHeaderValues() != null && !uriMsg.getResourceETagHeaderValues().isEmpty()) {
                String ifNoneMatchHeaderValue = StringUtils.collectionToDelimitedString(uriMsg.getResourceETagHeaderValues(), ", ");
                httpHeaders.add("If-None-Match", ifNoneMatchHeaderValue);
            }
            DatasetResponseWithStatusCodeAndHeaders datasetWithHeaders = linkedDataSource.getDatasetWithHeadersForResource(URI.create(uriMsg.getUri()), httpHeaders);
            ds = datasetWithHeaders.getDataset();
            etags = datasetWithHeaders.getResponseHeaders().get("ETag");
            // if dataset was not modified (304) we can treat the current crawl uri as done
            if (ds == null && datasetWithHeaders.getStatusCode() == 304) {
                sendDoneUriMessage(uriMsg, uriMsg.getWonNodeUri(), etags);
                return;
            }
            // if there is paging activated and the won node tells us that there is more
            // data (previous link)
            // to be downloaded, then we add this link to the crawling process too
            String prevLink = linkedDataSource.getPreviousLinkFromDatasetWithHeaders(datasetWithHeaders);
            if (prevLink != null) {
                CrawlUriMessage newUriMsg = new CrawlUriMessage(uriMsg.getBaseUri(), prevLink, uriMsg.getWonNodeUri(), CrawlUriMessage.STATUS.PROCESS, System.currentTimeMillis(), null);
                getSender().tell(newUriMsg, getSelf());
            }
        }
        lock = ds == null ? null : ds.getLock();
        lock.enterCriticalSection(true);
        // Save dataset to triple store
        sparqlService.updateNamedGraphsOfDataset(ds);
        String wonNodeUri = extractWonNodeUri(ds, uriMsg.getUri());
        if (wonNodeUri == null) {
            wonNodeUri = uriMsg.getWonNodeUri();
        }
        // do nothing more here if the STATUS of the message was SAVE
        if (uriMsg.getStatus().equals(CrawlUriMessage.STATUS.SAVE)) {
            log.debug("processed crawl uri event {} with status 'SAVE'", uriMsg);
            return;
        }
        // extract URIs from current resource and send extracted URI messages back to
        // sender
        log.debug("Extract URIs from message {}", uriMsg);
        Set<CrawlUriMessage> newCrawlMessages = sparqlService.extractCrawlUriMessages(uriMsg.getBaseUri(), wonNodeUri);
        for (CrawlUriMessage newMsg : newCrawlMessages) {
            getSender().tell(newMsg, getSelf());
        }
        // signal sender that this URI is processed and save meta data about crawling
        // the URI.
        // This needs to be done after all extracted URI messages have been sent to
        // guarantee consistency
        // in case of failure
        sendDoneUriMessage(uriMsg, wonNodeUri, etags);
        // bu
        if (AtomModelWrapper.isAAtom(ds)) {
            AtomModelWrapper atomModelWrapper = new AtomModelWrapper(ds, false);
            AtomState state = atomModelWrapper.getAtomState();
            AtomEvent.TYPE type = state.equals(AtomState.ACTIVE) ? AtomEvent.TYPE.ACTIVE : AtomEvent.TYPE.INACTIVE;
            log.debug("Created atom event for atom uri {}", uriMsg.getUri());
            long crawlDate = System.currentTimeMillis();
            AtomEvent atomEvent = new AtomEvent(uriMsg.getUri(), wonNodeUri, type, crawlDate, ds, Cause.CRAWLED);
            pubSubMediator.tell(new DistributedPubSubMediator.Publish(atomEvent.getClass().getName(), atomEvent), getSelf());
        }
    } catch (RestClientException e1) {
        // usually happens if the fetch of the dataset fails e.g.
        // HttpServerErrorException, HttpClientErrorException
        log.debug("Exception during crawling: " + e1);
        throw new CrawlWrapperException(e1, uriMsg);
    } catch (LinkedDataFetchingException e) {
        log.debug("Exception during crawling: " + e);
        Throwable cause = e.getCause();
        if (cause instanceof HttpClientErrorException && Objects.equals(((HttpClientErrorException) cause).getStatusCode(), HttpStatus.GONE)) {
            log.debug("Uri used to exist, but has been deleted, deleting from rdf store.");
            sendDeletedAtomMessage(uriMsg.getUri(), uriMsg.getWonNodeUri());
            sendDeletedUriMessage(uriMsg, uriMsg.getWonNodeUri(), etags);
        } else if (cause instanceof HttpClientErrorException && Objects.equals(((HttpClientErrorException) cause).getStatusCode(), HttpStatus.FORBIDDEN)) {
            log.debug("Not allowed to access uri, marking as done");
            sendDoneUriMessage(uriMsg, uriMsg.getWonNodeUri(), etags);
        } else {
            throw new CrawlWrapperException(e, uriMsg);
        }
    } catch (Exception e) {
        log.debug("Exception during crawling: " + e);
        throw new CrawlWrapperException(e, uriMsg);
    } finally {
        if (lock != null) {
            lock.leaveCriticalSection();
        }
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CrawlUriMessage(won.matcher.service.crawler.msg.CrawlUriMessage) ResourceCrawlUriMessage(won.matcher.service.crawler.msg.ResourceCrawlUriMessage) DatasetResponseWithStatusCodeAndHeaders(won.protocol.rest.DatasetResponseWithStatusCodeAndHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) AtomState(won.protocol.model.AtomState) DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) Dataset(org.apache.jena.query.Dataset) CrawlWrapperException(won.matcher.service.crawler.exception.CrawlWrapperException) CrawlWrapperException(won.matcher.service.crawler.exception.CrawlWrapperException) IncorrectPropertyCountException(won.protocol.exception.IncorrectPropertyCountException) RestClientException(org.springframework.web.client.RestClientException) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Lock(org.apache.jena.shared.Lock) ResourceCrawlUriMessage(won.matcher.service.crawler.msg.ResourceCrawlUriMessage) AtomEvent(won.matcher.service.common.event.AtomEvent) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) RestClientException(org.springframework.web.client.RestClientException) AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 4 with AtomState

use of won.protocol.model.AtomState in project webofneeds by researchstudio-sat.

the class LinkedDataWebController method showAtomURIListPage.

// webmvc controller method
@RequestMapping("${uri.path.page}/atom")
public String showAtomURIListPage(@RequestParam(value = "p", required = false) Integer page, @RequestParam(value = "resumebefore", required = false) String resumeBefore, @RequestParam(value = "resumeafter", required = false) String resumeAfter, @RequestParam(value = "modifiedafter", required = false) String modifiedAfter, @RequestParam(value = "createdafter", required = false) String createdAfter, @RequestParam(value = "filterBySocketTypeUri", required = false) String filterBySocketTypeUriString, @RequestParam(value = "filterByAtomTypeUri", required = false) String filterByAtomTypeUriString, @RequestParam(value = "state", required = false) String state, HttpServletRequest request, Model model, HttpServletResponse response) throws IOException, ParseException {
    Dataset rdfDataset;
    AtomState atomState = getAtomState(state);
    URI filterBySocketTypeUri = getURIOrNull(filterBySocketTypeUriString);
    URI filterByAtomTypeUri = getURIOrNull(filterByAtomTypeUriString);
    if (page == null && resumeBefore == null && resumeAfter == null && modifiedAfter == null && createdAfter == null) {
        rdfDataset = linkedDataService.listAtomURIs(atomState, filterBySocketTypeUri, filterByAtomTypeUri);
    } else if (page != null) {
        AtomInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listPagedAtomURIs(page, null, atomState);
        rdfDataset = resource.getContent();
    } else if (resumeBefore != null) {
        URI referenceAtom;
        try {
            referenceAtom = new URI(resumeBefore);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("resumeBefore must be a full, valid atom URI");
        }
        AtomInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listPagedAtomURIsAfter(referenceAtom, null, atomState);
        rdfDataset = resource.getContent();
    } else if (resumeAfter != null) {
        // resumeAfter != null
        URI referenceAtom;
        try {
            referenceAtom = new URI(resumeAfter);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("resumeAfter must be a full, valid atom URI");
        }
        AtomInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listPagedAtomURIsBefore(referenceAtom, null, atomState);
        rdfDataset = resource.getContent();
    } else if (createdAfter != null) {
        // do not support paging for now
        rdfDataset = linkedDataService.listAtomURIsCreatedAfter(new DateParameter(createdAfter).getDate(), atomState, filterBySocketTypeUri, filterByAtomTypeUri);
    } else {
        // modifiedafter != null && createdafter == null
        // do not support paging for now
        rdfDataset = linkedDataService.listAtomURIsModifiedAfter(new DateParameter(modifiedAfter).getDate(), atomState, filterBySocketTypeUri, filterByAtomTypeUri);
    }
    model.addAttribute("rdfDataset", rdfDataset);
    model.addAttribute("resourceURI", uriService.toResourceURIIfPossible(URI.create(request.getRequestURI())).toString());
    model.addAttribute("dataURI", uriService.toDataURIIfPossible(URI.create(request.getRequestURI())).toString());
    return "rdfDatasetView";
}
Also used : AtomInformationService(won.node.service.persistence.AtomInformationService) AtomState(won.protocol.model.AtomState) Dataset(org.apache.jena.query.Dataset) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Dataset (org.apache.jena.query.Dataset)4 AtomState (won.protocol.model.AtomState)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 HttpHeaders (org.springframework.http.HttpHeaders)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 AtomInformationService (won.node.service.persistence.AtomInformationService)2 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)1 MethodHandles (java.lang.invoke.MethodHandles)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang.StringUtils)1 Lock (org.apache.jena.shared.Lock)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1