6 Replies Latest reply on Jun 29, 2016 4:17 PM by Steven Hawkins

    Using a secure REST endpoint (Cookie based auth) for restservice-as-a-datasource from Teiid-embedded (9.0.0CR2)

    John Rodrigues Novice

      Based on your very informative example: https://github.com/teiid/teiid-embedded-examples/tree/master/restservice-as-a-datasource

      I wanted to know if you have an example of accessing a secure REST endpoint where you call a logon method for authentication,

      get a Cookie and then use that cookie for calling the REST endpoint.

       

      I have included sample code below on how this is done using Apache HttpClient in Java.

       

      // Logon and Setup the Cookie store

          private CookieStore establishAdfSession() throws IOException {

              if (cookieStore == null) {

                  cookieStore = new BasicCookieStore();

                  HttpClient httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();

                  HttpPost httpPost = new HttpPost(String.format("%s/login", appConfig.getAdfcoreServer()));

                  httpPost.addHeader("j_username", username);

                  httpPost.addHeader("j_password", password);

                  httpClient.execute(httpPost);

              }

              return cookieStore;

          }

       

      // This method accesses secure endpoint : http://localhost:9080/adfcore/artifacts/53

      // The cookie has been set in httpClient built by establishAdfSession

          public Map<String, Object> fetchArtifact(Integer artifactId) throws IOException {

              final CookieStore cookieStore = establishAdfSession();

              HttpClient httpClient =  HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();

              HttpGet httpGet = new HttpGet(String.format("%s/artifacts/%d", appConfig.getAdfcoreServer(), artifactId));

              HttpResponse httpResponse = httpClient.execute(httpGet);

              HttpEntity entity = httpResponse.getEntity();

              return extractResponse(entity, Map.class);

          }

       

      How does this translate to

        FROM

        (EXEC CustomerSource.invokeHttp('GET', null, 'http://localhost:8080/customer/customerList', 'TRUE')) AS f

      in your example? Could you please point me to documentation/examples of this?