Search in sources :

Example 1 with NoSuchConnectionException

use of won.protocol.exception.NoSuchConnectionException in project webofneeds by researchstudio-sat.

the class LinkedDataWebController method readConnectionEvents.

@RequestMapping(value = "${uri.path.data.connection}/{identifier}/events", method = RequestMethod.GET, produces = { "application/ld+json", "application/trig", "application/n-quads" })
public ResponseEntity<Dataset> readConnectionEvents(HttpServletRequest request, @PathVariable(value = "identifier") String identifier, @RequestParam(value = "p", required = false) Integer page, @RequestParam(value = "resumebefore", required = false) String beforeId, @RequestParam(value = "resumeafter", required = false) String afterId, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "deep", required = false, defaultValue = "false") boolean deep) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    logger.debug("readConnection() called");
    Dataset rdfDataset;
    HttpHeaders headers = new HttpHeaders();
    Integer preferedSize = getPreferredSize(request);
    URI connectionUri = URI.create(this.connectionResourceURIPrefix + "/" + identifier);
    URI connectionEventsURI = URI.create(connectionUri.toString() + "/" + "events");
    WonMessageType msgType = getMessageType(type);
    try {
        String passableMap = getPassableQueryMap("type", type);
        if (preferedSize == null) {
            // client doesn't not support paging - return all members; does not support type
            // filtering for clients that do
            // not support paging
            rdfDataset = linkedDataService.listConnectionEventURIs(connectionUri, deep);
        } else if (page == null && beforeId == null && afterId == null) {
            // client supports paging but didn't specify which page to return - return page
            // with latest events
            NeedInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listConnectionEventURIs(connectionUri, 1, preferedSize, msgType, // TODO: does not
            deep);
            // respect
            // preferredSize if
            // deep is used
            rdfDataset = resource.getContent();
            addPagedResourceInSequenceHeader(headers, connectionEventsURI, resource, passableMap);
        } else if (page != null) {
            // a page having particular page number is requested
            NeedInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listConnectionEventURIs(connectionUri, page, preferedSize, msgType, deep);
            rdfDataset = resource.getContent();
            addPagedResourceInSequenceHeader(headers, connectionEventsURI, resource, page, passableMap);
        } else if (beforeId != null) {
            // a page that precedes the item identified by the beforeId is requested
            URI referenceEvent = uriService.createEventURIForId(beforeId);
            NeedInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listConnectionEventURIsBefore(connectionUri, referenceEvent, preferedSize, msgType, deep);
            rdfDataset = resource.getContent();
            addPagedResourceInSequenceHeader(headers, connectionEventsURI, resource, passableMap);
        } else {
            // a page that follows the item identified by the afterId is requested
            URI referenceEvent = uriService.createEventURIForId(afterId);
            NeedInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listConnectionEventURIsAfter(connectionUri, referenceEvent, preferedSize, msgType, deep);
            rdfDataset = resource.getContent();
            addPagedResourceInSequenceHeader(headers, connectionEventsURI, resource, passableMap);
        }
    } catch (NoSuchConnectionException e) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    // TODO: events list information does change over time, unless the connection is
    // closed and cannot be reopened.
    // The events list of immutable connection information should never expire, the
    // mutable should
    addLocationHeaderIfNecessary(headers, URI.create(request.getRequestURI()), URI.create(this.connectionResourceURIPrefix));
    addMutableResourceHeaders(headers);
    addCORSHeader(headers);
    stopWatch.stop();
    logger.debug("readConnectionEvents took " + stopWatch.getLastTaskTimeMillis() + " millis");
    return new ResponseEntity<>(rdfDataset, headers, HttpStatus.OK);
}
Also used : NeedInformationService(won.protocol.service.NeedInformationService) HttpHeaders(org.springframework.http.HttpHeaders) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) Dataset(org.apache.jena.query.Dataset) WonMessageType(won.protocol.message.WonMessageType) URI(java.net.URI) StopWatch(org.springframework.util.StopWatch) ResponseEntity(org.springframework.http.ResponseEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with NoSuchConnectionException

use of won.protocol.exception.NoSuchConnectionException in project webofneeds by researchstudio-sat.

the class LinkedDataWebController method showConnectionEventsPage.

// webmvc controller method
@RequestMapping("${uri.path.page.connection}/{identifier}/events")
public String showConnectionEventsPage(@PathVariable String identifier, @RequestParam(value = "p", required = false) Integer page, @RequestParam(value = "resumebefore", required = false) String beforeId, @RequestParam(value = "resumeafter", required = false) String afterId, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "deep", required = false, defaultValue = "false") boolean deep, Model model, HttpServletResponse response) {
    try {
        URI connectionURI = uriService.createConnectionURIForId(identifier);
        String eventsURI = connectionURI.toString() + "/events";
        Dataset rdfDataset;
        WonMessageType msgType = getMessageType(type);
        if (page == null && beforeId == null && afterId == null) {
            // all events, does not support type filtering for clients that do not support
            // paging
            rdfDataset = linkedDataService.listConnectionEventURIs(connectionURI, deep);
        } else if (page != null) {
            // a page having particular page number is requested
            NeedInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listConnectionEventURIs(connectionURI, page, null, msgType, deep);
            rdfDataset = resource.getContent();
        } else if (beforeId != null) {
            // a page that precedes the item identified by the beforeId is requested
            URI referenceEvent = uriService.createEventURIForId(beforeId);
            NeedInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listConnectionEventURIsBefore(connectionURI, referenceEvent, null, msgType, deep);
            rdfDataset = resource.getContent();
        } else {
            // a page that follows the item identified by the afterId is requested
            URI referenceEvent = uriService.createEventURIForId(afterId);
            NeedInformationService.PagedResource<Dataset, URI> resource = linkedDataService.listConnectionEventURIsAfter(connectionURI, referenceEvent, null, msgType, deep);
            rdfDataset = resource.getContent();
        }
        model.addAttribute("rdfDataset", rdfDataset);
        model.addAttribute("resourceURI", eventsURI);
        model.addAttribute("dataURI", uriService.toDataURIIfPossible(URI.create(eventsURI)).toString());
        return "rdfDatasetView";
    } catch (NoSuchConnectionException e) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return "notFoundView";
    }
}
Also used : NeedInformationService(won.protocol.service.NeedInformationService) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) Dataset(org.apache.jena.query.Dataset) WonMessageType(won.protocol.message.WonMessageType) URI(java.net.URI) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with NoSuchConnectionException

