Search code examples
authenticationservletsfilenet-p8filenet-content-enginefilenet-cpe

Access FileNet Content Engine from servlet


I am trying to access IBM FileNet P8 V5 datastore from a servlet and need help in getting the WebSphere configuration correct.

I have a fresh install of FN P8 on a RedHat 7 server with IBM WebSphere.

I tried the example code given on https://www.ibm.com/support/knowledgecenter/en/SSNW2F_5.0.0/com.ibm.p8.ce.dev.ce.doc/ss_ecl_cews.htm

It works fine from a plain Java class; connects to the server and retrieves the datastore name.

I copied the code over to a servlet but getting the error: " com.filenet.api.exception.EngineRuntimeException: FNRCS0005E: SECURITY_INVALID_CREDENTIALS: Access to the Content Engine was not allowed because the Content Engine API library or the Web Service Interface (WSI) Listener could not find the required security context information. Expected credentials were not found in the security context"

As per the tutorial, I need to set the runtime environment. That's where I'm stuck at. I tried adding jaas.conf.WebSphere in via the WebSphere console in the JVM settings but am obviously not doing it correctly.

package server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.ObjectStoreSet;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.util.UserContext;

/**
 * Servlet implementation class FNTest
 * Ref: https://www.ibm.com/support/knowledgecenter/en/SSNW2F_5.0.0/com.ibm.p8.ce.dev.ce.doc/ss_ecl_cews.htm
 */
//@WebServlet(description = "Test for connection to Filenet", urlPatterns = { "/FNTest" })
@WebServlet("/FNTest")
public class FNTest extends HttpServlet {
    private static final long serialVersionUID = 2L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FNTest() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Filenet Function Test</title></head>");
        out.println("<body>");
        out.println("<h1>Filenet Function Test</h1>");
        out.println("<p>Request server at " + request.getServletPath() + ".</p><hr>");
        out.println("<p>Connecting to Filenet...</p>");
        // Connect to Filenet
        // Set connection parameters; substitute for the placeholders.
        String uri = "http://filenet.myserver.com:9080/wsi/FNCEWS40MTOM/";
        String username = "P8Admin";
        String password = "P8Password";

        // Make connection.   
        Connection conn = Factory.Connection.getConnection(uri);
        Subject subject = UserContext.createSubject(conn, username, password, null);
        UserContext.get().pushSubject(subject);

        try
        {
           // Get default domain.
           Domain domain = Factory.Domain.fetchInstance(conn, null, null);
           System.out.println("Domain: " + domain.get_Name());

           // Get object stores for domain.
           ObjectStoreSet osSet = domain.get_ObjectStores();
           ObjectStore store;
           Iterator osIter = osSet.iterator();

           while (osIter.hasNext()) 
           {
              store = (ObjectStore) osIter.next();
              System.out.println("Object store: " + store.get_Name());
           }
           System.out.println("Connection to Content Engine successful");
        }
        finally
        {
           UserContext.get().popSubject();
        }

        out.println("<p>Completed</p>");
        out.println("</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

The application with the servlet is installed on the same server.


Solution

  • I see you have null for a jaas stanza, this means it will take FileNetP8 as a default stanza. In stead of null try FileNetP8WSI as you are using a WSI endpoint to connect to.

    Subject subject = UserContext.createSubject(conn, username, password, "FileNetP8WSI");
    

    If you supply null it will use "FileNetP8" this is the jaas stanza to use for an IIOP connection