Search in sources :

Example 16 with User

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

the class WonWebSocketHandler method handleTextMessage.

/*
	 * User user = getCurrentUser();
	 * 
	 * logger.info("New Need:" + needPojo.getTextDescription() + "/" +
	 * needPojo.getCreationDate() + "/" + needPojo.getLongitude() + "/" +
	 * needPojo.getLatitude() + "/" + (needPojo.getState() == NeedState.ACTIVE));
	 * //TODO: using fixed Facets - change this needPojo.setFacetTypes(new String[]{
	 * FacetType.OwnerFacet.getURI().toString()}); NeedPojo createdNeedPojo =
	 * resolve(needPojo); Need need =
	 * needRepository.findOne(createdNeedPojo.getNeedId());
	 * user.getNeeds().add(need); wonUserDetailService.save(user); HttpHeaders
	 * headers = new HttpHeaders(); headers.setLocation(need.getNeedURI()); return
	 * new ResponseEntity<NeedPojo>(createdNeedPojo, headers, HttpStatus.CREATED);
	 */
@Override
@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
    logger.debug("OA Server - WebSocket message received: {}", message.getPayload());
    updateSession(session);
    if (!message.isLast()) {
        // we have an intermediate part of the current message.
        session.getAttributes().putIfAbsent(SESSION_ATTRIBUTE_PARTIAL_MESSAGE, new StringBuilder());
    }
    // now check if we have the partial message string builder in the session.
    // if we do, we're processing a partial message, and we have to append the
    // current message payload
    StringBuilder sb = (StringBuilder) session.getAttributes().get(SESSION_ATTRIBUTE_PARTIAL_MESSAGE);
    // will hold the final message
    String completePayload = null;
    if (sb == null) {
        // No string builder found in the session - we're not processing a partial
        // message.
        // The complete payload is in the current message. Get it and continue.
        completePayload = message.getPayload();
    } else {
        // the string builder is there - we're processing a partial message. append the
        // current piece
        sb.append(message.getPayload());
        if (message.isLast()) {
            // we've received the last part. pass it on to the next processing steps.
            completePayload = sb.toString();
            // also, we do not need the string builder in the session any longer. remove it:
            session.getAttributes().remove(SESSION_ATTRIBUTE_PARTIAL_MESSAGE);
        } else {
            // next part
            return;
        }
    }
    WonMessage wonMessage = WonMessageDecoder.decodeFromJsonLd(completePayload);
    // remember which user or (if not logged in) which needUri the session is bound
    // to
    User user = getUserForSession(session);
    if (user != null) {
        logger.debug("binding session to user {}", user.getId());
        this.webSocketSessionService.addMapping(user, session);
    }
    // anyway, we have to bind the URI to the session, otherwise we can't handle
    // incoming server->client messages
    URI needUri = wonMessage.getSenderNeedURI();
    logger.debug("binding session to need URI {}", needUri);
    this.webSocketSessionService.addMapping(needUri, session);
    try {
        AuthenticationThreadLocal.setAuthentication((Authentication) session.getPrincipal());
        ownerApplicationService.sendWonMessage(wonMessage);
    } finally {
        // be sure to remove the principal from the threadlocal
        AuthenticationThreadLocal.remove();
    }
}
Also used : User(won.owner.model.User) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with User

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

the class WonWebSocketHandler method process.

@Override
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public WonMessage process(final WonMessage wonMessage) {
    String wonMessageJsonLdString = WonMessageEncoder.encodeAsJsonLd(wonMessage);
    WebSocketMessage<String> webSocketMessage = new TextMessage(wonMessageJsonLdString);
    URI needUri = wonMessage.getReceiverNeedURI();
    User user = getUserForWonMessage(wonMessage);
    Set<WebSocketSession> webSocketSessions = findWebSocketSessionsForWonMessage(wonMessage, needUri, user);
    // check if we can deliver the message. If not, send email.
    if (webSocketSessions.size() == 0) {
        logger.info("cannot deliver message of type {} for need {}, receiver {}: no websocket session found", new Object[] { wonMessage.getMessageType(), wonMessage.getReceiverNeedURI(), wonMessage.getReceiverURI() });
        // send per email notifications if it applies:
        notifyPerEmail(user, needUri, wonMessage);
        return wonMessage;
    }
    // we can send it - pre-cache the delivery chain:
    eagerlyCachePopulatingProcessor.process(wonMessage);
    // send to owner webapp
    for (WebSocketSession session : webSocketSessions) {
        sendMessageForSession(wonMessage, webSocketMessage, session, needUri, user);
    }
    return wonMessage;
}
Also used : User(won.owner.model.User) URI(java.net.URI) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with User

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

