Download Programming RESTful SDK in Java with Eclipse - Getting

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Programming RESTful SDK in Java
with Eclipse - Getting Started
Applies to:
SAP BusinessObjects BI 4.0 SP05 or above
Summary:
This paper provides a step-by-step description on how to start developing java applications in an Eclipse environment
This sample simply shows how to log in and log off the platform
Some samples in .net can be found in SDN:
http://scn.sap.com/community/bi-platform/blog/2012/11/22/sap-bi-platform-restful-web-service-sdk-demos
Official documentation :
http://help.sap.com/businessobject/product_guides/boexir4/en/xi4_bip_rest_ws_en.pdf
Author Bio
Christian Champault ([email protected]) is an expert on SAP BusinessObjects Enterprise architecture,
SDKs, configuration and performance optimization.
Company:
SAP
Last update:
December 11 2012
SAP COMMUNITY NETWORK
© 2012 SAP AG
Final
1
Programming RESTful SDK in Java
with Eclipse - Getting Started
Table of Contents
Document update history ................................................................................................................................... 3
1
Background ........................................................................................................................................... 4
1.1
Business Intelligence Platform RESTful web service SDK ................................................................... 4
1.2
Web Intelligence RESTful web service SDK......................................................................................... 4
1.3
SAP Crystal Reports RESTful web service SDK .................................................................................. 5
2
Set up the development environment ................................................................................................... 6
2.1
Installing Eclipse ................................................................................................................................... 6
2.2
http request handling tool ...................................................................................................................... 6
3
Creating and configuring a new Eclipse Project ................................................................................... 7
3.1
Creating the new project ....................................................................................................................... 7
3.2
New project set up ................................................................................................................................ 7
3.3
Default class .......................................................................................................................................... 9
4
Login and logoff ................................................................................................................................... 11
4.1
Workflow.............................................................................................................................................. 11
4.2
Output.................................................................................................................................................. 11
4.3
Code .................................................................................................................................................... 12
Copyright .......................................................................................................................................................... 16
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
2
Programming RESTful SDK in Java
with Eclipse - Getting Started
Document update history
Date
Name
Alteration Reason
12.11.2012
Christian Champault
Initial document
SAP COMMUNITY NETWORK
© 2012 SAP AG
Version
0.1
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
3
Programming RESTful SDK in Java
1
with Eclipse - Getting Started
Background
The SAP RESTful web services SDK complies with the RESTful methodology. You
can access the RESTful web services SDK using any programming language that supports making HTTP requests.
We are going to focus in this document in programming with java.
From an architecture point of view, The WACS server is responsible for handling RESTful web service and interfacing
with the platform.
Dialog with applications is made with the HTTP protocol, on XML format
RESTful web services appeared in the BOE platform starting with BI 4.0 FP3. Its model has been extended with SP5.
It is planned to be extended also in future release of Business Objects, in order to replace other SDKs.
There are actually several webservice available:
1.1
Business Intelligence Platform RESTful web service SDK
The Business Intelligence platform RESTful web service SDK lets you access the BI platform using the
HTTP protocol. You can use this SDK to log on to the BI platform, navigate the BI platform repository,
access resources, and perform basic resource scheduling. You can access this SDK by writing
applications that use any programming language that supports the HTTP protocol, or by using any tool
that supports making HTTP requests
1.2
Web Intelligence RESTful web service SDK
The Web Intelligence RESTful web service SDK is an API used for manipulating the folIowing:
• manipulating Web Intelligence documents and reports
• retrieving data from a dataprovider
• retrieving a list of available universes and details of an universes
• scheduling documents
It cannot be used to edit/create SAP Web Intelligence documents.
The Web Intelligence RESTful web service SDK relies on the BI platform RESTful web services API for session
management and repository access.
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
4
Programming RESTful SDK in Java
1.3
with Eclipse - Getting Started
SAP Crystal Reports RESTful web service SDK
SAP Crystal Reports RESTful web services allow report data managed in a SAP BusinessObjects
Business Intelligence Platform repository to be consumed and embedded in mobile devices and
web-enabled technology. You can fetch report content in XML or JSON format, and manipulate a report
using the RESTful API and OData services. RESTful web services allow you to create applications
using the development language of your choice.
SAP Crystal Reports RESTful web services allow you to:
• Export a report to a number of different file types
• Retrieve report metadata.
• Get rows of data and calculations.
• Push a row of data to the report.
• Get and set report parameters.
• Retrieve data in XML or JSON format.
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
5
Programming RESTful SDK in Java
2
Set up the development environment
2.1
Installing Eclipse
with Eclipse - Getting Started
On the developer workstation:
Download Eclipse from the Eclipse web site:
http://www.eclipse.org/
The sample in this article has been built with Indigo for web developers SR1
http://www.eclipse.org/downloads/packages/release/indigo/sr1
Unzip the downloaded file.
Eclipse is not delivered with a jdk, so you may need to download one if it not already installed. (www.java.com)
Eclipse needs to know where the kdk is located, so make sure the java jdk is in the path environment variable of the
workstation.
Eclipse is ready to run!
2.2
http request handling tool
With Indigo, you won’t get necessary classes to handle http requests.
There are different projects that implement this functionality, like Jersey project, Apache CXF, and others.
We will use the Apache HttpClient project. I used version 4.2.1.
http://hc.apache.org/
the project is a set of seven jar file we will use in our application.
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
6
Programming RESTful SDK in Java
3
Creating and configuring a new Eclipse Project
3.1
Creating the new project
with Eclipse - Getting Started
Launch Eclipse, and create a new java project:
Name it, ie Restful01, then finish
The project is created!
3.2
New project set up
Now, we need to reference the Apache httpclient project jars in the build path, so we can use it.
Configure the build path:
Add Library
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
7
Programming RESTful SDK in Java
with Eclipse - Getting Started
We will create a user library that will include all the Apache httpclient jars.
Click on “User Librairies…” to create our library
In this screen, create the library “httpclient” and add the Apache httpclient project jars:
Once done, you can add it to our project:
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
8
Programming RESTful SDK in Java
with Eclipse - Getting Started
The build path contains now the jre system library, plus ours:
“OK”
The project is still empty, but we are now ready for the coding!
3.3
Default class
Create a class into the project :
Name it : (here, Raylight)
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
9
Programming RESTful SDK in Java
with Eclipse - Getting Started
Please note that the use of the default package is discouraged, but we don’t really mind for this sample.
The class is ready for receiving the code
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
10
Programming RESTful SDK in Java
4
Login and logoff
4.1
Workflow
with Eclipse - Getting Started
To log into the system, we need to POST at the address:
http://<servername>:6405/biprws/logon/long
the following XML :
<attrs xmlns="http://www.sap.com/rws/bip">
<attr name="userName" type="string">myUserName</attr>
<attr name="password" type="string">myPassword</attr>
<attr name="auth" type="string" possibilities="secEnterprise,secLDAP,secWinAD,secSAPR3">secEnterprise</attr>
</attrs>
In response to this POST request, we will received in the header the logon token, (X-SAP-LogonToken parameter)
But, when making any GET request at the same address, the RESTfull web service returns to the client a template of this
XML.
So, instead of building an xml from scratch to log in, we will:
1 – send a GET request to http://<servername>:6405/biprws/logon/long
2 – retrieve the template XML returned by the RESTful web service
3 – modify this XML with our login information (username, password, method)
4 – make a POST request back to the web service with this modified XML
5 – retrieve the logon token from the response of that request
4.2
Output
Running the program into Eclipse you will get the following output:
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
11
Programming RESTful SDK in Java
4.3
with Eclipse - Getting Started
Code
The code is as follow (you can copy and paste into the class). You still need to modify the login information!
import
import
import
import
java.io.BufferedInputStream;
java.io.InputStream;
java.io.StringReader;
java.io.StringWriter;
import
import
import
import
import
import
import
import
javax.xml.parsers.*;
javax.xml.transform.OutputKeys;
javax.xml.transform.Transformer;
javax.xml.transform.TransformerFactory;
javax.xml.transform.dom.DOMSource;
javax.xml.transform.stream.StreamResult;
org.w3c.dom.*;
org.xml.sax.InputSource;
import
import
import
import
import
import
import
import
org.apache.http.Header;
org.apache.http.HttpEntity;
org.apache.http.HttpHost;
org.apache.http.HttpResponse;
org.apache.http.client.methods.HttpGet;
org.apache.http.client.methods.HttpPost;
org.apache.http.entity.StringEntity;
org.apache.http.impl.client.DefaultHttpClient;
public class RayLight {
static HttpHost _target ;
public static void main(String[] args) {
String sUserName = "Administrator";
String sPassword = "MDP";
String sAuth = "secEnterprise";
String sServer = "Win2003EEx64";
String sPort = "6405";
// creation of an http host
// specify the host, protocol, and port
_target= new HttpHost(sServer, Integer.parseInt(sPort),"http");
try {
String sLogonToken = logonCMS(sUserName, sPassword, sAuth);
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
12
Programming RESTful SDK in Java
with Eclipse - Getting Started
logoffCMS(sLogonToken);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Document loadXMLFromString(String xml) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}
public static Document renseigneNode(Document myXmlDoc, String sItemName, String
sTextContent) {
NodeList nl = myXmlDoc.getElementsByTagName("attr");
for (int i=0;i<nl.getLength();i++) {
Node no = nl.item(i);
if (sItemName.equals(no.getAttributes().getNamedItem("name").getNodeValue()))
{
no.setTextContent(sTextContent);
}
}
return myXmlDoc;
}
public static String recupereStringInputStream (InputStream inputStream) throws Exception{
byte[] buffer = new byte[1024];
String sFinal="";
int bytesRead = 0;
BufferedInputStream bis = new BufferedInputStream(inputStream);
while ((bytesRead = bis.read(buffer)) != -1) {
sFinal = sFinal + new String(buffer, 0, bytesRead);
}
return sFinal;
}
public static String logonCMS (String sUserName, String sPassword, String sAuth) throws
Exception {
final DefaultHttpClient httpClient = new DefaultHttpClient();
try {
Document xmlResponse =null;
//
// send get and retrieve entity
//
HttpGet httpGetRequest = new HttpGet("/biprws/logon/long");
System.out.println("executing request to " + _target);
HttpResponse httpResponse = httpClient.execute(_target, httpGetRequest);
HttpEntity entity = httpResponse.getEntity();
// If the response does not enclose an entity, there is no need
// to bother about connection release
String sFinal="";
if (entity != null) {
InputStream inputStream = entity.getContent();
sFinal = recupereStringInputStream(inputStream);
//
// modify the xml for the post
//
try {
xmlResponse = loadXMLFromString(sFinal);
renseigneNode(xmlResponse, "userName", sUserName);
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
13
Programming RESTful SDK in Java
with Eclipse - Getting Started
renseigneNode(xmlResponse, "password", sPassword);
renseigneNode(xmlResponse, "auth", sAuth);
} catch (Exception e) {
e.printStackTrace();
}
//
// post preparation
//
HttpPost httpPostRequest = new HttpPost("/biprws/logon/long");
//
// transforms xml in String
//
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(xmlResponse), new
StreamResult(writer));
String s = writer.getBuffer().toString().replaceAll("\n|\r", "");
//
// put the xml inentity
//
StringEntity myEntity = new StringEntity(s);
//
// send post
//
myEntity.setContentType("application/xml");
httpPostRequest.setEntity(myEntity);
httpResponse = httpClient.execute(_target, httpPostRequest);
Header hLogonToken = httpResponse.getFirstHeader("X-SAP-LogonToken");
String sReturn = hLogonToken.getValue();
return sReturn;
}
} catch (Exception e) {
// thrown by entity.getContent();
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
return "";
}
public static void logoffCMS(String sLogonToken) throws Exception{
final DefaultHttpClient httpClient = new DefaultHttpClient();
//
// Post preparation
//
try {
HttpPost httpPostRequest = new HttpPost("/biprws/logoff");
httpPostRequest.addHeader("Accept", "application/xml");
httpPostRequest.addHeader("X-SAP-LogonToken", sLogonToken);
StringEntity myEntity = new StringEntity("");
myEntity.setContentType("application/xml");
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
14
Programming RESTful SDK in Java
with Eclipse - Getting Started
httpPostRequest.setEntity(myEntity);
HttpResponse httpResponse = httpClient.execute(_target, httpPostRequest);
} catch (Exception e ) {
System.out.println(e.getStackTrace());
}
finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
}
}
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
15
Programming RESTful SDK in Java
with Eclipse - Getting Started
Copyright
© Copyright 2012 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, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries,
xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture,
POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN,
DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or
registered trademarks of IBM Corporation.
Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated 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.
SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, 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 other countries.
Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business
Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S.A. in
the United States and in other countries. Business Objects is an SAP company.
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.
These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for
informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the
materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such
products and services, if any. Nothing herein should be construed as constituting an additional warranty.
SAP COMMUNITY NETWORK
© 2012 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
16