Class: T2Server::HttpConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/t2-server/net/connection.rb

Overview

A class representing a http connection to a Taverna Server. This class should only ever be created via the T2Server::Connection factory class.

Direct Known Subclasses

HttpsConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, params = nil) ⇒ HttpConnection

Open a http connection to the Taverna Server at the uri supplied.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/t2-server/net/connection.rb', line 92

def initialize(uri, params = nil)
  @uri = uri
  @params = params || DefaultConnectionParameters.new

  # Open a persistent HTTP connection.
  @http = Net::HTTP::Persistent.new("Taverna_Server_Ruby_Client")

  # Set timeouts if specified.
  @http.open_timeout = @params[:open_timeout] if @params[:open_timeout]
  @http.read_timeout = @params[:read_timeout] if @params[:read_timeout]
end

Instance Attribute Details

#uriObject (readonly)

The URI of this connection instance.



89
90
91
# File 'lib/t2-server/net/connection.rb', line 89

def uri
  @uri
end

Instance Method Details

#DELETE(uri, credentials) ⇒ Object

:call-seq:

DELETE(uri, credentials) -> true or false

Perform an HTTP DELETE on a uri on the server. If successful true is returned.



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/t2-server/net/connection.rb', line 204

def DELETE(uri, credentials)
  delete = Net::HTTP::Delete.new(uri.path)

  response = submit(delete, uri, credentials)

  case response
  when Net::HTTPNoContent
    # Success, carry on...
    true
  else
    report_error("DELETE", uri.path, response, credentials)
  end
end

#GET(uri, type, range, credentials, &block) ⇒ Object

:call-seq:

GET(uri, type, range, credentials) -> string

HTTP GET a resource at uri of type from the server. If successful the body of the response is returned. A portion of the data can be retrieved by specifying a byte range, start..end, with the range parameter.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/t2-server/net/connection.rb', line 111

def GET(uri, type, range, credentials, &block)
  get = Net::HTTP::Get.new(uri.path)
  get["Accept"] = type
  get["Range"] = "bytes=#{range.min}-#{range.max}" unless range.nil?

  response = submit(get, uri, credentials, &block)

  case response
  when Net::HTTPOK, Net::HTTPPartialContent
    return response.body
  when Net::HTTPNoContent
    return nil
  when Net::HTTPMovedTemporarily
    new_conn = redirect(response["location"])
    raise ConnectionRedirectError.new(new_conn)
  else
    report_error("GET", uri.path, response, credentials)
  end
end

#OPTIONS(uri, credentials) ⇒ Object

:call-seq:

OPTIONS(uri, credentials) -> hash

Perform the HTTP OPTIONS command on the given uri and return a hash of the headers returned.



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/t2-server/net/connection.rb', line 223

def OPTIONS(uri, credentials)
  options = Net::HTTP::Options.new(uri.path)

  response = submit(options, uri, credentials)

  case response
  when Net::HTTPOK
    response.to_hash
  else
    report_error("OPTIONS", uri.path, response, credentials)
  end
end

#POST(uri, data, type, credentials) ⇒ Object

:call-seq:

POST(uri, value, type, credentials) -> URI
POST(uri, stream, type, credentials) -> URI

Upload data via HTTP POST. Data may be specified as a value or as a stream. The stream can be any object that has a read(length) method; instances of File or IO, for example.

If successful the URI of the uploaded resource is returned.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/t2-server/net/connection.rb', line 180

def POST(uri, data, type, credentials)
  post = Net::HTTP::Post.new(uri.path)
  post.content_type = type

  set_upload_body(post, data)

  response = submit(post, uri, credentials)

  case response
  when Net::HTTPCreated
    # return the URI of the newly created item
    URI.parse(response['location'])
  when Net::HTTPServiceUnavailable
    raise ServerAtCapacityError.new
  else
    report_error("POST", uri.path, response, credentials)
  end
end

#PUT(uri, data, type, credentials) ⇒ Object

:call-seq:

PUT(uri, value, type, credentials) -> true or false
PUT(uri, value, type, credentials) -> URI
PUT(uri, stream, type, credentials) -> URI

Upload data via HTTP PUT. Data may be specified as a value or as a stream. The stream can be any object that has a read(length) method; instances of File or IO, for example.

If successful true or a URI to the uploaded resource is returned depending on whether the operation has altered a parameter (true) or uploaded new data (URI).



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
# File 'lib/t2-server/net/connection.rb', line 143

def PUT(uri, data, type, credentials)
  put = Net::HTTP::Put.new(uri.path)
  put.content_type = type

  set_upload_body(put, data)

  response = submit(put, uri, credentials)

  case response
  when Net::HTTPOK, Net::HTTPAccepted
    # We've either set a parameter or started a run so we get 200 or 202
    # back from the server, respectively. Return true to indicate success.
    true
  when Net::HTTPCreated
    # We've uploaded data so we get 201 back from the server. Return the
    # uri of the created resource.
    URI.parse(response['location'])
  when Net::HTTPNoContent
    # We've modified data so we get 204 back from the server. Return the
    # uri of the modified resource.
    uri
  when Net::HTTPServiceUnavailable
    raise ServerAtCapacityError.new
  else
    report_error("PUT", uri.path, response, credentials)
  end
end