Vous êtes sur la page 1sur 38

SAP TECHED 05

BUILDING AND USING WEB SERVICES WITH SAP NETWEAVER DEVELOPER STUDIO
CD254

Exercises / Solutions

CARSTEN BRANDT, SAP AG MARC CHAN, SAP HONG KONG TIMM FALTER, SAP AG PETER MCNULTY, SAP LABS, LLC. PETER TILLERT, SAP AG

SAP TECHED 05

EXERCISE 1: EXPOSING EJB METHODS AS WEB SERVICES In this exercise, you will learn how to expose an existing business process that has been written in Java as a web service. In SAP NetWeaver Developer Studio, SAPs comprehensive IDE for Java development, can deriving web services out of Enterprise JavaBeans (EJB) as well as plain Java classes. In this exercise, you will use the Studio to expose methods of a given EJB. You will also learn how to use the Web Service Homepage, a tool that can test run your web services and monitor the SOAP message exchanges that go with it. Finally, you will see how you can use Virtual Interfaces to fine-tune the signature of your web services. Part 1 Testing Web Services in the SAP NetWeaver Developer Studio In the first part of the exercise you test some custom web services that are already deployed on the engine. 1. Start the SAP NetWeaver Developer Studio using the shortcut in folder Session material CD254. Do not use any other shortcut to SAP NetWeaver Developer Studio (especially not the one that lies directly on the desktop)! Otherwise you will not be able to find the projects for this session in the Studios workspace. Make sure that the Web Services perspective is opened in the Studio by clicking on this icon:

2.

3.

In the Web Service Navigator tool, open the node that lists the services on the Local Server.

SAP TECHED 05

SEPTEMBER 2005

4.

Find the service named Employee WS and double-click on it. This should open the Web Service Homepage tool.

5.

Click on Test and then choose to run the method getAllEmployees in the web service. The result should be a list of employees. Repeat step 5. with method createEmployee to add an employee to the database. Repeat step 5. with method getAllEmployees to check that the newly added employee appears in the list of employees.

6.

7.

SAP TECHED 05

SEPTEMBER 2005

Part 2 Exposing a Method from a Given EJB Method as a Web Service This second part of the exercise demonstrates that it is very easy to add web services to an existing EEJB project. 1. 2. In the Web Services perspective, switch to the tab that holds the EJB Explorer tool. We will now work with the EmployeeEJB and EmployeeEAR projects that you can see in this tool. Open the node EmployeeEJB ejb-jar.xml Employee Session Bean. You can see that the EJB has another method, getEmployeePersonalAddress, which is not yet exposed in the EmployeeWS web service. To expose this method in a new web service, you right-click on the node Employee Session Bean and choose New Web Service.

3.

4.

In the wizard that comes up, the first screen asks you for a name for the new web service: Please enter GetAddressWS into the field Web Service Name and click Next.

SAP TECHED 05

SEPTEMBER 2005

5.

On the next screen you select the EJB method(s) to be published. Please deselect everything but getEmployeePersonalAddress before you click Next.

6.

Finally, you need to specify the EAR Project using the Browse button. Please choose the project EmployeeEAR in the Browse dialog, and then click Finish.

SAP TECHED 05

SEPTEMBER 2005

7.

Now you can rebuild the project so that it contains the new web service: Right click on the project EmployeeEAR and choose Build Application Archive. Building this project should automatically save your changes and should also build the dependent EmployeeEJB project. When the rebuild finished successfully, you can deploy the newly built enterprise archive, which now contains the new web service, to the server: Right click on EmployeeEAR EmployeeEAR.ear and choose Deploy to J2EE Engine. The deployment may take a few minutes. When you deploy something for the first during a Developer Studio session, the SDM (Software Deployment Manager) will prompt you for a password. Please enter the password teched. When the deployment process is finished, you will see the following messages in the Deploy Output View tool:

8.

9.

After you successfully deployed the archive, you can go back to the Web Service Navigator to test the new web service. To make the new web service appear in the list you need to refresh the list using the right-click menu or the refresh icon.

10. When you test the new web service in the Web Service Homepage, please enter a valid employeeID and addressType MA or HM (for main or home address).

