public function SetResourceContentOrHeader($resId, $format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     try {
         $sessionId = "";
         if ($resId->GetRepositoryType() == MgRepositoryType::Session) {
             $sessionId = $resId->GetRepositoryName();
         }
         $this->EnsureAuthenticationForSite($sessionId);
         $siteConn = new MgSiteConnection();
         $siteConn->Open($this->userInfo);
         $contentFilePath = $this->GetFileUploadPath("content");
         $headerFilePath = null;
         //Header not supported for session-based resources
         if ($resId->GetRepositoryType() != MgRepositoryType::Session) {
             $headerFilePath = $this->GetFileUploadPath("header");
         }
         $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
         $site = $siteConn->GetSite();
         $resIdStr = $resId->ToString();
         $mimeType = $this->GetMimeTypeForFormat($fmt);
         if ($contentFilePath) {
             $this->VerifyWhitelist($resIdStr, $mimeType, "SETRESOURCE", $fmt, $site, $this->userName);
         }
         if ($headerFilePath) {
             $this->VerifyWhitelist($resIdStr, $mimeType, "SETRESOURCEHEADER", $fmt, $site, $this->userName);
         }
         $content = null;
         $header = null;
         if ($contentFilePath != null) {
             $cntSource = new MgByteSource($contentFilePath);
             $content = $cntSource->GetReader();
         }
         if ($headerFilePath != null) {
             $hdrSource = new MgByteSource($headerFilePath);
             $header = $hdrSource->GetReader();
         }
         if ($fmt == "json") {
             if ($content != null) {
                 $body = $content->ToString();
                 $json = json_decode($body);
                 if ($json == NULL) {
                     throw new Exception($this->app->localizer->getText("E_MALFORMED_JSON_BODY"));
                 }
                 $body = MgUtils::Json2Xml($json);
                 $cntSource = new MgByteSource($body, strlen($body));
                 $content = $cntSource->GetReader();
             }
             if ($header != null) {
                 $body = $header->ToString();
                 $json = json_decode($body);
                 if ($json == NULL) {
                     throw new Exception($this->app->localizer->getText("E_MALFORMED_JSON_BODY"));
                 }
                 $body = MgUtils::Json2Xml($json);
                 $hdrSource = new MgByteSource($body, strlen($body));
                 $header = $hdrSource->GetReader();
             }
         }
         $resSvc->SetResource($resId, $content, $header);
         $this->app->response->setStatus(201);
         $body = MgBoxedValue::String($resId->ToString(), $fmt);
         if ($fmt == "xml") {
             $this->app->response->header("Content-Type", MgMimeType::Xml);
         } else {
             $this->app->response->header("Content-Type", MgMimeType::Json);
         }
         $this->app->response->setBody($body);
     } catch (MgException $ex) {
         $this->OnException($ex);
     }
 }
 public function UpdateFeatures($resId, $schemaName, $className, $format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     $mimeType = $this->GetMimeTypeForFormat($fmt);
     $trans = null;
     try {
         $sessionId = "";
         if ($resId->GetRepositoryType() == MgRepositoryType::Session) {
             $sessionId = $resId->GetRepositoryName();
         }
         $this->EnsureAuthenticationForSite($sessionId);
         $siteConn = new MgSiteConnection();
         $siteConn->Open($this->userInfo);
         $site = $siteConn->GetSite();
         $this->VerifyWhitelist($resId->ToString(), $mimeType, "UPDATEFEATURES", $fmt, $site, $this->userName);
         $body = $this->app->request->getBody();
         if ($fmt == "json") {
             $json = json_decode($body);
             if ($json == NULL) {
                 throw new Exception($this->app->localizer->getText("E_MALFORMED_JSON_BODY"));
             }
             $body = MgUtils::Json2Xml($json);
         }
         $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
         $perms = self::CheckPermissions($resSvc, $resId);
         //Not a session-based resource, must check for appropriate flag in header before we continue
         if ($sessionId === "") {
             if ($perms->AllowUpdate === false) {
                 $e = new Exception();
                 $this->OutputException("Forbidden", $this->app->localizer->getText("E_OPERATION_NOT_ALLOWED"), $this->app->localizer->getText("E_FEATURE_SOURCE_NOT_CONFIGURED_TO_ALLOW_UPDATES", $resId->ToString()), $e->getTraceAsString(), 403, $mimeType);
             }
         }
         $featSvc = $siteConn->CreateService(MgServiceType::FeatureService);
         $doc = new DOMDocument();
         $doc->loadXML($body);
         $commands = new MgFeatureCommandCollection();
         $filter = "";
         $filterNode = $doc->getElementsByTagName("Filter");
         if ($filterNode->length == 1) {
             $filter = $filterNode->item(0)->nodeValue;
         }
         $classDef = $featSvc->GetClassDefinition($resId, $schemaName, $className);
         $props = MgUtils::ParseSingleFeatureDocument($this->app, $classDef, $doc, "UpdateProperties");
         $updateCmd = new MgUpdateFeatures("{$schemaName}:{$className}", $props, $filter);
         $commands->Add($updateCmd);
         if ($perms->UseTransaction === true) {
             $trans = $featSvc->BeginTransaction($resId);
         }
         //HACK: Due to #2252, we can't call UpdateFeatures() with NULL MgTransaction, so to workaround
         //that we call the original UpdateFeatures() overload with useTransaction = false if we find a
         //NULL MgTransaction
         if ($trans == null) {
             $result = $featSvc->UpdateFeatures($resId, $commands, false);
         } else {
             $result = $featSvc->UpdateFeatures($resId, $commands, $trans);
         }
         if ($trans != null) {
             $trans->Commit();
         }
         $this->OutputUpdateFeaturesResult($commands, $result, $classDef, $fmt == "json");
     } catch (MgException $ex) {
         if ($trans != null) {
             $trans->Rollback();
         }
         $this->OnException($ex, $mimeType);
     }
 }
