From 369ff86fc674e9deedfaf5defd3e5ef29995a1ef Mon Sep 17 00:00:00 2001 From: Alex Rukhlin Date: Sun, 4 Jun 2017 17:10:28 -0400 Subject: [PATCH 1/2] Re-use location info among all REST clients --- .../common/WizardServerSelectionPage.java | 2 +- .../alm/client/TeeClientHandler.java | 48 ++++++++++++------- .../com/microsoft/tfs/core/TFSConnection.java | 23 +++++++-- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/source/com.microsoft.tfs.client.common.ui/src/com/microsoft/tfs/client/common/ui/wizard/common/WizardServerSelectionPage.java b/source/com.microsoft.tfs.client.common.ui/src/com/microsoft/tfs/client/common/ui/wizard/common/WizardServerSelectionPage.java index 369d6630e..cd3ad4c19 100644 --- a/source/com.microsoft.tfs.client.common.ui/src/com/microsoft/tfs/client/common/ui/wizard/common/WizardServerSelectionPage.java +++ b/source/com.microsoft.tfs.client.common.ui/src/com/microsoft/tfs/client/common/ui/wizard/common/WizardServerSelectionPage.java @@ -154,7 +154,7 @@ private List getUserAccounts( */ final TFSConnection vstsConnection = new TFSTeamProjectCollection(URIUtils.VSTS_ROOT_URL, azureAccessToken, new UIClientConnectionAdvisor()); - final TeeClientHandler clientHandler = new TeeClientHandler(vstsConnection.getHTTPClient()); + final TeeClientHandler clientHandler = new TeeClientHandler(vstsConnection); final ProfileHttpClient profileClient = new ProfileHttpClient(clientHandler, URIUtils.VSTS_ROOT_URL); diff --git a/source/com.microsoft.tfs.core/rest_core/com/microsoft/alm/client/TeeClientHandler.java b/source/com.microsoft.tfs.core/rest_core/com/microsoft/alm/client/TeeClientHandler.java index f07fb652a..059958be4 100644 --- a/source/com.microsoft.tfs.core/rest_core/com/microsoft/alm/client/TeeClientHandler.java +++ b/source/com.microsoft.tfs.core/rest_core/com/microsoft/alm/client/TeeClientHandler.java @@ -26,6 +26,7 @@ import com.microsoft.alm.visualstudio.services.webapi.ApiResourceLocationCollection; import com.microsoft.alm.visualstudio.services.webapi.ApiResourceVersion; import com.microsoft.tfs.core.Messages; +import com.microsoft.tfs.core.TFSConnection; import com.microsoft.tfs.core.httpclient.Header; import com.microsoft.tfs.core.httpclient.HttpClient; import com.microsoft.tfs.core.httpclient.HttpException; @@ -51,11 +52,18 @@ public class TeeClientHandler extends VssRestClientHandlerBase implements VssRes private final static String MEDIA_TYPE_PARAMETERS_SEPARATOR = ";"; //$NON-NLS-1$ private final HttpClient httpClient; + private final TFSConnection connection; public TeeClientHandler(final HttpClient httpClient) { + this.connection = null; this.httpClient = httpClient; } + public TeeClientHandler(final TFSConnection connection) { + this.connection = connection; + this.httpClient = connection.getHTTPClient(); + } + @Override public boolean checkConnection() { log.debug("Checking REST client connection"); //$NON-NLS-1$ @@ -90,25 +98,29 @@ public boolean checkConnection() { @Override public ApiResourceLocationCollection loadLocations() { - final URI optionsTarget = URIUtils.resolve(getBaseUrl(), OPTIONS_RELATIVE_PATH); - final HttpMethodBase request = createHttpMethod(HttpMethod.OPTIONS, optionsTarget); - request.setFollowRedirects(true); - - try { - request.setRequestHeader(VssHttpHeaders.ACCEPT, VssMediaTypes.APPLICATION_JSON_TYPE); - final int statusCode = httpClient.executeMethod(request); - - if (HttpStatus.isSuccessFamily(statusCode)) { - return JsonHelper.deserializeResponce(request, ApiResourceLocationCollection.class); - } else { - throw new HttpException(HttpStatus.getStatusText(statusCode)); + if (connection != null && connection.getBaseURI().equals(getBaseUrl())) { + return connection.getServerApiLocations(); + } else { + final URI optionsTarget = URIUtils.resolve(getBaseUrl(), OPTIONS_RELATIVE_PATH); + final HttpMethodBase request = createHttpMethod(HttpMethod.OPTIONS, optionsTarget); + request.setFollowRedirects(true); + + try { + request.setRequestHeader(VssHttpHeaders.ACCEPT, VssMediaTypes.APPLICATION_JSON_TYPE); + final int statusCode = httpClient.executeMethod(request); + + if (HttpStatus.isSuccessFamily(statusCode)) { + return JsonHelper.deserializeResponce(request, ApiResourceLocationCollection.class); + } else { + throw new HttpException(HttpStatus.getStatusText(statusCode)); + } + } catch (final Exception e) { + log.error(e.getMessage(), e); + setLastException(e); + return null; + } finally { + request.releaseConnection(); } - } catch (final Exception e) { - log.error(e.getMessage(), e); - setLastException(e); - return null; - } finally { - request.releaseConnection(); } } diff --git a/source/com.microsoft.tfs.core/src/com/microsoft/tfs/core/TFSConnection.java b/source/com.microsoft.tfs.core/src/com/microsoft/tfs/core/TFSConnection.java index 22d438f08..cd5bb2d8f 100644 --- a/source/com.microsoft.tfs.core/src/com/microsoft/tfs/core/TFSConnection.java +++ b/source/com.microsoft.tfs.core/src/com/microsoft/tfs/core/TFSConnection.java @@ -343,6 +343,8 @@ public abstract class TFSConnection implements Closable { private Version serverApiVersion = null; private final Object serverApiVersionLock = new Object(); + private ApiResourceLocationCollection serverApiLocations = null; + private final Object serverApiLocationsLock = new Object(); /** * Creates a {@link TFSConnection}. Both a {@link URI} and a @@ -1351,11 +1353,7 @@ public Version getServerApiVersion() { synchronized (serverApiVersionLock) { if (serverApiVersion == null) { - - final TeeClientHandler clientHandler = new TeeClientHandler(getHTTPClient()); - clientHandler.init(true, null, getBaseURI()); - - ApiResourceLocationCollection locations = clientHandler.getLocations(); + ApiResourceLocationCollection locations = getServerApiLocations(); serverApiVersion = new Version(0, 0); for (ApiResourceLocation location : locations.getLocations()) { @@ -1371,6 +1369,21 @@ public Version getServerApiVersion() { return serverApiVersion; } + public ApiResourceLocationCollection getServerApiLocations() { + + synchronized (serverApiLocationsLock) { + if (serverApiLocations == null) { + + final TeeClientHandler clientHandler = new TeeClientHandler(getHTTPClient()); + clientHandler.init(true, null, getBaseURI()); + + serverApiLocations = clientHandler.loadLocations(); + } + } + + return serverApiLocations; + } + /** * Obtains the {@link PersistenceStoreProvider} that determines where cache * and configuration data is stored. From acc0f0467df22be81bc21e9441fd5638a745c1c5 Mon Sep 17 00:00:00 2001 From: Alex Rukhlin Date: Mon, 5 Jun 2017 20:15:11 -0400 Subject: [PATCH 2/2] Some additional changes from home? --- .../teambuild/teamexplorer/favorites/BuildFavoriteItem.java | 2 +- .../sections/TeamExplorerBuildsFavoritesSection.java | 2 +- .../sections/TeamExplorerBuildsVNextDefinitionSection.java | 4 ++-- .../src/com/microsoft/tfs/core/TFSConnection.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/favorites/BuildFavoriteItem.java b/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/favorites/BuildFavoriteItem.java index 205e7d5e2..385adf2d0 100644 --- a/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/favorites/BuildFavoriteItem.java +++ b/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/favorites/BuildFavoriteItem.java @@ -105,7 +105,7 @@ private static BuildDefinition getBuildDefinitionFromFavorite( final TFSTeamProjectCollection connection = server.getConnection(); final BuildHttpClient buildClient = - new BuildHttpClient(new TeeClientHandler(connection.getHTTPClient()), connection.getBaseURI()); + new BuildHttpClient(new TeeClientHandler(connection), connection.getBaseURI()); final DefinitionReference definition = buildClient.getDefinition(oldDefinition.getProject().getId(), oldDefinition.getId(), null, null); diff --git a/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/sections/TeamExplorerBuildsFavoritesSection.java b/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/sections/TeamExplorerBuildsFavoritesSection.java index d1d5fcb3d..665feb210 100644 --- a/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/sections/TeamExplorerBuildsFavoritesSection.java +++ b/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/sections/TeamExplorerBuildsFavoritesSection.java @@ -91,7 +91,7 @@ protected void loadDefinitions(final TeamExplorerContext context) { final TFSTeamProjectCollection connection = context.getServer().getConnection(); final BuildHttpClient buildClient = - new BuildHttpClient(new TeeClientHandler(connection.getHTTPClient()), connection.getBaseURI()); + new BuildHttpClient(new TeeClientHandler(connection), connection.getBaseURI()); final UUID projectId = UUID.fromString(context.getCurrentProjectInfo().getGUID()); final List rawDefinitions = buildClient.getDefinitions(projectId); diff --git a/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/sections/TeamExplorerBuildsVNextDefinitionSection.java b/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/sections/TeamExplorerBuildsVNextDefinitionSection.java index e67377f39..42373edea 100644 --- a/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/sections/TeamExplorerBuildsVNextDefinitionSection.java +++ b/source/com.microsoft.tfs.client.common.ui.teambuild/src/com/microsoft/tfs/client/common/ui/teambuild/teamexplorer/sections/TeamExplorerBuildsVNextDefinitionSection.java @@ -172,7 +172,7 @@ private void createNewDefinition() { template = null; } else { final BuildHttpClient buildClient = - new BuildHttpClient(new TeeClientHandler(connection.getHTTPClient()), connection.getBaseURI()); + new BuildHttpClient(new TeeClientHandler(connection), connection.getBaseURI()); final List templates = buildClient.getTemplates(projectName); @@ -201,7 +201,7 @@ private void getBuildDefinitions(final TeamExplorerContext context) { final TFSTeamProjectCollection connection = context.getServer().getConnection(); final BuildHttpClient buildClient = - new BuildHttpClient(new TeeClientHandler(connection.getHTTPClient()), connection.getBaseURI()); + new BuildHttpClient(new TeeClientHandler(connection), connection.getBaseURI()); final UUID projectId = UUID.fromString(context.getCurrentProjectInfo().getGUID()); final List rawDefinitions = buildClient.getDefinitions(projectId); diff --git a/source/com.microsoft.tfs.core/src/com/microsoft/tfs/core/TFSConnection.java b/source/com.microsoft.tfs.core/src/com/microsoft/tfs/core/TFSConnection.java index cd5bb2d8f..4ba684ee7 100644 --- a/source/com.microsoft.tfs.core/src/com/microsoft/tfs/core/TFSConnection.java +++ b/source/com.microsoft.tfs.core/src/com/microsoft/tfs/core/TFSConnection.java @@ -1375,7 +1375,7 @@ public ApiResourceLocationCollection getServerApiLocations() { if (serverApiLocations == null) { final TeeClientHandler clientHandler = new TeeClientHandler(getHTTPClient()); - clientHandler.init(true, null, getBaseURI()); + clientHandler.init(false, null, getBaseURI()); serverApiLocations = clientHandler.loadLocations(); }