/** Licensed Materials - Property of IBM IBM Cognos Products: DOCS (C) Copyright IBM Corp. 2013 US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import com.ibm.json.java.JSONArray; import com.ibm.json.java.JSONObject; /** * This code demonstrates model modifications. It finds an English name of a cube * and creates a pseudo-translated name adding lead-in and lead-out character. */ public class PseudoTranslate { /** * URL to the server with the FMD SDK installed. */ final private static String BI_SERVER_URL = "http://localhost:9300/p2pd/servlet/dispatch/"; /** * The URL part that directs requests to the FMD SDK. */ final private static String BASE_URL = BI_SERVER_URL + "FmCommand/"; /** * */ final private static String LOCALE_FROM = "EN"; /** * */ final private static String LOCALE_TO = "zh"; /** * */ final private static String PSEUDO_TRANSLATE_LEAD_IN = "入口"; /** * */ final private static String PSEUDO_TRANSLATE_LEAD_OUT = "出口"; /** * POJO code that creates HTTP request passing data in JSON format. *
Should the request return a error or a warning, they are written out to the system error stream.
* @param keyword The URL path segment used to identify the request * @param method The HTTP method to use ("GET", "POST", "PUT", "DELETE"). * @param json The data to be passed with the request, might benull
* @param modelObjectId The Id of the model object targeted by this request, may be null
* @return The JSON data returned by the SDK. Might be null.
*/
static protected JSONObject callFMD(String keyword, String method, JSONObject json, String modelObjectId) {
OutputStream os = null;
BufferedReader br = null;
HttpURLConnection conn = null;
try {
URL url = (modelObjectId == null) ? new URL(BASE_URL + keyword) : new URL(BASE_URL + keyword + "/"+modelObjectId);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(method);
// By default, server only allows "GET" and "POST" requests. Use X-header
// to pass other verbs.
if ("GET".equals(method) || "POST".equals(method))
conn.setRequestMethod(method);
else if ("PUT".equals(method) || "DELETE".equals(method)) {
conn.setRequestMethod("POST");
conn.setRequestProperty("X-HTTP-Method-Override", method);
}
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
if (json != null) {
os = conn.getOutputStream();
os.write(json.serialize().getBytes());
os.flush();
}
br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String output;
StringBuffer tmp = new StringBuffer();
while ((output = br.readLine()) != null) {
tmp.append(output);
}
JSONObject result = JSONObject.parse(tmp.toString());
JSONArray errorsArray = (JSONArray) result.get("errors");
if (errorsArray != null)
System.err.println("Errors for request \"" + keyword + "\": " + errorsArray.toString());
JSONArray warningsArray = (JSONArray) result.get("warnings");
if (warningsArray != null)
System.err.println("Warnings for request \"" + keyword + "\": " + warningsArray.toString());
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (json != null)
json.clear();
try {
if (os != null)
os.close();
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (conn != null)
conn.disconnect();
}
return null;
}
/**
* A convenience function wrapping "save model to a stream" request.
* @param file The local file in which to save the stream contents
* @param modelObjectId The Id of the model object targeted by this request
* @return true
if model was saved successfully
*/
static protected boolean saveToFile(File file, String modelObjectId) {
BufferedReader replyReader = null;
HttpURLConnection conn = null;
BufferedWriter fileWriter = null;
try {
URL url = new URL(BASE_URL + "model_save_stream/" + modelObjectId);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "text/plain");
replyReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String output;
fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
while ((output = replyReader.readLine()) != null) {
fileWriter.write(output);
fileWriter.newLine();
}
fileWriter.flush();
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (replyReader != null)
replyReader.close();
if (fileWriter != null)
fileWriter.close();
if (conn != null)
conn.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return false;
}
/**
* A convenience function wrapping "open model to a stream" request.
* @param file The local file from which to read model contents
* @return The model Id, if model was successfully opened. May return null
if
* model opening failed.
*/
static protected String openFile(File file) {
BufferedReader fileReader = null;
BufferedReader replyReader = null;
HttpURLConnection conn = null;
try {
URL url = new URL(BASE_URL + "model_open_stream");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "text/plain");
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String inLine;
while ((inLine = fileReader.readLine()) != null) {
outStream.write(inLine.getBytes("UTF-8"));
}
outStream.flush();
outStream.close();
replyReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String output;
StringBuffer tmp = new StringBuffer();
while ((output = replyReader.readLine()) != null) {
tmp.append(output);
}
JSONObject result = JSONObject.parse(tmp.toString());
JSONArray errorsArray = (JSONArray) result.get("errors");
if (errorsArray != null)
System.err.println("Errors for request \"model_open_stream\": " + errorsArray.toString());
JSONArray warningsArray = (JSONArray) result.get("warnings");
if (warningsArray != null)
System.err.println("Warnings for request \"model_open_stream\": " + warningsArray.toString());
return (String) result.get("id");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null)
fileReader.close();
if (replyReader != null)
replyReader.close();
if (conn != null)
conn.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
/**
* Convenience method creating a pair of a text and locale.
*/
static protected JSONArray localizedText(String text, String locale) {
JSONObject tmp = new JSONObject();
tmp.put("text", text);
tmp.put("locale", locale);
JSONArray nameArray = new JSONArray(1);
nameArray.add(tmp);
return nameArray;
}
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("Please supply arguments: