RESTful produces binary file - java

I'm new using CXF and Spring to make RESTful webservices.
This is my problem: I want to create a service that produces "any" kind of file(can be image,document,txt or even pdf), and also a XML. So far I got this code:
#Path("/download/")
#GET
#Produces({"application/*"})
public CustomXML getFile() throws Exception;
I don't know exactly where to begin so please be patient.
EDIT:
Complete code of Bryant Luk(thanks!)
#Path("/download/")
#GET
public javax.ws.rs.core.Response getFile() throws Exception {
if (/* want the pdf file */) {
File file = new File("...");
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition", "attachment; filename =" + file.getName())
.build();
}
/* default to xml file */
return Response.ok(new FileInputStream("custom.xml")).type("application/xml").build();
}

If it will return any file, you might want to make your method more "generic" and return a javax.ws.rs.core.Response which you can set the Content-Type header programmatically:
#Path("/download/")
#GET
public javax.ws.rs.core.Response getFile() throws Exception {
if (/* want the pdf file */) {
return Response.ok(new File(/*...*/)).type("application/pdf").build();
}
/* default to xml file */
return Response.ok(new FileInputStream("custom.xml")).type("application/xml").build();
}

We also use CXF and Spring, and this is my preferable API.
import javax.ws.rs.core.Context;
#Path("/")
public interface ContentService
{
#GET
#Path("/download/")
#Produces(MediaType.WILDCARD)
InputStream getFile() throws Exception;
}
#Component
public class ContentServiceImpl implements ContentService
{
#Context
private MessageContext context;
#Override
public InputStream getFile() throws Exception
{
File f;
String contentType;
if (/* want the pdf file */) {
f = new File("...pdf");
contentType = MediaType.APPLICATION_PDF_VALUE;
} else { /* default to xml file */
f = new File("custom.xml");
contentType = MediaType.APPLICATION_XML_VALUE;
}
context.getHttpServletResponse().setContentType(contentType);
context.getHttpServletResponse().setHeader("Content-Disposition", "attachment; filename=" + f.getName());
return new FileInputStream(f);
}
}

Related

Problems downloading zip archive Spring boot

Good afternoon.
I can't get the browser to download the file from the server. I took the code from a previous project and it doesn't work. Please explain why.
On the server, files are collected in a zip archive. It is necessary to download the archive. I am using this:
My controller
#SneakyThrows
#GetMapping("/report/UploadDocuments")
#ResponseBody
public ResponseEntity<InputStreamResource> uploadDocuments(HttpServletResponse response,
#RequestParam("check") String check){
deleteAllFilesFolder(Directories.DYRECTORY_EXPORT);
ArrayList<Long> idDocuments = Converter.arrayStringInLong(check);
List<PaymentOrderArchive> documents = paymentOrderArchiveService.findAllById(idDocuments);
for (PaymentOrderArchive p : documents){
UploadingFiles.dowloadFileInDirectory(p);
}
//"method" create zip-file and return path
String s = method(documents);
//Problem here
UploadingFiles up = new UploadingFiles();
return up.downloadFile1(servletContext);
}
My method for dowload
public ResponseEntity<InputStreamResource> downloadFile1(ServletContext servletContext) throws IOException {
MediaType mediaType = MediaTypeUtils.getMediaTypeForFileName(servletContext, Directories.NAME_ZIP);
File file = new File(Directories.DYRECTORY_EXPORT + Directories.NAME_ZIP);
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok()
// Content-Disposition
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + Directories.NAME_ZIP)
// Content-Type
.contentType(mediaType)
// Contet-Length
.body(resource);
}
public class MediaTypeUtils {
public static MediaType getMediaTypeForFileName(ServletContext servletContext, String
fileName) {
String mineType = servletContext.getMimeType(fileName);
try {
MediaType mediaType = MediaType.parseMediaType(mineType);
return mediaType;
} catch (Exception e) {
return MediaType.APPLICATION_OCTET_STREAM;
}
}
Everything is working. The program does not give errors. But the file is not downloading. And I can't understand why.

Spring Boot Rest Response MultipartFile with additional fields

We have a RestController with the below endpoint
#PostMapping(path = "/downloadFile", produces = MediaType.MULTIPART_FORM_DATA_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
public FileDownloadResponse downloadFile(#RequestBody FileDownloadRequest request) {
FileDownloadResponse downloadResponse = new FileDownloadResponse();
File file = new File("c:/fileLocation/"+request.getFileName());
try (InputStream stream = new FileInputStream(file)) {
byte[] bytes = IOUtil.toByteArray(stream);
downloadResponse.setFileName(file.getName());
downloadResponse.setCheckSum(calculateCheckSum(bytes));
downloadResponse.setFileContents(new FileSystemResource(bytes, file.getName()));
} catch (Exception e) {
e.printStackTrace();
}
return downloadResponse;
}
public class FileDownloadResponse {
private String fileName;
private Long checkSum;
private Resource fileContents;
}
public static class FileSystemResource extends ByteArrayResource {
private String fileName;
public FileSystemResource(byte[] byteArray , String filename) {
super(byteArray);
this.fileName = filename;
}
public String getFilename() { return fileName; }
public void setFilename(String fileName) { this.fileName= fileName; }
}
And on the Client Side we have the below code,
public class FileDownloadResponseClient {
private String fileName;
private Long checkSum;
private MultipartFile fileContents;
}
public FileDownloadResponseClient download(FileDownloadRequest request) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(Mediatype.ALL));
HttpEntity<FileDownloadRequest> requestEntity = new HttpEntity<>(request, headers);
return restTemplate.postForEntity(downloadUrl, requestEntity, FileDownloadResponseClient.class);
}
When we run the Rest Client above, we are getting the below error,
org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : [no body]
Is it possible to download a multipartfile along with other additional fields? If yes, what is that we are missing here, please let us know.
Thanks in Advance!
org.springframework.web.multipart has a method boolean isEmpty() to find if the file has no content. Best put that check there and redirect to a message about such a file multipart form.
Of [no body] i have found that message on test requests to http server but in entirety generally means there is nothing in the form or no extra information needed for the server to complete the request.
For now i presume the spring framework handles all the url decoding and boundary marker stripping (on both ends) of uploaded files.

