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));
}
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);
}
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);
}
}
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");
}
}
}
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());
}
Aggregations