I am creating an rest api in Oracle Jdeveloper , to download a pdf file available in a pericular location in one of our server (https://************).
********Moderator action: removed private data ******
I am not getting any error. After debugging found that all the lines are added in the logs.
I am getting response code as 500Internal Server Error.
Code is :
@POST
@Path("/downlaodReportPdf")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM) // Return as binary stream (for files)
public Response downlaodReportPdf(JsonObject params) throws Exception {
// Extract data from the JsonObject
String jndiName = params.getString("jndiName");
String v5username = params.getString("v5username");
String v5userPassword = params.getString("v5userPassword");
String path = params.getString("location");
String loggingStatus = params.getString("loggingStatus");
logger(jndiName, loggingStatus, DEV_LOG, "location is : " + path);
logger(jndiName, loggingStatus, DEV_LOG, "downlaodReportPdf called");
///Certificate validation
Response validationResponse = validateUser(jndiName, v5username, v5userPassword, loggingStatus);
if (validationResponse != null) {
return validationResponse; // Return the error response if validation failed
}
/// Download impl start
File file = new File(path);
if (!file.exists()) {
logger(jndiName, loggingStatus, DEV_LOG, "Error: file not found");
return Response.status(Response.Status.NOT_FOUND)
.entity("File not found")
.build();
}
logger(jndiName, loggingStatus, DEV_LOG, "1");
// Create an input stream from the file
try (InputStream fileStream = new FileInputStream(file)) {
logger(jndiName, loggingStatus, DEV_LOG, "2");
// Build the response with the file stream and set appropriate headers
ResponseBuilder response = Response.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM);
response.header("Content-Disposition", "attachment; filename=" + file.getName());
logger(jndiName, loggingStatus, DEV_LOG, "downlaodReportPdf completed");
return response.build();
} catch (Exception e) {
// Log the error and return an internal server error response
logger(jndiName, loggingStatus, DEV_LOG, "Error while processing file download: " + e.getMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Error processing file download")
.build();
}
}
logger method logs data in the db, this is for logging functionality.
In the logs below logs were added without any error:
location is : something
downlaodReportPdf called
1
2
downlaodReportPdf completed
. Can you please guide me why is it happening. What workaround can be done to fix the issue so that pdf file gets downloaded.