Exemplo n.º 1
0
 def ofInputType(self, type):
     if self.entity.hasCapability():
         a = self.entity.getCapabilities()[0]
         i = Input()
         i.setType(type)
         a.addInput(i)
     else:
         a = Activity()
         i = Input()
         i.setType(type)
         a.addInput(i)
         self.entity.addCapability(a)
Exemplo n.º 2
0
    def storeToTransactionServer(cls, senderId, notary, *args):
        '''
        @param senderId: URI string
        @param notary: URI string
        @param args: a variable number of Transaction Objects  
        @return a boolean value
        '''
        try:
            client = HttpClient()
            # create an empty MIME multipart message
            sentHttpMessage = HttpMessage()

            # construct Request object
            req = RequestFactory().create(senderId)
            req.setMethod(RESOURCE.WRITE)
            cls.messageIdCounter = cls.messageIdCounter + 1
            req.setMessageId(cls.messageIdCounter)
            a = Activity()
            req.addActivity(a)
            i = Input()
            a.addInput(i)
            system = SystemOfInterest()
            system.setSameAs(notary)
            i.setSystemOfInterest(system)

            param = Parameter()
            param.setKey("transactions")
            param.setName("Transaction id list")
            param.setDescription(
                "List of ids of transactions that are sent for storing into the database."
            )

            for transaction in args:
                param.addValue(transaction.getIdentifierUri())
            i.addParameter(param)

            # save Request object to multipart message, Main part
            mainRequestString = Tools.toString(req)
            sentHttpMessage.addMainpart(mainRequestString)

            mainRequestHash = SmartAPICrypto().createEncodedMessageDigest(
                mainRequestString)
            requestSignature = SmartAPICrypto().sign(
                CryptoKeyWallet.getPrivateKey(), mainRequestHash)

            # create transaction for the main request
            if cls.transactionIdentifierUriPrefix == '':
                transactionIdentifierUri = senderId + uuid.uuid4().get_hex()
            else:
                transactionIdentifierUri = cls.transactionIdentifierUriPrefix + uuid.uuid4(
                ).get_hex()
            requestTransaction = Factory.createTransaction(
                transactionIdentifierUri, senderId)
            requestTransaction.setSigner(senderId)
            requestTransactionMessage = Message()
            requestTransactionMessage.setMessageId(req.getMessageId())
            requestTransactionMessage.setHashCode(mainRequestHash)
            requestTransactionMessage.setSignature(requestSignature)
            requestTransaction.setMessage(requestTransactionMessage)

            # save requestTransaction to multipart message, non-Main part
            requestTransactionString = Tools.toString(requestTransaction)
            sentHttpMessage.add(transactionIdentifierUri,
                                requestTransactionString)

            # save transactions that are sent to multipart message, non-Main part
            for trans in args:
                sentHttpMessage.add(trans.getIdentifierUri(),
                                    Tools.toString(trans))

            # send out this multipart message
            payload = removeFrontHeader(sentHttpMessage.asString())
            #print '*** sent ****'
            #print payload
            #print '*************'
            resp, contentType = client.sendPost(
                notary, payload, sentHttpMessage.getContentType())
            #print "----response -----"
            #print resp
            return True

        except:
            traceback.print_exc()
            return False
Exemplo n.º 3
0
    def fetchFromTransactionServer(cls, senderId, notary, *args):
        '''
        @param senderId: URI string
        @param notary: URI string
        @param args: a variable number of Transaction Objects  
        
        @return a dictionary, key is SeasIdentifierUri of each transaction, value is Transaction Object
        '''
        try:
            client = HttpClient()
            # create an empty MIME multipart message
            sentHttpMessage = HttpMessage()
            # main request
            req = RequestFactory().create(senderId)
            req.setMethod(RESOURCE.READ)
            cls.messageIdCounter = cls.messageIdCounter + 1
            req.setMessageId(cls.messageIdCounter)
            a = Activity()
            req.addActivity(a)
            i = Input()
            a.addInput(i)
            system = SystemOfInterest()
            system.setSameAs(notary)
            i.setSystemOfInterest(system)

            param = Parameter()
            param.setKey("transactions")
            param.setName("Transaction id list")
            param.setDescription(
                "List of ids of transactions that are sent to be fetched the database."
            )

            for transaction in args:
                param.addValue(transaction.getIdentifierUri())
            i.addParameter(param)

            # save Request object to multipart message, Main part
            mainRequestString = Tools.toString(req)
            sentHttpMessage.addMainpart(mainRequestString)

            mainRequestHash = SmartAPICrypto().createEncodedMessageDigest(
                mainRequestString)
            requestSignature = SmartAPICrypto().sign(
                CryptoKeyWallet.getPrivateKey(), mainRequestHash)

            #create transaction for the main request
            if cls.transactionIdentifierUriPrefix == '':
                transactionIdentifierUri = senderId + uuid.uuid4().get_hex()
            else:
                transactionIdentifierUri = transactionIdentifierUri + uuid.uuid4(
                ).get_hex()
            requestTransaction = Factory.createTransaction(
                transactionIdentifierUri, senderId)
            requestTransaction.setSigner(senderId)
            requestTransactionMessage = Message()
            requestTransactionMessage.setMessageId(req.getMessageId())
            requestTransactionMessage.setHashCode(mainRequestHash)
            requestTransactionMessage.setSignature(requestSignature)
            requestTransaction.setMessage(requestTransactionMessage)

            # add requestTransaction to multipart message, non-Main part
            requestTransactionString = Tools.toString(requestTransaction)
            sentHttpMessage.add(transactionIdentifierUri,
                                requestTransactionString)

            # add transactions that are sent to multipart message, non-Main part
            for trans in args:
                sentHttpMessage.add(trans.getIdentifierUri(),
                                    Tools.toString(trans))

            # send out this multipart message
            payload = removeFrontHeader(sentHttpMessage.asString())
            resp, contentType = client.sendPost(
                notary, payload, sentHttpMessage.getContentType())

            receivedHttpMessagestr = addFrontHeader(resp, contentType)
            receivedHttpMessage = parseMIMEmessage(receivedHttpMessagestr)

            map = {}
            if isinstance(receivedHttpMessage, HttpMessageSingle):
                non_main_parts_dict = {}
            else:
                non_main_parts_dict = receivedHttpMessage.getNonMainPartsAsDict(
                )
            counter = 0
            for part in non_main_parts_dict.values():
                counter = counter + 1
                # recover the part from string to Seas Obj
                model = Tools().fromString(part, SERIALIZATION.TURTLE)
                rootRes = Tools().getTopNode(model)[0]
                seasCls = Tools().getResourceClass(rootRes)
                recoveredObj = seasCls.parse(rootRes)

                if isinstance(
                        recoveredObj, Transaction
                ):  # We want to save the Transaction that hold the requested data
                    map[recoveredObj.getIdentifierUri()] = recoveredObj

            return map

        except:
            traceback.print_exc()