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

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

  # set up persistent http connection
  @http = Net::HTTP::Persistent.new("Taverna_Server_Ruby_Client")
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) -> bool

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/t2-server/net/connection.rb', line 208

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
  when Net::HTTPNotFound
    false
  when Net::HTTPForbidden
    raise AccessForbiddenError.new(uri)
  when Net::HTTPUnauthorized
    raise AuthorizationError.new(credentials)
  else
    raise UnexpectedServerResponse.new(response)
  end
end

#GET(uri, type, range, credentials) ⇒ 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.



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

def GET(uri, type, range, credentials)
  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)

  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)
  when Net::HTTPNotFound
    raise AttributeNotFoundError.new(uri.path)
  when Net::HTTPForbidden
    raise AccessForbiddenError.new("attribute #{uri.path}")
  when Net::HTTPUnauthorized
    raise AuthorizationError.new(credentials)
  else
    raise UnexpectedServerResponse.new(response)
  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.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/t2-server/net/connection.rb', line 233

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

  response = submit(options, uri, credentials)

  case response
  when Net::HTTPOK
    response.to_hash
  when Net::HTTPForbidden
    raise AccessForbiddenError.new("resource #{uri.path}")
  when Net::HTTPUnauthorized
    raise AuthorizationError.new(credentials)
  else
    raise UnexpectedServerResponse.new(response)
  end
end

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

:call-seq:

POST(uri, value, type, credentials)

Perform an HTTP POST of value to a location on the server specified by uri and return the URI of the created attribute.



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

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

  response = submit(post, uri, credentials)

  case response
  when Net::HTTPCreated
    # return the URI of the newly created item
    URI.parse(response['location'])
  when Net::HTTPNotFound
    raise AttributeNotFoundError.new(uri.path)
  when Net::HTTPForbidden
    if response.body.chomp.include? "server load exceeded"
      raise ServerAtCapacityError.new
    else
      raise AccessForbiddenError.new("attribute #{uri.path}")
    end
  when Net::HTTPUnauthorized
    raise AuthorizationError.new(credentials)
  else
    raise UnexpectedServerResponse.new(response)
  end
end

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

:call-seq:

PUT(uri, value, type, credentials) -> bool
PUT(uri, value, type, credentials) -> URI

Perform a HTTP PUT of value to a location on the server specified by uri . If successful true , or a URI to the PUT resource, is returned depending on whether the operation has set a parameter (true) or uploaded data (URI).



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

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

  response = submit(put, uri, credentials)

  case response
  when Net::HTTPOK
    # We've set a parameter so we get 200 back from the server. 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::HTTPNotFound
    raise AttributeNotFoundError.new(uri.path)
  when Net::HTTPForbidden
    raise AccessForbiddenError.new("attribute #{uri.path}")
  when Net::HTTPUnauthorized
    raise AuthorizationError.new(credentials)
  else
    raise UnexpectedServerResponse.new(response)
  end
end