Search in sources :

Example 1 with RESTException

use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.

the class StreamsOnlyConnectionTest method testBadConnections.

@Test
public void testBadConnections() throws Exception {
    assumeNotNull(System.getenv("STREAMS_REST_URL"));
    URL correctUrl = new URL(System.getenv("STREAMS_REST_URL"));
    URL badUrl = new URL(correctUrl.getProtocol(), correctUrl.getHost(), correctUrl.getPort(), "/streams/re");
    Set<Integer> okErrs = new HashSet<>();
    okErrs.add(404);
    okErrs.add(405);
    // send in wrong url
    StreamsConnection badConn = StreamsConnection.createInstance(null, null, badUrl.toExternalForm());
    badConn.allowInsecureHosts(true);
    try {
        badConn.getInstances();
    } catch (RESTException r) {
        assertTrue(r.toString(), okErrs.contains(r.getStatusCode()));
    }
    // send in url too long
    badUrl = new URL(correctUrl.getProtocol(), correctUrl.getHost(), correctUrl.getPort(), "/streams/rest/resourcesTooLong");
    badConn = StreamsConnection.createInstance(null, null, badUrl.toExternalForm());
    badConn.allowInsecureHosts(true);
    try {
        badConn.getInstances();
    } catch (RESTException r) {
        assertTrue(r.toString(), okErrs.contains(r.getStatusCode()));
    }
    // send in bad iName
    badConn = StreamsConnection.createInstance("fakeName", null, correctUrl.toExternalForm());
    badConn.allowInsecureHosts(true);
    try {
        badConn.getInstances();
    } catch (RESTException r) {
        assertEquals(r.toString(), 401, r.getStatusCode());
    }
    // send in wrong password
    badConn = StreamsConnection.createInstance(null, "badPassword", correctUrl.toExternalForm());
    badConn.allowInsecureHosts(true);
    try {
        badConn.getInstances();
    } catch (RESTException r) {
        assertEquals(r.toString(), 401, r.getStatusCode());
    }
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) URL(java.net.URL) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with RESTException

use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.

the class RestUtils method getResponseString.

/**
 * Gets a response to an HTTP GET call as a string
 *
 * @param executor HTTP client executor to use for call
 * @param auth Authentication header contents, or null
 * @param inputString REST call to make, i.e. the URI
 * @return response from the inputString
 * @throws IOException
 *
 * TODO: unify error handling between this and gsonFromResponse(), and
 * convert callers that want JSON to getGsonResponse()
 */
static String getResponseString(Executor executor, String auth, String inputString) throws IOException {
    TRACE.fine("HTTP GET: " + inputString);
    String sReturn = "";
    Request request = Request.Get(inputString).addHeader("accept", ContentType.APPLICATION_JSON.getMimeType()).useExpectContinue();
    if (null != auth) {
        request = request.addHeader(AUTH.WWW_AUTH_RESP, auth);
    }
    Response response = executor.execute(request);
    HttpResponse hResponse = response.returnResponse();
    int rcResponse = hResponse.getStatusLine().getStatusCode();
    if (HttpStatus.SC_OK == rcResponse) {
        sReturn = EntityUtils.toString(hResponse.getEntity());
    } else if (HttpStatus.SC_NOT_FOUND == rcResponse) {
        // with a 404 message, we are likely to have a message from Streams
        // but if not, provide a better message
        sReturn = EntityUtils.toString(hResponse.getEntity());
        if (sReturn != null && !sReturn.isEmpty()) {
            throw RESTException.create(rcResponse, sReturn + " for url " + inputString);
        } else {
            String httpError = "HttpStatus is " + rcResponse + " for url " + inputString;
            throw new RESTException(rcResponse, httpError);
        }
    } else {
        // all other errors...
        String httpError = "HttpStatus is " + rcResponse + " for url " + inputString;
        throw new RESTException(rcResponse, httpError);
    }
    TRACE.finest(rcResponse + ": " + sReturn);
    return sReturn;
}
Also used : HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) RESTException(com.ibm.streamsx.rest.RESTException) Request(org.apache.http.client.fluent.Request) HttpResponse(org.apache.http.HttpResponse)

Example 3 with RESTException

use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.

the class ToolkitAPITest method testDeleteToolkit.

