Search in sources :

Example 1 with Atom

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

the class AtomManagementService method sendTextMessageToOwner.

public void sendTextMessageToOwner(URI atomURI, String message) {
    if (atomURI == null) {
        logger.warn("sendTextMessageToOwner called but atomUri is null - doing nothing");
        return;
    }
    if (message == null || message.trim().length() == 0) {
        logger.warn("sendTextMessageToOwner called for atom {}, but message is null or empty - doing nothing", atomURI);
        return;
    }
    logger.debug("Sending FromSystem text message to atom {}", atomURI);
    // check if we have that atom (e.g. it's not an atom living on another node, or
    // does not exist at all)
    Atom atom = atomService.getAtomRequired(atomURI);
    if (atom == null) {
        logger.debug("deactivateAtom called for atom {} but that atom was not found in the repository - doing nothing", atomURI);
        return;
    }
    URI wonNodeURI = wonNodeInformationService.getWonNodeUri(atomURI);
    if (wonNodeURI == null) {
        logger.debug("deactivateAtom called for atom {} but we could not find a WonNodeURI for that atom - doing nothing", atomURI);
        return;
    }
    WonMessage msg = WonMessageBuilder.atomMessage().atom(atomURI).content().text(message).build();
    sendSystemMessage(msg);
}
Also used : WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) Atom(won.protocol.model.Atom)

Example 2 with Atom

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

the class ToOwnerSender method process.

@Override
public void process(Exchange exchange) throws Exception {
    WonMessage msg = getMessageToSendRequired(exchange);
    Objects.requireNonNull(msg);
    URI recipientAtom = getRecipientAtomURIRequired(exchange);
    Optional<Atom> atom = atomService.getAtom(recipientAtom);
    List<OwnerApplication> ownerApps = getOwnerApplications(msg, atom, getOwnerApplicationId(exchange));
    // String methodName =headers.get("methodName").toString();
    logger.debug("number of registered owner applications: {}", ownerApps == null ? 0 : ownerApps.size());
    List<String> queueNames = ownerApps.stream().flatMap(app -> {
        return app.getQueueNames().stream().map(queue -> ownerManagementService.sanitizeQueueNameForOwnerApplication(app, queue)).filter(queue -> ownerManagementService.existsCamelEndpointForOwnerApplicationQueue(queue));
    }).collect(Collectors.toList());
    if (queueNames.isEmpty()) {
        if (logger.isDebugEnabled()) {
            logger.warn("Cannot send message to {} via ownerApplication with Id(s){}: no queues registered", msg.getRecipientAtomURI(), Arrays.toString(ownerApps.stream().map(OwnerApplication::getOwnerApplicationId).collect(Collectors.toList()).toArray()));
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("sending message to owner(s) {}: {}", Arrays.toString(queueNames.toArray()), msg.toStringForDebug(true));
        }
        Exchange exchangeToOwners = new DefaultExchange(camelContext);
        putMessageIntoBody(exchangeToOwners, msg);
        exchangeToOwners.getIn().setHeader(WonCamelConstants.OWNER_APPLICATION_IDS_HEADER, queueNames);
        exchangeToOwners.getIn().setHeader("JMSExpiration", System.currentTimeMillis() + 20000);
        messagingService.send(exchangeToOwners, "direct:sendToOwnerApplications");
    }
    removeMessageToSend(exchange);
}
Also used : CamelContext(org.apache.camel.CamelContext) OwnerApplicationRepository(won.protocol.repository.OwnerApplicationRepository) java.util(java.util) Logger(org.slf4j.Logger) DefaultExchange(org.apache.camel.support.DefaultExchange) AbstractCamelProcessor(won.node.camel.processor.AbstractCamelProcessor) WonCamelHelper(won.node.camel.service.WonCamelHelper) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveMQOwnerManagementService(won.node.service.persistence.ActiveMQOwnerManagementService) Exchange(org.apache.camel.Exchange) Collectors(java.util.stream.Collectors) WonMessage(won.protocol.message.WonMessage) MessagingService(won.protocol.jms.MessagingService) URI(java.net.URI) WonCamelConstants(won.protocol.message.processor.camel.WonCamelConstants) Atom(won.protocol.model.Atom) OwnerApplication(won.protocol.model.OwnerApplication) DefaultExchange(org.apache.camel.support.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.support.DefaultExchange) OwnerApplication(won.protocol.model.OwnerApplication) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) Atom(won.protocol.model.Atom)

Example 3 with Atom

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

the class AclChecker method process.