SAP TECHED 05

SEPTEMBER 2005

Part 3 Using Virtual Interfaces to Adapt the Parameters of a Web Service We will now use virtual interfaces to create a different web service based on the same EJB method. The new web services will not require an input for address type, but only retrieve home addresses. 1. Repeating steps 1 to 6 from Part 2 of this exercise, create a web service named GetHomeAddressWS. Go to the node EmployeeEJB Virtual Interfaces GetHomeAddressWSVi and double-click to open the virtual interface for editing.

2.

3.

Select the Incoming parameter addressType and enter HM into the Fixed value field. Optionally, you can also rename the method in the web service by entering getEmployeePersonalHomeAddress in the field New name. Click the Save button in the main toolbar to save the change you made to the virtual interface.

SAP TECHED 05

SEPTEMBER 2005

4.

You can now proceed to build, deploy, and test the new web service by repeating steps 7 to 10 from Part 2. Note that the test environment no longer prompts you for the field addressType, and that the result that is returned is actually the employees home address.

SAP TECHED 05

SEPTEMBER 2005

Part 4 Adding a Web Service by Implementing a New EJB Method (optional) For completeness, you can now try to achieve the same effect as in Part 3 by implementing a new method directly in the EJB. 1. Double-click on the node EmployeeEJB ejb-jar.xml Employee Session Bean in the EJB Explorer. In the EJB Editor tool that comes up, open the tab Methods, select the bean in the list and click the Add button to create a new business method.

2.

Type the new name getEmployeePersonalHomeAddress for the business method into the Name field and click the Add button in the Parameters area to create a new parameter.

SAP TECHED 05

SEPTEMBER 2005

3.

Enter employeeID in the field Name for the new parameter and java.lang.String for the Fully qualified name in the Type area. In the same way, add a second parameter named actualDate of type java.util.Date. Finally, set the Fully qualified name in the Return type area to com.sap.teched.employee.util.EmpAddressDTO.

4.

Click the Save button in the main toolbar and then switch from the Methods to the Bean tab. Scroll to the bottom of the code pane to see the Java code that was generated for the new method.

SAP TECHED 05

SEPTEMBER 2005

10

5.

Fill in the following code for the implementation of the new method and save your work.

6.

Create a web service from the new method, analogous to Part 2 of this exercise. The new web service is similar to the one that came out of Part 3.

SAP TECHED 05

SEPTEMBER 2005

11

EXERCISE 2: CONSUMING A WEB SERVICE In this exercise, you will learn how to consume web services, which are already deployed on the local server. You will develop a Web Dynpro application for getting the address of an employee, using the GetAddressWS web service you already created in exercise 1. Netweaver Developer Studio wizards allow developers to use an already deployed web service on the local server and generate automatically all proxies needed for communication. Through the wizards you will also create the user interface which will consist of a simple input form for editing and a button. Part 1 - Create an GetAddressProject Web Dynpro Project 1. 2. 3. Start NetWeaver Developer Studio if you have not done so. You will first need to create a Web Dynpro Project. Click on menu File -> New -> Project Choose Web Dynpro -> Web Dynpro Project. Then click on Next.

4.

Enter the project name GetAddressProject and click on Finish

SAP TECHED 05

SEPTEMBER 2005

12

5. 6.

Create a Web Dynpro application with a Web Dynpro component. In the Web Dynpro Explorer, expand the newly created GetAddressProject -> Web Dynpro project. Right-click on Applications and select Create Application On the next window, enter the following information and click Next:

Name: GetAddressApp Package: com.sap.teched.address Accept all other default settings.

7.

Click on Next.

SAP TECHED 05

SEPTEMBER 2005

13

8.

Choose Create a New Component and click Next.

9.

On the next window, enter the following information and click Finish when done: Component Name: GetAddressComponent Window Name: GetAddressWindow View Name: GetAddressView Accept all other default settings.

SAP TECHED 05

SEPTEMBER 2005

14

10. You have now created the basic project structure of the new Web Dynpro Project GetAddressProject. The Web Dynpro Explorer shows the following nodes for the structure

