Search in sources :

Example 6 with RESTException

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;
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) GsonBuilder(com.google.gson.GsonBuilder) URL(java.net.URL)

Example 7 with RESTException

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();
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) InputStreamConsumer(com.ibm.streamsx.rest.internal.InputStreamConsumer) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) Request(org.apache.http.client.fluent.Request) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) StatusLine(org.apache.http.StatusLine)

Example 8 with RESTException

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();
    }
}
Also used : Operator(com.ibm.streamsx.rest.Operator) OutputPort(com.ibm.streamsx.rest.OutputPort) ProcessingElement(com.ibm.streamsx.rest.ProcessingElement) RESTException(com.ibm.streamsx.rest.RESTException) Instance(com.ibm.streamsx.rest.Instance) InputPort(com.ibm.streamsx.rest.InputPort) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) IOException(java.io.IOException) RESTException(com.ibm.streamsx.rest.RESTException) Metric(com.ibm.streamsx.rest.Metric) Job(com.ibm.streamsx.rest.Job)

Example 9 with RESTException

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());
    }
}
Also used : ProcessingElement(com.ibm.streamsx.rest.ProcessingElement) RESTException(com.ibm.streamsx.rest.RESTException) Instance(com.ibm.streamsx.rest.Instance) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) Test(org.junit.Test)

Aggregations

RESTException (com.ibm.streamsx.rest.RESTException)9 Test (org.junit.Test)4 StreamsConnection (com.ibm.streamsx.rest.StreamsConnection)3 URL (java.net.URL)3 Instance (com.ibm.streamsx.rest.Instance)2 ProcessingElement (com.ibm.streamsx.rest.ProcessingElement)2 Toolkit (com.ibm.streamsx.rest.build.Toolkit)2 IOException (java.io.IOException)2 HttpResponse (org.apache.http.HttpResponse)2 Request (org.apache.http.client.fluent.Request)2 Response (org.apache.http.client.fluent.Response)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 InputPort (com.ibm.streamsx.rest.InputPort)1 Job (com.ibm.streamsx.rest.Job)1 Metric (com.ibm.streamsx.rest.Metric)1 Operator (com.ibm.streamsx.rest.Operator)1 OutputPort (com.ibm.streamsx.rest.OutputPort)1 ICP4DAuthenticator (com.ibm.streamsx.rest.internal.ICP4DAuthenticator)1 InputStreamConsumer (com.ibm.streamsx.rest.internal.InputStreamConsumer)1