Java tutorial
/* * Copyright 2007 Pentaho Corporation. All rights reserved. * * This software was developed by Pentaho Corporation and is provided under the terms * of the Mozilla Public License, Version 1.1, or any later version. You may not use * this file except in compliance with the license. If you need a copy of the license, * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho * BI Platform. The Initial Developer is Pentaho Corporation. * * Software distributed under the Mozilla Public License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to * the license for the specific language governing your rights and limitations. * * Created * @author * */ package org.pentaho.supportutility.rest.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.PasswordAuthentication; import java.net.URL; import java.net.URLConnection; import java.util.Properties; import org.apache.commons.codec.binary.Base64; public class RestFulClient { /** * to call restful service to get license and data-source details * * @param server * @param required * @param prop * @param webXml * @return */ public static String restFulService(String server, String required, final Properties prop, String webXml) { String outPut = null; String qualifiedUrl = null; // based on server get restful url if (server.equalsIgnoreCase(RestFulURL.BI_SERVER)) { qualifiedUrl = prop.getProperty(RestFulURL.FULLY_QUALIFIED_BISERVER_URL); } else if (server.equalsIgnoreCase(RestFulURL.DI_SERVER)) { qualifiedUrl = prop.getProperty(RestFulURL.FULLY_QUALIFIED_DISERVER_URL); } else if (server.equalsIgnoreCase(RestFulURL.SPOON_IDE)) { qualifiedUrl = prop.getProperty(RestFulURL.FULLY_QUALIFIED_DISERVER_URL); } URL url = null; try { if (required.equalsIgnoreCase(RestFulURL.LICENSE)) { url = getLicenseUrl(qualifiedUrl); } else if (required.equalsIgnoreCase(RestFulURL.ANALYSIS)) { url = getAnalysisUrl(qualifiedUrl); } else if (required.equalsIgnoreCase(RestFulURL.METADATA)) { url = getMateDataUrl(qualifiedUrl); } else if (required.equalsIgnoreCase(RestFulURL.DSW)) { url = getDSW(qualifiedUrl); } // authenticating the restful service Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(prop.getProperty(RestFulURL.USER_NAME), prop.getProperty(RestFulURL.USER_PASSWORD).toCharArray()); } }); String basicAuth = RestFulURL.BASIC + new String(Base64.encodeBase64((prop.getProperty(RestFulURL.USER_NAME) + RestFulURL.COLON + prop.getProperty(RestFulURL.USER_PASSWORD)).getBytes())); if (url != null) { // calling restful service URLConnection urlConnection = url.openConnection(); HttpURLConnection conn = (HttpURLConnection) urlConnection; urlConnection.setRequestProperty(RestFulURL.AUTHOR, basicAuth); conn.connect(); if (conn.getResponseCode() == 200) { BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); outPut = br.readLine(); } conn.disconnect(); } } catch (MalformedURLException e) { e.getMessage(); } catch (IOException e) { e.getMessage(); } return outPut; } /** * * @param qualifiedUrl * @return * @throws MalformedURLException */ private static URL getLicenseUrl(String qualifiedUrl) throws MalformedURLException { URL url = new URL(qualifiedUrl + RestFulURL.LICENSE_URL); return url; } /** * * @param qualifiedUrl * @return * @throws MalformedURLException */ private static URL getAnalysisUrl(String qualifiedUrl) throws MalformedURLException { URL url = new URL(qualifiedUrl + RestFulURL.DATASOURCE_ANALYSIS_URL); return url; } /** * * @param qualifiedUrl * @return * @throws MalformedURLException */ private static URL getMateDataUrl(String qualifiedUrl) throws MalformedURLException { URL url = new URL(qualifiedUrl + RestFulURL.DATASOURCE_METADATA_URL); return url; } /** * * @param qualifiedUrl * @return * @throws MalformedURLException */ private static URL getDSW(String qualifiedUrl) throws MalformedURLException { URL url = new URL(qualifiedUrl + RestFulURL.DATASOURCE_DSW_URL); return url; } }