use of won.protocol.exception.NoSuchConnectionException in project webofneeds by researchstudio-sat.

the class ConnectMessageFromOwnerProcessor method process.

public void process(final Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI senderNeedURI = wonMessage.getSenderNeedURI();
    URI senderNodeURI = wonMessage.getSenderNodeURI();
    URI receiverNeedURI = wonMessage.getReceiverNeedURI();
    URI facetURI = WonRdfUtils.FacetUtils.getFacet(wonMessage);
    // if the uri is known already, we can load the connection!
    URI connectionURI = wonMessage.getSenderURI();
    Connection con = null;
    if (connectionURI != null) {
        con = connectionRepository.findOneByConnectionURIForUpdate(connectionURI);
        if (con == null)
            throw new NoSuchConnectionException(connectionURI);
    } else {
        con = connectionRepository.findOneByNeedURIAndRemoteNeedURIAndTypeURIForUpdate(senderNeedURI, receiverNeedURI, facetURI);
    }
    if (con == null) {
        // create Connection in Database
        URI connectionUri = wonNodeInformationService.generateConnectionURI(senderNodeURI);
        con = dataService.createConnection(connectionUri, senderNeedURI, receiverNeedURI, null, facetURI, ConnectionState.REQUEST_SENT, ConnectionEventType.OWNER_OPEN);
    }
    con.setState(con.getState().transit(ConnectionEventType.OWNER_OPEN));
    connectionRepository.save(con);
    // prepare the message to pass to the remote node
    URI remoteMessageUri = wonNodeInformationService.generateEventURI(wonMessage.getReceiverNodeURI());
    // set the sender uri in the envelope TODO: TwoMsgs: do not set sender here
    wonMessage.addMessageProperty(WONMSG.SENDER_PROPERTY, con.getConnectionURI());
    // add the information about the new local connection to the original message
    wonMessage.addMessageProperty(WONMSG.HAS_CORRESPONDING_REMOTE_MESSAGE, remoteMessageUri);
    // the persister will pick it up later
    // put the factory into the outbound message factory header. It will be used to generate the outbound message
    // after the wonMessage has been processed and saved, to make sure that the outbound message contains
    // all the data that we also store locally
    OutboundMessageFactory outboundMessageFactory = new OutboundMessageFactory(remoteMessageUri, con);
    message.setHeader(WonCamelConstants.OUTBOUND_MESSAGE_FACTORY_HEADER, outboundMessageFactory);
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 4 with NoSuchConnectionException

use of won.protocol.exception.NoSuchConnectionException in project webofneeds by researchstudio-sat.

the class BACCParticipantFacetImpl method sendMessageFromOwner.

// Participant sends message to Coordinator
public void sendMessageFromOwner(final Connection con, final Model message, final WonMessage wonMessage) throws NoSuchConnectionException, IllegalMessageForConnectionStateException {
    final URI remoteConnectionURI = con.getRemoteConnectionURI();
    // inform the other side
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            try {
                String messageForSending = new String();
                BACCEventType eventType = null;
                Model myContent = null;
                Resource r = null;
                // message (event) for sending
                // message as TEXT
                messageForSending = WonRdfUtils.MessageUtils.getTextMessage(message);
                if (messageForSending != null) {
                    logger.debug("Participant sends: " + messageForSending);
                    eventType = BACCEventType.getCoordinationEventTypeFromString(messageForSending);
                } else // message as MODEL
                {
                    NodeIterator ni = message.listObjectsOfProperty(message.getProperty(WON_TX.COORDINATION_MESSAGE.getURI()));
                    if (ni.hasNext()) {
                        String eventTypeURI = ni.toList().get(0).asResource().getURI().toString();
                        eventType = BACCEventType.getBAEventTypeFromURI(eventTypeURI);
                    }
                }
                myContent = ModelFactory.createDefaultModel();
                myContent.setNsPrefix("", "no:uri");
                Resource baseResource = myContent.createResource("no:uri");
                // message -> eventType
                if ((eventType != null)) {
                    if (eventType.isBACCParticipantEventType(eventType)) {
                        BACCState state, newState;
                        state = BACCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                        logger.debug("Current state of the Participant {} for Coordinator {} ", state.getURI().toString(), con.getRemoteNeedURI());
                        newState = state.transit(eventType);
                        stateManager.setStateForNeedUri(newState.getURI(), con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI());
                        storeBAStateForConnection(con, newState.getURI());
                        logger.debug("New state of the Participant:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()));
                        // eventType -> URI Resource
                        r = myContent.createResource(eventType.getURI().toString());
                        baseResource.addProperty(WON_TX.COORDINATION_MESSAGE, r);
                    // TODO: use new system
                    // needFacingConnectionClient.sendMessage(con, myContent, wonMessage);
                    } else {
                        logger.debug("The eventType: " + eventType.getURI().toString() + " can not be triggered by Participant.");
                    }
                } else {
                    logger.debug("The event type denoted by " + messageForSending + " is not allowed.");
                }
            } catch (Exception e) {
                logger.warn("caught Exception:", e);
            }
        }
    });
}
Also used : BACCEventType(won.node.facet.businessactivity.coordinatorcompletion.BACCEventType) URI(java.net.URI) BACCState(won.node.facet.businessactivity.coordinatorcompletion.BACCState) WonProtocolException(won.protocol.exception.WonProtocolException) IllegalMessageForConnectionStateException(won.protocol.exception.IllegalMessageForConnectionStateException) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException)

