Search code examples
web-servicesalfrescocmisopencmisalfresco-webscripts

Close session CMIS


I'm trying to access a web service that communicates with Alfresco through CMIS. I'm accessing this link: http://localhost:8080/Changes/ChangesPDF?filePath=/Documentos/examplepdf.pdf&ticket=TICKET_1dd4951f5d97c72232db40bdc8dceeb7be70aaed

When I start Alfresco, I can access the web service without problems, but when I shutdown Alfresco - it gives me unauthorized in the webservice, and even if I made login, continues to give me unauthorized.

Code to get session:

 public Session getSession(
            String connectionName, String token) {
        Session session = connections.get(connectionName);
        if (session == null) {
            logger.info("Not connected, creating new connection to" +
                    " Alfresco with the connection id (" + connectionName +
                    ")");
// No connection to Alfresco available, create a new one
            SessionFactory sessionFactory =
                    SessionFactoryImpl.newInstance();
            Map<String, String> parameters = new HashMap<>();
            parameters.put(SessionParameter.USER, "");
            parameters.put(SessionParameter.PASSWORD, token);
            parameters.put(SessionParameter.ATOMPUB_URL,
                    "http://localhost:8080/alfresco/api/-default-/cmis/versions/1.1/atom");
            parameters.put(SessionParameter.BINDING_TYPE,
                    BindingType.ATOMPUB.value());
            parameters.put(SessionParameter.COMPRESSION, "true");
            parameters.put(SessionParameter.CACHE_TTL_OBJECTS, "0");
// If there is only one repository exposed (e.g. Alfresco),
// these lines will help detect it and its ID
            List<Repository> repositories =
                    sessionFactory.getRepositories(parameters);
            Repository alfrescoRepository = null;
            if (repositories != null && repositories.size() > 0) {
                logger.info("Found (" + repositories.size() +
                        ") Alfresco repositories");
                alfrescoRepository = repositories.get(0);
                logger.info("Info about the first Alfresco repo [ID=" +
                        alfrescoRepository.getId() + "][name=" +
                        alfrescoRepository.getName() + "][CMIS ver supported=" +
                        alfrescoRepository.getCmisVersionSupported() + "]");
            } else {
                throw new CmisConnectionException(
                        "Could not connect to the Alfresco Server, " +
                                "no repository found!");
            }
// Create a new session with the Alfresco repository
            session = alfrescoRepository.createSession();
// Save connection for reuse
            connections.put(connectionName, session);
        } else {
            logger.info("Already connected to Alfresco with the " +
                    "connection id (" + connectionName + ")");
        }
        return session;
    }

Error below:

Request processing failed; nested exception is org.apache.chemistry.opencmis.commons.exceptions .CmisUnauthorizedException: Unauthorized

And enter here:

"Already connected to Alfresco with the " +
                    "connection id (" + connectionName + ")"7

How can I close the older connection with Alfresco?


Solution

  • you just need to close the session when the ticket change. You can do this with: session.clear(); session=null;