Allow username and password in requests to Tika server (#35)

This commit is contained in:
Jake Dale Ovenden 2017-11-23 10:24:32 +13:00 committed by Robbie Averill
parent 40ba6a245d
commit eb7a45865b

View File

@ -5,6 +5,29 @@ use Guzzle\Http\Exception\RequestException;
class TikaRestClient extends Client class TikaRestClient extends Client
{ {
/**
* Authentication options to be sent to the Tika server
*
* @var array
*/
protected $options = array('username' => null, 'password' => null);
/**
* @var array
*/
protected $mimes = array();
public function __construct($baseUrl = '', $config = null)
{
if (defined('SS_TIKA_USERNAME') && defined('SS_TIKA_PASSWORD')) {
$this->options = array(
'username' => SS_TIKA_USERNAME,
'password' => SS_TIKA_PASSWORD,
);
}
parent::__construct($baseUrl, $config);
}
/** /**
* Detect if the service is available * Detect if the service is available
* *
@ -13,10 +36,14 @@ class TikaRestClient extends Client
public function isAvailable() public function isAvailable()
{ {
try { try {
return $this $result = $this->get(null);
->get()->send() $result->setAuth($this->options['username'], $this->options['password']);
->getStatusCode() == 200; $result->send();
if ($result->getResponse()->getStatusCode() == 200) {
return true;
}
} catch (RequestException $ex) { } catch (RequestException $ex) {
SS_Log::log(sprintf("Tika unavailable - %s", $ex->getMessage()), SS_Log::ERR);
return false; return false;
} }
} }
@ -28,19 +55,19 @@ class TikaRestClient extends Client
*/ */
public function getVersion() public function getVersion()
{ {
$response = $this->get('version')->send(); $response = $this->get('version');
$response->setAuth($this->options['username'], $this->options['password']);
$response->send();
$version = 0.0;
// Parse output // Parse output
if ($response->getStatusCode() == 200 && if ($response->getResponse()->getStatusCode() == 200 &&
preg_match('/Apache Tika (?<version>[\.\d]+)/', $response->getBody(), $matches) preg_match('/Apache Tika (?<version>[\.\d]+)/', $response->getResponse()->getBody(), $matches)
) { ) {
return (float)$matches['version']; $version = (float)$matches['version'];
} }
return $version;
return 0.0;
} }
protected $mimes = array();
/** /**
* Gets supported mime data. May include aliased mime types. * Gets supported mime data. May include aliased mime types.
* *
@ -51,13 +78,13 @@ class TikaRestClient extends Client
if ($this->mimes) { if ($this->mimes) {
return $this->mimes; return $this->mimes;
} }
$response = $this->get( $response = $this->get(
'mime-types', 'mime-types',
array('Accept' => 'application/json') array('Accept' => 'application/json')
)->send(); );
$response->setAuth($this->options['username'], $this->options['password']);
return $this->mimes = $response->json(); $response->send();
return $this->mimes = $response->getResponse()->json();
} }
/** /**
@ -75,8 +102,10 @@ class TikaRestClient extends Client
'tika', 'tika',
array('Accept' => 'text/plain'), array('Accept' => 'text/plain'),
file_get_contents($file) file_get_contents($file)
)->send(); );
$text = $response->getBody(true); $response->setAuth($this->options['username'], $this->options['password']);
$response->send();
$text = $response->getResponse()->getBody(true);
} catch (RequestException $e) { } catch (RequestException $e) {
$msg = sprintf( $msg = sprintf(
'TikaRestClient was not able to process %s. Response: %s %s.', 'TikaRestClient was not able to process %s. Response: %s %s.',
@ -84,16 +113,13 @@ class TikaRestClient extends Client
$e->getResponse()->getStatusCode(), $e->getResponse()->getStatusCode(),
$e->getResponse()->getReasonPhrase() $e->getResponse()->getReasonPhrase()
); );
// Only available if tika-server was started with --includeStack // Only available if tika-server was started with --includeStack
$body = $e->getResponse()->getBody(true); $body = $e->getResponse()->getBody(true);
if ($body) { if ($body) {
$msg .= ' Body: ' . $body; $msg .= ' Body: ' . $body;
} }
SS_Log::log($msg, SS_Log::NOTICE); SS_Log::log($msg, SS_Log::NOTICE);
} }
return $text; return $text;
} }
} }