2 Replies Latest reply on May 14, 2009 11:55 AM by David Beaumont

    Problem getting component from AbstractResource

    David Beaumont Newbie

      Hello,


      I am trying to implement an abstract resource to serve files retrieved from a database with GET requests.



      @Scope(ScopeType.APPLICATION)
      @Name("fileDownloader")
      @BypassInterceptors
      @Install(precedence=Install.BUILT_IN)
      public class FileDownloader extends AbstractResource {
      
           @Override
           public void getResource(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                
                EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");
                UserHome userHome = (UserHome) Contexts.getSessionContext().get("userHome"); // FIXME
                
                String name = request.getParameter("name");
                Query q = entityManager.createNamedQuery("news.file.name.byrole").setParameter("name", name);
                if(userHome != null && userHome.getUser() != null) {
                     q.setParameter("roles", userHome.getUser().getAppRoles());
                } else {
                     q.setParameter("roles", null);
                }
                try {
                     Document doc = (Document) q.getSingleResult();
                     response.setContentType(doc.getContentType());
                     response.setHeader("Content-Disposition", "attachment;filename=\"" + doc.getName() + "\"");     
                     response.getOutputStream().write(doc.getFile().getData());
                } catch (NoResultException e) {
                     response.sendError(HttpServletResponse.SC_NOT_FOUND);
                } finally {
                     response.getOutputStream().flush();
                     response.getOutputStream().close();
                }
           }
      
           @Override
           public String getResourcePath() {
                return "/newsAttachment";
           }
      }
      



      entityManager is always found, but userHome never is. In conversation-scoped SFSBs, I can inject userHome with @In, no problems.


      I have tried with both Component.getInstance (injects something, but not the correct component - userHome.getUser() is null) and Contexts.getSessionContext.get() (just returns null, I believe).


      Why can this component not be found?

      Thanks,

      Dave

        • 1. Re: Problem getting component from AbstractResource
          Ben Catherall Newbie

          You need to wrap your resource code in a ContextualHttpServletRequest


          @Scope(ScopeType.APPLICATION)
          @Name("fileDownloader")
          @BypassInterceptors
          @Install(precedence=Install.BUILT_IN)
          public class FileDownloader extends AbstractResource {
          
               @Override
               public void getResource(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          
                          new ContextualHttpServletRequest(request){
          
                                 @Override
                                 public void process(){
                                       // logic goes here!
                                 }
          
                          }.run();
                    
               }
          
               @Override
               public String getResourcePath() {
                    return "/newsAttachment";
               }
          }