package com.cadrlife.devsearch.agent.service.stash; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.inject.Inject; import javax.inject.Named; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class StashRestClient { private String response = null; private HttpResponse httpResponse = null; private String username; private String password; @Inject public StashRestClient(@Named("repo.stash.username") String username, @Named("repo.stash.password") String password) { this.username = username; this.password = password; } public String performGetRequest(String url) { try { String encoded = new String(Base64.encodeBase64((username + ":" + password).getBytes())); HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); httpget.setHeader("Authorization", "Basic " + encoded); try { httpResponse = httpclient.execute(httpget); } catch (ClientProtocolException e1) { e1.printStackTrace(); } HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = null; try { instream = entity.getContent(); BufferedReader reader = new BufferedReader( new InputStreamReader(instream)); response = reader.readLine(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); httpget.abort(); } finally { // Closing the input stream will trigger connection release instream.close(); } httpclient.getConnectionManager().shutdown(); } } catch (IOException e) { // In case of an IOException the connection will be released // back to the connection manager automatically e.printStackTrace(); } return response; } }