cancel
Showing results for 
Search instead for 
Did you mean: 

[urgent]where to close the session after downloading

unknown-user
Champ on-the-rise
Champ on-the-rise
Hi

Can anybuddy tell me where to close the alfresco session after downloading a content from alfresco server.

Regards
Nishant
7 REPLIES 7

openpj
Elite Collaborator
Elite Collaborator
Can you provide your source code about Alfresco web service client?

unknown-user
Champ on-the-rise
Champ on-the-rise
Hi

Below is the code which i am using for downloading the content from the alfresco server.
I am starting the session in webservices (server side) and I have to close the session after downloading the content.

If I close the session in jsp after downloading then I am not able to download the file.
I think it requires sometime to download the content.
But how can I guess how much time it requires.

Please just give me the solution where I need to close the session.
Because I am not in the situation to edit the whole code.
Any suggestion regarding this is highly appreciated.


Content[] content = WebServiceFactory.getContentService().read(new Predicate(new Reference[]{fileRef}, STORE, null)  ,  "{http://www.alfresco.org/model/content/1.0}content" );
url = content[0].getUrl();

String strTicket = org.alfresco.webservice.util.AuthenticationUtils.getTicket();
url = url +"?ticket="+ strTicket;

then I am opening this url with the help of java script (window.open(url)).

Can I close the session after some time (1 or 2 minute) programaticaly?

Kindly help me.

Regards
Nishant

openpj
Elite Collaborator
Elite Collaborator
Why don't you copy this content and set it in the response to user agent?
Are you using any frameworks dedicated to MVC?
I think that you can implement this feature in a better way without using Javascript.

Below a snippet to create a file reading a content from Alfresco:

byte[] contentFile = null;
WebServiceFactory.setEndpointAddress(ALFRESCO_URL);
AuthenticationService authenticationService = WebServiceFactory.getAuthenticationService();
AuthenticationUtils.startSession(USERNAME, PASSWORD);

String ticket = WebServiceFactory.getCurrentTicket();

Reference ref = node.getReference();
Content[] readResult = contentService.read(new Predicate(new Reference[] { ref }, STORE, null), Constants.PROP_CONTENT);
Content content = readResult[0];

InputStream is = getContentAsInputStream(content.getUrl(), helper);
strUrl += "?ticket=" + ticket;
URL url = new URL(strUrl);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bufferSize = is.available();
byte[] dati = new byte[bufferSize];

int readbyte = 0;
while ((readbyte = is.read(dati, 0, bufferSize)) > -1) {
   baos.write(dati, 0, readbyte);
}

baos.close();
is.close();
contentFile = baos.toByteArray();
AuthenticationUtils.endSession();
Hope this helps.

sisao
Champ in-the-making
Champ in-the-making
Can I close the session after some time (1 or 2 minute) programaticaly?
I'd rather leave the session ticket alive instead!

Sessions tickets are overwritten when generated within the same credentials contest, this means that as long as the ticket string is not null you wont be prompted an alfresco login when trying to download contents. So your real issue there is to be sure that your links will properly work using that "?ticket=" parameter in your urls, you should check that the session ticket is not null BEFORE building up that url….and this is NOT guaranteed by the fact that you start the session somewhere in your flow as they expire.
Saying that, you can safely close your session when the application is logged out or whatever, or even let it expire imo.

Regards.

unknown-user
Champ on-the-rise
Champ on-the-rise
Hi Openpj and sisao

Thanks for the response.

Openpj :
              I am using the same code upto Content object. Can you pls tell me what is helper in this line–

              InputStream is = getContentAsInputStream(content.getUrl(),helper );

              In my application, user uploads the files (pdf, doc, jpg etc) in alfresco server and after uploading
              I am enabling one link to view the upload file. For that, I am using window.open(url) method using
              java script. Can you pls tell me where i need to close the session after viewing the file.

sisao :   
             If I leave the session object, then it'll  affect the concurrency.
             I am using 4GB RAM and 2x linux server CPU.
             My JVM PremSize configuration is 1024m because this is the recommanded configuration for 50 concurrent
             users (for more info pls check this url  http://wiki.alfresco.com/wiki/Repository_Hardware). I can extend it
             upto 2 GB.


For the time being, I configured the session for 1 minute in web.xml file. In my case, hardly 50 users will do upload and download(view) simultaneously. But if i configure web.xml then it affect both the operations (upload and download). Users are allowed to upload 2 MB file at a time. 1 minute is enough to upload and download(view) the file.

In upload case, I start the session, upload the file in alfresco server and close the session.
Same way I want to use for download(view) also.
Kindly help me.

Regards
Nishant

sisao
Champ in-the-making
Champ in-the-making
I usually use getContentAsInputStream(content) from org.alfresco.webservcie.util.ContentUtils.*

About concurrency, ofc you have to guarantee sessions closing somewhere but i dont see a real big issue about concurrent connections.
That's in general a resources limit you cannot work around of.
I dont know how your app is made so i cant help on where or when to call an endSession method really….i do it when a user is properly logged out of the whole application usually…and i open a ticket at login time.
I dont really think configuring the expire time is a proper solution but it may works for now while you search for better ways…for what i've understood there is NOT an application where this users are logged in…are you maybe populating some jsp or html page with alfresco nodes, with urls etc.?

unknown-user
Champ on-the-rise
Champ on-the-rise
i do it when a user is properly logged out of the whole application usually…and i open a ticket at login time.

Hi sisao,

Actualy I don't want to manage the alfresco session with my application session. Whenever user want to upload and view the file, he just start the session and do the operations (upload & view) and close the session. In upload case, I am able to close the session but same thing is not working in download (view) case.

Download steps :

                    In a jsp page,
                                         1) I start the session
                                         2) get the ticket
                                         3) get the Url to content exists in alfresco repository
                                         4) append the ticket with url
                                         5) using window.open(url)
                                          Upto 5 steps it's working fine
                                         6) Close session
                                          If I close the session, I am not able to download the content

Regards
Nishant