Part 2 - Create a Web Service Model 1. A web service model performs the data exchange between the Web Dynpro component and the web service end point. Expand the project to: GetAddressProject -> Web Dynpro -> Web Dynpro Components and double click on GetAddressComponent to open the Diagram View also known as the MVC diagram.

SAP TECHED 05

SEPTEMBER 2005

15

2. 3.

Right click on the Used Models area and choose Create Model Choose Import Web Service Model and click Next

4.

On the next window, enter the following information and click Next when done: Model Name: Package Name: Source Folder: Select WSDL Source: GetAddressModel com.sap.teched.address.model src/packages Local Server

SAP TECHED 05

SEPTEMBER 2005

16

5.

On the next window, select the GetAddressWS web service on the local server, which you already created in the first exercise and click Next.

6.

On the next window, click Finish to generate the Web Dynpro Model with all java proxies needed to communicate with the web service.

SAP TECHED 05

SEPTEMBER 2005

17

7.

The next window will display the Diagram View of the Web Dynpro component GetAddressComponent

Part 3 - Create New Custom Controller and Complete Binding 1. 2. Right-click on the Custom Controllers area and choose Apply Template Choose Service Controller and click Next.

SAP TECHED 05

SEPTEMBER 2005

18

3.

Name the custom controller GetAddressController. To do Model Binding automatically between the created model and the custom controller GetAddressController choose Request_GetAddressWSViDocument_getEmployeePersonalAddress and click Next.

SAP TECHED 05

SEPTEMBER 2005

19

4.

On the next window, select all context elements and click Next.

5.

By using templates, Developer Studio wizards automatically generates a method to execute the created model. Accept all default settings and click Finish.

SAP TECHED 05

SEPTEMBER 2005

20

6.

Move GetAddressController above GetAddressModel for better view.

7.

In the left toolbar, choose Create Data Link

8.

Starting from the GetAddressView, press the left mouse button and draw a line to the GetAddressController. A Context Mapping window will show

SAP TECHED 05

SEPTEMBER 2005

21

9.

Drag-and-Drop Request_GetAddressWSViDocument_getEmployeePersonalAddress from GetAddressController Context to GetAddressView Context.

10. Choose all Context elements offered on the left and click OK.

SAP TECHED 05

SEPTEMBER 2005

22

11. On the next window, expand context nodes to see binding done between node elements. Then click Finish.

12. You have now created the necessary Context Binding between the View Controller GetAddressView and the Component Controller GetAddressController. The Diagram View will look as follows:

SAP TECHED 05

SEPTEMBER 2005

23

13. Up to this point, UI elements have not been created. Double-click on GetAddressView and confirm that the Layout tab contains only one default UI element GetAddressView

Part 4 Create UI Elements and Action Method 1. 2. Right-click on GetAddressView in the Diagram View and choose Apply Template. Choose the Form template and click Next

SAP TECHED 05

SEPTEMBER 2005

24

3.

Choose all Context elements except the input attribute actualDate and the result attributes employeeId (in both subnodes address and employee), careOfName, county and addressType. Click Next

4. 5.

Arrange all UI fields before they get created. Using the arrow controls, you can arrange the order of the following screenshot. On the Editor column change for all output attributes from InputField to TextView and click Finish.

SAP TECHED 05

SEPTEMBER 2005

25

6.

The Layout tab of the GetAddressView containing all UI elements will look as follows.

7.

To trigger the execution of the web servive a Get Address button is needed. Developer Studio wizards will automatically generate an event handler, creating an action method and binding that action to a button. Right-click on GetAddressView in the Diagram View and choose Apply Template. Choose the ActionButton template and click Next.

8.

SAP TECHED 05

SEPTEMBER 2005

26

9.

On the next window, enter the following information and click Next when done: Button Label: Get Address Accept all other default settings.

10. On the next window, enter the following information and click Finish when done: Call Method: Selected Controller: GetAddressController Accept all other default settings

SAP TECHED 05

SEPTEMBER 2005

27

Part 5 Deploy and Run the Application

1.

