elearning web service

Upload: plu9in

Post on 06-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 ELearning Web Service

    1/38

    SAP TECHED 05

    BUILDING AND USING WEB SERVICESWITH SAP NETWEAVER DEVELOPERSTUDIOCD254

    Exercises / Solutions

    CARSTEN BRANDT,SAPAG

    MARC CHAN,SAPHONG KONG

    TIMM FALTER,SAPAG

    PETER MCNULTY,SAPLABS,LLC.

    PETER TILLERT,SAPAG

  • 8/3/2019 ELearning Web Service

    2/38

    SAP TECHED 05 1

    EXERCISE 1: EXPOSING EJB METHODS AS WEB SERVICESIn this exercise, you will learn how to expose an existing business process that has been written in Java as a web service. In SAPNetWeaver Developer Studio, SAPs comprehensive IDE for Java development, can deriving web services out of EnterpriseJavaBeans (EJB) as well as plain Java classes. In this exercise, you will use the Studio to expose methods of a given EJB. You willalso 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 othershortcut to SAP NetWeaver Developer Studio (especially not the one that lies directly on the desktop)! Otherwise you willnot be able to find the projects for this session in the Studios workspace.

    2. Make sure that the Web Services perspective is opened in the Studio by clicking on this icon:

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

  • 8/3/2019 ELearning Web Service

    3/38

    SAP TECHED 05 SEPTEMBER 2005 2

    4. Find theservicenamed

    EmployeeWS anddouble-clickon it. This

    should openthe Web

    ServiceHomepagetool.

    5. Click on Test andthen choose to runthe methodgetAllEmployeesin the web service.

    The result shouldbe a list ofemployees.

    6. Repeat step 5. withmethodcreateEmployeeto add an employee

    to the database.

    7. Repeat step 5. withmethod

    getAllEmployeesto check that thenewly addedemployee appearsin the list ofemployees.

  • 8/3/2019 ELearning Web Service

    4/38

    SAP TECHED 05 SEPTEMBER 2005 3

    Part 2 Exposing a Method from a Given EJB Method as a Web ServiceThis second part of the exercise demonstrates that it is very easy to add web services to an existing EEJB project.

    1. In the Web Services perspective, switch to the tab that holds the EJB Explorer tool. We will now work with theEmployeeEJB and EmployeeEAR projects that you can see in this tool.

    2. 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 theEmployeeWS web service.

    3. To expose this method in a new web service, you right-click on the nodeEmployee Session Bean and choose New

    Web Service.

    4. In the wizard that comes up, the first screen asks youfor a name for the new web service: Please enterGetAddressWS into the field Web Service Nameand click Next.

  • 8/3/2019 ELearning Web Service

    5/38

    SAP TECHED 05 SEPTEMBER 2005 4

    5. On the next screen you select the EJB method(s) to bepublished. Please deselect everything butgetEmployeePersonalAddress before you click

    Next.

    6. Finally, you need to specify the EAR Project usingthe Browse button. Please choose the projectEmployeeEAR in the Browse dialog, and thenclick Finish.

  • 8/3/2019 ELearning Web Service

    6/38

    SAP TECHED 05 SEPTEMBER 2005 5

    7. Now you can rebuild the project so that it contains the new web service: Right click on the project EmployeeEAR andchoose Build Application Archive. Building this project should automatically save your changes and should also build thedependent EmployeeEJB project.

    8. When the rebuild finished successfully, you can deploy the newly built enterprise archive, which now contains the new webservice, to the server: Right click on EmployeeEAR EmployeeEAR.ear and choose Deploy to J2EE Engine.

    The deployment may take a fewminutes. When you deploy

    something for the first during aDeveloper Studio session, the

    SDM (Software DeploymentManager) will prompt you for a

    password. Please enter thepassword teched.When the

    deployment process is finished,you will see the following

    messages in the Deploy OutputView tool:

    9. After you successfully deployed the archive, you can go back to the Web Service Navigator to test the new web service. Tomake 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 testthe new web

    service in theWeb Service

    Homepage,please enter avalid employeeIDand addressType

    MA or HM(for main or

    home address).

  • 8/3/2019 ELearning Web Service

    7/38

    SAP TECHED 05 SEPTEMBER 2005 6

    Part 3 Using Virtual Interfaces to Adapt the Parameters of a Web ServiceWe will now use virtual interfaces to create a different web service based on the same EJB method. The new web services will notrequire 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.

    2. Go to the node EmployeeEJB Virtual InterfacesGetHomeAddressWSVi and double-click to open the

    virtual interface for editing.

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

  • 8/3/2019 ELearning Web Service

    8/38

    SAP TECHED 05 SEPTEMBER 2005 7

    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 testenvironment no longer prompts you for the field addressType, and that the result that is returned is actually the employeeshome address.

  • 8/3/2019 ELearning Web Service

    9/38

    SAP TECHED 05 SEPTEMBER 2005 8

    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 Editortool that comes up, open the tab Methods, select the bean in the list and click the Add button to create a new businessmethod.

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

  • 8/3/2019 ELearning Web Service

    10/38

    SAP TECHED 05 SEPTEMBER 2005 9

    3. Enter employeeID in the field Name for the new parameter and java.lang.String for the Fully qualified name in theType area. In the same way, add a second parameter named actualDate of type java.util.Date. Finally, set the Fullyqualified 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 ofthe code pane to see the Java code that was generated for the new method.

  • 8/3/2019 ELearning Web Service

    11/38

    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 onethat came out of Part 3.

  • 8/3/2019 ELearning Web Service

    12/38

    SAP TECHED 05 SEPTEMBER 2005 11

    EXERCISE 2: CONSUMING A WEB SERVICEIn this exercise, you will learn how to consume web services, which are already deployed on the local server. You will develop a WebDynpro 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 generateautomatically 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. Start NetWeaver Developer Studio if you have not done so.2. You will first need to create a Web Dynpro Project. Click on menu File -> New -> Project3. Choose Web Dynpro -> Web Dynpro Project. Then click on Next.

    4. Enter the project name GetAddressProject and click on Finish

  • 8/3/2019 ELearning Web Service

    13/38

    SAP TECHED 05 SEPTEMBER 2005 12

    5. Create a Web Dynpro application with a Web Dynpro component. In the Web Dynpro Explorer, expand the newly createdGetAddressProject -> Web Dynpro project. Right-click on Applications and select Create Application

    6. 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.

  • 8/3/2019 ELearning Web Service

    14/38

    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.

  • 8/3/2019 ELearning Web Service

    15/38

    SAP TECHED 05 SEPTEMBER 2005 14

    10. You have now created the basic project structure of the new Web Dynpro Project GetAddressProject. The Web DynproExplorer 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.

  • 8/3/2019 ELearning Web Service

    16/38

    SAP TECHED 05 SEPTEMBER 2005 15

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

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

  • 8/3/2019 ELearning Web Service

    17/38

    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 theweb service.

  • 8/3/2019 ELearning Web Service

    18/38

    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. Right-click on the Custom Controllers area and choose Apply Template2. Choose Service Controller and click Next.

  • 8/3/2019 ELearning Web Service

    19/38

    SAP TECHED 05 SEPTEMBER 2005 18

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

    click Next.

  • 8/3/2019 ELearning Web Service

    20/38

    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 alldefault settings and click Finish.

  • 8/3/2019 ELearning Web Service

    21/38

    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 ContextMapping window will show

  • 8/3/2019 ELearning Web Service

    22/38

    SAP TECHED 05 SEPTEMBER 2005 21

    9. Drag-and-Drop Request_GetAddressWSViDocument_getEmployeePersonalAddress from GetAddressController Context toGetAddressView Context.

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

  • 8/3/2019 ELearning Web Service

    23/38

    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 ComponentController GetAddressController. The Diagram View will look as follows:

  • 8/3/2019 ELearning Web Service

    24/38

    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 tabcontains only one default UI element GetAddressView

    Part 4 Create UI Elements and Action Method

    1. Right-click on GetAddressView in the Diagram View and choose Apply Template.2. Choose the Form template and click Next

  • 8/3/2019 ELearning Web Service

    25/38

    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. Arrange all UI fields before they get created. Using the arrow controls, you can arrange the order of the following screenshot.5. On the Editor column change for all output attributes from InputField to TextView and click Finish.

  • 8/3/2019 ELearning Web Service

    26/38

    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 automaticallygenerate 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.

    8. Choose the ActionButton template and click Next.

  • 8/3/2019 ELearning Web Service

    27/38

    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

  • 8/3/2019 ELearning Web Service

    28/38

    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 Tasksview does not display any errors for your project. You can ignore any warning messages that have not been set.

    2. In Web Dynpro Explorer expand project to GetAddressProject -> Web Dynpro -> Applications3. Right-click on GetAddressApp and choose Deploy New Archive and Run.

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

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

  • 8/3/2019 ELearning Web Service

    29/38

    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 thisapplication 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)

    b. Create a new Custom Controller named CommunicationController and complete binding (use the same steps as youdid in Part 3)

    c. Create UI elements within the already existing views EmailView and SmsView and add action methods for sendingand an email and a sms (use the same steps as you did in Part 4)

    d. 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);

    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 asyou did in Part 5)

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

  • 8/3/2019 ELearning Web Service

    30/38

    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 usergroup in the Web Application Server. You will then modify your Employee EJB to require authorization against certain role. Afterthat, you will modify the web service configuration to accommodate these changes in the EJB to require user logon for accessing theweb service.

    Part 1 Creating a User for the application

    1. Start the Visual Administrator. To do it, go to \usr\sap\\JC\j2ee\admin and run go.bat.2. 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.

  • 8/3/2019 ELearning Web Service

    31/38

    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: HRGroupClick 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

  • 8/3/2019 ELearning Web Service

    32/38

    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 HRGroupgroup 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 username 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 ofthis 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.

  • 8/3/2019 ELearning Web Service

    33/38

    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 NetweaverDeveloper 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-refand

    click on Add.

    19. On the Role Name field, enter HRGroup. Then click on the Add button next to the Role Link field, and chooseHRGroup.

  • 8/3/2019 ELearning Web Service

    34/38

    SAP TECHED 05 SEPTEMBER 2005 33

    20. Click on Save.21. Rebuild the EAR by right click on EmployeeEARand 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 clickon Search.

  • 8/3/2019 ELearning Web Service

    35/38

    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 onthe Add button to map the user to the role. You will see HRUser appear under the Mapping Users.

    29. Close the Visual Admin.

  • 8/3/2019 ELearning Web Service

    36/38

    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 ofHRGroup. This will require the web service to authenticate against theHRGroup 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. After deploying the web service, you may want to test run it to make sure it works.2. Select createEmployee as the testing method. Click on Send on the following page.3. It will then prompt you for user logon. Logon as the user you just created:

    User name: HRUser Password: payroll

  • 8/3/2019 ELearning Web Service

    37/38

    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. Itmay be a browser cache issue.

  • 8/3/2019 ELearning Web Service

    38/38

    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 theexpress 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 componentsof 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 WebConsortium, 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 inGermany 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 agreementwith 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 accuracyor 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, orconsequential 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 theinformation 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 ibilit f i i i th t i l