示例#3
0
 /**
  * Handles PUT requests for this adapter. Overridable. Does nothing if not overridden.
  */
 public function HandlePut($single)
 {
     $trans = null;
     try {
         $tokens = explode(":", $this->className);
         $schemaName = $tokens[0];
         $className = $tokens[1];
         if ($this->app->REQUEST_BODY_DOCUMENT == null) {
             $json = json_decode($this->app->request->getBody());
             $body = MgUtils::Json2Xml($json);
             $doc = new DOMDocument();
             $doc->loadXML($body);
         } else {
             $doc = $this->app->REQUEST_BODY_DOCUMENT;
         }
         $commands = new MgFeatureCommandCollection();
         $classDef = $this->featSvc->GetClassDefinition($this->featureSourceId, $schemaName, $className);
         $filter = "";
         //If single-record, infer filter from URI
         if ($single === true) {
             $idProps = $classDef->GetIdentityProperties();
             if ($idProps->GetCount() != 1) {
                 $app->halt(400, $this->app->localizer->getText("E_CANNOT_APPLY_UPDATE_CANNOT_UNIQUELY_IDENTIFY", $this->featureId, $idProps->GetCount()));
             } else {
                 $idProp = $idProps->GetItem(0);
                 if ($idProp->GetDataType() == MgPropertyType::String) {
                     $filter = $idProp->GetName() . " = '" . $this->featureId . "'";
                 } else {
                     $filter = $idProp->GetName() . " = " . $this->featureId;
                 }
             }
         } else {
             //Otherwise, use the filter from the request envelope (if specified)
             $filterNode = $doc->getElementsByTagName("Filter");
             if ($filterNode->length == 1) {
                 $filter = $filterNode->item(0)->nodeValue;
             }
         }
         $classDef = $this->featSvc->GetClassDefinition($this->featureSourceId, $schemaName, $className);
         $props = MgUtils::ParseSingleFeatureDocument($this->app, $classDef, $doc, "UpdateProperties");
         $updateCmd = new MgUpdateFeatures("{$schemaName}:{$className}", $props, $filter);
         $commands->Add($updateCmd);
         if ($this->useTransaction) {
             $trans = $this->featSvc->BeginTransaction($this->featureSourceId);
         }
         //HACK: Due to #2252, we can't call UpdateFeatures() with NULL MgTransaction, so to workaround
         //that we call the original UpdateFeatures() overload with useTransaction = false if we find a
         //NULL MgTransaction
         if ($trans == null) {
             $result = $this->featSvc->UpdateFeatures($this->featureSourceId, $commands, false);
         } else {
             $result = $this->featSvc->UpdateFeatures($this->featureSourceId, $commands, $trans);
         }
         if ($trans != null) {
             $trans->Commit();
         }
         $this->OutputUpdateFeaturesResult($commands, $result, $classDef, true);
     } catch (MgException $ex) {
         if ($trans != null) {
             $trans->Rollback();
         }
         $this->OnException($ex, MgMimeType::Json);
     }
 }