forked from Islandora/tuque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryConnection.php
229 lines (212 loc) · 6.6 KB
/
RepositoryConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* @file
* This file contains the implementation of a connection to Fedora. And the
* interface for a repository configuration.
*/
require_once 'HttpConnection.php';
require_once 'RepositoryException.php';
/**
* The general interface for a RepositoryConfig object.
*/
interface RepositoryConfigInterface {
/**
* Simple constructor defintion for the repository
*/
function __construct($url, $username, $password);
}
/**
* Specific RepositoryConfig implementation that extends the CurlConnection
* class so that we can do specific processing on Curl requests for Fedora.
* This also wraps the exceptions thrown by Curl, so that we keep our exception
* encapsulation.
*
* It also doesn't take urls but instead uses relative paths that it adds the
* fedora URL to. This makes is a bit easier to use. It also makes sure that
* we always send usernames and passwords.
*/
class RepositoryConnection extends CurlConnection implements RepositoryConfigInterface {
public $url;
public $username;
public $password;
/**
* This constructor for the RepositoryConnection.
*
* @param string $url
* The URL to Fedora.
* @param string $username
* The username to connect with.
* @param string $password
* The password to connect with.
*/
function __construct($url = 'http://localhost:8080/fedora', $username = NULL, $password = NULL) {
// Make sure the url doesn't have a trailing slash.
$this->url = rtrim($url, "/");
$this->username = $username;
$this->password = $password;
try {
parent::__construct();
}
catch (HttpConnectionException $e) {
throw new RepositoryException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* This private function makes a fedora URL from a noraml URL.
*/
protected function buildUrl($url) {
$url = ltrim($url, "/");
return "{$this->url}/$url";
}
/**
* These functions are used a lot when connecting to Fedora to create the
* correct arguements for REST calls. This will encode and add an array
* of arguements to a request URL.
*
* @param string $request
* The request that is being built.
* @param character $seperator
* This is a helper to make sure that the first arguement gets a ? and the
* rest of them get a &.
* @param array $params
* An array of parameters.
* @param string $name
* The name of the parameter that we are adding.
*/
public function addParamArray(&$request, &$seperator, $params, $name) {
if (is_array($params)) {
if (array_key_exists($name, $params)) {
$this->addParam($request, $seperator, $name, $params[$name]);
}
}
}
/**
* This function adds a specific parameter to a RESTful request. It makes
* sure that PHP booleans are changes into true and false and that the
* parameters are properly URL encoded.
*
* @param string $request
* The request that is being built.
* @param character $seperator
* This is a helper to make sure that the first arguement gets a ? and the
* rest of them get a &.
* @param string $name
* The name of the parameter that is being added
* @param string $value
* the value of hte parameter.
*/
public function addParam(&$request, &$seperator, $name, $value) {
if ($value !== NULL) {
if (is_bool($value)) {
$parameter = $value ? 'true' : 'false';
}
else {
$parameter = urlencode($value);
}
$request .= "{$seperator}{$name}={$parameter}";
$seperator = '&';
}
}
/**
* Do a get request.
*
* @param string $url
* The URL relative to the fedora path to use.
* @param string $headers_only
* Returns only the headers
* @param string $file
* The filename to output the request to. If this is set then no headers
* will be returned.
*
* @return array
* The contents of the get request
*
* @see CurlConnection::getRequest()
*/
public function getRequest($url, $headers_only = false, $file = NULL) {
try {
return parent::getRequest($this->buildUrl($url), $headers_only, $file);
}
catch (HttpConnectionException $e) {
$this->parseFedoraExceptions($e);
}
}
/**
* @see CurlConnection::postRequest()
*/
public function postRequest($url, $type = 'none', $data = NULL, $content_type = NULL) {
try {
return parent::postRequest($this->buildUrl($url), $type, $data, $content_type);
}
catch (HttpConnectionException $e) {
$this->parseFedoraExceptions($e);
}
}
/**
* @see CurlConnection::patchRequest()
*/
public function patchRequest($url, $type = 'none', $data = NULL, $content_type = NULL) {
try {
return parent::patchRequest($this->buildUrl($url), $type, $data, $content_type);
}
catch (HttpConnectionException $e) {
$this->parseFedoraExceptions($e);
}
}
/**
* @see CurlConnection::putRequest()
*/
public function putRequest($url, $type = 'none', $file = NULL) {
try {
return parent::putRequest($this->buildUrl($url), $type, $file);
}
catch (HttpConnectionException $e) {
$this->parseFedoraExceptions($e);
}
}
/**
* @see CurlConnection::deleteRequest()
*/
public function deleteRequest($url) {
try {
return parent::deleteRequest($this->buildUrl($url));
}
catch (HttpConnectionException $e) {
$this->parseFedoraExceptions($e);
}
}
/**
* This function attempts to parse the exceptions that are recieved from
* Fedora into something more reasonable. This it very hard since really
* things like this are garbage in garbage out, but we do what we can.
*
* @param Exception $e
* The exception being parsed
*/
protected function parseFedoraExceptions($e) {
$code = $e->getCode();
switch ($code) {
case '400':
// When setting an error 400 often Fedora puts useful error messages
// in the message body, we might as well expose them.
$response = $e->getResponse();
$message = $response['content'];
if (!$message || strpos($message, 'Exception') !== FALSE) {
$message = $e->getMessage();
}
break;
case '500':
// When setting an error 500 Fedora is usually returning a java stack
// trace. This isn't great, but we can give a better message by return
// the message set in that exception .
$response = $e->getResponse();
$message = preg_split('/$\R?^/m', $response['content']);
$message = $message[0];
break;
default:
$message = $e->getMessage();
break;
}
throw new RepositoryException($message, $code, $e);
}
}