/** * Decode avro data * * @param string $text * @param string $rawSchema * @return mixed */ public function decodeText($text, $rawSchema) { $schema = AvroSchema::parse($rawSchema); $datum_reader = new AvroIODatumReader($schema, $schema); $read_io = new AvroStringIO($text); $decoder = new AvroIOBinaryDecoder($read_io); $results = $datum_reader->read($decoder); return $results; }
function cse() { // Open the log file to which to write outputs $fp = fopen('test.log', 'at'); // Get all http headers in the received message $headers = getallheaders(); // Get the posted message body // NOTE: The message body is currently in Avro binary form $post_data = file_get_contents("php://input"); // Get the URI of the Avro schema on the OCL server that adheres to the /cse/offer/create contract $schema_uri = $headers['X-XC-SCHEMA-URI']; // Get the contents of the Avro schema identified by the URI retrieved above $content = file_get_contents($schema_uri); // Parse the CSE Avro schema and place results in an AvroSchema object $schema = AvroSchema::parse($content); //fwrite($fp, $schema); //fwrite($fp, "\n"); // Use Avro to decode and deserialize the binary-encoded message body. // The result is the plain text version of the message body // The message sender used Avro to binary-encode the text version of the message body before sending the message. // Create an AvroIODatumReader object for the supplied AvroSchema. // An AvroIODatumReader object handles schema-specific reading of data from the decoder and // ensures that each datum read is consistent with the reader's schema. $datum_reader = new AvroIODatumReader($schema); // Create an AvroStringIO object and assign it the encoded message body $read_io = new AvroStringIO($post_data); // Create an AvroIOBinaryDecoder object and assign it the $read_io object $decoder = new AvroIOBinaryDecoder($read_io); // Decode and deserialize the data using the CSE schema and the supplied decoder // The data is retrieved from the AvroStringIO object $read_io created above // Upon return, $message contains the plain text version of the X.commerce message sent by the publisher $message = $datum_reader->read($decoder); //fwrite($fp, $post_data); fwrite($fp, "\n"); //fwrite($fp, print_r($message, true)); fwrite($fp, print_r($headers, true)); // Connect to the Mongo server running on your machine // NOTE: you must start the Mongo server prior to running this web application $conn = new Mongo('localhost'); // Access the cse_data database // If this database does not exist, Mongo creates it $db = $conn->cse_data; // Access the google collection // If this collection does not exist, Mongo creates it $collection = $db->google; // Insert a new document into the google collection $item = $message["products"]; $collection->insert($item); // Write to log file fwrite($fp, print_r($item, true)); fwrite($fp, "Inserted document with ID: " . $item['_id']); fclose($fp); // Disconnect from the MongoDB server $conn->close(); }
public function testSchemaMatching() { $writers_schema = <<<JSON { "type": "map", "values": "bytes" } JSON; $readers_schema = $writers_schema; $this->assertTrue(AvroIODatumReader::schemas_match(AvroSchema::parse($writers_schema), AvroSchema::parse($readers_schema))); }
/** * @internal Would be nice to implement data() as an iterator, I think * @returns array of data from object container. */ public function data() { $data = array(); while (true) { if (0 == $this->block_count) { if ($this->is_eof()) { break; } if ($this->skip_sync()) { if ($this->is_eof()) { break; } } $this->read_block_header(); } $data[] = $this->datum_reader->read($this->decoder); $this->block_count -= 1; } return $data; }
/** * Entry point to process one procedure call. * @param string $call_request the serialized procedure call request * @param Transceiver $transceiver the transceiver used for the response * @return string|null the serialiazed procedure call response or null if it's a one-way message * @throw AvroException */ public function respond($call_request, Transceiver $transceiver) { $buffer_reader = new AvroStringIO($call_request); $decoder = new AvroIOBinaryDecoder($buffer_reader); $buffer_writer = new AvroStringIO(); $encoder = new AvroIOBinaryEncoder($buffer_writer); $error = null; $response_metadata = array(); try { $remote_protocol = $this->process_handshake($decoder, $encoder, $transceiver); if (is_null($remote_protocol)) { return $buffer_writer->string(); } $request_metadata = $this->meta_reader->read($decoder); $remote_message_name = $decoder->read_string(); if (!isset($remote_protocol->messages[$remote_message_name])) { throw new AvroException("Unknown remote message: {$remote_message_name}"); } $remote_message = $remote_protocol->messages[$remote_message_name]; if (!isset($this->local_protocol->messages[$remote_message_name])) { throw new AvroException("Unknown local message: {$remote_message_name}"); } $local_message = $this->local_protocol->messages[$remote_message_name]; $datum_reader = new AvroIODatumReader($remote_message->request, $local_message->request); $request = $datum_reader->read($decoder); try { $response_datum = $this->invoke($local_message, $request); // if it's a one way message we only send the handshake if needed if ($this->local_protocol->messages[$remote_message_name]->is_one_way()) { return $buffer_writer->string() == "" ? null : $buffer_writer->string(); } } catch (AvroRemoteException $e) { $error = $e; } catch (Exception $e) { $error = new AvroRemoteException($e->getMessage()); } $this->meta_writer->write($response_metadata, $encoder); $encoder->write_boolean(!is_null($error)); if (is_null($error)) { $datum_writer = new AvroIODatumWriter($local_message->response); $datum_writer->write($response_datum, $encoder); } else { $datum_writer = new AvroIODatumWriter($remote_message->errors); $datum_writer->write($error->getDatum(), $encoder); } } catch (AvroException $e) { $error = new AvroRemoteException($e->getMessage()); $buffer_writer = new AvroStringIO(); $encoder = new AvroIOBinaryEncoder($buffer_writer); $this->meta_writer->write($response_metadata, $encoder); $encoder->write_boolean(!is_null($error)); $datum_writer = new AvroIODatumWriter($this->system_error_schema); $datum_writer->write($error->getMessage(), $encoder); } return $buffer_writer->string(); }
$headers = getallheaders(); // Open the log file $fp = fopen('Twitter_Capability.log', 'at'); // Verify the message came from the Fabric if ($headers['Authorization'] != FABRIC_CRED_TWITTER) { fwrite($fp, "\n\nFATAL ERROR: Authorization header does not contain correct Fabric credentials.\n\n"); fclose($fp); die; // terminate this script } // Get the URI of the Avro schema from the OCL server. This URI is in the request header X-XC-SCHEMA-URI $schema_uri = $headers['X-XC-SCHEMA-URI']; // Deserialize the data in the Middleman's message's body $content = file_get_contents($schema_uri); $schema = AvroSchema::parse($content); $datum_reader = new AvroIODatumReader($schema); $read_io = new AvroStringIO($post_data); $decoder = new AvroIOBinaryDecoder($read_io); $message = $datum_reader->read($decoder); print $message . "\n\n\n"; //Here process the message and do the Twitter API stuff /* ini_set('max_execution_time', 123456); system('"C:\wamp\bin\php\php5.3.13\php.exe" -f "C:\wamp\www\filter-track.php" -- "#love" "50" "24"', $retValue); echo $retValue . "\n"; */ // Get the full request URI $request_uri = $_SERVER['REQUEST_URI']; // Get the topic out of the request URI if (!($topic = strstr($request_uri, "/com.x.hashtagsale.v1/experimental/StartHTS"))) { fwrite($fp, "\n\nUnexpected topic: " . $request_uri . "\n\n");