Exemplo n.º 1
0
 /**
  * @param $toPartner
  * @param $fromPartner
  * @param $messageContent
  * @throws \Exception
  * @throws \TechData\AS2SecureBundle\Models\AS2Exception
  * @throws \TechData\AS2SecureBundle\Models\Exception
  */
 public function sendMessage($toPartner, $fromPartner, $messageContent)
 {
     // process request to build outbound AS2 message to VAR
     // initialize outbound AS2Message object
     $message = $this->messageFactory->build(false, array('partner_from' => $fromPartner, 'partner_to' => $toPartner));
     // initialize AS2Adapter for public key encryption between StreamOne and the receiving VAR
     $adapter = $this->adapterFactory->build($fromPartner, $toPartner);
     // write the EDI message that will be sent to a temp file, then use the AS2 adapter to encrypt it
     $tmp_file = $adapter->getTempFilename();
     file_put_contents($tmp_file, $messageContent);
     $message->addFile($tmp_file, 'application/edi-x12');
     $message->encode();
     // send AS2 message
     $result = $this->client->sendRequest($message);
     $messageSent = new MessageSent();
     $messageSent->setMessage(print_r($result, true));
     $this->eventDispatcher->dispatch(MessageSent::EVENT, $messageSent);
 }
Exemplo n.º 2
0
 /**
  * Handle a request (server side)
  *
  * @param request (If not set, get data from standard input)
  *
  * @return request    The request handled
  */
 public function handle(Request $request)
 {
     // handle any problem in case of SYNC MDN process
     ob_start();
     try {
         $error = null;
         $headers = $request->getHeaders();
         $object = $request->getObject();
     } catch (Exception $e) {
         // get error while handling request
         $error = $e;
         //throw $e;
     }
     //
     $mdn = null;
     if ($object instanceof Message || !is_null($error) && !$object instanceof MDN) {
         $object_type = self::TYPE_MESSAGE;
         $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'Incoming transmission is a Message.'));
         try {
             if (is_null($error)) {
                 $object->decode();
                 $files = $object->getFiles();
                 $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, count($files) . ' payload(s) found in incoming transmission.'));
                 foreach ($files as $key => $file) {
                     $content = file_get_contents($file['path']);
                     $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'Payload #' . ($key + 1) . ' : ' . round(strlen($content) / 1024, 2) . ' KB / "' . $file['filename'] . '".'));
                     $this->saveMessage($content, array(), 'payload');
                 }
                 $mdn = $object->generateMDN($error);
                 $mdn->encode($object);
             } else {
                 throw $error;
             }
         } catch (Exception $e) {
             $params = array('partner_from' => $headers->getHeader('as2-from'), 'partner_to' => $headers->getHeader('as2-to'));
             $mdn = $this->mdnFactory->build($e, $params);
             $mdn->setAttribute('original-message-id', $headers->getHeader('message-id'));
             $mdn->encode();
         }
     } elseif ($object instanceof MDN) {
         $object_type = self::TYPE_MDN;
         $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'Incoming transmission is a MDN.'));
     } else {
         $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_ERROR, 'Malformed data.'));
     }
     // build MDN
     if (!is_null($error) && $object_type == self::TYPE_MESSAGE) {
         $params = array('partner_from' => $headers->getHeader('as2-from'), 'partner_to' => $headers->getHeader('as2-to'));
         $mdn = $this->mdnFactory->build($e, $params);
         $mdn->setAttribute('original-message-id', $headers->getHeader('message-id'));
         $mdn->encode();
     }
     // send MDN
     if (!is_null($mdn)) {
         if (!$headers->getHeader('receipt-delivery-option')) {
             // SYNC method
             // re-active output data
             ob_end_clean();
             // send headers
             foreach ($mdn->getHeaders() as $key => $value) {
                 $header = str_replace(array("\r", "\n", "\r\n"), '', $key . ': ' . $value);
                 header($header);
             }
             // output MDN
             echo $mdn->getContent();
             $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'An AS2 MDN has been sent.'));
         } else {
             // ASYNC method
             // cut connection and wait a few seconds
             $this->closeConnectionAndWait(5);
             // delegate the mdn sending to the client
             $result = $this->client->sendRequest($mdn);
             if ($result['info']['http_code'] == '200') {
                 $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_INFO, 'An AS2 MDN has been sent.'));
             } else {
                 $this->eventDispatcher->dispatch('log', new Log(Log::TYPE_ERROR, 'An error occurs while sending MDN message : ' . $result['info']['http_code']));
             }
         }
     }
     return $request;
 }