users@jersey.java.net

[Jersey] Need Help with CORS

From: bharat joshi <bharatjoshi1_at_gmail.com>
Date: Tue, 24 Jul 2012 10:41:09 +0530

Hi All,

I am using jersey with jetty in an equinox container. I have a task where i
need to do Cross Origin Request. From a javascript i need to upload a file
at a different server which is jetty again. So in the resource file I have


@OPTIONS
    @Path("upload")
    public Response
corsMyResource(@HeaderParam("Access-Control-Request-Headers") String
requestH) {
          return makeCORS(Response.ok().build(), requestH);
    }


private Response makeCORS(Response req, String returnMethod) {
        _corsHeaders = returnMethod;
           ResponseBuilder rb =
javax.ws.rs.core.Response.ok().header("Access-Control-Allow-Origin",
"*").header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");

           if (!"".equals(returnMethod)) {
              rb.header("Access-Control-Allow-Headers", returnMethod);
           }
           System.out.println("*****************************");
           return rb.build();
        }


And then I have this function to handle the original post request
@POST
    @Path("upload")
    public Response upload(@Context HttpServletRequest request) throws
IOException{
        System.out.println("Till here its fine");
        Response resp=null;
        InputStream uploadedStream = null;
        List items = null;
        ServletFileUpload uploader = new ServletFileUpload(
                new DiskFileItemFactory());
        if (ServletFileUpload.isMultipartContent(request)) {
       try {
                items = uploader.parseRequest(request);
                System.out.println("SIZE is "+items.size());

The problem is I am getting empty request object. "Till here its fine" gets
printed but the file is not there in request object. On
parseRequest(request), items size is zero. Its not that it is problem with
POI, because the same code in a servlet works fine. And also on firebug, i
get POST request cancelled. Any tutorial on CORS or any help is
appreciated. Thank You.

-- 
Regards,
Bharat Joshi
Download VEDICS @ http://vedics.sourceforge.net/