Problem
You would like to use REST API to upload a Mondrian Schema to the BI Server.
(The bellow solution was tested with Pentaho 6.0.1.0 Version)
Dependencies
For the below code you will need two external dependencies: one for sending HTTP requests and the other one for encrypting your credentials for Basic Authentication.
1 2 3 4 5 6 |
dependencies { compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1' compile 'commons-codec:commons-codec:1.10' } |
Code
The code is pretty much self-explanatory, but here are some highlights:
- We encode our credentials to the server with Base64 for the Basic Authentication.
- Create a request against BI Server’s endpoint for uploading Mondrian Schemas.
- We add the basic authentication header to the request.
- In the request we send a multipart value with our Mondrian Schema alongside few addtional parameters (explained in the official documentation).
- Send the request and receive a response.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class Application { public static void main(String[] args) { try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("http://serverUrl:8080/pentaho/plugin/data-access/api/datasource/analysis/import") .addHeader("Authorization", "Basic " + encodeCredentials()) .put(RequestBody.create(MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundaryNLNb246RTFIn1elY"), getData())) .build(); Response response = client.newCall(request).execute(); } catch (IOException e) { e.printStackTrace(); } } private static String encodeCredentials() { String usernameAndPassword = "userName:password"; byte[] encoded = Base64.encodeBase64(usernameAndPassword.getBytes()); return new String(encoded); } private static String getData() { return new String(Files.readAllBytes(Paths.get("C:\\tmp\\multipart-data.xml"))); } |
Multipart-data.xml
Last but not least the file with multi-part data that among others contains a Mondrian Schema that we wish to upload to the server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
------WebKitFormBoundaryNLNb246RTFIn1elY Content-Disposition: form-data; name="uploadInput"; filename="Example.xml" Content-Type: text/xml <Schema name="Example"> <!-- Your schema definition goes here --> </Schema> ------WebKitFormBoundaryNLNb246RTFIn1elY Content-Disposition: form-data; name="parameters" DataSource=datasourceInBi;overwrite=true ------WebKitFormBoundaryNLNb246RTFIn1elY Content-Disposition: form-data; name="schemaFileInfo" Example.xml ------WebKitFormBoundaryNLNb246RTFIn1elY Content-Disposition: form-data; name="catalogName" Example ------WebKitFormBoundaryNLNb246RTFIn1elY Content-Disposition: form-data; name="xmlaEnabledFlag" true ------WebKitFormBoundaryNLNb246RTFIn1elY-- |