@Test
public void testDeleteToolkit() throws Exception {
    Toolkit bingo = connection.uploadToolkit(bingo0Path);
    assertNotNull(bingo);
    assertEquals(bingo.getName(), bingoToolkitName);
    assertEquals(bingo.getVersion(), bingo0Version);
    waitForToolkit(bingo.getName(), Optional.of(bingo.getVersion()));
    // deleting once should succeed.
    assertTrue(bingo.delete());
    // Verify that it has been removed
    assertToolkitNotExists(bingoToolkitName);
    // deleting again should fail.
    assertFalse(bingo.delete());
    // RESTException if we try to get index on a deleted toolkit.
    try {
        bingo.getIndex();
        fail("Expected RESTException");
    } catch (RESTException e) {
    }
    // Post it again, then find it in the list of toolkits and delete
    // it from the Toolkit object from the list.
    bingo = connection.uploadToolkit(bingo0Path);
    assertNotNull(bingo);
    assertEquals(bingo.getName(), bingoToolkitName);
    assertEquals(bingo.getVersion(), bingo0Version);
    List<Toolkit> foundToolkits = findMatchingToolkits(bingoToolkitName, Optional.of(bingo0Version));
    assertEquals(foundToolkits.size(), 1);
    assertTrue(foundToolkits.get(0).delete());
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) Toolkit(com.ibm.streamsx.rest.build.Toolkit) Test(org.junit.Test)

Example 4 with RESTException

use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.

the class ToolkitAPITest method testGetTookit.

// Test getting a toolkit by id.
@Test
public void testGetTookit() throws Exception {
    Toolkit bingo = connection.uploadToolkit(bingo1Path);
    assertNotNull(bingo);
    waitForToolkit(bingoToolkitName, Optional.of(bingo1Version));
    Toolkit found = connection.getToolkit(bingo.getId());
    assertNotNull(found);
    assertEquals(bingoToolkitName, found.getName());
    assertEquals(bingo1Version, found.getVersion());
    assertEquals("4.2", found.getRequiredProductVersion());
    assertEquals("toolkit", found.getResourceType());
    // We don't know what value this attribute will have, but it should
    // have a value
    assertNotNull(found.getPath());
    // The ID is 'streams-toolkits'/name-version
    String toolkitId = "streams-toolkits/" + bingoToolkitName + "-" + bingo1Version;
    found = connection.getToolkit(toolkitId);
    assertNotNull(found);
    // Using just the name fails
    toolkitId = "streams-toolkits/" + bingoToolkitName;
    try {
        found = connection.getToolkit(toolkitId);
        fail("Expected RESTException");
    } catch (RESTException e) {
    }
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) Toolkit(com.ibm.streamsx.rest.build.Toolkit) Test(org.junit.Test)

Example 5 with RESTException

use of com.ibm.streamsx.rest.RESTException in project streamsx.topology by IBMStreams.

the class BuildService method ofEndpoint.

public static BuildService ofEndpoint(String endpoint, String name, String userName, String password, boolean verify) throws IOException {
    if (name == null && System.getenv(Util.STREAMS_INSTANCE_ID) == null) {
        // StandaloneAuthenticator, needs resources endpoint.
        if (endpoint == null) {
            endpoint = Util.getenv(Util.STREAMS_BUILD_URL);
        }
        URL url = new URL(endpoint);
        // TODO: standalone: Not tested
        String path;
        if (url.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
            path = url.getPath().replaceFirst("/builds[/]?$", "/resources");
        } else if (url.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
            path = url.getPath().replaceFirst("/builds[/]?$", "/roots");
        } else {
            throw new RESTException("build endpoint '" + endpoint + "' cannot be transformed to build resources URL");
        }
        URL resourcesUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
        String resourcesEndpoint = resourcesUrl.toExternalForm();
        StandaloneAuthenticator auth = StandaloneAuthenticator.of(resourcesEndpoint, userName, password);
        JsonObject serviceDefinition = auth.config(verify);
        if (serviceDefinition == null) {
            // user and password are required, and endpoint is builds path
            if (userName == null || password == null) {
                String[] values = Util.getDefaultUserPassword(userName, password);
                userName = values[0];
                password = values[1];
            }
            String basicAuth = RestUtils.createBasicAuth(userName, password);
            String buildsEndpoint = endpoint;
            if (!buildsEndpoint.endsWith(STREAMS_BUILD_PATH)) {
                URL buildUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), STREAMS_BUILD_PATH);
                buildsEndpoint = buildUrl.toExternalForm();
            }
            return StreamsBuildService.of(e -> basicAuth, buildsEndpoint, verify);
        }
        return StreamsBuildService.of(auth, serviceDefinition, verify);
    } else {
        ICP4DAuthenticator auth = ICP4DAuthenticator.of(endpoint, name, userName, password);
        return StreamsBuildService.of(auth, auth.config(verify), verify);
    }
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) ICP4DAuthenticator(com.ibm.streamsx.rest.internal.ICP4DAuthenticator) JsonObject(com.google.gson.JsonObject) URL(java.net.URL) StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator)

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