upload files using web service in web dynpro java application

24
Upload file using Web Service in Web Dynpro Application SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 1 Upload file using Web Service in Web Dynpro Java Application Summary In a Web Dynpro application you can upload a file to server using web services. First of all you have to develop a web service which can handle attachments (binary data). Then utilize that web service from Web Dynpro to upload file. To develop this application you have to follow the steps, described later, sequentially. The overall scenario is described in Figure 1.0 Author(s): Sudip Das Company: HCL Technologies Created on: 2 April 2007 Author Bio Sudip Das is working as a Netweaver Consultant for HCL Technologies, Kolkata.

Upload: phuongnt2710

Post on 15-Nov-2014

122 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 1

Upload file using Web Service in Web Dynpro Java Application

Summary In a Web Dynpro application you can upload a file to server using web services. First of all you have to develop a web service which can handle attachments (binary data). Then utilize that web service from Web Dynpro to upload file. To develop this application you have to follow the steps, described later, sequentially. The overall scenario is described in Figure 1.0 Author(s): Sudip Das Company: HCL Technologies Created on: 2 April 2007

Author Bio Sudip Das is working as a Netweaver Consultant for HCL Technologies, Kolkata.

Page 2: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 2

Table of Contents Steps are to be followed .................................................................................................................. 3 Web Service Creation and Deployment .......................................................................................... 3

Create Business Method.............................................................................................................. 3 Prerequisites............................................................................................................................. 3 Procedure ................................................................................................................................. 4

Consume Web Service in a Web Dynpro Application ................................................................... 11 Prerequisites........................................................................................................................... 11 Procedure ............................................................................................................................... 11

Build, Deploy and Run Application ................................................................................................ 22 Related Content............................................................................................................................. 23 Disclaimer and Liability Notice....................................................................................................... 24

Page 3: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 3

Steps are to be followed

1. Create Web Service (WS) which can handle (file) attachments. 2. Bundle that web service with an EAR file and deploy into server. 3. Create a Web Dynpro application. 4. Create a Model in Web Dynpro application and import that web service into this model. 5. Read the file from client side and send the file to WS using Logical Port. 6. WS will handle that file on server side and do the specific job, like store that file in local file system of

server or can store in a database in the form BLOB data.

Web Service Creation and Deployment To expose web services you have to create Enterprise JavaBean (EJB) or Java class. Here we are exposing web services from an EJB. In EJB we develop business method which can handle file as binary data.

Create Business Method

Prerequisites • FileHandleModule EJB project is created. • FileHandleModule project is currently displayed in J2EE Explorer of J2EE Development Perspective. • SAP J2EE engine and Software Deployment Manager (SDM) have been running properly in WAS

(Web Application Server). Again you have checked that the J2EE server is correctly selected in the NetWeaver Developer Studio.

Page 4: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 4

Procedure • Double click on FileHandleBean under ejb-jar.xml file and select Methods Tab. Now select Business

Methods and press Add Pushbutton. (Figure 1.1) • In this wizard, give the following values of the parameters. Figure 1.2 depicts the result after

providing the values. Note that you have to select the type of fileContent parameter as byte.

Name Value Comment

Method Name storeFile

Return Type Void

Parameter 1 Name fileName

Parameter 1 Type String

Parameter 2 Name fileContent

Parameter 2 Type byte Select Array of size 1

Page 5: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 5

• Select Bean Tab of FileHandleBean and inside storeFile method place the custom code to handle

the file as binary data for specific job purpose. Here we store the file in location file system of server. Figure 1.3 shows that custom code for file processing.

Page 6: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 6

• Select Web Service from the context menu of FileHandleBean. In the appeared wizard assign FileHandleWS to Web Service Name field and leave the entire field’s value as default. Choose Next button. (Figure 1.4, Figure 1.5)

Page 7: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 7

• Now select the methods of the FileHandleBean which will to be contained in the Virtual Interface

(Web Service), if they are not already selected. (Figure 1.6) Choose Next button.

Page 8: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 8

• Now bundle the EJB project (FileHandleModule) into an EAR project. In the appeared wizard select the EAR project (if it is currently opened in J2EE Explorer) using Browse button or you can give any name for new EAR project. Provide FileHandleEAR for the EAR project name, this EAR project will

Page 9: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 9

be created. Leave the other field’s value default like value of Virtual Interface is FileHandleWSVi and value of Web Service Definition is FileHandleWSWsd (Figure 1.7)

Page 10: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 10

• Select Build EJB Archive from the context menu of the EJB project (FileHandleModule). EJB JAR file named FileHandleModule.jar will be generated.

• Now select Build Application Archive from the context menu of the EAR project (FileHandleEAR). EAR file named FileHandleEAR.ear will be generated.

