Skip to content
This repository has been archived by the owner on Dec 19, 2020. It is now read-only.

solr http Authentication

chauhansudhir edited this page Sep 12, 2010 · 2 revisions

If Solr application has basic http authentication then the need to have the following code which pass http authentication headers


# [email protected]
# override solr class to set http authentication
require 'solr'
class Solr::Connection
  # send the http post request to solr; for convenience there are shortcuts
  # to some requests: add(), query(), commit(), delete() or send()
  def post(request)

    # in gem user name and password can be initialized in constructor  
    @user = @password = nil
    @user = URI.decode(@url.user) if @url.user
    @password = URI.decode(@url.password) if @url.password


    response = @connection.post(@url.path + "/" + request.handler,
                                request.to_s, build_headers({"Content-Type" => request.content_type})
                                )
    case response
    when Net::HTTPSuccess then response.body
    else
      response.error!
    end  
  end

  # build headers
  def build_headers(header)
    authorization_header.update(header)
  end

  # generate http basic authentication header
  def authorization_header
    (@user || @password ? { 'Authorization' => 'Basic ' + ["#{@user}:#{ @password}"].pack('m').delete("\r\n") } : {})
  end
end

Clone this wiki locally