9

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();
}
Marco Aviles
  • 5,516
  • 6
  • 33
  • 47
  • Try beginning by explaining what your problem is. So far, you've only described what you've done, but you haven't mentioned what happens when the code runs, what errors you've encountered, etc. – Brian Kelly Oct 04 '11 at 00:56
  • Are you trying to make the framework call your `getFile()` for every request under `/download`, so it can produce the requested file? I *think* what you're asking, in that case, is how the implementation of `getFile()` can find out what was actually requested. – Wyzard Oct 04 '11 at 01:03
  • @Wyzard yes, I hope is not much asking for implementation and annotation type – Marco Aviles Oct 04 '11 at 01:07

2 Answers2

15

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();
}
Bryant Luk
  • 2,448
  • 21
  • 22
0

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);
    }
}
t7tran
  • 1,899
  • 1
  • 18
  • 16