use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.
the class StreamsBuildService method getToolkitsURL.
String getToolkitsURL() throws IOException {
if (toolkitsUrl == null) {
// Examples for external build endpoints:
// CPD < 3.5: https://ivan34-cpd-ivan34.apps.cpstreamsx6.cp.fyre.ibm.com/streams-build/instances/sample-streams/ivan34/
// CPD >= 3.5: https://nbgf2-cpd-nbgf2.apps.cpstreamsx8.cp.fyre.ibm.com/streams_build_service/v1/namespaces/nbgf2/instances/sample-streams/builds/
// Examples for internal endpoints:
// CPD < 3.5: serviceBuildEndpoint": "https://build-sample-streams-build.ivan34:8445/streams/rest/builds"
// CPD >= 3.5: serviceBuildEndpoint": "https://build-sample-streams-build.nbgf2:8445/streams/v1/builds"
URL epUrl = new URL(endpoint);
String urlPath;
if (epUrl.getPath().startsWith("/streams-build/")) {
// external URL, CPD < 3.5
// /streams-build/instances/sample-streams/ivan34/
// /streams-build-resource/instances/sample-streams/ivan34/
urlPath = epUrl.getPath().replaceFirst("^/streams-build/", "/streams-build-resource/");
} else if (epUrl.getPath().startsWith("/streams_build_service/v1/")) {
// external URL, CPD >= 3.5
// /streams_build_service/v1/namespaces/nbgf2/instances/sample-streams/builds/
// /streams_build_service/v1/namespaces/nbgf2/instances/sample-streams/roots/
urlPath = epUrl.getPath().replaceFirst("/builds[/]?$", "/roots");
} else if (epUrl.getPath().startsWith("/streams/rest/")) {
// internal URL, CPD < 3.5
// https://build-sample-streams-build.ivan34:8445/streams/rest/builds
// https://build-sample-streams-build.ivan34:8445/streams/rest/resources
urlPath = epUrl.getPath().replaceFirst("/builds[/]?$", "/resources");
} else if (epUrl.getPath().startsWith("/streams/v1/")) {
// internal URL, CPD >= 3.5
// https://build-sample-streams-build.nbgf2:8445/streams/v1/builds
// https://build-sample-streams-build.nbgf2:8445/streams/v1/roots
urlPath = epUrl.getPath().replaceFirst("/builds[/]?$", "/roots");
} else {
// use the path "as-is"
urlPath = epUrl.getPath();
}
String resourcesUrl = (new URL(epUrl.getProtocol(), epUrl.getHost(), epUrl.getPort(), urlPath)).toExternalForm();
// Query the resourcesUrl to find the resources URL
String response = getResponseString(resourcesUrl);
ResourcesArray resources = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().fromJson(response, ResourcesArray.class);
for (Resource resource : resources.resources) {
if (TOOLKITS_RESOURCE_NAME.equals(resource.name)) {
toolkitsUrl = resource.resource;
break;
}
}
if (toolkitsUrl == null) {
// try to find toolkits URL under the application build pool
if (this.poolsEndpoint == null) {
// If we couldn't find toolkits something is wrong
throw new RESTException("Unable to find toolkits resource in application build pool: No buildPools endpoint determined.");
}
// find out the right build pool; we use the first build pool with type 'image'
final String poolType = "application";
List<BuildPool> appBuildPools = BuildPool.createPoolList(this, this.poolsEndpoint, poolType);
if (appBuildPools.size() == 0) {
throw new RESTException("No build pool of '" + poolType + "' type found.");
}
toolkitsUrl = appBuildPools.get(0).getToolkits();
}
if (toolkitsUrl == null) {
// If we couldn't find toolkits something is wrong
throw new RESTException("Unable to find toolkits resource from resources URL: " + resourcesUrl + " or application build pool");
}
}
return toolkitsUrl;
}
use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.
the class StreamsRestUtils method rawStreamingGet.
/**
* Gets an entity in streaming mode.
* @param executor
* @param auth
* @param url
* @param streamConsumer
* @return The return of {@link InputStreamConsumer#getResult()}
* @throws IOException
*/
static <T> T rawStreamingGet(Executor executor, String auth, String url, final InputStreamConsumer<T> streamConsumer) throws IOException {
TRACE.fine("HTTP GET: " + url);
Request request = Request.Get(url).addHeader("accept", ContentType.APPLICATION_JSON.getMimeType()).useExpectContinue().socketTimeout(// throw Exception when we do not read data for more than x millis
STREAMING_GET_SO_TIMEOUT_MILLIS);
if (null != auth) {
request = request.addHeader(AUTH.WWW_AUTH_RESP, auth);
}
Response response = executor.execute(request);
response.handleResponse(new ResponseHandler<InputStreamConsumer<T>>() {
@Override
public InputStreamConsumer<T> handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
StatusLine statusLine = response.getStatusLine();
int rcResponse = statusLine.getStatusCode();
HttpEntity entity = response.getEntity();
if (HttpStatus.SC_OK == rcResponse) {
if (entity != null) {
try (InputStream is = entity.getContent()) {
streamConsumer.consume(is);
}
return streamConsumer;
} else {
throw new ClientProtocolException("Response contains no content");
}
} else {
// all other errors...
String httpError = "HttpStatus is " + rcResponse + " for url " + url;
throw new RESTException(rcResponse, httpError);
}
}
});
return streamConsumer.getResult();
}
use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.
the class StreamsConnectionSample method main.
public static void main(String[] args) throws IOException {
String userName = args[0];
String authToken = args[1];
String url = args[2];
String instanceName = args[3];
/*
* Create the connection to the instance indicated
*/
StreamsConnection sClient = StreamsConnection.createInstance(userName, authToken, url);
/*
* This option is only used to by-pass the certificate certification
*/
if (args.length == 5 && "true".equals(args[4])) {
sClient.allowInsecureHosts(true);
}
try {
System.out.println("Instance: ");
Instance instance = sClient.getInstance(instanceName);
/*
* From the Instance, get a list of jobs
*/
List<Job> jobs = instance.getJobs();
for (Job job : jobs) {
System.out.println("Job: " + job.toString());
/*
* For each job, get a list of operators
*/
List<Operator> operators = job.getOperators();
for (Operator op : operators) {
System.out.println("Operator: " + op.toString());
List<Metric> metrics = op.getMetrics();
/*
* For each operator, you can get a list of metrics, output
* ports and input ports
*/
for (Metric m : metrics) {
System.out.println("Metric: " + m.toString());
}
List<OutputPort> outP = op.getOutputPorts();
for (OutputPort oport : outP) {
System.out.println("Output Port: " + oport.toString());
/*
* For each output port, you can get a list of metrics
*/
for (Metric om : oport.getMetrics()) {
System.out.println("Output Port Metric: " + om.toString());
}
}
List<InputPort> inP = op.getInputPorts();
/*
* For each input port, get a list of metrics
*/
for (InputPort ip : inP) {
System.out.println("Input Port: " + ip.toString());
for (Metric im : ip.getMetrics()) {
System.out.println("Input Port Metric: " + im.toString());
}
}
}
/*
* For each job, get a list of processing elements
*/
for (ProcessingElement pe : job.getPes()) {
System.out.println("ProcessingElement:" + pe.toString());
}
}
try {
/*
* Get a specific job in the instance
*/
instance.getJob("99999");
} catch (RESTException e) {
/*
* This shows what is available in the RESTException should
* something fail
*/
System.out.println("Status Code: " + e.getStatusCode());
System.out.println("Message Id: " + e.getStreamsErrorMessageId());
System.out.println("MessageAsJson: " + e.getStreamsErrorMessageAsJson().toString());
System.out.println("Message: " + e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.
the class StreamsOnlyConnectionTest method testGetInstances.
@Test
public void testGetInstances() throws Exception {
assumeNotNull(System.getenv("STREAMS_REST_URL"));
StreamsConnection connection = StreamsConnection.createInstance(null, null, null);
connection.allowInsecureHosts(true);
// get all instances in the domain
List<Instance> instances = connection.getInstances();
// there should be at least one instance
assertTrue(instances.size() > 0);
Instance i2;
String instanceName = System.getenv("STREAMS_INSTANCE_ID");
if (instanceName != null) {
i2 = connection.getInstance(instanceName);
assertEquals(instanceName, i2.getId());
i2.refresh();
assertEquals(instanceName, i2.getId());
} else {
i2 = instances.get(0);
}
List<ProcessingElement> instancePes = i2.getPes();
for (ProcessingElement pe : instancePes) {
assertNotNull(pe);
}
for (Instance instance : instances) StreamsConnectionTest.checkDomainFromInstance(instance);
try {
// try a fake instance name
connection.getInstance("fakeName");
fail("the connection.getInstance call should have thrown an exception");
} catch (RESTException r) {
// not a failure, this is the expected result
assertEquals(r.toString(), 404, r.getStatusCode());
}
}
Aggregations