use of won.protocol.message.WonMessage 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();
}
}
use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class WonMessageSlipComputerTests method testSendMessageFromOwner.
@Test
public void testSendMessageFromOwner() throws Exception {
Exchange exchange = new DefaultExchange(new DefaultCamelContext());
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_HEADER, new WonMessage(DatasetFactory.createGeneral()));
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_TYPE_HEADER, URI.create(WONMSG.TYPE_CONNECTION_MESSAGE_STRING));
exchange.getIn().setHeader(WonCamelConstants.DIRECTION_HEADER, URI.create(WonMessageDirection.FROM_OWNER.getResource().getURI().toString()));
exchange.getIn().setHeader(WonCamelConstants.FACET_TYPE_HEADER, URI.create(WON.OWNER_FACET_STRING));
String slip = wonMessageSlipComputer.evaluate(exchange, String.class);
Assert.assertEquals("sendMessageFromOwnerProcessor,sendMessageFromOwnerOwnerFacetImpl", slip);
}
use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class WonMessageSlipComputerTests method testConnectMessageFromNode.
@Test
public void testConnectMessageFromNode() throws Exception {
Exchange exchange = new DefaultExchange(new DefaultCamelContext());
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_HEADER, new WonMessage(DatasetFactory.createGeneral()));
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_TYPE_HEADER, URI.create(WONMSG.TYPE_CONNECT_STRING));
exchange.getIn().setHeader(WonCamelConstants.DIRECTION_HEADER, URI.create(WonMessageDirection.FROM_EXTERNAL.getResource().getURI().toString()));
exchange.getIn().setHeader(WonCamelConstants.FACET_TYPE_HEADER, URI.create(WON.OWNER_FACET_STRING));
String slip = wonMessageSlipComputer.evaluate(exchange, String.class);
Assert.assertEquals("connectMessageFromNodeProcessor,connectFromNodeOwnerFacetImpl", slip);
}
use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class WonMessageSlipComputerTests method testSendMessageFromNode.
@Test
public void testSendMessageFromNode() throws Exception {
Exchange exchange = new DefaultExchange(new DefaultCamelContext());
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_HEADER, new WonMessage(DatasetFactory.createGeneral()));
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_TYPE_HEADER, URI.create(WONMSG.TYPE_CONNECTION_MESSAGE_STRING));
exchange.getIn().setHeader(WonCamelConstants.DIRECTION_HEADER, URI.create(WonMessageDirection.FROM_EXTERNAL.getResource().getURI().toString()));
exchange.getIn().setHeader(WonCamelConstants.FACET_TYPE_HEADER, URI.create(WON.OWNER_FACET_STRING));
String slip = wonMessageSlipComputer.evaluate(exchange, String.class);
Assert.assertEquals("sendMessageFromNodeProcessor,sendMessageFromNodeOwnerFacetImpl", slip);
}
use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class WonMessageSlipComputerTests method testCreateFromOwner.
@Test
public void testCreateFromOwner() throws Exception {
Exchange exchange = new DefaultExchange(new DefaultCamelContext());
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_HEADER, new WonMessage(DatasetFactory.createGeneral()));
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_TYPE_HEADER, URI.create(WONMSG.TYPE_CREATE_STRING));
exchange.getIn().setHeader(WonCamelConstants.DIRECTION_HEADER, URI.create(WonMessageDirection.FROM_OWNER.getResource().getURI().toString()));
String slip = wonMessageSlipComputer.evaluate(exchange, String.class);
Assert.assertEquals("createNeedMessageProcessor", slip);
}
Aggregations