2016-04-03 17 views
0

我已經開始使用Lucidworks Fusion(2.1.2),並且使用Groovy進行了最爲舒適的黑客攻擊。附註:python'requests'處理這個無縫,但我固執,不想使用python ...如何使用groovy連接到Lucidworks Fusion(solr wrapper)API?

融合有前途API,我期待着與Groovy合作。

如何最好地連接到融合使用Groovy的Fusion身份驗證的API(以groovy-ish的方式)?

我嘗試了幾種方法(最後發現了一些比工作)。我歡迎反饋意見,爲什麼基本RESTClient不適合我,以及其他「簡單」解決方案。

這裏是我的嘗試:

groovyx.net.http.HTTPBuilder hb = new HTTPBuilder(FUSION_API_BASE) 
hb.auth.basic(user, pass) 

失敗,出現一個401未授權的(因爲編碼的,我相信)。

compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1' 

我也試過:HTTPBuilder從gradle這個來

HttpPost httpPost = new HttpPost(url); 
List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 
nvps.add(new BasicNameValuePair("username", "sean")); 
nvps.add(new BasicNameValuePair("password", "mypass")); 
httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 
CloseableHttpResponse response2 = httpclient.execute(httpPost); 

,並得到:

{"code":"unauthorized"} 

也試過:

String path = '/api/apollo/introspect' 
URL url = new URL('http', 'corp', 8764, path) 
try { 
    def foo = url.getContent() 
    log.info "Foo: $foo" 
} catch (IOException ioe){ 
    log.warn "IO ERR: $ioe" 
} 

它扔了(一個目前預計) IOError:401.如果有人想要更多對我的失敗讓我知道,我可能會給你帶來大量的技術細節。

我很無恥地回答我自己的問題(下面),但希望在那裏的一些時髦的意識可以啓發我一點。

所以回顧一下:有沒有比我在下面發現的更好的/更加優秀的解決方案?

回答

0

所以我問了這個問題,併發布了我找到的解決方案。希望人們會增加更好的解決方案,甚至可以解釋我在最初的嘗試中錯過了什麼。

這裏是我的首選解決方案(所有三個下面的解決方案是從谷歌上搜索,但我失去了聯繫,隨時捅我,我會挖起來 - 榮譽原始海報):

String furl = "${FUSION_API_BASE}${path}" //http://localhost:8764/api/apollo/introspect 
RESTClient rc = new RESTClient(furl) 
rc.headers['Authorization'] = 'Basic ' + "$user:$pass".bytes.encodeBase64() 
//rc.headers['Authorization'] = 'Basic ' + "$user:$pass".getBytes('iso-8859-1').encodeBase64() 
def foo = rc.get([:]) 
log.info "Foo: $foo" 

而另一工作液:

RESTClient rest = new RESTClient('http://localhost:8764/') 
HttpClient client = rest.client 
client.addRequestInterceptor(new HttpRequestInterceptor() { 
    void process(HttpRequest httpRequest, HttpContext httpContext) { 
     httpRequest.addHeader('Authorization', 'Basic ' + 'sean:mypass'.bytes.encodeBase64().toString()) 
    } 
}) 
def resp = rest.get(path : path) 
assert resp.status == 200 // HTTP response code; 404 means not found, etc. 
println resp.getData() 

而且在家裏那些保持得分,比較的蟒蛇解決方案:

import requests 
from requests.auth import HTTPBasicAuth 
rsp = requests.get('http://corp:8764/api/apollo/introspect', auth=HTTPBasicAuth('sean', 'lucid4pass')) 
print "Response ok/status code: %s/%s", rsp.ok, rsp.status_code 
print "Response content: %s", rsp.content 

HTH,

肖恩