public function testBoxString()
 {
     $bvXmlStr = MgBoxedValue::String("foo", "xml");
     $bvJsonStr = MgBoxedValue::String("bar", "json");
     $doc = new DOMDocument();
     $doc->loadXML($bvXmlStr);
     $this->assertTrue($doc->getElementsByTagName("Type")->length == 1);
     $this->assertEquals("String", $doc->getElementsByTagName("Type")->item(0)->nodeValue);
     $this->assertTrue($doc->getElementsByTagName("Value")->length == 1);
     $this->assertEquals("foo", $doc->getElementsByTagName("Value")->item(0)->nodeValue);
     $val = json_decode($bvJsonStr);
     $this->assertEquals("String", $val->PrimitiveValue->Type);
     $this->assertEquals("bar", $val->PrimitiveValue->Value);
 }
 public function GetSiteVersion($format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     try {
         $this->EnsureAuthenticationForSite();
         $admin = new MgServerAdmin();
         $admin->Open($this->userInfo);
         $body = MgBoxedValue::String($admin->GetSiteVersion(), $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 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 ExecuteHttpRequest($req, $ovHandler = NULL)
 {
     $param = $req->GetRequestParam();
     $origMimeType = $param->GetParameterValue("FORMAT");
     try {
         //If JSON format specified, replace it with XML response and use our
         //own XML to JSON converter. This is to allow for "cleaner" JSON output
         //that only our converter can do
         if ($origMimeType == MgMimeType::Json) {
             $param->SetParameterValue("FORMAT", MgMimeType::Xml);
             if (!$param->ContainsParameter("X-FORCE-JSON-CONVERSION")) {
                 $param->AddParameter("X-FORCE-JSON-CONVERSION", "true");
             } else {
                 $param->SetParameterValue("X-FORCE-JSON-CONVERSION", "true");
             }
         }
         $response = $req->Execute();
         $result = $response->GetResult();
         $status = $result->GetStatusCode();
         //If there's a custom handler passed in, it takes responsibility
         if ($ovHandler != null && is_callable($ovHandler)) {
             call_user_func_array($ovHandler, array($result, $status));
             return $status;
         } else {
             $bDownload = false;
             if ($param->GetParameterValue("X-DOWNLOAD-ATTACHMENT") === "true") {
                 $bDownload = true;
             }
             if ($status == 200) {
                 $resultObj = $result->GetResultObject();
                 if ($resultObj != null) {
                     $mimeType = $result->GetResultContentType();
                     $this->app->response->headers->set("Content-Type", $mimeType);
                     //Set download response headers if specified
                     if ($bDownload === true) {
                         $filebasename = "download";
                         if ($param->ContainsParameter("X-DOWNLOAD-ATTACHMENT-NAME")) {
                             $filebasename = $param->GetParameterValue("X-DOWNLOAD-ATTACHMENT-NAME");
                         }
                         $this->app->response->headers->set("Content-Disposition", "attachment; filename=" . MgUtils::GetFileNameFromMimeType($filebasename, $mimeType));
                     }
                     if ($resultObj instanceof MgByteReader) {
                         if ($param->GetParameterValue("X-FORCE-JSON-CONVERSION") === "true") {
                             if ($result->GetResultContentType() === MgMimeType::Xml && $param->ContainsParameter("XSLSTYLESHEET")) {
                                 $body = MgUtils::XslTransformByteReader($this->app, $resultObj, $param->GetParameterValue("XSLSTYLESHEET"), $this->CollectXslParameters($param));
                                 $this->app->response->header("Content-Type", MgMimeType::Json);
                                 $this->app->response->setBody(MgUtils::Xml2Json($body));
                             } else {
                                 $this->OutputXmlByteReaderAsJson($resultObj);
                             }
                         } else {
                             if ($result->GetResultContentType() === MgMimeType::Xml && $param->ContainsParameter("XSLSTYLESHEET")) {
                                 if ($param->ContainsParameter("X-OVERRIDE-CONTENT-TYPE")) {
                                     $this->app->response->header("Content-Type", $param->GetParameterValue("X-OVERRIDE-CONTENT-TYPE"));
                                 }
                                 $this->app->response->setBody(MgUtils::XslTransformByteReader($this->app, $resultObj, $param->GetParameterValue("XSLSTYLESHEET"), $this->CollectXslParameters($param)));
                             } else {
                                 $this->OutputByteReader($resultObj, $param->GetParameterValue("X-CHUNK-RESPONSE") === "true", $param->GetParameterValue("X-PREPEND-XML-PROLOG") === "true");
                             }
                         }
                     } else {
                         if ($resultObj instanceof MgStringCollection) {
                             $this->OutputMgStringCollection($resultObj, $param->GetParameterValue("FORMAT"));
                         } else {
                             if ($resultObj instanceof MgHttpPrimitiveValue) {
                                 $fmt = "xml";
                                 if ($param->GetParameterValue("X-FORCE-JSON-CONVERSION") === "true") {
                                     $fmt = "json";
                                     $this->app->response->header("Content-Type", MgMimeType::Json);
                                 } else {
                                     $this->app->response->header("Content-Type", MgMimeType::Xml);
                                 }
                                 $body = null;
                                 switch ($resultObj->GetType()) {
                                     case 1:
                                         $body = MgBoxedValue::Boolean($resultObj->GetBoolValue(), $fmt);
                                         break;
                                     case 2:
                                         $body = MgBoxedValue::Integer($resultObj->GetIntegerValue(), $fmt);
                                         break;
                                     case 3:
                                         $body = MgBoxedValue::String($resultObj->GetStringValue(), $fmt);
                                         break;
                                 }
                                 if ($body != null) {
                                     $this->app->response->setBody($body);
                                 }
                             } else {
                                 if (method_exists($resultObj, "ToXml")) {
                                     $byteReader = $resultObj->ToXml();
                                     if ($param->GetParameterValue("X-FORCE-JSON-CONVERSION") === "true") {
                                         $this->OutputXmlByteReaderAsJson($byteReader);
                                     } else {
                                         $this->OutputByteReader($byteReader, $param->GetParameterValue("X-CHUNK-RESPONSE") === "true");
                                     }
                                 } else {
                                     $this->ServerError($this->app->localizer->getText("E_DONT_KNOW_HOW_TO_OUTPUT", $resultObj->ToString()));
                                 }
                             }
                         }
                     }
                 }
             } else {
                 $format = $origMimeType;
                 if ($param->ContainsParameter("XSLSTYLESHEET")) {
                     $format = MgMimeType::Html;
                 } else {
                     $format = $this->GetMimeTypeForFormat($format);
                 }
                 if ($format != "") {
                     $this->OutputError($result, $format);
                 } else {
                     $this->OutputError($result);
                 }
             }
         }
         return $status;
     } catch (MgException $ex) {
         $this->OnException($ex, $origMimeType);
     }
 }
 public function CreateSession($format)
 {
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     try {
         $this->EnsureAuthenticationForSite();
         $siteConn = new MgSiteConnection();
         $siteConn->Open($this->userInfo);
         $site = $siteConn->GetSite();
         $session = $site->CreateSession();
         $this->app->response->setStatus(201);
         $body = MgBoxedValue::String($session, $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 ConvertWktToEpsg($wkt, $format)
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("xml", "json"));
     $factory = new MgCoordinateSystemFactory();
     $cs = $factory->Create($wkt);
     $body = MgBoxedValue::Int32($cs->GetEpsgCode(), $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);
 }