How to return ModelandView when metod returning ResponseEntiry<Resource> throws an error?

I have the following method
#GetMapping("/{fileName}")
public Object downloadFile(#PathVariable String fileName) {
// Load file from database
errors.clear();
DBFile dbFile;
try {
dbFile = dBFileStorageService.getFileByName(fileName);
} catch (MyFileNotFoundException ex) {
logger.info("File has not been found.");
errors.add(ex.getMessage());
return new ModelAndView("redirect:/");
}
logger.info("Delivering file");
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
Instead of returning Object I would like to return ResponseEntity<Resource> if it possible to return the file or ModelAndView("redirect:/") otherwise.
I tried:
HttpHeaders headers = new HttpHeaders();
headers.add("Location", "/member/uploadImage");
return new ResponseEntity<>(headers,HttpStatus.FOUND);
But instead of redirection I got message that the file I am trying to download is corrupted.
Summing up I would like to change method signature to:
public ResponseEntiry<Resource> downloadFile(#PathVariable String fileName)
About your first question on returning the file through ResponseEntity is already answered here :
Return file from Spring #Controller having OutputStream
Agreeing wwith #chrylis, as he suggested the best approch for you to take in case of an exception is to throw the exception and handle it in sprind #ControllerAdvice class using #ExceptionHandler method annotation.
#ControllerAdvice
class GlobalControllerExceptionHandler {
#ResponseStatus(HttpStatus.CONFLICT) // 409 or according to your need any code
#ExceptionHandler(Exception.class)
protected ModelAndView unhandledExceptionHandler(Exception ex){
System.out.println("handling exception here!!!");
ModelAndView mv = new ModelAndView();
mv.setViewName("errorView");
mv.addObject("ERROR", "ERROR OCCURRED REDIRECTED "+ex.getMessage());
return mv;
}
}
Official Doc.

Jersey InputStream is modified in filter. Unable to figure out how to access modified inputStream in Jersey Resource

As discussed in How to use Jersey interceptors to get request body, I am modifying the EntityInputStream in a ContainerRequestFilter.
public filter(ContainerRequest request){
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = request.getEntityInputStream();
try{
Readerwriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
// DO SOMETHING WITH BYTES HERE
request.setEntityInputStream(new ByteArrayInputStream(requestEntity));
}/// error handling code here
}
However, later on I can't figure out how to access the modified InputStream. I can get the ServletContext in the resource, but I can't figure out how to get ahold of the object I actually modified in the filter, the ContainerRequest.
Can I do something like this? Jersey can't start up out when I try this:
#Post
#Path("/test")
public Response test(#Context ContainerRequest cr){
// blah blah
return....
}
Jersey error:
Missing dependecy for method public javax.ws.rs.core.Response example.TestController.test(com.sun.jersey.spi.container.ContainerRequest), annotated with POST of resource, class example.TestController, is not recognized as a valid resource method.
I am stuck on an old version of jersey, 1.8, so I'm not sure if that's part of the problem.
All you need to do is accept an InputStream as the entity body in your resource method. If you want the ByteArrayInputStream just cast it.
#POST
public Response post(InputStream in) {
ByteArrayInputStream bin = (ByteArrayInputStream)in;
}
If you don't already know, how Jersey converts the request stream (for the request body) into Java types (for instance JSON to POJO) is through MessageBodyReaders. You can read more about them at JAX-RS Entity Providers.
Jersey already comes with some standard readers for easily convertible types, for instance String. Most content-types can be converted to String. Likewise, it has a reader to handle InputStream. This is probably the easiest conversion, as the request is already coming in as an InputStream, so really all the reader would need to do is return the original stream, and that's what would get passed to our method.
If we look at the implementation InputStreamProvider, we can see that that's what actually happens. The original stream is simply returned. And since the filter happens before the readers, the reader simply returns the stream that we set.
Here is a complete example using Jersey Test Framework
public class StreamFilterTest extends JerseyTest {
public static class InputStreamFilter implements ContainerRequestFilter {
#Override
public ContainerRequest filter(ContainerRequest request) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = request.getEntityInputStream();
ReaderWriter.writeTo(in, out);
byte[] requestBytes = out.toByteArray();
byte[] worldBytes = " World".getBytes(StandardCharsets.UTF_8);
byte[] newBytes = new byte[requestBytes.length + worldBytes.length];
System.arraycopy(requestBytes, 0, newBytes, 0, requestBytes.length);
System.arraycopy(worldBytes, 0, newBytes, requestBytes.length, worldBytes.length);
request.setEntityInputStream(new ByteArrayInputStream(newBytes));
} catch (IOException ex) {
Logger.getLogger(InputStreamFilter.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
return request;
}
}
#Path("stream")
public static class StreamResource {
#POST
public String post(InputStream in) throws Exception {
ByteArrayInputStream bin = (ByteArrayInputStream) in;
StringWriter writer = new StringWriter();
ReaderWriter.writeTo(new InputStreamReader(bin), writer);
return writer.toString();
}
}
public static class AppConfig extends DefaultResourceConfig {
public AppConfig() {
super(StreamResource.class);
getContainerRequestFilters().add(new InputStreamFilter());
}
}
#Override
public WebAppDescriptor configure() {
return new WebAppDescriptor.Builder()
.initParam(WebComponent.RESOURCE_CONFIG_CLASS,
AppConfig.class.getName())
.build();
}
#Test
public void should_return_hello_world() {
String response = resource().path("stream").post(String.class, "Hello");
assertEquals("Hello World", response);
}
}
Here's the test dependency
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<version>1.17.1</version>
<scope>test</scope>
</dependency>

send a .apk file from REST web service to client?

I want send my android .apk file to my client(browser) from java restful web service.I try to use bellow code. But it produce a file named "MyPath" without any file extension (require .apk).Thanks in advance
#Path("MyPath")
public class MyPathResource {
#Context
private UriInfo context;
/**
* Creates a new instance of MyPathResource
*/
public MyPathResource() {
}
#GET
#Produces("application/vnd.android.package-archive")
public File getFile() {
// return my file
return new File("E:\\CommandLineAndroidProjet1\\bin\\FirstCommandLineApp-release.apk");
}
}
Try creating an explicit javax.ws.core.Response:
return Response.status(Response.Status.OK)
.entity(entity)
.header("Content-Disposition", "attachment; filename=" + fileName)
.build();
while entity is your file as byte array and fileName is the file name with .apk suffix.

Categories

Resources