• Select Deploy to J2EE engine from the context menu of FileHandleEAR.ear file, under the EAR project FileHandleEAR. (Figure 1.8). You have made sure that the SAP J2EE Engine and Software Deployment Manager (SDM) has been running properly. Again you have checked that the J2EE server is correctly selected in the Developer Studio, if it is not configured then configure it from Window → Preferences → SAP J2EE Engine.

• Now provide the password for SDM. If the deployment is successful then a successful message will be displayed in the Deploy Output View pane. (Figure 1.10)

Page 11: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 11

Consume Web Service in a Web Dynpro Application To consume a web service you have to create a Web Dynpro application. Using Model of Web Dynpro a web service can be imported to a Web Dynpro application. The whole procedure is described below.

Prerequisites • FileHandleEAR (EAR Project) should be developed and currently deployed in to WAS. • SAP J2EE engine and Software Deployment Manager (SDM) have been running properly in WAS

(Web Application Server). Again you have checked that the J2EE server is correctly selected in the NetWeaver Developer Studio. Here the same WAS is used, where the FileHandleEAR file is deployed which contains the web service (FileHandleWS).

• A Web Dynpro Project (WSDemoWD) is developed and currently displayed in Web Dynpro Explorer of Web Dynpro Perspective.

Procedure • Now select Create Model from the context menu of Model in the Web Dynpro Explorer. (Figure 1.11)

Page 12: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 12

• Select Import Web Service Model in appeared wizard and choose Next button. (Figure 1.12)

Page 13: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 13

• In next wizard, provide FileHandleMdl for Model Name and com.hcl.model.fhandle for the Model Package. Choose Local Server for the WSDL (Web Service Definition Language) source or any other suitable option where the Web Service’s WSDL file is placed. (Figure 1.13)

Page 14: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 14

• Now select FileHandleWS from the list of web services are available on the server. (Figure 1.14) Choose Next button. In the appeared wizard the name of the Virtual Interface will be displayed under URI. Choose Finish button. (Figure 1.15). If the import of web service is successful then the Web Dynpro project structure will be like Figure 1.16.

Page 15: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 15

Page 16: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 16

• In the Used Models area, right-click and choose Add. Select the Web Service model imported previously (FileHandleMdl) and choose OK button. (Figure 1.17 and 1.18)

Page 17: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 17

• Now don’t create any context node or attribute to map the byte[ ] type in your controller or view context, just send it externally (using Logical Port) because there's a problem in the current release.

Page 18: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 18

• Place some UI components in the view, StartView to select the file from client’s file system. Here we use FileUpload UI Component for this purpose. The overall design view (and Context binding with UI elements) of StartView is presented in Figure 1.19. Create three Value Attributes in the Context of StartView, listed below.

Name Type

msg string

fName string

fData Binary

uIVisibility com.sap.ide.webdynpro.uielementdefinitions.Visibility

Provide the id for the FileUpload and Button UI element as ChosseFile and Send respectively. Bind fData and fName context element to data and fileName field of the FileUpload (ChosseFile) UI element respectively. Bind uIVisibility context element to visible field of both ChooseFile and Send UI Elements. Now bind msg context element to the text field of UploadMessage UI Element.

Page 19: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 19

• Provide Send to Name to define New Action for the Send button. Now go to the implementation of the onActionSend (in StartView) and put the following code (it also includes the necessary import statements), shown in Figure 1.20. Here we collect the file name and file data using getFName() and getFData() method. Then we create the instance of model and stub, the names of the classes you can find under src folder of com.hcl.model.fhandle.proxies package (Figure 1.21)

Page 20: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 20

Page 21: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 21

• Now put the following code, shown in Figure 1.22 in wdDoInit (in StartView) method, necessary for the FileUpload UI element.

Page 22: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 22

Build, Deploy and Run Application • Select Save All metadata from the context menu of WSDemoWD project (Web Dynpro Project).

Select Rebuild Project from the context menu of WSDemoWD project (Web Dynpro Project). Now select Deploy New Archive and Run from the context menu of WSDemoWDApp (Web Dynpro Application).

• After deploying and running the application provide the file name to be uploaded using Browse button and the check the specified location (mentioned in business method of EJB) of WAS where the file to be stored.

Page 23: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 23

Related Content • http://help.sap.com/saphelp_nw04/helpdata/en/f7/af60f2e04d0848888675a800623a81/frameset.htm

• http://help.sap.com/saphelp_nw04/helpdata/en/49/12eb82e057474994765faf83995ecb/frameset.htm

• http://help.sap.com/saphelp_nw04/helpdata/en/90/c1343e8c7f6329e10000000a114084/frameset.htm

Page 24: Upload Files Using Web Service in Web Dynpro Java Application

Upload file using Web Service in Web Dynpro Java Application

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2007 SAP AG 24

Disclaimer and Liability Notice This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.