/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2016,2017
*/
package com.ibm.streamsx.topology.internal.context;
import static com.ibm.streamsx.topology.context.AnalyticsServiceProperties.SERVICE_NAME;
import static com.ibm.streamsx.topology.context.AnalyticsServiceProperties.VCAP_SERVICES;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import com.ibm.json.java.JSONObject;
public class Contexts {
static void preBundle(Map<String, Object> config) {
if (!config.containsKey(SERVICE_NAME))
throw new IllegalStateException("Service name is not defined, please set property: " + SERVICE_NAME);
if (!config.containsKey(VCAP_SERVICES)) {
throw new IllegalStateException("VCAP services are not defined, please set property: " + VCAP_SERVICES);
}
}
static JSONObject getJsonResponse(CloseableHttpClient httpClient,
HttpRequestBase request) throws IOException, ClientProtocolException {
request.addHeader("accept",
ContentType.APPLICATION_JSON.getMimeType());
CloseableHttpResponse response = httpClient.execute(request);
JSONObject jsonResponse;
try {
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
final String errorInfo;
if (entity != null)
errorInfo = " -- " + EntityUtils.toString(entity);
else
errorInfo = "";
throw new IllegalStateException(
"Unexpected HTTP resource from service:"
+ response.getStatusLine().getStatusCode() + ":" +
response.getStatusLine().getReasonPhrase() + errorInfo);
}
if (entity == null)
throw new IllegalStateException("No HTTP resource from service");
jsonResponse = JSONObject.parse(new BufferedInputStream(entity
.getContent()));
EntityUtils.consume(entity);
} finally {
response.close();
}
return jsonResponse;
}
public static Map<String, Object> jsonDeployToMap(JSONObject deploy) {
Map<String, Object> config = new HashMap<>();
if(deploy.containsKey(VCAP_SERVICES))
config.put(VCAP_SERVICES, deploy.get(VCAP_SERVICES));
if(deploy.containsKey(SERVICE_NAME))
config.put(SERVICE_NAME, deploy.get(SERVICE_NAME));
return config;
}
}