2007-01-02 23:57:00 -05:00
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
require 'net/http'
|
|
|
|
|
|
|
|
module Solr
|
|
|
|
class Connection
|
|
|
|
attr_reader :url
|
|
|
|
|
|
|
|
def initialize(url)
|
|
|
|
@url = URI.parse(url)
|
2007-01-12 21:53:39 -05:00
|
|
|
unless @url.kind_of? URI::HTTP
|
|
|
|
raise "invalid http url: #{url}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# sends a commit message
|
|
|
|
def commit
|
|
|
|
self.send(Solr::Request::Commit.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
# sends a ping message
|
|
|
|
def ping
|
|
|
|
response = send(Solr::Request::Ping.new)
|
2007-01-02 23:57:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def send(request)
|
2007-01-04 11:40:07 -05:00
|
|
|
data = post(request)
|
2007-01-12 21:53:39 -05:00
|
|
|
case request.response_format
|
|
|
|
when :ruby
|
|
|
|
return RubyResponse.new(data)
|
|
|
|
when :xml
|
|
|
|
return XmlResponse.new(data)
|
|
|
|
else
|
|
|
|
raise "Unknown response format: #{request.response_format}"
|
|
|
|
end
|
2007-01-04 11:40:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def post(request)
|
2007-01-12 21:53:39 -05:00
|
|
|
post = Net::HTTP::Post.new(@url.path + "/" + request.handler)
|
|
|
|
post.body = request.to_s
|
2007-01-02 23:57:00 -05:00
|
|
|
post.content_type = 'application/x-www-form-urlencoded; charset=utf-8'
|
|
|
|
response = Net::HTTP.start(@url.host, @url.port) do |http|
|
|
|
|
http.request(post)
|
|
|
|
end
|
2007-01-04 11:40:07 -05:00
|
|
|
|
2007-01-11 08:54:22 -05:00
|
|
|
case response
|
|
|
|
when Net::HTTPSuccess then response.body
|
|
|
|
else
|
|
|
|
response.error!
|
|
|
|
end
|
|
|
|
|
2007-01-02 23:57:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|