Example 5 with NoSuchConnectionException

use of won.protocol.exception.NoSuchConnectionException in project webofneeds by researchstudio-sat.

the class BAPCCoordinatorFacetImpl method sendMessageFromOwner.

// Coordinator sends message to Participant
public void sendMessageFromOwner(final Connection con, final Model message, final WonMessage wonMessage) throws NoSuchConnectionException, IllegalMessageForConnectionStateException {
    final URI remoteConnectionURI = con.getRemoteConnectionURI();
    // inform the other side
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            try {
                String messageForSending = new String();
                BAPCEventType eventType = null;
                Model myContent = null;
                Resource r = null;
                // message (event) for sending
                // message as TEXT
                messageForSending = WonRdfUtils.MessageUtils.getTextMessage(message);
                if (messageForSending != null) {
                    eventType = BAPCEventType.getCoordinationEventTypeFromString(messageForSending);
                    logger.debug("*** Coordinator sends the text message {}", eventType);
                    logger.debug("coordinator {}, participant {}", con.getNeedURI(), con.getRemoteNeedURI());
                    logger.debug("con {}", con.getConnectionURI());
                } else // message as MODEL
                {
                    NodeIterator ni = message.listObjectsOfProperty(message.getProperty(WON_TX.COORDINATION_MESSAGE.getURI().toString()));
                    if (ni.hasNext()) {
                        String eventTypeURI = ni.toList().get(0).asResource().getURI().toString();
                        eventType = BAPCEventType.getBAEventTypeFromURI(eventTypeURI);
                        logger.debug("*** Coordinator sends the text message {}", eventType.getURI());
                        logger.debug("coordinator {}, participant {}", con.getNeedURI(), con.getRemoteNeedURI());
                        logger.debug("con {}", con.getConnectionURI());
                    } else {
                        logger.debug("ERROR: Message {} does not contain a proper content.", message.toString());
                        return;
                    }
                }
                myContent = ModelFactory.createDefaultModel();
                myContent.setNsPrefix("", "no:uri");
                Resource baseResource = myContent.createResource("no:uri");
                if ((eventType != null)) {
                    if (BAPCEventType.isBAPCCoordinatorEventType(eventType)) {
                        BAPCState state, newState;
                        state = BAPCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                        logger.debug("Before sending Coordinator has the BAState {} ", state.getURI().toString());
                        newState = state.transit(eventType);
                        stateManager.setStateForNeedUri(newState.getURI(), con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI());
                        storeBAStateForConnection(con, newState.getURI());
                        logger.debug("New state of the Coordinator:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()));
                        // eventType -> URI Resource
                        r = myContent.createResource(eventType.getURI().toString());
                        baseResource.addProperty(WON_TX.COORDINATION_MESSAGE, r);
                    // TODO: use new system
                    // needFacingConnectionClient.sendMessage(con, myContent, wonMessage);
                    } else {
                        logger.debug("The eventType: " + eventType.getURI().toString() + " can not be triggered by Coordinator.");
                    }
                } else {
                    logger.debug("The event type denoted by " + messageForSending + " is not allowed.");
                }
            } catch (Exception e) {
                logger.warn("caught Exception;", e);
            }
        }
    });
}
Also used : BAPCEventType(won.node.facet.businessactivity.participantcompletion.BAPCEventType) URI(java.net.URI) WonProtocolException(won.protocol.exception.WonProtocolException) IllegalMessageForConnectionStateException(won.protocol.exception.IllegalMessageForConnectionStateException) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) BAPCState(won.node.facet.businessactivity.participantcompletion.BAPCState)

