现在的位置: 首页 > 综合 > 正文

Soap call webservice provided by Liferay

2013年05月16日 ⁄ 综合 ⁄ 共 5029字 ⁄ 字号 评论关闭

参考文章:

 http://support.liferay.com/browse/LEP-1319

 

或者在http://support.liferay.com/secure/QuickSearch.jspa 去搜索 soap 关键字

 

5.0.1 的版本我怀疑存在bug,直接在浏览器中输入这样的URL:http://localhost:8080/tunnel-web/axis/Portal_ClassNameService 都会报错,懒得理他了

 

5.1 的版本正常

 

从5.X 的版本开始,提供了三种方式,也就是UserID(4.x 好像只有这一种,可以从com.liferay.portal.servlet.filters.secureSecureFilter.java 中看的出来),Email和ScreenName ,为了进行这几种区分,提供了后缀的方式进行判断,例如如果是userID ,则需要带上 @uid ,例如 2◎uid; 如果是screenname ;则需要带上 @sn,例如joebloggs@sn。默认的方式就是通过Email ,因此就无需后缀了。

 

密码可以传递明文,也可以传递加密后的,这个在portal.properties 中的portal.jaas.strict.password=false 可以进行控制

测试代码(5.1)

 

package test;

 

import java.net.URL;
import org.apache.axis.client.Stub;
import com.liferay.client.portal.model.ClassNameSoap;
import com.liferay.client.portal.model.UserSoap;
import com.liferay.client.portal.service.http.ClassNameServiceSoap;
import com.liferay.client.portal.service.http.ClassNameServiceSoapServiceLocator;
import com.liferay.client.portal.service.http.UserServiceSoap;
import com.liferay.client.portal.service.http.UserServiceSoapServiceLocator;

/**
 * 使用Liferay 提供的Soap服务的例子
 * @author rockyzheng
 */

public class SoapTest {

 /**
  * 测试不需要认证的,也就是不需要用户登录的Service
  * 例如Classname
  */
 private void testUnAuthedSoap(){
  
  try{
   ClassNameServiceSoapServiceLocator locator = new ClassNameServiceSoapServiceLocator();
   ClassNameServiceSoap soap = locator.getPortal_ClassNameService(getURL("Portal_ClassNameService", false));
   ClassNameSoap nameSoap = soap.getClassName(10001);
   System.out.println(nameSoap.getValue());   
  }catch (Exception e) {   
   e.printStackTrace();
  }  
 } 
 
 /**
  * 测试需要认证的,也就是需要登录的Service
  * 例如用户服务
  */
 private void testAuthedSoap() {
  try{
   UserServiceSoapServiceLocator locator = new UserServiceSoapServiceLocator();
   UserServiceSoap soap = locator.getPortal_UserService(getURL("Portal_UserService", true));
   if (soap instanceof Stub){
    Stub stub = (Stub)soap;
    //stub.setUsername("2@uid"); //UID
    //stub.setUsername("test@liferay.com");//Email
    stub.setUsername("joebloggs@sn"); //ScreenName    
    stub.setPassword("test");
   }
   UserSoap userSoap = soap.getUserById(2);
   System.out.println(userSoap.getGreeting());
  }catch (Exception e) {
   e.printStackTrace();
   
  }
 }
 
 private URL getURL(String serviceName,boolean authenticatedUrl) throws Exception{
  String url;
  if (authenticatedUrl){   
   url = "http://@192.168.1.12:8080/tunnel-web/secure/axis/" + serviceName;
  }else{
   url = "http://192.168.1.12:8080/tunnel-web/axis/" + serviceName;
  } 
  return  new URL(url);
  
 }
 
 
 public static void main(String[] args) {
  SoapTest test = new SoapTest();
 
  test.testUnAuthedSoap();
  test.testAuthedSoap();
 }
}

 

运行环境中需要的lib 则下载对应版本的liferay-portal-client ,将下面所需要的包都丢进去,例如 liferay-portal-client-5.1.0。

 

4.4.1 的版本是存在Bug 的,参考http://support.liferay.com/browse/LEP-4480 ,需要将util-java.jar  移动到tomcat/common/lib/ext 下面去,然后代码中只需要改动的地方是 :setuid(2),其他没有变化。

4.4.1中不移动会抛出的错误:

 

AxisFault
 faultCode: {http://xml.apache.org/axis/}HTTP
 faultSubcode:
 faultString: (403)Access to the requested resource has been denied
 faultActor:
 faultNode:
 faultDetail:
 {}:return code:  403
<html><head><title>Apache Tomcat/5.5.25 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 403 - Access to the requested resource has been denied</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Access to the requested resource has been denied</u></p><p><b>description</b> <u>Access to the specified resource (Access to the requested resource has been denied) has been forbidden.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.5.25</h3></body></html>
 {http://xml.apache.org/axis/}HttpErrorCode:403

(403)Access to the requested resource has been denied
 at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:744)
 at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144)
 at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
 at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
 at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
 at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
 at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
 at org.apache.axis.client.Call.invoke(Call.java:2767)
 at org.apache.axis.client.Call.invoke(Call.java:2443)
 at org.apache.axis.client.Call.invoke(Call.java:2366)
 at org.apache.axis.client.Call.invoke(Call.java:1812)

 

 

嘿嘿,折磨了几天了,终于搞好了,开源的这个东西啊,咋说来?

 

 

 

 

抱歉!评论已关闭.