public function testSetDataTable()
 {
     $request = new Request();
     $data = new DataTable();
     $data->addColumn(new ColumnDescription('textfield', ValueType::STRING));
     $data->addColumn(new ColumnDescription('intfield', ValueType::NUMBER));
     $response = new Response($request);
     $response->setDataTable($data);
     $getRequest = $response->getRequest();
     $getDataTable = $response->getDataTable();
     $this->assertEquals($request, $getRequest, "Request must match");
     $this->assertEquals($data, $getDataTable, "DataTable must match");
 }
 /**
  * @param Response $response
  * @param ResponseStatus $responseStatus
  * @return string JSON or JSONP output
  */
 public function render(Response $response, ResponseStatus $responseStatus = null)
 {
     $output = '';
     $request = $response->getRequest();
     $outputTypeCode = $request->getOutputType()->getCode();
     $isJsonP = $outputTypeCode === OutputType::JSONP;
     if ($isJsonP) {
         $responseHandler = $request->getParameters()->getParameter(RequestParameters::RESPONSE_HANDLER_PARAMETER);
         $output .= $responseHandler . '(';
     }
     $dataTable = $response->getDataTable();
     $output .= "{\"version\":\"0.6\"";
     $reqId = $request->getRequestId();
     if ($reqId !== false) {
         $output .= ",\"reqId\":" . json_encode($reqId);
     }
     if ($responseStatus === null) {
         $sig = $request->getSignature();
         if ($sig !== false && $sig === $this->getSignature($dataTable)) {
             $responseStatus = new ResponseStatus(new StatusType(StatusType::ERROR), new ReasonType(ReasonType::NOT_MODIFIED));
         } else {
             $responseStatus = new ResponseStatus(new StatusType(StatusType::OK));
         }
     }
     $statusTypeCode = $responseStatus->getStatusType()->getCode();
     $output .= ",\"status\":" . json_encode($statusTypeCode);
     if ($statusTypeCode != StatusType::OK) {
         if ($statusTypeCode == StatusType::WARNING) {
             $warnings = $dataTable->getWarnings();
             $warningStrings = array();
             foreach ($warnings as $warning) {
                 $warningStrings[] = $this->getFaultString($warning->getReasonType(), $warning->getMessage());
             }
             $output .= ",\"warnings\":[" . implode(",", $warningStrings) . "]";
         } else {
             $errorsOutput = $this->getFaultString($responseStatus->getReasonType(), $responseStatus->getDescription());
             $output .= ",\"errors\":[" . $errorsOutput . "]";
         }
     }
     if ($statusTypeCode != StatusType::ERROR && $dataTable !== null) {
         $output .= ",\"sig\":" . json_encode($this->getSignature($dataTable));
         $output .= ",\"table\":" . $this->renderDataTable($dataTable, true, true, $isJsonP);
     }
     $output .= "}";
     if ($isJsonP) {
         $output .= ");";
     }
     return $output;
 }
 /**
  * @param Response $response
  * @throws Exception\NotImplementedException
  * @throws Exception
  */
 protected function prepare(Response $response)
 {
     $request = $response->getRequest();
     $requestParams = $request->getParameters();
     $outputTypeCode = $requestParams->getParameter(RequestParameters::OUTPUT_TYPE_PARAMETER);
     switch ($outputTypeCode) {
         case OutputType::TSV_EXCEL:
             throw new NotImplementedException($outputTypeCode . " output is not implemented");
             break;
         case OutputType::CSV:
             $this->headers['Content-Type'] = 'text/csv; charset=UTF-8';
             $outFilename = $requestParams->getParameter(RequestParameters::OUTPUT_FILE_NAME_PARAMETER);
             if ($outFilename === false) {
                 $outFilename = 'results.csv';
             }
             // For security reasons, make sure this filename ends with .csv
             if (substr($outFilename, -4) !== '.csv') {
                 $outFilename .= '.csv';
             }
             $this->headers['Content-Disposition'] = 'attachment; filename=' . $outFilename;
             break;
         case OutputType::HTML:
             $this->headers['Content-Type'] = 'text/html; charset=UTF-8';
             break;
         case OutputType::JSONP:
             $this->headers['Content-Type'] = 'text/javascript; charset=UTF-8';
             break;
         case OutputType::JSON:
             $this->headers['Content-Type'] = 'application/json; charset=UTF-8';
             break;
         default:
             throw new Exception("Unknown Output Type: " . $outputTypeCode);
             break;
     }
     $this->output = $response->__toString();
 }
Esempio n. 4
0
 /**
  * @param Response &$response [required] [IN] [OUT]
  */
 protected function executeTrue(Response &$response)
 {
     $request = $response->getRequest();
     // Verify that the user is granted access to the data
     if ($this->isRestrictedAccessMode()) {
         $this->verifyAccessAllowed($request->getOutputType());
     }
     // Populate the data
     $data =& $this->getDataTable($request);
     // Apply query, if any
     $query = Query::constructFromString($request->getQuery());
     if (!$query->isEmpty()) {
         $data =& QueryEngine::execute($query, $data);
     }
     $response->setDataTable($data);
 }