Results 1 to 6 of 6

Thread: Serving binaries from a controller

  1. #1

    Default Serving binaries from a controller

    I am trying to output jpegs from a database to the browser, but I don't know what to return from the handleRequest.
    I've set the contentType and filled up the response, but when I return null I get a NPE.

    Is this the right method for serving binaries? If so what am I supposed to return.


    Code:
    public class GetImageController implements Controller
      {
      
      private ClipCache clipcache ;
      
      /** Logger for this class and subclasses */
      protected final Log logger = LogFactory.getLog (getClass ()) ;
    
      public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response)
          throws Exception
        {
        String clipIdStr = StringUtils.clean (request.getParameter ("cid")) ;
        if (clipIdStr == null)
          return null ;
        String zoneIdStr = StringUtils.clean (request.getParameter ("zid")) ;
        if (zoneIdStr == null)
          return null ;
        
        int zid = StringUtils.toInt ("zoneid", zoneIdStr) ;
        int cid = StringUtils.toInt ("clipid", clipIdStr) ;
    
        logger.info ("Requested clip is zoneid=" + zid + " clipid=" + cid) ;
    
        Clip clip = clipcache.get (zid, cid) ;
        ClipThumbnail ct = clip.getClipThumbnail() ;
        
        response.setContentType (ct.getMimeType ()) ;
        OutputStream os = response.getOutputStream () ;
        InputStream fin = ct.getContent () ;
        
        byte[] buf = new byte[4096] ;
        int count = 0 ;
        while (true)
          {
          int n = fin.read (buf) ;
          if (n == -1)
            {
            break ;
            }
          count = count + n ;
          os.write (buf, 0, n) ;
          }
        os.flush () ;
        os.close () ;
        fin.close () ;
        
        return null ;
        }
      
      }

  2. #2
    Join Date
    Jul 2008
    Posts
    239

    Default

    but when I return null ...
    What do you mean by this ?

    If you mean this:
    Code:
    if (zoneIdStr == null)
          return null ;
    Then u have coded the following semantics:

    "If the parameters supplied are null, which is a logical error/invalid argument, then report to the Controller client that you are having processed the request manually, so that it (the client - in this case the controller servlet) not to bother resolving the views"

    In any case what do you mean by "when", like in "when I return null" ?
    Can u post some error messages ?
    Last edited by sandstorm; Aug 18th, 2008 at 12:47 PM.

  3. #3

    Default

    Thanks sandstorm,
    If the parameters supplied are null, which is a logical error/invalid argument, then report to the Controller client that you are having processed the request manually, so that it (the client - in this case the controller servlet) not to bother resolving the views
    really helped a lot.

    As it turned out my thing was blowing up because there was a bad injection.

  4. #4
    Join Date
    Jul 2008
    Posts
    239

    Default

    No problems ;]
    I was thinking that too...
    The problem I saw was not what u spoke of, so the problem lies elsewhere- the injection (possibly).

  5. #5

    Default

    If I want to say that the document they are looking for is not available at the moment (404 or similar) how can i do that? This error message would be different to an invalid parameter message.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,626

    Default

    Code:
    response.sendError(HttpServletResponse.SC_NOT_FOUND, "Some message indicating that something was missing");
    The above should do that. You could ommit the message and use the sendError with the error code only.

    Or you can throw some specific Exception and configure an HandlerExceptionResolver.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •