Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
AWS Toolkit for Eclipse: v201803062120 Release.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhx committed Mar 6, 2018
1 parent 1ffe7b8 commit 8717ded
Show file tree
Hide file tree
Showing 23 changed files with 478 additions and 144 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"current": [
"* **Update Error Report dialog to include the Github issue link.**"
],
"v201801042359": [
"* **Merge Pull Request #93.**",
"* **Resolve Issues: #87, #94, #95, #96, #97.**"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.amazonaws.eclipse.codecommit.CodeCommitPlugin;
import com.amazonaws.eclipse.codecommit.wizard.CloneRepositoryWizard;
import com.amazonaws.eclipse.core.AwsToolkitCore;
import com.amazonaws.eclipse.core.exceptions.AwsActionException;
import com.amazonaws.eclipse.core.mobileanalytics.AwsToolkitMetricType;
import com.amazonaws.eclipse.core.regions.RegionUtils;
import com.amazonaws.eclipse.core.regions.ServiceAbbreviations;
Expand Down Expand Up @@ -117,7 +118,8 @@ protected void doRun() {

} catch (Exception e) {
actionFailed();
CodeCommitPlugin.getDefault().reportException("Failed to create repository!", e);
CodeCommitPlugin.getDefault().reportException("Failed to create repository!",
new AwsActionException(AwsToolkitMetricType.EXPLORER_CODECOMMIT_CREATE_REPO.getName(), e.getMessage(), e));
}
} else {
actionCanceled();
Expand Down Expand Up @@ -254,7 +256,8 @@ protected void doRun() {
dialog.open();
actionSucceeded();
} catch (Exception e) {
CodeCommitPlugin.getDefault().reportException(e.getMessage(), e);
CodeCommitPlugin.getDefault().reportException("Failed to clone CodeCommit repository!",
new AwsActionException(AwsToolkitMetricType.EXPLORER_CODECOMMIT_CLONE_REPO.getName(), e.getMessage(), e));
actionFailed();
} finally {
actionFinished();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import com.amazonaws.eclipse.core.egit.jobs.ImportProjectJob;
import com.amazonaws.eclipse.core.egit.ui.CloneDestinationPage;
import com.amazonaws.eclipse.core.egit.ui.SourceBranchPage;
import com.amazonaws.eclipse.core.exceptions.AwsActionException;
import com.amazonaws.eclipse.core.maven.MavenUtils;
import com.amazonaws.eclipse.core.mobileanalytics.AwsToolkitMetricType;
import com.amazonaws.eclipse.core.model.GitCredentialsDataModel;
import com.amazonaws.eclipse.core.regions.RegionUtils;
import com.amazonaws.eclipse.core.util.WorkbenchUtils;
Expand Down Expand Up @@ -137,7 +139,8 @@ public void run(IProgressMonitor monitor)
});
} catch (InvocationTargetException e) {
CodeCommitAnalytics.trackCloneRepository(EventResult.FAILED);
CodeCommitPlugin.getDefault().reportException(e.getMessage(), e);
CodeCommitPlugin.getDefault().reportException("Failed to clone git repository.",
new AwsActionException(AwsToolkitMetricType.EXPLORER_CODECOMMIT_CLONE_REPO.getName(), e.getMessage(), e));
return false;
} catch (InterruptedException e) {
CodeCommitAnalytics.trackCloneRepository(EventResult.CANCELED);
Expand Down
1 change: 1 addition & 0 deletions bundles/com.amazonaws.eclipse.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Export-Package: com.amazonaws.eclipse.core,
com.amazonaws.eclipse.core.egit,
com.amazonaws.eclipse.core.egit.jobs,
com.amazonaws.eclipse.core.egit.ui,
com.amazonaws.eclipse.core.exceptions,
com.amazonaws.eclipse.core.maven,
com.amazonaws.eclipse.core.mobileanalytics,
com.amazonaws.eclipse.core.model,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.eclipse.core;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;

public class AwsToolkitHttpClient {
private final HttpClient httpClient;

private AwsToolkitHttpClient(
Integer connectionTimeout,
Integer socketTimeout,
String userAgent,
HttpHost proxy,
CredentialsProvider credentialsProvider) {

Builder requestConfigBuilder = RequestConfig.custom();
if (connectionTimeout != null) {
requestConfigBuilder.setConnectionRequestTimeout(connectionTimeout);
}
if (socketTimeout != null) {
requestConfigBuilder.setSocketTimeout(socketTimeout);
}

HttpClientBuilder builder = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfigBuilder.build())
.setUserAgent(userAgent);
if (proxy != null) {
builder.setProxy(proxy);
if (credentialsProvider != null) {
builder.setDefaultCredentialsProvider(credentialsProvider);
}
}
httpClient = builder
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
.build();
}

/**
* Head the given URL for the last modified date.
*
* @param url The target URL resource.
* @return The last modified date give the URL, or null if the date cannot be retrieved
* @throws ClientProtocolException
* @throws IOException
*/
public Date getLastModifiedDate(String url) throws ClientProtocolException, IOException {
HttpHead headMethod = new HttpHead(url);
HttpResponse response = httpClient.execute(headMethod);
Header header = response.getFirstHeader("Last-Modified");
if (header != null) {
String lastModifiedDateString = header.getValue();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
try {
return dateFormat.parse(lastModifiedDateString);
} catch (ParseException e) {
return null;
}
}
return null;
}

/**
* Fetch the content of the target URL and redirect to the output stream.
* If no content is fetched, do nothing.
*
* @param url The target URL
* @param output The OutputStream
* @throws ClientProtocolException
* @throws IOException
*/
public void outputEntityContent(String url, OutputStream output) throws ClientProtocolException, IOException {
try (InputStream inputStream = getEntityContent(url)) {
if (inputStream == null) {
return;
}
int length;
byte[] buffer = new byte[2048];
while ((length = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
}
}

/**
* Return the input stream of the underlying resource in the target URL.
* @param url The target URL
* @return The underlying input stream, or null if it doesn't have entity
* @throws ClientProtocolException
* @throws IOException
*/
public InputStream getEntityContent(String url) throws ClientProtocolException, IOException {
HttpGet getMethod = new HttpGet(url);
HttpResponse response = httpClient.execute(getMethod);
HttpEntity entity = response.getEntity();
return entity == null ? null : entity.getContent();
}

static AwsToolkitHttpClientBuilder builder() {
return new AwsToolkitHttpClientBuilder();
}

static final class AwsToolkitHttpClientBuilder {
private Integer connectionTimeout;
private Integer socketTimeout;
private String userAgent;
private HttpHost proxy;
private CredentialsProvider credentialsProvider;

private AwsToolkitHttpClientBuilder() {}

public AwsToolkitHttpClient build() {
return new AwsToolkitHttpClient(connectionTimeout, socketTimeout, userAgent, proxy, credentialsProvider);
}

public Integer getConnectionTimeout() {
return connectionTimeout;
}

public AwsToolkitHttpClientBuilder setConnectionTimeout(Integer connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}

public Integer getSocketTimeout() {
return socketTimeout;
}

public AwsToolkitHttpClientBuilder setSocketTimeout(Integer socketTimeout) {
this.socketTimeout = socketTimeout;
return this;
}

public String getUserAgent() {
return userAgent;
}

public AwsToolkitHttpClientBuilder setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}

public HttpHost getProxy() {
return proxy;
}

public AwsToolkitHttpClientBuilder setProxy(HttpHost proxy) {
this.proxy = proxy;
return this;
}

public CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}

public AwsToolkitHttpClientBuilder setCredentialsProvider(CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.runtime.Plugin;
Expand All @@ -39,55 +34,51 @@
*/
public final class HttpClientFactory {

public static DefaultHttpClient create(Plugin plugin, String url) {
HttpParams httpClientParams = new BasicHttpParams();

public static AwsToolkitHttpClient create(Plugin plugin, String url) {
IPreferenceStore preferences = AwsToolkitCore.getDefault().getPreferenceStore();

int connectionTimeout = preferences.getInt(PreferenceConstants.P_CONNECTION_TIMEOUT);
int socketTimeout = preferences.getInt(PreferenceConstants.P_SOCKET_TIMEOUT);

HttpConnectionParams.setConnectionTimeout(httpClientParams, connectionTimeout);
HttpConnectionParams.setSoTimeout(httpClientParams, socketTimeout);

HttpProtocolParams.setUserAgent(httpClientParams,
AwsClientUtils
.formatUserAgentString("AWS-Toolkit-For-Eclipse", plugin));

DefaultHttpClient httpclient = new DefaultHttpClient(httpClientParams);
configureProxy(httpclient, url);
IProxyData data = getEclipseProxyData(url);
HttpHost httpHost = null;
BasicCredentialsProvider credentialsProvider = null;
if (data != null) {
httpHost = new HttpHost(data.getHost(), data.getPort());
if (data.isRequiresAuthentication()) {
credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(httpHost),
new NTCredentials(data.getUserId(), data.getPassword(), null, null));
}
}

return httpclient;
return AwsToolkitHttpClient.builder()
.setConnectionTimeout(connectionTimeout)
.setSocketTimeout(socketTimeout)
.setUserAgent(AwsClientUtils.formatUserAgentString("AWS-Toolkit-For-Eclipse", plugin))
.setProxy(httpHost)
.setCredentialsProvider(credentialsProvider)
.build();
}

private static void configureProxy(DefaultHttpClient client, String url) {
private static IProxyData getEclipseProxyData(String url) {
AwsToolkitCore plugin = AwsToolkitCore.getDefault();
if (plugin != null) {
IProxyService proxyService =
AwsToolkitCore.getDefault().getProxyService();

if (proxyService.isProxiesEnabled()) {
try {
IProxyData[] proxyData;
proxyData = proxyService.select(new URI(url));
IProxyData[] proxyData = proxyService.select(new URI(url));
if (proxyData.length > 0) {

IProxyData data = proxyData[0];
client.getParams().setParameter(
ConnRoutePNames.DEFAULT_PROXY,
new HttpHost(data.getHost(), data.getPort()));

if (data.isRequiresAuthentication()) {
client.getCredentialsProvider().setCredentials(
new AuthScope(data.getHost(), data.getPort()),
new NTCredentials(data.getUserId(), data.getPassword(), null, null));
}
return proxyData[0];
}
} catch (URISyntaxException e) {
plugin.logError(e.getMessage(), e);
}
}
}

return null;
}

private HttpClientFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package com.amazonaws.eclipse.core.accounts;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -40,6 +43,7 @@
import com.amazonaws.eclipse.core.accounts.profiles.SdkProfilesCredentialsConfiguration;
import com.amazonaws.eclipse.core.preferences.PreferenceConstants;
import com.amazonaws.eclipse.core.ui.preferences.AwsAccountPreferencePage;
import com.amazonaws.eclipse.core.util.FileUtils;
import com.amazonaws.util.StringUtils;

/**
Expand Down Expand Up @@ -204,19 +208,21 @@ private void reloadProfileAccountInfo(final boolean boostrapCredentialsFile, fin

String credFileLocation = prefStore
.getString(PreferenceConstants.P_CREDENTIAL_PROFILE_FILE_LOCATION);
File credFile = new File(credFileLocation);

ProfilesConfigFile profileConfigFile = null;

try {
if (!credFile.exists() && boostrapCredentialsFile) {
Path credFilePath = Paths.get(credFileLocation);
if (!Files.exists(credFilePath) && boostrapCredentialsFile) {
File credFile = FileUtils.createFileWithPermission600(credFileLocation);
// TODO We need to reconsider whether to dump an empty credentials profile when we cannot find one.
ProfilesConfigFileWriter.dumpToFile(
credFile,
true, // overwrite=true
new Profile(PreferenceConstants.DEFAULT_ACCOUNT_NAME, new BasicAWSCredentials("", "")));
}

profileConfigFile = new ProfilesConfigFile(credFile);
profileConfigFile = new ProfilesConfigFile(credFilePath.toFile());
} catch (Exception e) {
String errorMsg = String.format("Failed to load credential profiles from (%s).",
credFileLocation);
Expand Down
Loading

0 comments on commit 8717ded

Please sign in to comment.