* @return The result status.
*/
@Override
public Status sendRequest(Request request) {
Status result = null;
Representation entity = request.isEntityAvailable() ? request
.getEntity() : null;
// Get the connector service to callback
org.restlet.service.ConnectorService connectorService = ConnectorHelper
.getConnectorService();
if (connectorService != null) {
connectorService.beforeSend(entity);
}
try {
try {
// Set the request headers
List<MessageHeader> headers = new CopyOnWriteArrayList<SdcFrame.MessageHeader>();
for (Parameter header : getRequestHeaders()) {
if (!header.getName().equals(
HeaderConstants.HEADER_CONTENT_LENGTH)
&& !header.getName().equals(
HeaderConstants.HEADER_PROXY_AUTHORIZATION)) {
headers.add(MessageHeader.newBuilder()
.setKey(header.getName())
.setValue(header.getValue()).build());
}
}
if (!Method.GET.equals(request.getMethod())) {
headers.add(MessageHeader.newBuilder()
.setKey("x-sdc-http-method")
.setValue(request.getMethod().getName()).build());
}
if (entity != null) {
OutputStream requestStream = getRequestEntityStream();
if (requestStream != null) {
entity.write(requestStream);
requestStream.flush();
requestStream.close();
// Build the fetch request
setFetchRequest(FetchRequest
.newBuilder()
.setId(UUID.randomUUID().toString())
.setResource(
request.getResourceRef().toString())
.setStrategy("HTTPClient")
.addAllHeaders(headers)
.setContents(
ByteString
.copyFrom(this.requestEntityStream
.toByteArray()))
.build());
}
} else {
// Build the fetch request
setFetchRequest(FetchRequest.newBuilder()
.setId(UUID.randomUUID().toString())
.setResource(request.getResourceRef().toString())
.setStrategy("HTTPClient").addAllHeaders(headers)
.build());
}
getConnection().sendRequest(this);
// Block the thread until we receive the response or a
// timeout occurs
if (!getLatch()
.await(IoUtils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
// Timeout detected
result = new Status(Status.CONNECTOR_ERROR_INTERNAL,
"The calling thread timed out while waiting for a response to unblock it.");
} else {
result = Status.valueOf(getFetchReply().getStatus());
}
} catch (Exception e) {
getHelper()
.getLogger()
.log(Level.FINE,
"An unexpected error occurred during the sending of the HTTP request.",
e);
result = new Status(Status.CONNECTOR_ERROR_INTERNAL, e);
}
// Now we can access the status code, this MUST happen after closing
// any open request stream.
result = new Status(getStatusCode(), null, getReasonPhrase(), null);
} catch (IOException ioe) {
getHelper()
.getLogger()
.log(Level.FINE,
"An error occured during the communication with the remote HTTP server.",
ioe);
result = new Status(Status.CONNECTOR_ERROR_COMMUNICATION, ioe);
} finally {
if (entity != null) {
entity.release();
}
// Call-back after writing
if (connectorService != null) {
connectorService.afterSend(entity);