cancel
Showing results for 
Search instead for 
Did you mean: 

Alfresco community edition 4.0.d- Java web services Session Handling

mrpathi
Champ in-the-making
Champ in-the-making

Hi,

We are using alfresco community edition 4.0.d. Using java web services we use to upload documents and download documents.  We have photo album functionality which requests photo url from alfresco using asynchronous call. 

Every asynchronous call afresco creates new session.  For an example if we have 10 photos we made 10 calls to alfresco to get the content url to load. For all 10 calls alfresco creates 10 new sessions which causes java heap space exceptions. Before creating session we just check whether old ticket available or not then we proceed to create new session. 

AuthenticationUtils.getTicket() method will always returns null for every request. How can we handle session in alfresco to use existing open sessions. I have attached code snippet how we are doing.  

Is there any guidelines to get it done. Awaiting for your response.

public String getCurrentDocumentDownloadURL(String accountId, String projectId, String documentId){

		// Start alfresco session
		this.startAlfrescoSession();

		String ticket = AuthenticationUtils.getTicket();

		// check whether document Exists Or not
		Node node = this.getContentNodeFromRepository(documentId);
		if (node == null) {
			throw new ConnectorException(new IllegalArgumentException("Cannot download the document. The document "
					+ documentId + " does not exist in project " + projectId));
		}

		Reference reference = new Reference(this.getStore(), documentId, null);
		Predicate predicate = new Predicate(new Reference[] { reference }, this.getStore(), null);
		ContentServiceSoapBindingStub contentService = this.getContentService();
		Content contentRef = null;
		try {
			Content[] readResult = contentService.read(predicate, Constants.PROP_CONTENT);
			contentRef = readResult[0];
		} catch (ContentFault e) {
			LOG.error("Cannot get the document: " + documentId + " url in account: " + accountId + " project: "
					+ projectId + " because : " + e.getMessage());
			LOG.error(LoggingUtils.getStackTraceAsString(e));
			throw new ConnectorException("Cannot get the document: " + documentId + " url in account: " + accountId
					+ " project: " + projectId + " because: " + e.getLocalizedMessage(), e);
		} catch (RemoteException e) {
			LOG.error("Cannot get the document: " + documentId + " url in account: " + accountId + " project: "
					+ projectId);
			LOG.error(LoggingUtils.getStackTraceAsString(e));
			throw new ConnectorException("Cannot get the document: " + documentId + " url in account: " + accountId
					+ " project: " + projectId + " because: " + e.getLocalizedMessage(), e);
		}

		
		String downloadURL = null;
		if (contentRef != null) {
			String ticketURL = AlfrescoConstants.TICKET_URL + ticket;
			String url = null;
			if (doc != null && doc.getName() != null) {
				url = this.constructDownloadFileName(contentRef.getUrl(), sample.jpg, null);
			} else {
				url = contentRef.getUrl();
			}
			downloadURL = url + ticketURL;
		}

		if (downloadURL == null) {
			// Close the Alfresco Session
			this.closeAlfrescoSession();
		}
		return downloadURL;
	}
	
	
	private void startAlfrescoSession() throws ConnectorException {
		try {
			AuthenticationDetails details = AuthenticationUtils.getAuthenticationDetails();
			if (AuthenticationUtils.getTicket() == null) {
				WebServiceFactory.setEndpointAddress(TrackerConfiguration.getInstance().getDmsEndPointURL());
				AuthenticationResult result = 
						WebServiceFactory.getAuthenticationService().startSession(
								TrackerConfiguration.getInstance().getDmsLoginId(), 
								TrackerConfiguration.getInstance().getDmsLoginPwd()); 
				details = 
						new AuthenticationDetails(result.getUsername(), result.getTicket(), result.getSessionid());
				AuthenticationUtils.setAuthenticationDetails(details);
				
				String msg = "Started Alfresco Session: "
						+ "[" + result.getSessionid() + " & " + result.getTicket()+"]";
				
				System.out.println(msg);
				LOG.debug(msg);
			}
		} catch (AuthenticationFault e) {
			LOG.error("Cannot start Alfresco Session. Because: " + e.getCause());
			LOG.error(LoggingUtils.getStackTraceAsString(e));
			throw new ConnectorException("Cannot start Alfresco Session. Because: " + e.getCause(), e);
		} catch (Exception e) {
			LOG.error("Cannot establish alfresco session. Because: " + e.getCause());
			LOG.error(LoggingUtils.getStackTraceAsString(e));
			throw new ConnectorException("Cannot establish alfresco session. Because: " + e.getCause(), e);
		}
	}
	
	
	private void closeAlfrescoSession() {
		AuthenticationDetails details = AuthenticationUtils.getAuthenticationDetails();
		if (details != null && details.getTicket() != null) {
			AuthenticationUtils.endSession();
			String msg = "Closed Alfresco Session: [" + details.getSessionId() + " & " + details.getTicket()+"]";
			LOG.debug(msg);
			System.out.println(msg);
		}
	}
1 REPLY 1

kaynezhang
World-Class Innovator
World-Class Innovator

 I recommend you to upgrade your alfresco to a newer version and use  restful api.