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