Вы находитесь на странице: 1из 7

Web Services in Liferay Liferay Knowledge Share

Liferay Knowledge Share

Java J2EE Liferay

Web Services in Liferay


March 23, 2010 tags: j2ee, java, Liferay, web service

i 11 Votes What is web service Web service is nothing but exposes your software as service for other application over the network. Applications can be wri en in any programming language and if they support the statandard they can be integrated.It removes the concern of writing custom interfaces and REST/HTTP Post based calls for each integration point. How they can be used , examples As of now we have not used this feature in our platform. but there are several ways this can be used. 1. Data Migration. 2. External third party software integration Applications like Salesforge Exposing an entity as a SOAP Webservice in Liferay In this example we will create a table in Database and expose a method to update this table via web service Create a table CREATE TABLE [dbo].[MyBook]( [bookid] [bigint] NOT NULL, [bookname] [varchar](500) NULL, CONSTRAINT [PK_MyBook] PRIMARY KEY CLUSTERED ( [bookid] ASC ) Generate Service Layer code for table 1) Create service.xml place it in ext-impl\src\com\ext\portlet\mybook\service.xml <?xml version="1.0"?>

<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN" "http://www.liferay.com/dtd/liferay-s <service-builder package-path="com.ext.portlet.mybook"> <namespace>MyBook</namespace> <entity name="MyBook" local-service="true" remote-service="true"> <column name="bookid" type="long" primary="true" /> <column name="bookname" type="String" /> </entity> <exceptions> <exception>BookEntryName</exception> </exceptions> </service-builder>

http://arvindm.com/2010/03/23/web-services-in-liferay/

Web Services in Liferay Liferay Knowledge Share

2) Go to ext-impl\build-parent.xml and add ant task to build this service. <target name="build-service-portlet-mybook"> <antcall target="build-service"> <param name="service.file" value="src/com/ext/portlet/mybook/service.xml" /> </antcall> </target> run the newly created ant task build-service-portlet-mybook. It will generate service layer code for you. Now we will write a method to which will add a new book entry to the table.Open the generated le MyBookServiceImpl.java. if your entity name is MyEntity in service.xml the generated class name will MyEntityServiceImpl.java public MyBook addBook(String bookName) throws PortalException, SystemException { MyBook mybook = null; long bookId = CounterLocalServiceUtil.increment(MyBook.class.getName()); mybook = MyBookUtil.create(bookId); mybook.setBookname(bookName); return MyBookUtil.update(mybook, false); } Once this is done run the newly created ant task build-service-portlet-mybook again. It will propogate this method rest of the interfaces and classes.Generate web service code for this method Go to ext-impl\build-parent.xml and add a new ant task to generate the web service. <target name="build-wssd-portlet-mybook"> <antcall target="build-wsdd"> <param name="service.file" value="src/com/ext/portlet/mybook/service.xml" /> </antcall> </target> run the newly created ant task build-wssd-portlet-mybook. It will generate web service layer code for you. Later on for easier convenience , you can add following task in ext-impl\build-parent.xml to build service and wsdd at same time. <target name="build-mybook-service-wssd"> <antcall target="build-service-portlet-mybook" /> <antcall target="build-wssd-portlet-mybook" /> </target>

Consuming Liferay Web services


Enabling web services The portal uses Apache Axis to generate web services. The default Axis conguration is specied in the server-cong.wsdd le under the /portal/tunnel-web/ docroot/WEB-INF folder. type h p://127.0.0.1:8080/ tunnel-web/axis in your browser, you will see a list of SOAP services. To access a service remotely, the host must be allowed via the portal-ext. properties properties le. After that, the user must have permission to access the portal resources. The default se ings to access a service remotely are specied in the portal.properties le as follows: axis.servlet.hosts.allowed=127.0.0.1,SERVER_IP axis.servlet.https.required=false The code above shows the IPs to access the Axis servlet. You can input a blank list to allow any IP to access this servlet. SERVER_IP will be replaced with the IP of the host server. By default, 127.0.0.1 is the IP for local host. This is the reason that you can access web services only by the IP 127.0.0.1 Example to access services 1. Service can be browsed at h p://127.0.0.1:8080/tunnel-web/axis 2. To access the wsdl , click on the wsdl for any of the service.This is the example for User Service wsdl h p://127.0.0.1:8080 /tunnel-web/axis/Portal_UserService?wsdl 3. From WSDL you can generate stub and other classes using eclipse or netbeans. 4. Following is the sample client code to call a user service service