the class KeystoreEnabledDaoAuthenticationProvider method authenticate.

@Override
@Transactional
public Authentication authenticate(Authentication authentication) {
    String password = (String) authentication.getCredentials();
    String username = (String) authentication.getPrincipal();
    UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) super.authenticate(authentication);
    User user = (User) auth.getPrincipal();
    // can't use that object as it's detached. load the user again:
    user = userRepository.findOne(user.getId());
    KeystorePasswordHolder keystorePasswordHolder = user.getKeystorePasswordHolder();
    if (keystorePasswordHolder == null || keystorePasswordHolder.getEncryptedPassword() == null || keystorePasswordHolder.getEncryptedPassword().length() == 0) {
        keystorePasswordHolder = new KeystorePasswordHolder();
        // generate a password for the keystore and save it in the database, encrypted with a symmetric key
        // derived from the user's password
        keystorePasswordHolder.setPassword(KeystorePasswordUtils.generatePassword(KeystorePasswordUtils.KEYSTORE_PASSWORD_BYTES), password);
        // keystorePasswordHolder = keystorePasswordRepository.save(keystorePasswordHolder);
        // generate the keystore for the user
        user.setKeystorePasswordHolder(keystorePasswordHolder);
    }
    String keystorePassword = keystorePasswordHolder.getPassword(password);
    KeystoreHolder keystoreHolder = user.getKeystoreHolder();
    KeyStore keystore = null;
    if (keystoreHolder == null || keystoreHolder.getKeystoreBytes() == null || keystoreHolder.getKeystoreBytes().length == 0) {
        // new user or legacy user that has no keystore yet: create keystoreHolder
        keystoreHolder = new KeystoreHolder();
        keystore = openOrCreateKeyStore(keystorePassword, auth.getName(), keystoreHolder);
        // keystoreHolder = keystoreHolderRepository.save(keystoreHolder);
        user.setKeystoreHolder(keystoreHolder);
    } else {
        try {
            keystore = keystoreHolder.getKeystore(keystorePassword);
        } catch (Exception e) {
            throw new IllegalStateException("could not open keystore for user " + username);
        }
    }
    userRepository.save(user);
    KeystoreEnabledUserDetails ud = new KeystoreEnabledUserDetails(user, keystore, keystorePassword);
    return new UsernamePasswordAuthenticationToken(ud, null, auth.getAuthorities());
}
Also used : User(won.owner.model.User) KeystoreHolder(won.owner.model.KeystoreHolder) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) KeystorePasswordHolder(won.owner.model.KeystorePasswordHolder) KeyStore(java.security.KeyStore) Transactional(javax.transaction.Transactional)

Aggregations

User (won.owner.model.User)18 URI (java.net.URI)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 Transactional (org.springframework.transaction.annotation.Transactional)6 UserNeed (won.owner.model.UserNeed)5 URISyntaxException (java.net.URISyntaxException)4 Draft (won.owner.model.Draft)3 KeystoreHolder (won.owner.model.KeystoreHolder)3 KeystorePasswordHolder (won.owner.model.KeystorePasswordHolder)3 CheapInsecureRandomString (won.protocol.util.CheapInsecureRandomString)3 KeyStore (java.security.KeyStore)2 ArrayList (java.util.ArrayList)2 ResponseEntity (org.springframework.http.ResponseEntity)2 CreateDraftPojo (won.owner.pojo.CreateDraftPojo)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Transactional (javax.transaction.Transactional)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1