@Override
public void process(Exchange exchange) throws Exception {
    URI recipientAtomUri = WonCamelHelper.getRecipientAtomURIRequired(exchange);
    WonMessage message = WonCamelHelper.getMessageRequired(exchange);
    if (message.getMessageType().isCreateAtom()) {
        // no checks required for a create message
        return;
    }
    if (message.getMessageType().isHintMessage()) {
        // no checks required for a hint message
        return;
    }
    URI signer = message.getSignerURIRequired();
    URI sender = message.getSenderAtomURIRequired();
    URI senderNode = message.getSenderNodeURIRequired();
    WonMessageDirection direction = WonCamelHelper.getDirectionRequired(exchange);
    if (direction.isFromExternal() || direction.isFromSystem()) {
        if (signer.equals(senderNode)) {
            // nodes are allowed to messages on behalf of an atom (for now)
            return;
        }
    }
    Atom atom = atomService.getAtomForMessageRequired(message, direction);
    Optional<Graph> aclGraph = atom.getAclGraph();
    if (aclGraph.isEmpty()) {
        // no checks if no acl present
        if (!sender.equals(signer)) {
            throw new IllegalMessageSignerException(String.format("%s must not be signer of %s message %s", signer, message.getMessageTypeRequired(), message.getMessageURIRequired()));
        }
        return;
    }
    boolean isMessageOnBehalf = false;
    URI requestor = sender;
    if (direction.isFromOwner()) {
        if (signer.equals(sender)) {
            // no checks if the owner is the signer
            return;
        } else {
            isMessageOnBehalf = true;
            requestor = signer;
        }
    }
    OperationRequest operationRequest = OperationRequest.builder().setReqAtom(atom.getAtomURI()).setReqAtomState(AuthUtils.toAuthAtomState(atom.getState())).setRequestor(requestor).build();
    if (message.getMessageType().isConnectionSpecificMessage()) {
        if (direction.isFromOwner() || direction.isFromSystem()) {
            Optional<Socket> s = socketService.getSocket(message.getSenderSocketURIRequired());
            operationRequest.setReqSocketType(s.get().getTypeURI());
            operationRequest.setReqSocket(s.get().getSocketURI());
        } else if (direction.isFromExternal()) {
            Optional<Socket> s = socketService.getSocket(message.getRecipientSocketURIRequired());
            operationRequest.setReqSocketType(s.get().getTypeURI());
            operationRequest.setReqSocket(s.get().getSocketURI());
        }
    } else {
        operationRequest.setReqPosition(POSITION_ROOT);
    }
    Optional<Connection> con = WonCamelHelper.getConnection(exchange, connectionService);
    if (con.isPresent()) {
        operationRequest.setReqPosition(POSITION_CONNECTION);
        Connection c = con.get();
        operationRequest.setReqConnection(c.getConnectionURI());
        operationRequest.setReqConnectionTargetAtom(c.getTargetAtomURI());
        operationRequest.setReqConnectionState(AuthUtils.toAuthConnectionState(c.getState()));
        // we already set these values, but now that we know we have a connection, make
        // sure these values match the connection's
        operationRequest.setReqSocketType(c.getTypeURI());
        operationRequest.setReqSocket(c.getSocketURI());
    } else {
        operationRequest.setReqPosition(POSITION_SOCKET);
    }
    if (isMessageOnBehalf) {
        operationRequest.setOperationMessageOperationExpression(MessageOperationExpression.builder().addMessageOnBehalfsMessageType(AuthUtils.toAuthMessageType(message.getMessageType())).build());
    } else {
        operationRequest.setOperationMessageOperationExpression(MessageOperationExpression.builder().addMessageTosMessageType(AuthUtils.toAuthMessageType(message.getMessageType())).build());
    }
    WonAclEvaluator evaluator = wonAclEvaluatorFactory.create(aclGraph.get());
    if (message.getMessageType().isReplace()) {
    // decision for replace messages is deferred to where replacement actually
    // happens
    } else {
        AclEvalResult result = evaluator.decide(operationRequest);
        if (DecisionValue.ACCESS_DENIED.equals(result.getDecision())) {
            throw new ForbiddenMessageException(String.format("Message not allowed"));
        }
    }
    WonCamelHelper.putWonAclEvaluator(exchange, evaluator);
    WonCamelHelper.putWonAclOperationRequest(exchange, operationRequest);
}
Also used : Optional(java.util.Optional) OperationRequest(won.auth.model.OperationRequest) Connection(won.protocol.model.Connection) WonAclEvaluator(won.auth.WonAclEvaluator) AclEvalResult(won.auth.model.AclEvalResult) URI(java.net.URI) Atom(won.protocol.model.Atom) Graph(org.apache.jena.graph.Graph) ForbiddenMessageException(won.protocol.exception.ForbiddenMessageException) WonMessage(won.protocol.message.WonMessage) IllegalMessageSignerException(won.protocol.exception.IllegalMessageSignerException) WonMessageDirection(won.protocol.message.WonMessageDirection) Socket(won.protocol.model.Socket)

Example 4 with Atom

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

the class UriAlreadyUsedCheckingWonMessageProcessor method checkAtomURI.

