Search in sources :

Example 81 with WonMessage

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();
    }
}
Also used : User(won.owner.model.User) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) Transactional(org.springframework.transaction.annotation.Transactional)

Example 82 with WonMessage

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) WonMessage(won.protocol.message.WonMessage) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 83 with WonMessage

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) WonMessage(won.protocol.message.WonMessage) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 84 with WonMessage

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) WonMessage(won.protocol.message.WonMessage) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 85 with WonMessage

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) WonMessage(won.protocol.message.WonMessage) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Aggregations

WonMessage (won.protocol.message.WonMessage)89 URI (java.net.URI)47 Connection (won.protocol.model.Connection)19 Test (org.junit.Test)18 Message (org.apache.camel.Message)17 Dataset (org.apache.jena.query.Dataset)17 Exchange (org.apache.camel.Exchange)13 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)13 DefaultExchange (org.apache.camel.impl.DefaultExchange)13 WonMessageProcessingException (won.protocol.message.processor.exception.WonMessageProcessingException)11 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)10 MissingMessagePropertyException (won.protocol.message.processor.exception.MissingMessagePropertyException)9 Event (won.bot.framework.eventbot.event.Event)8 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)8 EventListener (won.bot.framework.eventbot.listener.EventListener)7 Need (won.protocol.model.Need)7 WonNodeInformationService (won.protocol.service.WonNodeInformationService)7 Model (org.apache.jena.rdf.model.Model)5 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)5 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)4