Liferay文档库Web服务:如何简单地读取文件的内容?

Oza*_*zan 5 wsdl web-services liferay

我还在Liferay社区委员会上发布了这个问题.

我正在尝试通过Web服务访问Liferay的文档库.为此,我使用Eclipse从Liferay的Portlet_DL_DLAppService wsdl创建了一个Web客户端.一切顺利,我认为我可以轻松地远程创建文件,但生成的类似乎没有任何功能来回读文件的内容.

DLAppService具有获取FileEntries的方法,但是Interface FileEntry具有方法getContentStream(),但生成的Web客户端(FileEntrySoap)中的代理类不具有.

我觉得我从根本上缺少一些东西,因为我认为我不是第一个通过Web服务使用Liferay的文档库的人?

任何提示或指针都是受欢迎的.

Mar*_*lin 5

另一种解决方法是使用http请求获取文件.

以下示例使用Apache commons-httpclient(v4)和基本身份验证.如果guest有权查看文件,则不需要身份验证部分.

对于基本身份验证,您需要 com.liferay.portal.security.auth.BasicAuthHeaderAutoLogin作为auto.login.hooks属性中的登录挂钩之一(在portal-ext.properties中)

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

从您从Web服务获得的FileEntry,您可以获取groupId,folderId和title并调用该方法