Announcement Announcement Module
Collapse
No announcement yet.
Serving binaries from a controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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, 12:47 PM.

    Comment


    • #3
      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.

      Comment


      • #4
        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).

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment

            Working...
            X