Reputation: 33
HELP! Ive tried everything I can and nothing seems to work. My issue is as follows:
I've got a Spring REST service, that makes a REST call, receives a base64 encoded PDF. I then decode the PDF, and display it in the browser. I can get this part to work perfectly! BUT! I am having issues handling 404s, 500s, ect. ERROR messages.
Unfortunately, Error messages are served back to me in the API as a message in a hashmap, as you can see by my logic below.
Ive tried creating PDFs, but I cant convert that into a File Object, I've tried to Stream the bytes directly to the browser, but it isnt working for me, no matter what I do.
Is there a way to change the Response Headers based on Logic? When successful, return a pdf, and when error return a text file to the browser?
Im completely exhausted here and am not sure where to go. Any advice or pointers would be extremely appreciated.
Ive also tried throwing a 404 to the browser, but its expecting a pdf file, and I get MIME errors.
@Controller
@Path("/mon")
public class WSController {
@Autowired
Service service;
@GET
@Produces("application/pdf")
@Path("/ws")
public File getWs(@QueryParam("vin") String vin) throws IOException, URISyntaxException {
Map response = service.getWs(vin);
if (response == null) {
return createErrorFile("Server Error, please try again later.");
} else if (response.get("ErrorCode") != null && response.get("ErrorCode").equals("E")) {
return createErrorFile((String) response.get("ErrorDescription"));
} else {
return decodeBase64(((String) response.get("Base64String")).replace("\n",""));
}
}
private File decodeBase64(String encodedFile) throws IOException {
byte[] decodedBytes = DatatypeConverter.parseBase64Binary(encodedFile);
File file = new File("wS.pdf");
FileOutputStream fop = new FileOutputStream(file);
fop.write(decodedBytes);
fop.flush();
fop.close();
return file;
}
private File createErrorFile(String errorDescription) throws IOException {
//Not sure what to do here
return null;
}
EDIT: Here is what ended up working for me. Thank you so much for the advice. This has been a wonderful learning experience.
@GET
@Path("/ws")
public Response getWS(@QueryParam("vin") String vin) throws IOException, URISyntaxException {
Map results = Service.getWS(vin);
if (results == null) {
Response.ResponseBuilder rBuild = Response.status(Response.Status.BAD_REQUEST);
return rBuild.type(MediaType.TEXT_PLAIN)
.entity("Server error, please try again later.")
.build();
} else if (results.get("ErrorCode") != null && results.get("ErrorCode").equals("E")) {
Response.ResponseBuilder rBuild = Response.status(Response.Status.BAD_REQUEST);
return rBuild.type(MediaType.TEXT_PLAIN)
.entity(results.get("ErrorDescription"))
.build();
} else {
File responseData = decodeBase64(((String) results.get("Base64String")).replace("\n", ""));
Response.ResponseBuilder rBuild = Response.ok(responseData, "application/pdf");
return rBuild.build();
}
}
Upvotes: 2
Views: 3862
Reputation: 208984
I see a few problems you are mentioning in your post, but I only read these actual questions
Is there a way to change the Response Headers based on Logic? When successful, return a pdf, and when error return a text file to the browser?
So I will address them. I myself, I prefer to always return a Response
. You can set all your headers there, as well as much more stuff also. For example
@GET
public Response getSomething() {
...
Response.ResponseBuilder builder = Response.status(200);
Response response = builder.entity(file)
.header("Content-Type", "someType")
.build();
return response;
}
entity(...)
is what you normally return as the entity body.
Response.ResponseBuilder
. Most of the static methods in Response
return a Response.ResponseBuilder
, and that is where so have many options to build the request.As far as exceptions that aren't handle in your methods, you can right an ExceptionMapper(s)
to handle different exceptions. See here. You can return a Response
from the mapper.
You can write custom WebApplicationException
(s), so as not to clutter your code with response building for exception. And just throw that exception. JAX-RS already has a mechanism build in to handle the exception. You can create the exception with a built in Response
. The link has an exception. There a lot of different options for working with exceptions.
Upvotes: 1
Reputation: 107
Don't panic :-) You can achieve complete control over the response by asking spring for a HttpServletResponse object. The you can set the mime type of the response or set error codes:
@GET
@Produces("application/pdf")
@Path("/ws")
public void getWs(@QueryParam("vin") String vin, HttpServletRequest request) {
...
...
request.setContentType("text/html");
...
Upvotes: 1