0

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

  • That's because you are returning an html and not JSON. Change the `return` to return a JSON representation if `report` – Leonardo Emmanuel de Azevedo Dec 07 '22 at 13:42
  • Yes but if i set text/html as response it returns also 406 – Karlito Brigante Dec 07 '22 at 13:54
  • 2
    HTTP 406 means [Not Acceptable](https://www.rfc-editor.org/rfc/rfc2616#section-10.4.7), which means your REST service is returning a type which is not one of the types the caller expects. The problem is on the calling end: the caller is not including text/html in their [Accept](https://www.rfc-editor.org/rfc/rfc2616#section-14.1) request header. – VGR Dec 07 '22 at 14:01

1 Answers1

0

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