Skip to content

Commit

Permalink
Convert URL to Path in a robust way (#2638)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh authored Dec 6, 2017
1 parent 56ebeae commit f3dff41
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.google.cloud.tools.login.Account;
import com.google.common.base.Predicate;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -445,24 +446,24 @@ public void testCreateServiceAccountKey_ioException() throws IOException, CoreEx
}

@Test
public void testGetServiceAccountKeyPath() {
public void testGetServiceAccountKeyPath() throws URISyntaxException {
tab.initializeFrom(launchConfig);
accountSelector.selectAccount("account1@example.com");
projectSelector.selectProjectId("project-A");

Path expected = Paths.get(Platform.getConfigurationLocation().getURL().getPath())
Path expected = Paths.get(Platform.getConfigurationLocation().getURL().toURI())
.resolve("com.google.cloud.tools.eclipse")
.resolve("app-engine-default-service-account-key-project-A.json");
assertEquals(expected, tab.getServiceAccountKeyPath());
}

@Test
public void testGetServiceAccountKeyPath_internal() {
public void testGetServiceAccountKeyPath_internal() throws URISyntaxException {
tab.initializeFrom(launchConfig);
accountSelector.selectAccount("account2@example.com");
projectSelector.selectProjectId("google.com:project-D");

Path expected = Paths.get(Platform.getConfigurationLocation().getURL().getPath())
Path expected = Paths.get(Platform.getConfigurationLocation().getURL().toURI())
.resolve("com.google.cloud.tools.eclipse")
.resolve("app-engine-default-service-account-key-google.com.project-D.json");
assertEquals(expected, tab.getServiceAccountKeyPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -241,7 +242,10 @@ public void widgetSelected(SelectionEvent event) {
BusyIndicator.showWhile(createServiceKey.getDisplay(), new Runnable() {
@Override
public void run() {
createServiceAccountKey(getServiceAccountKeyPath());
Path keyPath = getServiceAccountKeyPath();
if (keyPath != null) {
createServiceAccountKey(keyPath);
}
}});
}
});
Expand Down Expand Up @@ -303,7 +307,7 @@ public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {

@Override
public void initializeFrom(ILaunchConfiguration configuration) {
this.accountEmailModel = getAttribute(configuration, ATTRIBUTE_ACCOUNT_EMAIL, ""); //$NON-NLS-1$
accountEmailModel = getAttribute(configuration, ATTRIBUTE_ACCOUNT_EMAIL, ""); //$NON-NLS-1$

Map<String, String> environmentMap = getEnvironmentMap(configuration);
gcpProjectIdModel = Strings.nullToEmpty(environmentMap.get(PROJECT_ID_ENVIRONMENT_VARIABLE));
Expand Down Expand Up @@ -413,9 +417,14 @@ Path getServiceAccountKeyPath() {

// can't use colons in filenames on Windows
filename = filename.replace(':', '.');

String configurationLocation = Platform.getConfigurationLocation().getURL().getPath();
return Paths.get(configurationLocation + "/com.google.cloud.tools.eclipse/" + filename);

try {
Path configurationLocation = Paths.get(Platform.getConfigurationLocation().getURL().toURI());
return configurationLocation.resolve("com.google.cloud.tools.eclipse").resolve(filename);
} catch (URISyntaxException e) {
logger.log(Level.SEVERE, "Cannot convert configuration location into URI", e); //$NON-NLS-1$
return null;
}
}

@VisibleForTesting
Expand Down

0 comments on commit f3dff41

Please sign in to comment.