Search in sources :

Example 26 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project musiccabinet by hakko.

the class ArtistRelationServiceTest method getArtistSimilarityClient.

@SuppressWarnings("unchecked")
private ArtistSimilarityClient getArtistSimilarityClient(WebserviceHistoryService historyService) throws IOException {
    // create a HTTP client that always returns Cher artist relations
    HttpClient httpClient = mock(HttpClient.class);
    ClientConnectionManager connectionManager = mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(connectionManager);
    String httpResponse = new ResourceUtil(CHER_ARTIST_RELATIONS).getContent();
    when(httpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).thenReturn(httpResponse);
    // create a throttling service that allows calls at any rate
    ThrottleService throttleService = mock(ThrottleService.class);
    // create a client that allows all calls and returns Cher artist relations
    ArtistSimilarityClient asClient = new ArtistSimilarityClient();
    asClient.setWebserviceHistoryService(historyService);
    asClient.setHttpClient(httpClient);
    asClient.setThrottleService(throttleService);
    return asClient;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ResponseHandler(org.apache.http.client.ResponseHandler) ArtistSimilarityClient(com.github.hakko.musiccabinet.ws.lastfm.ArtistSimilarityClient) HttpClient(org.apache.http.client.HttpClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 27 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project musiccabinet by hakko.

the class ArtistTopTracksServiceTest method getArtistTopTracksClient.

@SuppressWarnings("unchecked")
private ArtistTopTracksClient getArtistTopTracksClient(WebserviceHistoryService historyService) throws IOException {
    // create a HTTP client that always returns Cher top tracks
    HttpClient httpClient = mock(HttpClient.class);
    ClientConnectionManager connectionManager = mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(connectionManager);
    String httpResponse = new ResourceUtil(CHER_TOP_TRACKS).getContent();
    when(httpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).thenReturn(httpResponse);
    // create a throttling service that allows calls at any rate
    ThrottleService throttleService = mock(ThrottleService.class);
    // create a client that allows all calls and returns Cher top tracks
    ArtistTopTracksClient attClient = new ArtistTopTracksClient();
    attClient.setWebserviceHistoryService(historyService);
    attClient.setHttpClient(httpClient);
    attClient.setThrottleService(throttleService);
    return attClient;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ResponseHandler(org.apache.http.client.ResponseHandler) ArtistTopTracksClient(com.github.hakko.musiccabinet.ws.lastfm.ArtistTopTracksClient) HttpClient(org.apache.http.client.HttpClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 28 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project musiccabinet by hakko.

the class ScrobbledTracksServiceTest method getScrobbledTracksClient.

@SuppressWarnings("unchecked")
private ScrobbledTracksClient getScrobbledTracksClient() throws IOException {
    // create a HistoryDao that allows all calls
    WebserviceHistoryService historyService = mock(WebserviceHistoryService.class);
    when(historyService.isWebserviceInvocationAllowed(Mockito.any(WebserviceInvocation.class))).thenReturn(true);
    // create a HTTP client that always returns sampled scrobbled tracks from last.fm
    HttpClient httpClient = mock(HttpClient.class);
    ClientConnectionManager connectionManager = mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(connectionManager);
    String httpResponse = new ResourceUtil(SCROBBLED_TRACKS).getContent();
    when(httpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).thenReturn(httpResponse);
    // create a client out of the components above
    ScrobbledTracksClient stClient = new ScrobbledTracksClient();
    stClient.setWebserviceHistoryService(historyService);
    stClient.setHttpClient(httpClient);
    // create a throttling service that allows calls at any rate
    ThrottleService throttleService = mock(ThrottleService.class);
    stClient.setThrottleService(throttleService);
    return stClient;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ResponseHandler(org.apache.http.client.ResponseHandler) HttpClient(org.apache.http.client.HttpClient) ThrottleService(com.github.hakko.musiccabinet.service.lastfm.ThrottleService) WebserviceInvocation(com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation) ScrobbledTracksClient(com.github.hakko.musiccabinet.ws.lastfm.ScrobbledTracksClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 29 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project musiccabinet by hakko.

the class AbstractWSGetClientTest method getTestWSClient.

/*
	 * Help method to create a base TestWSClient.
	 */
private TestWSGetClient getTestWSClient(boolean allowCalls) {
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    ClientConnectionManager connectionManager = Mockito.mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(connectionManager);
    WebserviceHistoryService historyService = mock(WebserviceHistoryService.class);
    when(historyService.isWebserviceInvocationAllowed(Mockito.any(WebserviceInvocation.class))).thenReturn(allowCalls);
    WebserviceInvocation invocation = mock(WebserviceInvocation.class);
    List<NameValuePair> params = new ArrayList<>();
    TestWSGetClient testClient = new TestWSGetClient(invocation, params);
    testClient.setWebserviceHistoryService(historyService);
    testClient.setHttpClient(httpClient);
    // create a throttling service that allows calls at any rate
    ThrottleService throttleService = mock(ThrottleService.class);
    testClient.setThrottleService(throttleService);
    return testClient;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) WebserviceHistoryService(com.github.hakko.musiccabinet.service.lastfm.WebserviceHistoryService) HttpClient(org.apache.http.client.HttpClient) ArrayList(java.util.ArrayList) ThrottleService(com.github.hakko.musiccabinet.service.lastfm.ThrottleService) WebserviceInvocation(com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 30 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project Fling by entertailion.

the class HttpRequestHelper method createHttpClient.

public static DefaultHttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 20000);
    HttpConnectionParams.setSoTimeout(params, 20000);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    return new DefaultHttpClient(conMgr, params);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)74 Scheme (org.apache.http.conn.scheme.Scheme)51 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)48 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)39 HttpParams (org.apache.http.params.HttpParams)37 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)35 BasicHttpParams (org.apache.http.params.BasicHttpParams)28 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)26 IOException (java.io.IOException)21 Test (org.junit.Test)15 CertificateException (java.security.cert.CertificateException)14 HttpClient (org.apache.http.client.HttpClient)14 KeyManagementException (java.security.KeyManagementException)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 SSLContext (javax.net.ssl.SSLContext)12 HttpResponse (org.apache.http.HttpResponse)11 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)11 KeyStoreException (java.security.KeyStoreException)10 PoolingClientConnectionManager (org.apache.http.impl.conn.PoolingClientConnectionManager)10 UnrecoverableKeyException (java.security.UnrecoverableKeyException)9