Home > Blockchain >  Java return Inputstream via rest API with html file in inputstream
Java return Inputstream via rest API with html file in inputstream

Time:12-13

I am currently trying to return an input stream via my API. The input stream contains an html file that I previously fetch from Jenkins via the Cdancy Jenkinsclient via the input stream. I want to pass this html through my endpoint. If I enter Json as @Produce, then the HTML content comes with the note that the JSON can not be parsed. If I specify another MediyType, then a 406 comes back. Is it even bestpractise to return an inputstream or should I transform it into an outputstream first?

This is my Code:

Endpoint

@GET
@Path(API_RESOURCE_IMAGE_REPORT)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Operation(summary = "", description = "")
@APIResponses(
        value = {
                @APIResponse(
                        responseCode = "200",
                        description =
                                "",
                        content = @Content(mediaType = MediaType.APPLICATION_JSON)),
                @APIResponse(
                        responseCode = "400",
                        description = "",
                        content =
                        @Content(
                                mediaType = MediaType.APPLICATION_JSON,
                                schema = @Schema(implementation = ErrorResponseDO.class))),
        })
public Response getReport(@Parameter(
        description = "",
        required = true)
                          @PathParam("imageName") final String imageName,
                          @Parameter(description = "", required = true)
                          @PathParam("tag") final String tag,
                          @Parameter(description = "")
                          @PathParam("type") String type
) throws ApplicationException, IOException {

    InputStream report = jenkinsClient.getReport(imageName, tag, type);

    return Response.status(HttpURLConnection.HTTP_ACCEPTED).entity(report).build();
}

Jenkinsclient:

    public InputStream getReport(final String imageName, final String tag, final String type) throws ApplicationException {

        try {
            final int lastSuccessfulBuildnumber = jenkinsClient.api().jobsApi().jobInfo(imageName, tag).lastSuccessfulBuild().number();
            LOG.info("Last successful buildnumber: "   lastSuccessfulBuildnumber);

            final InputStream report = jenkinsClient.api().jobsApi().artifact(imageName, tag, lastSuccessfulBuildnumber, Objects.equals(type, "image") ? "trivy_image_report.html" : "trivy_Dockerfile_report.html");
            

            if (report == null) {
                throw new NotFoundException();
            }

            return report;

        } catch (Exception e) {
            throw new NotFoundException();

        }
    }

Output: Output is 406 everytime (TEXT_HTML, OCTET_STREAM, TEXT_PLAINE). Only with @Produces(MediaType.APPLICATION_JSON) it is successfull with the html code bud with the message: json cant be parsed.

Thanks for your help

CodePudding user response:

Like VGR stated. Problem was the caller which was not using text/html. Ive tested in swaggerui and set it to "text/html". Works as expected. Was application/json beforen and the reason for working only with application json as produce annoation.

enter image description here

  • Related