Search in sources :

Example 1 with UserAtom

use of won.owner.model.UserAtom 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 UserAtom

use of won.owner.model.UserAtom in project webofneeds by researchstudio-sat.

the class ServerSideActionController method connectSockets.

// rsponses: 204 (no content) or 409 (conflict)
@RequestMapping(value = "/connect", method = RequestMethod.POST)
public ResponseEntity connectSockets(@RequestBody(required = true) ServerSideConnectPayload connectAction) {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    // cannot use user object from context since hw doesn't know about created in
    // this session atom,
    // therefore, we have to retrieve the user object from the user repository
    User user = userService.getByUsername(username);
    if (user == null) {
        return new ResponseEntity("Cannot process connect action: not logged in.", HttpStatus.FORBIDDEN);
    }
    if (StringUtils.isEmpty(connectAction.getFromSocket())) {
        return new ResponseEntity("Cannot process connect action: from socket not specified.", HttpStatus.CONFLICT);
    }
    if (StringUtils.isEmpty(connectAction.getToSocket())) {
        return new ResponseEntity("Cannot process connect action: to socket not specified.", HttpStatus.CONFLICT);
    }
    Set<UserAtom> atoms = user.getUserAtoms();
    if (!(connectAction.isFromPending() || atoms.stream().anyMatch(atom -> connectAction.getFromSocket().startsWith(atom.getUri().toString())))) {
        return new ResponseEntity("Cannot process connect action: from atom is not owned nor in pending.", HttpStatus.CONFLICT);
    }
    if (connectAction.isAutoOpen() && !(connectAction.isToPending() || atoms.stream().anyMatch(atom -> connectAction.getToSocket().startsWith(atom.getUri().toString())))) {
        return new ResponseEntity("Cannot process connect autoOpen action: to atom is not owned nor in pending.", HttpStatus.CONFLICT);
    }
    serverSideActionService.connect(connectAction, SecurityContextHolder.getContext().getAuthentication());
    return new ResponseEntity(HttpStatus.NO_CONTENT);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) UserAtom(won.owner.model.UserAtom) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) Controller(org.springframework.stereotype.Controller) UserAtomRepository(won.owner.repository.UserAtomRepository) ServerSideConnectPayload(won.owner.pojo.ServerSideConnectPayload) RequestBody(org.springframework.web.bind.annotation.RequestBody) HttpStatus(org.springframework.http.HttpStatus) User(won.owner.model.User) ServerSideActionService(won.owner.web.service.ServerSideActionService) ResponseEntity(org.springframework.http.ResponseEntity) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) UserService(won.owner.service.impl.UserService) ResponseEntity(org.springframework.http.ResponseEntity) User(won.owner.model.User) UserAtom(won.owner.model.UserAtom) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UserAtom

use of won.owner.model.UserAtom in project webofneeds by researchstudio-sat.

the class UserAtomService method removeUserAtomBecauseOfFailedCreation.

private void removeUserAtomBecauseOfFailedCreation(final WonMessage wonMessage, User user) {
    URI atomUri = getOwnedAtomURI(wonMessage);
    // Get the atom from owner application db
    UserAtom userAtom = userAtomRepository.findByAtomUri(atomUri);
    if (userAtom != null) {
        user = userRepository.findById(user.getId()).get();
        // Delete atom in users atom list and save changes
        user.getUserAtoms().remove(userAtom);
        user = userRepository.save(user);
        // Delete atom in atom repository
        userAtomRepository.delete(userAtom);
    }
}
Also used : UserAtom(won.owner.model.UserAtom) URI(java.net.URI)

Example 4 with UserAtom

use of won.owner.model.UserAtom in project webofneeds by researchstudio-sat.

the class UserAtomService method deleteUserAtom.

private void deleteUserAtom(final WonMessage wonMessage, User user) {
    URI atomUri = getOwnedAtomURI(wonMessage);
    // Get the atom from owner application db
    UserAtom userAtom = userAtomRepository.findByAtomUri(atomUri);
    if (userAtom != null) {
        if (userAtom.getState() == AtomState.DELETED) {
            // reload the user so we can save it
            // (the user object we get from getUserForSession is detached)
            user = userRepository.findById(user.getId()).get();
            // Delete atom in users atom list and save changes
            user.getUserAtoms().remove(userAtom);
            user = userRepository.save(user);
            // Delete atom in atom repository
            userAtomRepository.delete(userAtom);
        } else {
            throw new IllegalStateException("atom not in state deleted");
        }
    }
}
Also used : UserAtom(won.owner.model.UserAtom) URI(java.net.URI)

Example 5 with UserAtom

use of won.owner.model.UserAtom in project webofneeds by researchstudio-sat.

the class UserAtomService method saveAtomUriWithUser.

private void saveAtomUriWithUser(final WonMessage wonMessage, User user) {
    URI atomUri = getOwnedAtomURI(wonMessage);
    logger.debug("adding atom {} to atoms of user {}", atomUri, user.getId());
    UserAtom userAtom = new UserAtom(atomUri);
    // reload the user so we can save it
    // (the user object we get from getUserForSession is detached)
    user = userRepository.findById(user.getId()).get();
    userAtom = userAtomRepository.save(userAtom);
    logger.debug("saved user atom {}", userAtom.getId());
    user.addUserAtom(userAtom);
    userRepository.save(user);
    logger.debug("atom {} added to atoms of user {}", userAtom.getId(), user.getId());
}
Also used : UserAtom(won.owner.model.UserAtom) URI(java.net.URI)

Aggregations

UserAtom (won.owner.model.UserAtom)15 URI (java.net.URI)9 User (won.owner.model.User)8 StringUtils (org.apache.commons.lang.StringUtils)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Transactional (org.springframework.transaction.annotation.Transactional)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 MethodHandles (java.lang.invoke.MethodHandles)2 URISyntaxException (java.net.URISyntaxException)2 MessageDigest (java.security.MessageDigest)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 ObjectNode (org.codehaus.jackson.node.ObjectNode)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2