# HG changeset patch # User Severin Gehwolf # Date 1505228514 -7200 # Node ID d844cfb19af1faab14ad6dd6d314139cbc914d29 # Parent d717e6133812a407630005dc459a9008c569ede8 Use singleton for HttpClient instance. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024970.html PR3447 diff -r d717e6133812 -r d844cfb19af1 agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/BasicHttpService.java --- a/agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/BasicHttpService.java Thu Sep 07 18:56:23 2017 +0200 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/BasicHttpService.java Tue Sep 12 17:01:54 2017 +0200 @@ -56,7 +56,7 @@ protected HttpClientFacade client; protected AgentStartupConfiguration agentStartupConfiguration; protected StorageCredentials creds; - + BasicHttpService() { this(new HttpClientCreator(), new ConfigCreator(), new CredentialsCreator()); } @@ -66,19 +66,19 @@ this.configCreator = configCreator; this.credsCreator = credsCreator; } - - protected void doActivate(CommonPaths commonPaths, SSLConfiguration sslConfig) { + + protected void doActivate(CommonPaths commonPaths, SSLConfiguration sslConfig, final String serviceName) { try { agentStartupConfiguration = configCreator.create(commonPaths); client = httpClientCreator.create(sslConfig); creds = credsCreator.create(commonPaths); client.start(); - logger.log(Level.FINE, "HttpRequestService activated"); + logger.log(Level.FINE, serviceName + " activated"); } catch (Exception e) { - logger.log(Level.SEVERE, "HttpRequestService failed to start correctly. Behaviour undefined.", e); + logger.log(Level.SEVERE, serviceName + " failed to start correctly. Behaviour undefined.", e); } } - + static class ConfigCreator { AgentStartupConfiguration create(CommonPaths commonPaths) { AgentConfigsUtils.setConfigFiles(commonPaths.getSystemAgentConfigurationFile(), @@ -86,17 +86,17 @@ return AgentConfigsUtils.createAgentConfigs(); } } - + static class CredentialsCreator { StorageCredentials create(CommonPaths paths) { return new FileStorageCredentials(paths.getUserAgentAuthConfigFile()); } } - + static class HttpClientCreator { HttpClientFacade create(SSLConfiguration config) { - return new HttpClientFacade(config); + return HttpClientFacadeFactory.getInstance(config); } } diff -r d717e6133812 -r d844cfb19af1 agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/HttpClientFacadeFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/HttpClientFacadeFactory.java Tue Sep 12 17:01:54 2017 +0200 @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * . + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.internal.http; + +import com.redhat.thermostat.shared.config.SSLConfiguration; + +class HttpClientFacadeFactory { + + private static final Object instanceLock = new Object(); + private static HttpClientFacade instance; + + static HttpClientFacade getInstance(SSLConfiguration sslConfig) { + synchronized (instanceLock) { + if (instance == null) { + instance = new HttpClientFacade(sslConfig); + } + return instance; + } + } +} diff -r d717e6133812 -r d844cfb19af1 agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/HttpRequestServiceImpl.java --- a/agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/HttpRequestServiceImpl.java Thu Sep 07 18:56:23 2017 +0200 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/HttpRequestServiceImpl.java Tue Sep 12 17:01:54 2017 +0200 @@ -63,7 +63,7 @@ @Component @Service(value = HttpRequestService.class) public class HttpRequestServiceImpl extends BasicHttpService implements HttpRequestService { - + private static final Logger logger = LoggingUtils.getLogger(HttpRequestServiceImpl.class); private static final String UNKNOWN_CREDS = "UNKNOWN:UNKNOWN"; @@ -86,7 +86,7 @@ @Activate public void activate() { - super.doActivate(commonPaths, sslConfig); + super.doActivate(commonPaths, sslConfig, HttpRequestService.class.getSimpleName()); } /** @@ -96,6 +96,7 @@ * @param requestMethod The HTTP request type: GET, PUT, POST or DELETE * @return The returned body for GET requests. {@code null} otherwise. */ + @Override public String sendHttpRequest(String jsonPayload, URI uri, Method requestMethod) throws RequestFailedException { // Normalize URI to ensure any duplicate slashes are removed uri = uri.normalize(); @@ -129,7 +130,7 @@ throw new RequestFailedException(e); } } - + private String getBasicAuthHeaderValue() { String username = creds.getUsername(); char[] pwdChar = creds.getPassword(); @@ -141,15 +142,15 @@ String pwd = new String(pwdChar); userpassword = username + ":" + pwd; } - + @SuppressWarnings("restriction") String encodedAuthorization = new sun.misc.BASE64Encoder() .encode(userpassword.getBytes()); return "Basic " + encodedAuthorization; } - + // DS bind methods - + protected void bindTokenService(KeycloakAccessTokenService tokenService) { this.tokenService = tokenService; } diff -r d717e6133812 -r d844cfb19af1 agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/KeycloakAccessTokenServiceImpl.java --- a/agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/KeycloakAccessTokenServiceImpl.java Thu Sep 07 18:56:23 2017 +0200 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/internal/http/KeycloakAccessTokenServiceImpl.java Tue Sep 12 17:01:54 2017 +0200 @@ -65,15 +65,15 @@ private static final Logger logger = LoggingUtils.getLogger(KeycloakAccessTokenServiceImpl.class); private static final String KEYCLOAK_TOKEN_SERVICE = "/auth/realms/__REALM__/protocol/openid-connect/token"; private static final String KEYCLOAK_CONTENT_TYPE = "application/x-www-form-urlencoded"; - private final Object accessTokenLock = new Object(); + private final Object accessTokenLock = new Object(); private final Gson gson = new GsonBuilder().create(); private KeycloakAccessToken keycloakAccessToken; - + @Reference private SSLConfiguration sslConfig; @Reference private CommonPaths commonPaths; - + public KeycloakAccessTokenServiceImpl() { this(new HttpClientCreator(), new ConfigCreator(), new CredentialsCreator()); } @@ -82,12 +82,12 @@ KeycloakAccessTokenServiceImpl(HttpClientCreator clientCreator, ConfigCreator configCreator, CredentialsCreator credsCreator) { super(clientCreator, configCreator, credsCreator); } - + @Activate public void activate() { - super.doActivate(commonPaths, sslConfig); + super.doActivate(commonPaths, sslConfig, KeycloakAccessTokenService.class.getSimpleName()); } - + @Override public KeycloakAccessToken getAccessToken() throws RequestFailedException { synchronized(accessTokenLock) { @@ -96,18 +96,18 @@ } else if (keycloakAccessToken.isKeycloakTokenExpired()) { logger.log(Level.FINE, "Keycloak Token expired attempting to reacquire via refresh_token"); keycloakAccessToken = refreshKeycloakToken(); - + if (keycloakAccessToken == null) { logger.log(Level.WARNING, "Unable to refresh Keycloak token, attempting to acquire new token"); keycloakAccessToken = acquireKeycloakToken(); } - + if (keycloakAccessToken == null) { logger.log(Level.SEVERE, "Unable to reacquire KeycloakToken."); throw new RequestFailedException("Keycloak token expired and attempt to refresh and reacquire Keycloak token failed."); } } - + } return keycloakAccessToken; } @@ -158,7 +158,7 @@ return "grant_type=refresh_token&client_id=" + agentStartupConfiguration.getKeycloakClient() + "&refresh_token=" + keycloakAccessToken.getRefreshToken(); } - + // For testing purpuses void setKeycloakAccessToken(KeycloakAccessToken token) { synchronized (accessTokenLock) { diff -r d717e6133812 -r d844cfb19af1 agent/core/src/test/java/com/redhat/thermostat/agent/internal/http/HttpClientFacadeFactoryTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/core/src/test/java/com/redhat/thermostat/agent/internal/http/HttpClientFacadeFactoryTest.java Tue Sep 12 17:01:54 2017 +0200 @@ -0,0 +1,56 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * . + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.internal.http; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.mock; + +import org.junit.Test; + +import com.redhat.thermostat.shared.config.SSLConfiguration; + +public class HttpClientFacadeFactoryTest { + + @Test + public void testInstantiate() { + HttpClientFacade first = HttpClientFacadeFactory.getInstance(mock(SSLConfiguration.class)); + HttpClientFacade second = HttpClientFacadeFactory.getInstance(mock(SSLConfiguration.class)); + assertNotNull(first); + assertSame(first, second); + } +}