|
35 | 35 | import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.EXP_UUID; |
36 | 36 | import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.FILTER_QUERY_PARAMETERS; |
37 | 37 | import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.FILTER_QUERY_PARAMETERS_EX; |
| 38 | +import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.RPT; |
38 | 39 | import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.INDEX; |
39 | 40 | import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.INDEX_EX; |
40 | 41 | import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.INVALID_PARAMETERS; |
|
74 | 75 | import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.VTB; |
75 | 76 | import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.X_Y; |
76 | 77 |
|
| 78 | +import java.io.File; |
| 79 | +import java.io.IOException; |
| 80 | +import java.nio.file.Files; |
77 | 81 | import java.util.ArrayList; |
78 | 82 | import java.util.Collection; |
79 | 83 | import java.util.Collections; |
@@ -1101,6 +1105,83 @@ private Response getTree(UUID expUUID, String outputId, QueryParameters queryPar |
1101 | 1105 | } |
1102 | 1106 | } |
1103 | 1107 |
|
| 1108 | + /** |
| 1109 | + * Query the provider for a report. Based on the given output, this |
| 1110 | + * endpoint will return a specific report, which can be an image, a |
| 1111 | + * textual data, a HTML, etc. |
| 1112 | + * |
| 1113 | + * @param expUUID |
| 1114 | + * desired experiment UUID |
| 1115 | + * @param outputId |
| 1116 | + * Output ID for the data provider to query |
| 1117 | + * @return {@link Response} with the corresponding report data |
| 1118 | + */ |
| 1119 | + @GET |
| 1120 | + @Path("/report/{outputId}") |
| 1121 | + @Tag(name = RPT) |
| 1122 | + @Produces({ MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON }) |
| 1123 | + @Operation(summary = "API to get an image", responses = { |
| 1124 | + @ApiResponse(responseCode = "200", description = "Returns the image file", content = @Content(mediaType = "application/octet-stream")), |
| 1125 | + @ApiResponse(responseCode = "400", description = MISSING_PARAMETERS, content = @Content(schema = @Schema(implementation = String.class))), |
| 1126 | + @ApiResponse(responseCode = "404", description = PROVIDER_NOT_FOUND, content = @Content(schema = @Schema(implementation = String.class))), |
| 1127 | + @ApiResponse(responseCode = "500", description = "Error reading the image file", content = @Content(schema = @Schema(implementation = String.class))) |
| 1128 | + }) |
| 1129 | + public Response getReport( |
| 1130 | + @Parameter(description = EXP_UUID) @PathParam("expUUID") UUID expUUID, |
| 1131 | + @Parameter(description = OUTPUT_ID) @PathParam("outputId") String outputId) { |
| 1132 | + |
| 1133 | + if (outputId == null) { |
| 1134 | + return Response.status(Status.BAD_REQUEST).entity(MISSING_OUTPUTID).build(); |
| 1135 | + } |
| 1136 | + |
| 1137 | + TmfExperiment experiment = ExperimentManagerService.getExperimentByUUID(expUUID); |
| 1138 | + if (experiment == null) { |
| 1139 | + return Response.status(Status.NOT_FOUND).entity(NO_SUCH_TRACE).build(); |
| 1140 | + } |
| 1141 | + |
| 1142 | + IDataProviderDescriptor descriptor = getDescriptor(experiment, outputId); |
| 1143 | + if (descriptor == null || descriptor.getType() != ProviderType.REPORT) { |
| 1144 | + return Response.status(Status.NOT_FOUND).entity(NO_SUCH_PROVIDER).build(); |
| 1145 | + } |
| 1146 | + |
| 1147 | + ProviderType reportType = descriptor.getSubType(); |
| 1148 | + if (reportType == null) { |
| 1149 | + return Response.status(Status.NOT_FOUND).entity(NO_SUCH_PROVIDER).build(); |
| 1150 | + } |
| 1151 | + |
| 1152 | + ITmfConfiguration config = descriptor.getConfiguration(); |
| 1153 | + if (config == null) { |
| 1154 | + return Response.status(Status.NOT_FOUND).entity("No configuration found for this provider").build(); //$NON-NLS-1$ |
| 1155 | + } |
| 1156 | + |
| 1157 | + switch (reportType) { |
| 1158 | + case IMAGE: |
| 1159 | + String imagePath = (String) config.getParameters().get("path"); //$NON-NLS-1$ |
| 1160 | + if (imagePath == null) { |
| 1161 | + return Response.status(Status.NOT_FOUND).entity("Image path not found in configuration").build(); //$NON-NLS-1$ |
| 1162 | + } |
| 1163 | + |
| 1164 | + File imageFile = new File(imagePath); |
| 1165 | + if (!imageFile.exists() || !imageFile.isFile()) { |
| 1166 | + return Response.status(Status.NOT_FOUND).entity("Image file not found").build(); //$NON-NLS-1$ |
| 1167 | + } |
| 1168 | + |
| 1169 | + try { |
| 1170 | + String contentType = Files.probeContentType(imageFile.toPath()); |
| 1171 | + if (contentType == null) { |
| 1172 | + contentType = MediaType.APPLICATION_OCTET_STREAM; |
| 1173 | + } |
| 1174 | + |
| 1175 | + return Response.ok(imageFile, contentType).build(); |
| 1176 | + } catch (IOException e) { |
| 1177 | + return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); |
| 1178 | + } |
| 1179 | + //$CASES-OMITTED$ |
| 1180 | + default: |
| 1181 | + return Response.status(Status.NOT_FOUND).entity("Unsupported report type").build(); //$NON-NLS-1$ |
| 1182 | + } |
| 1183 | + } |
| 1184 | + |
1104 | 1185 | /** |
1105 | 1186 | * Query the provider for styles |
1106 | 1187 | * |
|
0 commit comments