示例#1
0
    def as2message(self):
        """ Returns an object of pyas2lib's Message class"""
        if self.direction == 'IN':
            as2m = As2Message(sender=self.partner.as2partner,
                              receiver=self.organization.as2org)
        else:
            as2m = As2Message(sender=self.organization.as2org,
                              receiver=self.partner.as2partner)

        as2m.message_id = self.message_id
        as2m.mic = self.mic

        return as2m
示例#2
0
    def form_valid(self, form):
        # Send the file to the partner
        payload = form.cleaned_data['file'].read()
        as2message = As2Message(
            sender=form.cleaned_data['organization'].as2org,
            receiver=form.cleaned_data['partner'].as2partner)
        as2message.build(
            payload,
            filename=form.cleaned_data['file'].name,
            subject=form.cleaned_data['partner'].subject,
            content_type=form.cleaned_data['partner'].content_type)

        message, _ = Message.objects.create_from_as2message(
            as2message=as2message,
            payload=payload,
            direction='OUT',
            status='P')
        message.send_message(as2message.headers, as2message.content)
        if message.status == 'S':
            messages.success(self.request,
                             'Message has been successfully send to Partner.')
        else:
            messages.error(
                self.request,
                'Message transmission failed, check Messages tab for details.')
        return super(SendAs2Message, self).form_valid(form)
    def test_duplicate_error(self, mock_request):
        partner = Partner.objects.create(
            name='AS2 Server',
            as2_name='as2server',
            target_url='http://localhost:8080/pyas2/as2receive',
            signature='sha1',
            signature_cert=self.server_crt,
            encryption='tripledes_192_cbc',
            encryption_cert=self.server_crt,
            mdn=True,
            mdn_mode='SYNC',
            mdn_sign='sha1',
        )

        # Send the message once
        as2message = As2Message(sender=self.organization.as2org,
                                receiver=partner.as2partner)
        as2message.build(self.payload,
                         filename='testmessage.edi',
                         subject=partner.subject,
                         content_type=partner.content_type)
        in_message, _ = Message.objects.create_from_as2message(
            as2message=as2message,
            payload=self.payload,
            direction='OUT',
            status='P')

        mock_request.side_effect = SendMessageMock(self.client)
        in_message.send_message(as2message.headers, as2message.content)

        # Check the status of the message
        self.assertEqual(in_message.status, 'S')
        out_message = Message.objects.get(message_id=in_message.message_id,
                                          direction='IN')
        self.assertEqual(out_message.status, 'S')

        # send it again to cause duplicate error
        in_message.send_message(as2message.headers, as2message.content)

        # Make sure out message was created
        self.assertEqual(in_message.status, 'E')
        out_message = Message.objects.get(message_id=in_message.message_id +
                                          '_duplicate',
                                          direction='IN')
        self.assertEqual(out_message.status, 'E')
示例#4
0
    def build_and_send(self, partner, mock_request):
        # Build and send the message to server
        as2message = As2Message(sender=self.organization.as2org,
                                receiver=partner.as2partner)
        as2message.build(self.payload,
                         filename='testmessage.edi',
                         subject=partner.subject,
                         content_type=partner.content_type)
        in_message, _ = Message.objects.create_from_as2message(
            as2message=as2message,
            payload=self.payload,
            direction='OUT',
            status='P')

        mock_request.side_effect = SendMessageMock(self.client)
        in_message.send_message(as2message.headers, as2message.content)

        return in_message
示例#5
0
    def post(self, request, *args, **kwargs):

        # extract the  headers from the http request
        as2headers = ''
        for key in request.META:
            if key.startswith('HTTP') or key.startswith('CONTENT'):
                as2headers += '%s: %s\n' % (key.replace("HTTP_", "").replace(
                    "_", "-").lower(), request.META[key])

        # build the body along with the headers
        request_body = as2headers.encode() + b'\r\n' + request.body
        logger.debug('Received an HTTP POST from {} with payload :\n{}'.format(
            request.META['REMOTE_ADDR'], request_body))

        # First try to see if this is an MDN
        try:
            logger.debug('Check to see if payload is an Asynchronous MDN.')
            as2mdn = As2Mdn()

            # Parse the mdn and get the message status
            status, detailed_status = as2mdn.parse(request_body,
                                                   self.find_message)
            message = Message.objects.get(
                message_id=as2mdn.orig_message_id,
                direction='OUT',
            )
            logger.info(
                'Asynchronous MDN received for AS2 message {} to organization '
                '{} from partner {}'.format(as2mdn.message_id,
                                            message.organization.as2_name,
                                            message.partner.as2_name))

            # Update the message status and return the response
            if status == 'processed':
                message.status = 'S'
                run_post_send(message)
            else:
                message.status = 'E'
                message.detailed_status = \
                    'Partner failed to process message: %s' % detailed_status
            # Save the message and create the mdn
            message.save()
            Mdn.objects.create_from_as2mdn(as2mdn=as2mdn,
                                           message=message,
                                           status='R')

            return HttpResponse(_('AS2 ASYNC MDN has been received'))

        except MDNNotFound:
            logger.debug('Payload is not an MDN parse it as an AS2 Message')
            as2message = As2Message()
            status, exception, as2mdn = as2message.parse(
                request_body, self.find_organization, self.find_partner,
                self.check_message_exists)
            logger.info(
                'Received an AS2 message with id {} for organization {} from '
                'partner {}'.format(as2message.headers.get('message-id'),
                                    as2message.headers.get('as2-to'),
                                    as2message.headers.get('as2-from')))

            # In case of duplicates update message id
            if isinstance(exception[0], DuplicateDocument):
                as2message.message_id += '_duplicate'

            # Create the Message and MDN objects
            message, full_fn = Message.objects.create_from_as2message(
                as2message=as2message,
                payload=as2message.content,
                direction='IN',
                status='S' if status == 'processed' else 'E',
                detailed_status=exception[1])

            # run post receive command on success
            if status == 'processed':
                run_post_receive(message, full_fn)

            # Return the mdn in case of sync else return text message
            if as2mdn and as2mdn.mdn_mode == 'SYNC':
                message.mdn = Mdn.objects.create_from_as2mdn(as2mdn=as2mdn,
                                                             message=message,
                                                             status='S')
                response = HttpResponse(as2mdn.content)
                for key, value in as2mdn.headers.items():
                    response[key] = value

                return response
            elif as2mdn and as2mdn.mdn_mode == 'ASYNC':
                Mdn.objects.create_from_as2mdn(as2mdn=as2mdn,
                                               message=message,
                                               status='P',
                                               return_url=as2mdn.mdn_url)
            return HttpResponse(_('AS2 message has been received'))