In Web Dynpro Explorer, right-click in GetAddressProject project and choose Rebuild Project. Make sure that the Tasks view does not display any errors for your project. You can ignore any warning messages that have not been set. In Web Dynpro Explorer expand project to GetAddressProject -> Web Dynpro -> Applications Right-click on GetAddressApp and choose Deploy New Archive and Run.

2. 3.

4.

The Developer Studio deploys the application in one single step, based on an automatically generated Enterprise Archive File, and then launches your application in the browser

5.

Congratulations!!. Test the application by entering for instance 1001 as employeeID and HM as addressType.

SAP TECHED 05

SEPTEMBER 2005

28

Part 6 Optional: Enhance GetEmployeesProject Application 1. The same steps you did in Part 2 to Part 5 in the exercise 2 you can do for the GetEmployeesProject, in order to enhance this application by sending emails or sms. Here are the steps you need to do: a. Create a new web service model named CommunicationModel based on the locally deployed CommunicationWS (use the same steps as you did in Part 2) Create a new Custom Controller named CommunicationController and complete binding (use the same steps as you did in Part 3) Create UI elements within the already existing views EmailView and SmsView and add action methods for sending and an email and a sms (use the same steps as you did in Part 4) Add following lines of code within the //@begin and //@end sections of the onPlugFromEmployeesView method, which you can find on the Implementation tab of the EmailView and SmsView: For the EmailView add following line within the above mentioned method on the Implementation tab:
wdContext.currentRequest_CommunicationWSViDocument_sendEmailElement().setTo(to);

b.

c.

d.

For the SmsView add following line within the above mentioned method on the Implementation tab:
wdContext.currentRequest_CommunicationWSViDocument_sendSMSElement().setNumber(number);

e.

Right-click on the GetEmployeesApp Application and choose Deploy New Archive and Run (use the same steps as you did in Part 5)

2.

If you successfully finished these steps, then you can send emails and sms to the listed employees:

SAP TECHED 05

SEPTEMBER 2005

29

EXERCISE 3: ADDING SECURITY FEATURES TO YOUR WEB SERVICE (OPTIONAL) In this part of the exercise, you will extend the web service you have created in Ex.1 for security features. You will create HR user group in the Web Application Server. You will then modify your Employee EJB to require authorization against certain role. After that, you will modify the web service configuration to accommodate these changes in the EJB to require user logon for accessing the web service. Part 1 Creating a User for the application 1. 2. Start the Visual Administrator. To do it, go to \usr\sap\<SID>\JC<Sys Nr>\j2ee\admin and run go.bat. Connect and logon to the server using the username and password provided to you. When the logon is complete, you will see 100% at the lower right corner.

3.

Expand the Server node and then go to Services -> Security Provider.

4.

We will first create a user group for the users are going to create. Click on the tab User Management.

SAP TECHED 05

SEPTEMBER 2005

30

5.

Switch to EDIT mode

6.

Select the Groups tab and Click on the Create Group button. Then enter the following for creating the user group:

Group Name: HRGroup Click on OK. This will create the user group 7. Now we can create a user and assign it to this group. Click on the button Create User. Enter the following: User Name: HRUser Password: payroll Confirm Password: payroll

SAP TECHED 05

SEPTEMBER 2005

31

8.

Under the Group tab, enter HRGroup in the Name field. Then click on the Search button. Select the searched HRGroup group and click on OK.

9.

We need to disable the feature of require password change of this user. To do it, under the User tab enter HRUser on the user name field and click on the Search button.

10. The user HRUser will be shown underneath the Search button. Click on this HRUser to select it. You will see the detail of this user. 11. Check the checkbox No password change required in the user detail as shown below.

12. Leave the Visual Admin tool open as we will use it again soon.

SAP TECHED 05

SEPTEMBER 2005

32

Part 2 Modify the EJB to require user authorization 13. We will now modify the EmployeeSessionBean we have created in Ex.1 to require user authorization. Start your Netweaver Developer Studio if you havent done so. 14. Under the EmployeeEJB project, double click on ejb-jar.xml to open it. 15. On the right panel, click on the tab Assembly. 16. Select the security-role folder and click on the Add button. On the Role Name field, enter HRGroup.