Aggregations

URI (java.net.URI)15 NoSuchConnectionException (won.protocol.exception.NoSuchConnectionException)15 Dataset (org.apache.jena.query.Dataset)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 IllegalMessageForConnectionStateException (won.protocol.exception.IllegalMessageForConnectionStateException)6 HttpHeaders (org.springframework.http.HttpHeaders)4 ResponseEntity (org.springframework.http.ResponseEntity)4 WonProtocolException (won.protocol.exception.WonProtocolException)4 NeedInformationService (won.protocol.service.NeedInformationService)4 ParseException (java.text.ParseException)3 BACCEventType (won.node.facet.businessactivity.coordinatorcompletion.BACCEventType)3 BACCState (won.node.facet.businessactivity.coordinatorcompletion.BACCState)3 BAPCEventType (won.node.facet.businessactivity.participantcompletion.BAPCEventType)3 BAPCState (won.node.facet.businessactivity.participantcompletion.BAPCState)3 NoSuchNeedException (won.protocol.exception.NoSuchNeedException)3 WonMessageType (won.protocol.message.WonMessageType)3 Message (org.apache.camel.Message)2 NoSuchMessageException (org.springframework.context.NoSuchMessageException)2 WonMessage (won.protocol.message.WonMessage)2 Connection (won.protocol.model.Connection)2