From 770137b6d37bde3e22c893eec16cc557178c3dea Mon Sep 17 00:00:00 2001 From: Martin Lippert Date: Mon, 21 Dec 2020 11:02:51 +0100 Subject: [PATCH] use try-with-resource to close file handle when reading cf cli config json file --- .../client/cftarget/CfCliParamsProvider.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/headless-services/commons/commons-cf/src/main/java/org/springframework/ide/vscode/commons/cloudfoundry/client/cftarget/CfCliParamsProvider.java b/headless-services/commons/commons-cf/src/main/java/org/springframework/ide/vscode/commons/cloudfoundry/client/cftarget/CfCliParamsProvider.java index 1a4574dab..be50fade7 100644 --- a/headless-services/commons/commons-cf/src/main/java/org/springframework/ide/vscode/commons/cloudfoundry/client/cftarget/CfCliParamsProvider.java +++ b/headless-services/commons/commons-cf/src/main/java/org/springframework/ide/vscode/commons/cloudfoundry/client/cftarget/CfCliParamsProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017, 2018 Pivotal, Inc. + * Copyright (c) 2017, 2020 Pivotal, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -79,13 +79,6 @@ public class CfCliParamsProvider implements ClientParamsProvider { } - /* - * (non-Javadoc) - * - * @see org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget. - * ClientParamsProvider#getParams() - */ - @SuppressWarnings("unchecked") @Override public List getParams() throws NoTargetsException, ExecutionException { List params = new ArrayList<>(); @@ -93,26 +86,7 @@ public class CfCliParamsProvider implements ClientParamsProvider { try { File file = getConfigJsonFile(); if (file != null) { - Map userData = gson.fromJson(new FileReader(file), Map.class); - if (userData != null) { - String refreshToken = (String) userData.get(REFRESH_TOKEN); - // Only support connecting to CF via refresh token for now - if (isRefreshTokenSet(refreshToken)) { - CFCredentials credentials = CFCredentials.fromRefreshToken(refreshToken); - boolean sslDisabled = (Boolean) userData.get(SSL_DISABLED); - String target = (String) userData.get(TARGET); - Map orgFields = (Map) userData.get(ORGANIZATION_FIELDS); - Map spaceFields = (Map) userData.get(SPACE_FIELDS); - if (target != null && orgFields != null && spaceFields != null) { - String orgName = (String) orgFields.get(NAME); - String spaceName = (String) spaceFields.get(NAME); - if (!StringUtil.hasText(orgName) || !StringUtil.hasText(spaceName)) { - throw new NoTargetsException(getMessages().getNoOrgSpace()); - } - params.add(new CFClientParams(target, null, credentials, orgName, spaceName, sslDisabled)); - } - } - } + readConfigFile(params, file); } } catch (IOException | InterruptedException e) { throw new ExecutionException(e); @@ -125,6 +99,34 @@ public class CfCliParamsProvider implements ClientParamsProvider { } } + @SuppressWarnings("unchecked") + private void readConfigFile(List params, File file) + throws NoTargetsException, IOException { + + try (FileReader reader = new FileReader(file)) { + Map userData = gson.fromJson(reader, Map.class); + if (userData != null) { + String refreshToken = (String) userData.get(REFRESH_TOKEN); + // Only support connecting to CF via refresh token for now + if (isRefreshTokenSet(refreshToken)) { + CFCredentials credentials = CFCredentials.fromRefreshToken(refreshToken); + boolean sslDisabled = (Boolean) userData.get(SSL_DISABLED); + String target = (String) userData.get(TARGET); + Map orgFields = (Map) userData.get(ORGANIZATION_FIELDS); + Map spaceFields = (Map) userData.get(SPACE_FIELDS); + if (target != null && orgFields != null && spaceFields != null) { + String orgName = (String) orgFields.get(NAME); + String spaceName = (String) spaceFields.get(NAME); + if (!StringUtil.hasText(orgName) || !StringUtil.hasText(spaceName)) { + throw new NoTargetsException(getMessages().getNoOrgSpace()); + } + params.add(new CFClientParams(target, null, credentials, orgName, spaceName, sslDisabled)); + } + } + } + } + } + private boolean isRefreshTokenSet(String token) { return StringUtil.hasText(token); }