17. Click on Save. 18. Click on the Enterprise Beans tab. Expand the session beans folder and EmployeeSessionBean. Select security-role-ref and click on Add. 19. On the Role Name field, enter HRGroup. Then click on the Add button next to the Role Link field, and choose HRGroup.

SAP TECHED 05

SEPTEMBER 2005

33

20. Click on Save. 21. Rebuild the EAR by right click on EmployeeEAR and select Build Application Archive.

22. After the build is finish, expand the project, right click on EmployeeEAR.ear and select Deploy to J2EE Engine. 23. This will redeploy the EJB with the security settings to the server. 24. Go back to the Visual Admin tool. 25. Go to Server -> Service -> Security Provider. 26. Click on the Policy Configurations tab again. Underneath the tab, select the application sap.com/VacationRuleApp*VacationRuleEJB.jar.

27. Click on the Security Roles tab. In the Mappings area, there is a User tab. Inside the Name field, enter HRUser and click on Search.

SAP TECHED 05

SEPTEMBER 2005

34

28. Select the HRUser under from the search result. Also select the HRGroup inside the Security Roles column. Then click on the Add button to map the user to the role. You will see HRUser appear under the Mapping Users. 29. Close the Visual Admin.

SAP TECHED 05

SEPTEMBER 2005

35

Part 3 Modify the Web Service configurations for security 30. Expand the project EmployeeEJB. Double click on Web Service Configuration to open it. 31. On the right panel, expand Web Service Configurations -> EmployeeService -> Config1 and select Security. 32. Make sure the following settings have been set: Authentication Mechanism: HTTP Authentication The option of Basic (username/password) is selected. 33. Under the Authorization tab, check the checkbox of HRGroup. This will require the web service to authenticate against the HRGroup group.

34. Click on Save. 35. Rebuild the EAR by right click on EmployeeApp and select Build Ear File. 36. After the build is finish, expand the project, right click on EmployeeApp.ear and select Deploy to J2EE Engine. 37. This will redeploy the Web Service with the security settings to the server. Part 4 Testing the web service 1. 2. 3. After deploying the web service, you may want to test run it to make sure it works. Select createEmployee as the testing method. Click on Send on the following page. It will then prompt you for user logon. Logon as the user you just created: User name: HRUser Password: payroll

SAP TECHED 05

SEPTEMBER 2005

36

4.

If the authentication is ok, you will see the result and the SOAP messages exchange.

Note: if you are not prompted for logon or you get an error when running it, try to close the Developer Studio and run it again. It may be a browser cache issue.

SAP TECHED 05

SEPTEMBER 2005

37

Copyright 2005 SAP AG. All Rights Reserved


No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, MVS/ESA, AIX, S/390, AS/400, OS/390, OS/400, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere, Netfinity, Tivoli, and Informix are trademarks or registered trademarks of IBM Corporation in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. MaxDB is a trademark of MySQL AB, Sweden. SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and in several other countries all over the world. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. The information in this document is proprietary to SAP. No part of this document may be reproduced, copied, or transmitted in any form or for any purpose without the express prior written permission of SAP AG. This document is a preliminary version and not subject to your license agreement or any other agreement with SAP. This document contains only intended strategies, developments, and functionalities of the SAP product and is not intended to be binding upon SAP to any particular course of business, product strategy, and/or development. Please note that this document is subject to change and may be changed by SAP at any time without notice. SAP assumes no responsibility for errors or omissions in this document. SAP does not warrant the accuracy or completeness of the information, text, graphics, links, or other items contained within this material. This document is provided without a warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. SAP shall have no liability for damages of any kind including without limitation direct, special, indirect, or consequential damages that may result from the use of these materials. This limitation shall not apply in cases of intent or gross negligence. The statutory liability for personal injury and defective products is not affected. SAP has no control over the information that you may access through the use of hot links contained in these materials and does not endorse your use of third-party Web pages nor provide any warranty whatsoever relating to third-party Web pages.. SAP assumes no responsibility for errors or omissions in these materials

Vous aimerez peut-être aussi