Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liferay Document Library web service: how to simply read a file's content?

I also posted this question on the Liferay community board.

I'm trying to access Liferay's document library via web service. To do this I used Eclipse to create a web client from Liferay's Portlet_DL_DLAppService wsdl. It all went well and I think I can easily create a file remotely, but the generated Classes do not seem to have any functionality to read back a file's content.

DLAppService has methods to get FileEntries, but while the Interface FileEntry has the method getContentStream(), but the proxy class in the generated web client (FileEntrySoap) does not.

I feel like I'm missing something fundamentally, since I think I'm not the first one to use Liferay's document library via web service?

Any hint or pointer would be welcome.

like image 632
Ozan Avatar asked Nov 17 '12 01:11

Ozan


3 Answers

Another workaround would be to get file with http request.

Following example is using Apache commons-httpclient (v4) with basic auth. If guest has permission to view file than authentication part is not needed.

For basic auth you'll need com.liferay.portal.security.auth.BasicAuthHeaderAutoLogin as one of login hooks in auto.login.hooks property (in portal-ext.properties)

Example

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;

private static byte[] getFile(inal long groupId, final long folderId, final String title, final String hostname, final int port, final String username, final String password) throws ClientProtocolException, IOException {
    final DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        final String filename = URLEncoder.encode(title);

        final HttpHost targetHost = new HttpHost(hostname, port);
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);

        final AuthScope authscope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        httpclient.getCredentialsProvider().setCredentials(authscope, creds);

        final AuthCache authCache = new BasicAuthCache();
        final BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);

        final BasicHttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);

        final HttpGet httpget = new HttpGet("/documents/" + groupId + "/" + folderId + "/" + filename);


        final HttpResponse response = httpclient.execute(targetHost, httpget, localContext);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            final HttpEntity entity = response.getEntity();

            if (entity != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                entity.writeTo(baos);
                return baos.toByteArray();
            }
        }
        return null;
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

}

From FileEntry that you got from web service you can get groupId, folderId and title and call the method

like image 163
Martin Gamulin Avatar answered Oct 27 '22 00:10

Martin Gamulin


After digging through their website for quite a while - I finally came across both your posting in the forums, and then this, older one:

http://www.liferay.com/community/forums/-/message_boards/message/7943515

In general, it looks like the service just doesn't provide a whole lot of documentation at all, much less for this specific sub-set of features.

However - following these links led me to a few resources which might help:

Using Liferay Portal as a content management system and Getting Started with the Documents and Media

The two links are inside the User Guide, as you can see.

It looks like you can get the WebDAV URL for the document from the Documents and Media portal page - which is probably a reflection of the non-API way to do what Olaf suggests in his answer via API.

I'm curious whether either of the above answers helped you solve the problem, as neither are marked as correct - but I'm guessing not, due to the Bounty. I hope these resources help.

like image 31
Troy Alford Avatar answered Oct 27 '22 01:10

Troy Alford


Sorry, I can't provide the full solution right now, but if everything else fails, you could try to use a workaround through WebDAV: DLUtil.getWebDavURL()

InputStream is something that is not good to transport through SOAP, but if you just get a URL and then open the file through this means, you'll most likely be able to get what you need. This will also enable you to update the file.

There probably is a bunch of WebDAV implementations out there - depends on the environment (programming language) you need to access the doclib from.

like image 24
Olaf Kock Avatar answered Oct 27 '22 00:10

Olaf Kock