public static function fromXml($data) { $doc = new DOMDocument(); $parseOk = false; $parseOk = $doc->loadXML($data); if (!$parseOk) { throw new EsuException('Failed to parse Policy XML'); } $policy = new Policy(); $root = $doc->firstChild; if ($root->localName != 'policy') { throw new EsuException('Expected root node to be \'policy\'. It was ' . $root->localName); } $policy->loadFromElement($root); return $policy; }
public function testSerializePolicy() { $policy = new Policy(); $policy->expiration = 1354363200; $policy->maxUploads = 1; $policy->source = new SourceType(); $policy->source->allow[] = "127.0.0.0/24"; $policy->contentLengthRange = new ContentLengthRangeType(); $policy->contentLengthRange->from = 10; $policy->contentLengthRange->to = 11000; $formField = new FormFieldType(); $formField->name = 'x-emc-redirect-url'; $policy->formField[] = $formField; $formField = new FormFieldType(); $formField->name = 'x-emc-meta'; $formField->optional = True; $formField->matches[] = "^(\\w+=\\w+)|((\\w+=\\w+),(\\w+, \\w+))\$"; $policy->formField[] = $formField; $xml = $policy->toXml(); PHPUnit_Framework_Assert::assertEquals($this->xml_output, $xml); }
/** * Creates a new anonymous access token. * @param Policy $policy The policy to apply to the access token. * @param Identifier $id optional identifier to associate with the access * token. For download tokens, this can be either an ObjectPath or * ObjectId. For upload tokens, this can be an ObjectPath. * @param MetadataList $metadata for upload tokens, metadata to associate * with the newly created object. * @param Acl $acl For upload tokens, an ACL to associate with the newly * @return string the new access token ID. */ public function createAccessToken($policy = NULL, $id = NULL, $metadata = NULL, $acl = NULL) { // Build the request $resource = $this->context . '/accesstokens'; $req = $this->buildRequest($resource, null); $response = null; $headers = array(); $mimeType = 'text/xml'; $headers['Content-Type'] = $mimeType; $headers['x-emc-uid'] = $this->uid; // Process metadata if ($metadata != null) { $this->processMetadata($metadata, $headers); } if (isset($headers['x-emc-meta'])) { $this->trace('meta ' . $headers['x-emc-meta']); } if ($this->utf8) { $headers['x-emc-utf8'] = 'true'; } // Add acl if ($acl != null) { $this->processAcl($acl, $headers); } // Process data if ($policy != null) { $req->setBody($policy->toXml()); } else { $req->setBody(''); } // Add date $headers['Date'] = gmdate('r'); // Check if $id is set if ($id != null) { if (is_a($id, 'Atmos\\ObjectId')) { $headers['x-emc-objectid'] = $id->__toString(); } elseif (is_a($id, 'Atmos\\ObjectPath')) { if ($this->utf8) { $headers['x-emc-path'] = $this->urlencode($id->__toString()); } else { $headers['x-emc-path'] = $id->__toString(); } } else { throw new EsuException("Invalid ID for access token: {$id}"); } } // Sign and send the request $this->signRequest($req, 'POST', $resource, $headers); try { $response = @$req->send(); } catch (HTTP_Request2_Exception $e) { throw new EsuException('Sending request failed: ' . $e->__toString()); } // Return the response if ($response->getStatus() > 299) { $this->handleError($response); } // The new object ID is returned in the location response header $location = $response->getHeader('location'); $pos = array(); // Parse the value out of the URL preg_match(EsuRestApi::$ACCESS_TOKEN_EXTRACTOR, $location, $pos); $this->trace('Location: ' . $location); $this->trace('regex: ' . EsuRestApi::$ACCESS_TOKEN_EXTRACTOR); $this->trace('pos 1 ' . $pos[1]); return $pos[1]; }