private void checkAtomURI(final WonMessage message) {
    if (message.getMessageType() == WonMessageType.CREATE_ATOM) {
        URI atomURI = WonRdfUtils.AtomUtils.getAtomURI(message.getCompleteDataset());
        Optional<Atom> atom = atomService.getAtom(atomURI);
        if (!atom.isPresent()) {
            return;
        } else {
            throw new UriAlreadyInUseException(message.getSenderAtomURI().toString());
        }
    }
    return;
}
Also used : UriAlreadyInUseException(won.protocol.exception.UriAlreadyInUseException) URI(java.net.URI) Atom(won.protocol.model.Atom)

Example 5 with Atom

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

the class ConnectFromNodeReviewSocketImpl method addReviewToAtom.

private void addReviewToAtom(Map<Property, String> reviewData, URI connectionUri) throws IllegalArgumentException {
    String aboutAtomURI = reviewData.get(SCHEMA.ABOUT);
    Double rating = Double.parseDouble(reviewData.get(SCHEMA.RATING_VALUE)) > 0.0 ? Double.parseDouble(reviewData.get(SCHEMA.RATING_VALUE)) : 0.0;
    Atom aboutAtom = atomService.getAtomRequired(URI.create(aboutAtomURI));
    Dataset atomDataset = aboutAtom.getDatatsetHolder().getDataset();
    Model derivationModel = atomDataset.getNamedModel(aboutAtom.getAtomURI() + "#derivedData");
    Resource aboutAtomResource = derivationModel.getResource(aboutAtomURI);
    Resource conRes = derivationModel.getResource(connectionUri.toString());
    Statement reviewdConnectionsProperty = derivationModel.getProperty(aboutAtomResource, WXREVIEW.reviewedConnection);
    if (reviewdConnectionsProperty == null) {
        derivationModel.add(aboutAtomResource, WXREVIEW.reviewedConnection, conRes);
        reviewdConnectionsProperty = derivationModel.getProperty(aboutAtomResource, WXREVIEW.reviewedConnection);
    } else {
        Property ratedConnections = derivationModel.getProperty(WXREVIEW.reviewedConnection.toString());
        if (derivationModel.contains(aboutAtomResource, ratedConnections, conRes)) {
            logger.debug("Connection already reviewed {}", connectionUri.toString());
            throw new IllegalArgumentException("Connection already reviewed");
        } else {
            derivationModel.add(aboutAtomResource, ratedConnections, conRes);
        }
    }
    Statement aggregateRatingProperty = derivationModel.getProperty(aboutAtomResource, SCHEMA.AGGREGATE_RATING);
    if (aggregateRatingProperty == null) {
        derivationModel.addLiteral(aboutAtomResource, SCHEMA.AGGREGATE_RATING, rating);
        aggregateRatingProperty = derivationModel.getProperty(aboutAtomResource, SCHEMA.AGGREGATE_RATING);
    }
    int ratingCount = 0;
    Statement reviewCountProperty = derivationModel.getProperty(aboutAtomResource, SCHEMA.REVIEW_COUNT);
    if (reviewCountProperty != null) {
        ratingCount = reviewCountProperty.getInt();
    } else {
        derivationModel.addLiteral(aboutAtomResource, SCHEMA.REVIEW_COUNT, 0.0);
        reviewCountProperty = derivationModel.getProperty(aboutAtomResource, SCHEMA.REVIEW_COUNT);
    }
    Double aggregateRating = aggregateRatingProperty.getDouble();
    Double ratingSum = 0.0;
    if (ratingCount < 1) {
        ratingSum = aggregateRating;
    } else {
        ratingSum = (aggregateRating * ratingCount) + rating;
    }
    ratingCount++;
    Double newAggregateRating = ratingSum / ratingCount;
    aggregateRatingProperty.changeLiteralObject(newAggregateRating);
    reviewCountProperty.changeLiteralObject(ratingCount);
    aboutAtom.getDatatsetHolder().setDataset(atomDataset);
    atomRepository.save(aboutAtom);
}
Also used : Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) Atom(won.protocol.model.Atom)

Aggregations

Atom (won.protocol.model.Atom)24 URI (java.net.URI)17 WonMessage (won.protocol.message.WonMessage)15 Connection (won.protocol.model.Connection)11 Graph (org.apache.jena.graph.Graph)5 Message (org.apache.camel.Message)4 WonMessageProcessingException (won.protocol.exception.WonMessageProcessingException)4 Dataset (org.apache.jena.query.Dataset)3 WonAclEvaluator (won.auth.WonAclEvaluator)3 URISyntaxException (java.net.URISyntaxException)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 MethodHandles (java.lang.invoke.MethodHandles)1 Date (java.util.Date)1 Optional (java.util.Optional)1 Supplier (java.util.function.Supplier)1 Transactional (javax.transaction.Transactional)1