13 namespace Fossology\UI\Api\Controllers;
32 use Psr\Http\Message\ServerRequestInterface;
33 use Slim\Psr7\Factory\StreamFactory;
34 use Slim\Psr7\Request as SlimRequest;
35 use Slim\Psr7\UploadedFile as SlimUploadedFile;
36 use Symfony\Component\HttpFoundation\File\UploadedFile;
37 use Symfony\Component\HttpFoundation\Request;
84 public function getReport($request, $response, $args)
89 if ($apiVersion == ApiVersion::V2) {
90 $query = $request->getQueryParams();
91 $uploadId = $query[
'uploadId'];
92 $reportFormat = $query[
'reportFormat'];
94 $uploadId = $request->getHeaderLine(
'uploadId');
95 $reportFormat = $request->getHeaderLine(
'reportFormat');
98 if (! in_array($reportFormat, $this->reportsAllowed)) {
100 "reportFormat must be from [" . implode(
",", $this->reportsAllowed) .
104 if (get_class($upload) === Info::class) {
105 return $response->withJson($upload->getArray(), $upload->getCode());
110 switch ($reportFormat) {
116 $spdxGenerator = $this->restHelper->getPlugin(
'ui_spdx2');
117 list ($jobId, $jobQueueId, $error) = $spdxGenerator->scheduleAgent(
118 $this->restHelper->getGroupId(), $upload, $reportFormat);
122 $readmeGenerator = $this->restHelper->getPlugin(
'ui_readmeoss');
123 list ($jobId, $jobQueueId, $error) = $readmeGenerator->scheduleAgent(
124 $this->restHelper->getGroupId(), $upload);
126 case 'unifiedreport':
128 $unifiedGenerator = $this->restHelper->getPlugin(
'agent_founifiedreport');
129 list ($jobId, $jobQueueId, $error) = $unifiedGenerator->scheduleAgent(
130 $this->restHelper->getGroupId(), $upload);
134 $clixmlGenerator = $this->restHelper->getPlugin(
'ui_clixml');
135 list ($jobId, $jobQueueId) = $clixmlGenerator->scheduleAgent(
136 $this->restHelper->getGroupId(), $upload);
138 case 'decisionexporter':
140 $decisionExporter = $this->restHelper->getPlugin(
'agent_fodecisionexporter');
141 list($jobId, $jobQueueId) = $decisionExporter->scheduleAgent(
142 $this->restHelper->getGroupId(), $upload);
146 $cyclonedxGenerator = $this->restHelper->getPlugin(
'ui_cyclonedx');
147 list ($jobId, $jobQueueId) = $cyclonedxGenerator->scheduleAgent(
148 $this->restHelper->getGroupId(), $upload);
155 $spdx3Generator = $this->restHelper->getPlugin(
'ui_spdx3');
156 list ($jobId, $jobQueueId, $error) = $spdx3Generator->scheduleAgent(
157 $this->restHelper->getGroupId(), $upload, $reportFormat);
163 $info =
new Info(201, $download_path, InfoType::INFO);
164 return $response->withJson($info->getArray(), $info->getCode());
176 if (empty($uploadId) || ! is_numeric($uploadId) || $uploadId <= 0) {
179 $uploadDao = $this->restHelper->getUploadDao();
180 if (! $uploadDao->isAccessible($uploadId, $this->restHelper->getGroupId())) {
183 $upload = $uploadDao->getUpload($uploadId);
184 if ($upload ===
null) {
198 $url_parts = $request->getUri();
200 if (!empty($url_parts->getScheme())) {
201 $download_path .= $url_parts->getScheme() .
"://";
203 if (!empty($url_parts->getHost())) {
204 $download_path .= $url_parts->getHost();
206 if (!empty($url_parts->getPort())) {
207 $download_path .=
':' . $url_parts->getPort();
209 $endpoint = substr($url_parts->getPath(), 0, strpos($url_parts->getPath(),
210 $GLOBALS[
"apiBasePath"]) + strlen($GLOBALS[
"apiBasePath"]));
211 if (substr($endpoint, -1) !==
'/') {
214 $endpoint .=
"report/" . $jobId;
215 $download_path .= $endpoint;
216 return $download_path;
228 public function downloadReport($request, $response, $args)
233 $ui_download = $this->restHelper->getPlugin(
'download');
234 $responseFile = $ui_download->getReport($args[
'id']);
235 $responseContent = $responseFile->getFile();
236 $newResponse = $response->withHeader(
'Content-Description',
238 ->withHeader(
'Content-Type',
239 $responseContent->getMimeType())
240 ->withHeader(
'Content-Disposition',
241 $responseFile->headers->get(
'Content-Disposition'))
242 ->withHeader(
'Cache-Control',
'must-revalidate')
243 ->withHeader(
'Pragma',
'private')
244 ->withHeader(
'Content-Length', filesize($responseContent->getPathname()));
245 $sf =
new StreamFactory();
246 return $newResponse->withBody(
247 $sf->createStreamFromFile($responseContent->getPathname())
260 $dbManager = $this->dbHelper->getDbManager();
261 $row = $dbManager->getSingleRow(
262 'SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1', array(
264 ),
"reportValidity");
265 if (empty($row) || ! in_array($row[
'jq_type'], $this->reportsAllowed)) {
267 "No report scheduled with given job id.");
269 $row = $dbManager->getSingleRow(
'SELECT job_upload_fk FROM job WHERE job_pk = $1',
270 array($id),
"reportFileUpload");
273 "No report scheduled with given job id.");
275 $uploadId = intval($row[
'job_upload_fk']);
276 $uploadDao = $this->restHelper->getUploadDao();
277 if (! $uploadDao->isAccessible($uploadId, $this->restHelper->getGroupId())) {
280 $row = $dbManager->getSingleRow(
'SELECT * FROM reportgen WHERE job_fk = $1',
281 array($id),
"reportFileName");
284 "Report is not ready. Retry after 10s."))
285 ->setHeaders([
'Retry-After' =>
'10']);
300 public function importReport(ServerRequestInterface $request,
303 $query = $request->getQueryParams();
304 if (!array_key_exists(
"upload", $query)) {
307 if (!array_key_exists(
"reportFormat", $query) ||
308 !in_array($query[
"reportFormat"], $this->importAllowed)) {
309 throw new HttpBadRequestException(
310 "Missing or wrong query param 'reportFormat'");
312 $upload_pk = intval($query[
'upload']);
315 $response_from_scheduler, $error_info);
316 if (!$commu_status) {
317 throw new HttpServiceUnavailableException(
"Scheduler is not running!");
319 $files = $request->getUploadedFiles();
322 if (empty($files[
'report'])) {
323 throw new HttpBadRequestException(
"No file uploaded");
326 $slimFile = $files[
'report'];
328 $reportFormat = $query[
"reportFormat"];
329 switch ($reportFormat) {
330 case $this->importAllowed[0]:
331 $returnVal = $this->importDecisionJson($request, $response,
332 $upload_pk, $slimFile);
334 case $this->importAllowed[1]:
335 $returnVal = $this->importSpdxReport($request, $response, $upload_pk,
339 throw new HttpBadRequestException(
340 "Report format $reportFormat not supported. Supported formats are [" .
341 implode(
", ", $this->importAllowed) .
"]");
356 private function importDecisionJson(ServerRequestInterface $request,
357 ResponseHelper $response,
int $uploadId,
358 SlimUploadedFile $slimFile): ResponseHelper
362 $decisionImporter = $this->restHelper->getPlugin(
"ui_fodecisionimporter");
363 $symfonyRequest =
new Request();
367 if (!array_key_exists(
"importerUser", $reqBody)) {
368 throw new HttpBadRequestException(
"Missing parameter 'importerUser'");
371 $importerUser = intval($reqBody[
"importerUser"]);
372 if (empty($importerUser)) {
373 $importerUser = $this->restHelper->getUserId();
376 $uploadedFile =
new UploadedFile($slimFile->getFilePath(),
377 $slimFile->getClientFilename(), $slimFile->getClientMediaType());
379 $symfonyRequest->files->set(
'report', $uploadedFile);
380 $symfonyRequest->request->set(
'uploadselect', $uploadId);
381 $symfonyRequest->request->set(
'userselect', $importerUser);
383 $agentResp = $decisionImporter->handleRequest($symfonyRequest);
385 if ($agentResp ===
false) {
386 throw new HttpBadRequestException(
"Missing required fields");
388 $info =
new Info(201, intval($agentResp[0]), InfoType::INFO);
389 return $response->withJson($info->getArray(), $info->getCode());
401 public function importSpdxReport(ServerRequestInterface $request,
402 ResponseHelper $response,
int $upload_pk,
403 SlimUploadedFile $slimFile): ResponseHelper
407 $reportImport = $this->restHelper->getPlugin(
'ui_reportImport');
408 $symfonyRequest =
new Request();
412 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'] .
"/ReportImport/";
413 if (!is_dir($fileBase)) {
414 mkdir($fileBase, 0755,
true);
416 $targetFile = time() .
'_' . rand() .
'_' . $slimFile->getClientFilename();
417 $slimFile->moveTo($fileBase . $targetFile);
420 $addNewLicensesAs =
"candidate";
421 $addLicenseInfoFromInfoInFile =
"true";
422 $addLicenseInfoFromConcluded =
"false";
423 $addConcludedAsDecisions =
"true";
424 $addConcludedAsDecisionsTBD =
"true";
425 $addCopyrights =
"false";
426 if (array_key_exists(
"addNewLicensesAs", $reqBody) &&
427 $reqBody[
"addNewLicensesAs"] ===
"license") {
428 $addNewLicensesAs =
"license";
430 if (array_key_exists(
"addLicenseInfoFromInfoInFile", $reqBody) &&
431 !filter_var($reqBody[
"addLicenseInfoFromInfoInFile"],
432 FILTER_VALIDATE_BOOLEAN)) {
433 $addLicenseInfoFromInfoInFile =
"false";
435 if (array_key_exists(
"addLicenseInfoFromConcluded", $reqBody) &&
436 filter_var($reqBody[
"addLicenseInfoFromConcluded"],
437 FILTER_VALIDATE_BOOLEAN)) {
438 $addLicenseInfoFromConcluded =
"true";
440 if (array_key_exists(
"addConcludedAsDecisions", $reqBody) &&
441 !filter_var($reqBody[
"addConcludedAsDecisions"],
442 FILTER_VALIDATE_BOOLEAN)) {
443 $addConcludedAsDecisions =
"false";
445 if (array_key_exists(
"addConcludedAsDecisionsTBD", $reqBody) &&
446 !filter_var($reqBody[
"addConcludedAsDecisionsTBD"],
447 FILTER_VALIDATE_BOOLEAN)) {
448 $addConcludedAsDecisionsTBD =
"false";
450 if (array_key_exists(
"addCopyrights", $reqBody) &&
451 filter_var($reqBody[
"addCopyrights"],
452 FILTER_VALIDATE_BOOLEAN)) {
453 $addCopyrights =
"true";
457 $symfonyRequest->request->set(
'addNewLicensesAs', $addNewLicensesAs);
458 $symfonyRequest->request->set(
'addLicenseInfoFromInfoInFile',
459 $addLicenseInfoFromInfoInFile);
460 $symfonyRequest->request->set(
'addLicenseInfoFromConcluded',
461 $addLicenseInfoFromConcluded);
462 $symfonyRequest->request->set(
'addConcludedAsDecisions',
463 $addConcludedAsDecisions);
464 $symfonyRequest->request->set(
'addConcludedAsDecisionsTBD',
465 $addConcludedAsDecisionsTBD);
466 $symfonyRequest->request->set(
'addCopyrights', $addCopyrights);
468 $agentResp = $reportImport->runImport($upload_pk, $targetFile, $symfonyRequest);
469 $returnVal =
new Info(201, intval($agentResp[0]), InfoType::INFO);
470 return $response->withJson($returnVal->getArray(), $returnVal->getCode());
FOSSology Decision Exporter UI plugin.
Agent plugin for Readme_OSS agent.
Call SPDX3 agent to generate report from UI.
Controller for Maintenance model.
static buildDownloadPath($request, $jobId)
Base controller for REST calls.
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
Different type of infos provided by REST.
Info model to contain general error and return values.
Unified report generator UI plugin.
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.