http://arvindm.com/2010/03/23/web-services-in-liferay/

Web Services in Liferay Liferay Knowledge Share

public class LiferayUserServiceClient { public static void main(String[] args) { try { UserServiceSoapServiceLocator locatorUser = new UserServiceSoapServiceLocator(); UserServiceSoap soapUser = locatorUser.getPortal_UserService("http://abc:abc@127.0.0.1:8080 UserSoap soapUserModel = soapUser.getUserById(10806); System.out.println(" getEmailAddress:" + soapUserModel.getEmailAddress()); } catch (Exception e) { e.printStackTrace(); } } } from Liferay 29 Comments leave one 1. Vidya Padigel permalink April 3, 2010 10:41 pm Great content Mishra,,,, U alwys rock Reply 2. Ankita Modi permalink May 20, 2010 3:13 am Hi Arvind, It was really great to learn from your article. I too am stucked with one issue wherein I want to access all the friends of a user alongwith their image data. All this needs to exposed as a web service.Since the user object does not include any image info, I planned to create a new model which includes picking the user rst name. last name, user id and image data. Tried creating a similar seriveSeriailzer classes also and included implementation of my methid in the WallEntryServiceImpl le. However after building the service and wsdd, I could no where see the method ge ing exposed. May be I am not using the right approach for my case where I do not want to create a new entity altogether but use the existing columns present in two of the liferay tables and then expose the service. Do I need to make some entry in service.xml for this case too? If yes. what they should be ? Please suggest ASAP. Thanks. Can also check one of the posts of mine : h p://www.liferay.com/community/forums/-/message_boards/message/4984907 Reply Arvind Mishra permalink* May 20, 2010 9:47 am What do you mean by image data , is it prole image? Prole Image or Portrait is saved in Image Table as bytes. The portraitID in User_ table is mapped to imageId in Image Table , thats how you can the image for each user. You dont need to write any new code to fetch the data. Thanks Arvind Reply 3. Edgar Norris permalink May 29, 2010 12:31 am You have done it again! Superb article. Reply 4. Franc permalink August 12, 2010 8:11 pm Hi Arvind, your articel is great but how to call addBook web service that you created before? Reply Arvind Mishra permalink* August 13, 2010 12:09 am The example in post is for user service. You can replace it with bookservice. Reply Franc permalink

http://arvindm.com/2010/03/23/web-services-in-liferay/

Web Services in Liferay Liferay Knowledge Share

August 13, 2010 1:57 am so, where can I get MyBookServiceSoapServiceLocator and MyBookServiceSoap ? I can not nd any of these les. Thanks for replying. 5. Franc permalink August 13, 2010 3:34 am Never mind about my question before. I have already solved the problem. Thank you for post. It is really helpfull. Reply chaitu1211 permalink April 17, 2012 5:58 am Hi Franc, Service Builder is not generating MyBookServiceSoapServiceLocator please help me. Reply 6. jason permalink September 15, 2010 4:29 am I have a question. Which one these methods are web method?.How can i assign methods like webmethod ? Reply 7. Vishal Marne permalink May 9, 2011 5:46 am HI Team, Right now i am trying to read data from message board of life ray using web service and incorporate that in .Net.would you please tell me the name of Webservice and how i can implement it in my code. Thank You Regards Vishal Marne Reply 8. Lana permalink May 27, 2011 4:19 pm Hi, How can I create a user with address and phone via soapUser.addUser(, List addresses, List phones, ) I mean how to construct addresses List? Thank you! Reply Arvind Mishra permalink* June 17, 2011 12:47 am Address, Phones are dierent entities. You need to call the respective web service for this entities. Reply 9. David Garca permalink August 16, 2011 8:33 am Great post, but you could also post it in the liferay.com forums to put a link to this blog. Reply Arvind Mishra permalink* August 16, 2011 9:14 am Link is posted for few replies in the forumns. Let me know any specic thread you want me to place it. i will be happy to do that. In Liferay.com i post as h p://www.liferay.com/web/arvind/prole Thank Reply David Garca permalink August 17, 2011 3:29 am You could reference this post from this WIKI entry. They are related to each other: h p://www.liferay.com/es/community/wiki/-/wiki/Main/Creating+Liferay+6+plugin+web+service 10. Rajeev permalink September 28, 2011 7:18 am Hi Arvind, I tried using the above class LiferayUserServiceClient to call the service.

http://arvindm.com/2010/03/23/web-services-in-liferay/

Web Services in Liferay Liferay Knowledge Share

I am ge ing a error :AxisFault faultCode: {h p://schemas.xmlsoap.org/soap/envelope/}Server.userException faultSubcode: faultString: java.rmi.RemoteException: PermissionChecker not initialized How do I resolve this? Using Soap UI, I modify my endpoint like h p://{server}:8080/tunnel-web/secure/axis/Portal_UserService and pass the username/passwordand it works. but programmatically how do I do it..how to resolve PermissionChecker not initialized error? I am using 6.0.5 Reply Arvind Mishra permalink* September 28, 2011 12:44 pm Hi Rajeev I just tested the WebService client in Liferay 6 and i am able to get the user. My Portal is setup to login using screen name. here is working client code, It li le bit dierent from the Blog Post. import java.net.URL; import com.liferay.portal.model.UserSoap; import com.liferay.portal.service.h p.UserServiceSoap; import com.liferay.portal.service.h p.UserServiceSoapServiceLocator; public class LiferayUserServiceClient { public static void main(String[] args) { try { UserServiceSoapServiceLocator locatorUser = new UserServiceSoapServiceLocator(); URL url = new URL(h p://arvind.mishra:youngsoft@127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService); UserServiceSoap soapUser = locatorUser.getPortal_UserService(url); UserSoap soapUserModel = soapUser.getUserById(16369); System.out.println( getEmailAddress: + soapUserModel.getEmailAddress()); } catch (Exception e) { e.printStackTrace(); } } } Reply 11. Raghu permalink December 13, 2011 6:17 am Hi Arvind, I have created webservice client for blog in liferay6 as URL below h p://localhost:8080/tunnel-web/axis/Portlet_Blogs_BlogsEntryService?wsdl I am ge ing an exception permission checker in not intalized. Is Anything Going Wrong In the URL??? Reply 12. sunil permalink February 23, 2012 8:01 am Hi Arvind, Can u please tell me what is the means of abc in this url h p://abc:abc@127.0.0.1:8080/tunnel-web/secure /axis/Portal_UserService. Thanks Reply Arvind Mishra permalink* April 2, 2012 4:14 pm abc:abc is the username:password to connect to portal. Reply 13. sunil permalink February 27, 2012 5:15 am

http://arvindm.com/2010/03/23/web-services-in-liferay/

Web Services in Liferay Liferay Knowledge Share

Hi Arvind, I am trying to add a user in liferay by using this code Portal_UserServiceSoapBindingStub stub = (Portal_UserServiceSoapBindingStub)locator.getPortal_UserService(new URL(h p://10130:test@192.168.2.189:18080/tunnel-web/secure/axis/Portal_UserService); stub.addUser( companyId, //companyId, false, //autoPassword, rstName, //password1, rstName, //password2, false, //autoScreenName, screenName, //screenName, usersModel.getStrEmailAddress(), //emailAddress, usersModel.getStrLocale()+, //locale, rstName, //rstName, , //middleName, lastName, //lastName, 0, //prexId, 0, //suxId, true, //male, 1, //birthdayMonth, 1, //birthdayDay, 1970, //birthdayYear, default, //jobTitle, organizationIds, //organizationIds, false //sendEmail ); } Bt i m ge ing this error. [CongurationException] Exception: org.apache.axis.CongurationException: No service named Portal_UserService is available org.apache.axis.CongurationException: No service named Portal_UserService is available at org.apache.axis.conguration.FileProvider.getService(FileProvider.java:233) at org.apache.axis.AxisEngine.getService(AxisEngine.java:311) at org.apache.axis.MessageContext.setTargetService(MessageContext.java:755) at org.apache.axis.client.Call.invoke(Call.java:2671) at org.apache.axis.client.Call.invoke(Call.java:2424) at org.apache.axis.client.Call.invoke(Call.java:2347) at org.apache.axis.client.Call.invoke(Call.java:1804) at com.liferay.client.portal.service.h p.Portal_UserServiceSoapBindingStub.addUser(Portal_UserServiceSoapBindingStub.java:807) at com.zycus.admin.controlleragent.TmsUserAgent.handleRenderRequest(TmsUserAgent.java:123) can u pls get me out from this problem . Thanks in advance. Reply Arvind Mishra permalink* April 2, 2012 4:12 pm Which version of liferay are you using ? Liferay 6 and earlier , webservices can be listed at -> h p://localhost:8080/tunnel-web/axis Liferay 6.1, webservices can be listed at -> h p://localhost:8080/api/axis Please check your portal.properties to see the IP from which Client is making is call is allowed to do so on server side. the property which needs to be set is axis.servlet.hosts.allowed -Thanks Arvind Reply 14. chaitu1211 permalink April 17, 2012 5:56 am HI Arvind, My service builder is not generating ServiceLocator class. Olease help me. Reply

http://arvindm.com/2010/03/23/web-services-in-liferay/

Web Services in Liferay Liferay Knowledge Share

15. shakeelstha permalink May 1, 2012 4:13 am Hi Arvind, I am using Liferay Portal Community Edition 6.1.0 CE. While consuming web service I get the following exception. java.rmi.RemoteException: Runtime exception; nested exception is: deserialization error: deserialization error: java.lang.NumberFormatException: For input string: at com.sun.xml.rpc.client.StreamingSender._handleRuntimeExceptionInSend(StreamingSender.java:331) at com.sun.xml.rpc.client.StreamingSender._send(StreamingSender.java:313) My Code : try { Stub stub = (Stub) (new CustomerServiceSoapService_Impl().getPlugin_customer_CustomerService()); stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, h p://localhost:8080/Customer-portlet/api/axis/Plugin_customer_CustomerService?wsdl); CustomerServiceSoap_Stub customerServiceSoapService = (CustomerServiceSoap_Stub) stub; CustomerSoap custList[] = customerServiceSoapService.getListByScreenName(shakeelstha); System.out.println(Customer List Size :: + custList.length); } catch (Exception ex) { ex.printStackTrace(); } Reply Arvind Mishra permalink* May 28, 2012 10:49 pm Can you post your exception trace ? Do you have @ symbol in the password you are using to connect to service. Reply

Trackbacks
1. RE: Cmo generar Web Services a partir de servicios del Service Builder Lif - Foros - Liferay.com 2. Web Services In Liferay - Blogs - Youngsoft 3. RE: WebService in liferay6 - Forums - Liferay.com

Blog at WordPress.com. Theme: Vigilance by The Theme Foundry.

http://arvindm.com/2010/03/23/web-services-in-liferay/

Вам также может понравиться