users@jersey.java.net

Jersey's use of ThreadLocals

From: jwheeler79 <jwheeler1_at_gmail.com>
Date: Tue, 28 Sep 2010 15:40:41 -0700 (PDT)

Hi,

I'm using a Google API called Blobstore that stores arbitrary objects. In
order to get things into blobstore, you call
blobService.createUploadUrl(callbackUri), take the string that's returned,
and put it into your multipart form's action attribute. I'd rather have a
restful URL and forward the request to the URL Google generated.

So I

    @POST
    @Path("{caseImageId}/attach")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public void attach(@Context ServletContext context,
            @Context ThreadLocal<HttpServletRequest> reqTL,
            @Context ThreadLocal<HttpServletResponse> resTL,
            @PathParam("caseFileId") Long caseFileId,
            @PathParam("caseImageId") Long caseImageId) {
        
        logger.info("attaching an image");

        try {
            HttpServletRequest req = reqTL.get();
            HttpServletResponse res = resTL.get();
            
            String requestUri = req.getRequestURI();

            String callbackUrl = requestUri + "_callback";
            String uploadUrl = blobService.createUploadUrl(callbackUrl);

            RequestDispatcher rd = context.getRequestDispatcher(uploadUrl);
            rd.forward(req, res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I stumbled across the ThreadLocal references from looking at the source code
to LoggingFilter. This works, but if I use straight HttpServletRequest and
HttpServletResponse (and not the ThreadLocal versions), I get an
IllegalStateException. Interestingly, if I use straight request and response
in conjunction with a LoggingFilter in web.xml, I don't get an
IllegalStateException, but an OutOfMemoryException. I am not too concerned
about that. I just want to understand why the threadlocals?

JOhn
-- 
View this message in context: http://jersey.576304.n2.nabble.com/Jersey-s-use-of-ThreadLocals-tp5581529p5581529.html
Sent from the Jersey mailing list archive at Nabble.com.