cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to get 'WebApplicationContext' in custom servlet

sam4alf
Champ in-the-making
Champ in-the-making
All Java JCR enthusiasts out there,

I have a custom servlet from where I'm trying to access repository and a node. I need to get this node's content and send it to an applet running in the same context. I have user login ticket and node reference. Using those, I'm trying to get the node and it's content.

Here is my code. Somehow, I always get NullPointerException while getting request. It won't move anywhere from that line. What am I missing? Any clues are really appreciated. Thanks.
       
  
 
             System.out.println("ticketFromApplet given to content handler is: " +    ticketFromApplet);
             System.out.println("NodeRef given to content handler is: " +  nodeRef);
             Ticket ticket = new Ticket("http://<host>:8080/alfresco",  ticketFromApplet);
             StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE,  "SpacesStore");
             HttpServletRequest request = (HttpServletRequest) MessageContext.getCurrentContext()                                                                                     .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
            ServletContext sc = request.getSession().getServletContext();
            WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
            // get registry of services
            final ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
            SearchService searchService = serviceRegistry.getSearchService();
            ResultSet rs = searchService.query(storeRef,SerchService.LANGUAGE_LUCENE, "ID:\"" + nodeRef + "\"");
            ResultSetRow row = rs.getRow(0);
            NodeRef docRef = row.getNodeRef();
            ContentServiceImpl contentService = new ContentServiceImpl();
            contentService.readContentIntoStream(ticket, docRef, QName.createQName("cm:content"), outputStream);

-Sarma.
12 REPLIES 12

lesoft
Champ in-the-making
Champ in-the-making
Hi Sarma -

I have the exact POC requirements to integrate Alfresco Share with Snowbound VirtualViewer. Do you mind share with me the code?

Thanks in advance!
Tuan

sam4alf
Champ in-the-making
Champ in-the-making
Hello Tuan,

After I started that POC, I got some suggestions not to follow that way so I stopped that POC. Anyway, here is the code I have for flexsnap content handler. Once you get the content as byte array, pass it back to applet.


// now get the content and pass it on to Applet
            /*String ticketFromApplet = contentHandlerInput.getDocumentId();
            String nodeRef = contentHandlerInput.getClientInstanceId();

            System.out.println("ticketFromApplet given to content handler is: " +
                ticketFromApplet);
            System.out.println("NodeRef given to content handler is: " +
                nodeRef);

            System.out.println("Getting servlet context…");

            ServletContext objContext = getServletConfig().getServletContext();

            System.out.println("Creating noderef…");

            // NodeRef of the document
            NodeRef objRef = new NodeRef(nodeRef);

            System.out.println("Getting context…");

            // Get webapplication context
            WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(objContext);

            System.out.println("Getting Service registry…");

            ServiceRegistry serviceRegistry = (ServiceRegistry) wc.getBean(ServiceRegistry.SERVICE_REGISTRY);

            System.out.println("Getting Authentication service…");

            // Get authentication service
            AuthenticationService objService = serviceRegistry.getAuthenticationService();
            System.out.println(
                "Validating ticket using authentication service…");

            // MUST TO AVOID THE ERROR - a valid SecureContext was not provided in the RequestContext
            // example : req.getParameter("alf_ticket") = TICKET_2fa55c64371087b642fdf8107083456f81fd1e1f
            objService.validate(ticketFromApplet);

            System.out.println("Getting Content service…");

            ContentService contentService = serviceRegistry.getContentService();

            System.out.println("Getting Content reader for the given node…");

            // Get the content reader
            ContentReader contentReader = contentService.getReader(objRef,
                    ContentModel.PROP_CONTENT);
            System.out.println("Getting content as bytes…");

            InputStream in = contentReader.getContentInputStream();

            try {
                int nextChar;

                while ((nextChar = in.read()) != -1) {
                    outputStream.write((char) nextChar);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            byte[] binaryData = outputStream.toByteArray();

lesoft
Champ in-the-making
Champ in-the-making
Hi Sarma - Thanks very much for sharing the code.