Search in sources :

Example 1 with TransformationNotFoundException

use of org.opentosca.toscana.api.exceptions.TransformationNotFoundException in project TOSCAna by StuPro-TOSCAna.

the class TransformationController method getTransformationArtifact.

/**
 *     Returns the URL to download the target Artifact (Wrapped in json)<p>
 *     <p>
 *     Accessed with http call <code>GET /csars/{csar}/transformations/{platform}/artifacts</code>
 *     <table summary="">
 *     <tr>
 *     <td>HTTP-Code</td>
 *     <td>Mime-Type</td>
 *     <td>Description (Returned if)</td>
 *     </tr>
 *     <tr>
 *     <td>200</td>
 *     <td>application/hal+json</td>
 *     <td>Returns a Json object containing the download url for the artifact</td>
 *     </tr>
 *     <tr>
 *     <td>400</td>
 *     <td>application/json</td>
 *     <td>Returned if the transformation is not in a valid state (has to be in DONE or ERROR) to set inputs</td>
 *     </tr>
 *     <tr>
 *     <td>404</td>
 *     <td>application/json</td>
 *     <td>Returns a error message if the csar is not found or if the csar does not have a transformation for the given
 *     name (see returned error message for details)</td>
 *     </tr>
 *     </table>
 */
@RequestMapping(path = "/{platform}/artifact", method = RequestMethod.GET, produces = "application/octet-stream")
@ApiOperation(value = "Download the target artifact archive", tags = { "transformations" }, notes = "Once the transformation is done (in the state DONE) or it has encountered a error (state ERROR). " + "It is possible to download a archive (ZIP format) of all the files generated while the transformation was " + "running.")
@ApiResponses({ @ApiResponse(code = 200, message = "The operation was executed successfully"), @ApiResponse(code = 400, message = "There is nothing to download yet because the execution of the transformation has not yet started " + "or is not finished (With or without errors)", response = RestErrorResponse.class), @ApiResponse(code = 404, message = "There is no CSAR for the given identifier or the CSAR does not have " + "a Transformation for the specified platform.", response = RestErrorResponse.class) })
public ResponseEntity<Void> getTransformationArtifact(@ApiParam(value = "The unique identifier for the CSAR", required = true, example = "test") @PathVariable(name = "csarId") String csarName, @ApiParam(value = "The identifier for the platform", required = true, example = "kubernetes") @PathVariable(name = "platform") String platform, HttpServletResponse response) throws IOException {
    Csar csar = csarService.getCsar(csarName).orElseThrow(CsarNotFoundException::new);
    Transformation transformation = csar.getTransformation(platform).orElseThrow(TransformationNotFoundException::new);
    TargetArtifact artifact = transformation.getTargetArtifact().orElseThrow(() -> new IllegalTransformationStateException(format("Artifact for csar '{}' and platform '{}' not found", csarName, platform)));
    response.setHeader("Content-Disposition", "attachment; filename=\"" + artifact.name + "\"");
    response.setHeader("Content-Type", "application/octet-stream");
    response.setHeader("Content-Length", artifact.getFileSize() + "");
    InputStream in = artifact.readAccess();
    OutputStream out = response.getOutputStream();
    IOUtils.copy(in, out);
    in.close();
    out.close();
    return ResponseEntity.ok().build();
}
Also used : TargetArtifact(org.opentosca.toscana.core.transformation.artifacts.TargetArtifact) IllegalTransformationStateException(org.opentosca.toscana.api.exceptions.IllegalTransformationStateException) Csar(org.opentosca.toscana.core.csar.Csar) Transformation(org.opentosca.toscana.core.transformation.Transformation) TransformationNotFoundException(org.opentosca.toscana.api.exceptions.TransformationNotFoundException) CsarNotFoundException(org.opentosca.toscana.api.exceptions.CsarNotFoundException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 CsarNotFoundException (org.opentosca.toscana.api.exceptions.CsarNotFoundException)1 IllegalTransformationStateException (org.opentosca.toscana.api.exceptions.IllegalTransformationStateException)1 TransformationNotFoundException (org.opentosca.toscana.api.exceptions.TransformationNotFoundException)1 Csar (org.opentosca.toscana.core.csar.Csar)1 Transformation (org.opentosca.toscana.core.transformation.Transformation)1 TargetArtifact (org.opentosca.toscana.core.transformation.artifacts.TargetArtifact)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1