You are here

public function CmisConnectionApi::checkConnectionIsAlive in CMIS API 3.0.x

Same name and namespace in other branches
  1. 8.2 src/CmisConnectionApi.php \Drupal\cmis\CmisConnectionApi::checkConnectionIsAlive()

Check CMIS Connection is Alive.

Parameters

string $config_id: Config ID.

bool $is_ajax: Is call is AJAX.

File

src/CmisConnectionApi.php, line 322

Class

CmisConnectionApi
Description of CmisConnectionApi.

Namespace

Drupal\cmis

Code

public function checkConnectionIsAlive($config_id, $is_ajax = FALSE) {
  $tempstore = \Drupal::service('tempstore.private')
    ->get('cmis_alfresco_auth_user');
  if ($tempstore
    ->get('ticket')) {
    $this
      ->setConfig($config_id);
    $this
      ->setHttpInvoker();
    try {
      $this->httpInvoker
        ->request('GET', $this
        ->getConfig()
        ->getCmisUrl());
      return TRUE;
    } catch (RequestException $e) {

      // TODO: It would be better to fire an event. See
      // https://www.drupal.org/project/cmis/issues/3162184.
      $guzzle_request = Message::toString($e
        ->getRequest());
      \Drupal::logger('cmis')
        ->notice($guzzle_request);
      if ($e
        ->getCode() === 401) {
        $guzzle_response = Message::toString($e
          ->getResponse());
        \Drupal::logger('cmis')
          ->notice($guzzle_response);
        if (\Drupal::currentUser()
          ->isAuthenticated()) {
          user_logout();
          if ($is_ajax) {
            $command = new RedirectCommand(Url::fromRoute('user.login')
              ->toString());
            $response = new AjaxResponse();
            $response
              ->addCommand($command);
          }
          else {
            $response = new RedirectResponse(Url::fromRoute('user.login')
              ->toString());
            $response
              ->send();
          }
        }
      }
    } catch (\Exception $e) {
      \Drupal::logger('cmis')
        ->error($e
        ->getMessage());
    }
    return FALSE;
  }
}