Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion enos-http-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>enos-http</artifactId>
<version>0.2.1</version>
<version>2.4.0</version>

<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static Part createFormData(@NonNull UploadFileInfo fileInfo) throws IOExc
.addUnsafeNonAscii("Content-MD5", md5(fileInfo.getFile()))
.build();

return Part.create(headers,
return MultipartBody.Part.create(headers,
RequestBody.create(MediaType.parse(HttpConnection.MEDIA_TYPE_OCTET_STREAM), fileInfo.getFile()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,33 @@
* @date 2020/2/11 9:28
*/
public class EccSSLSocketFactory extends SSLSocketFactory {
private static final String[] ECC_CIPHER_SUITES = new String[]{
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"
};
private static final String[] RSA_CIPHER_SUITES = new String[]{
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384",
"TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256",
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
"TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384",
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
"TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256"
};

private final String[] cipherSuites;

private SSLSocketFactory factory;

EccSSLSocketFactory(SSLSocketFactory factory) {
private Boolean isEccConnect;

EccSSLSocketFactory(SSLSocketFactory factory, Boolean isEccConnect) {
this.factory = factory;
this.isEccConnect = isEccConnect;
this.cipherSuites = Boolean.TRUE.equals(isEccConnect)
? ECC_CIPHER_SUITES :
RSA_CIPHER_SUITES;
}

@Override
Expand All @@ -29,12 +52,12 @@ public Socket createSocket() throws IOException {

@Override
public String[] getDefaultCipherSuites() {
return new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"};
return cipherSuites;
}

@Override
public String[] getSupportedCipherSuites() {
return new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"};
return cipherSuites;
}

@Override
Expand All @@ -44,7 +67,11 @@ public Socket createSocket(Socket socket, String s, int i, boolean b) throws IOE

private Socket encapsulated(Socket socket) {
if (socket instanceof SSLSocket) {
((SSLSocket) socket).setEnabledCipherSuites(getDefaultCipherSuites());
if (isEccConnect) {
((SSLSocket) socket).setEnabledCipherSuites(ECC_CIPHER_SUITES);
} else {
((SSLSocket) socket).setEnabledCipherSuites(RSA_CIPHER_SUITES);
}
}
return socket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,25 @@ private static OkHttpClient buildOkHttpsClient(boolean isEccConnect, String jksP
X509TrustManager trustManagerVerifyCa = (X509TrustManager) trustManagers[0];
// TSL or SSL
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, new SecureRandom());

TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates,
String s) throws CertificateException {}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates,
String s) throws CertificateException {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
};
sslContext.init(keyManagers, trustAllCerts, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

if (isEccConnect) {
sslSocketFactory = new EccSSLSocketFactory(sslSocketFactory);
}
sslSocketFactory = new EccSSLSocketFactory(sslSocketFactory, isEccConnect);

// check cert
okHttpBuilder.sslSocketFactory(sslSocketFactory, trustManagerVerifyCa);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@
package com.envisioniot.enos.iot_mqtt_sdk.core;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
* Factory that provides thread pools for handling connect, publish, time-out scheduler,
* async callback. Note that this factory is per MqttClient. <br/>
* <br/>
* User can customize the relevant thread pool through the provided <b>set</b> method.
*
* @author zhensheng.cai
* @author jian.zhang4
*/
public class ExecutorFactory implements IExecutorFactory {

/**
* Thread pools that handles mqtt publish action in async way.
*/
private ExecutorService publishExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("publish-executor-%d").build());

/**
* We should only have one connect action per MqttClient at the same time. So one
* thread should be good enough to handle async connection.
*/
private ExecutorService connectExecutor = Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder().setNameFormat("connect-executor-%d").build());

/**
* callback timeout pool
*/
private ScheduledExecutorService timeoutScheduler = new ScheduledThreadPoolExecutor(3,
new ThreadFactoryBuilder().setNameFormat("timeout-pool-%d").build());

/**
* Thread pools that execute async callback provided by user
*/
private ExecutorService callbackExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setNameFormat("callback-executor-%d").build());


/**
* shutdown all thread pools managed by this factory
*/
public void shutdownExecutorServices() {
publishExecutor.shutdownNow();
connectExecutor.shutdownNow();
timeoutScheduler.shutdownNow();
callbackExecutor.shutdownNow();
}

public ExecutorService getPublishExecutor() {
return publishExecutor;
}

public void setPublishExecutor(ExecutorService publishExecutor) {
this.publishExecutor = publishExecutor;
}

public ExecutorService getConnectExecutor() {
return connectExecutor;
}

public void setConnectExecutor(ExecutorService connectExecutor) {
this.connectExecutor = connectExecutor;
}

public ScheduledExecutorService getTimeoutScheduler() {
return timeoutScheduler;
}

public void setTimeoutScheduler(ScheduledExecutorService timeoutScheduler) {
this.timeoutScheduler = timeoutScheduler;
}

public ExecutorService getCallbackExecutor() {
return callbackExecutor;
}

public void setCallbackExecutor(ExecutorService callbackExecutor) {
this.callbackExecutor = callbackExecutor;
}
}
package com.envisioniot.enos.iot_mqtt_sdk.core;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
/**
* Factory that provides thread pools for handling connect, publish, time-out scheduler,
* async callback. Note that this factory is per MqttClient. <br/>
* <br/>
* User can customize the relevant thread pool through the provided <b>set</b> method.
*
* @author zhensheng.cai
* @author jian.zhang4
*/
public class ExecutorFactory implements IExecutorFactory {
/**
* Thread pools that handles mqtt publish action in async way.
*/
private ExecutorService publishExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("publish-executor-%d").build());
/**
* We should only have one connect action per MqttClient at the same time. So one
* thread should be good enough to handle async connection.
*/
private ExecutorService connectExecutor = Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder().setNameFormat("connect-executor-%d").build());
/**
* callback timeout pool
*/
private ScheduledExecutorService timeoutScheduler = new ScheduledThreadPoolExecutor(3,
new ThreadFactoryBuilder().setNameFormat("timeout-pool-%d").build());
/**
* Thread pools that execute async callback provided by user
*/
private ExecutorService callbackExecutor = new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setNameFormat("callback-executor-%d").build());
/**
* shutdown all thread pools managed by this factory
*/
public void shutdownExecutorServices() {
publishExecutor.shutdownNow();
connectExecutor.shutdownNow();
timeoutScheduler.shutdownNow();
callbackExecutor.shutdownNow();
}
public ExecutorService getPublishExecutor() {
return publishExecutor;
}
public void setPublishExecutor(ExecutorService publishExecutor) {
this.publishExecutor = publishExecutor;
}
public ExecutorService getConnectExecutor() {
return connectExecutor;
}
public void setConnectExecutor(ExecutorService connectExecutor) {
this.connectExecutor = connectExecutor;
}
public ScheduledExecutorService getTimeoutScheduler() {
return timeoutScheduler;
}
public void setTimeoutScheduler(ScheduledExecutorService timeoutScheduler) {
this.timeoutScheduler = timeoutScheduler;
}
public ExecutorService getCallbackExecutor() {
return callbackExecutor;
}
public void setCallbackExecutor(ExecutorService callbackExecutor) {
this.callbackExecutor = callbackExecutor;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.envisioniot.enos.iot_mqtt_sdk.core;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;

public interface IExecutorFactory {

void shutdownExecutorServices();

ExecutorService getPublishExecutor();

ExecutorService getConnectExecutor();

ScheduledExecutorService getTimeoutScheduler();

ExecutorService getCallbackExecutor();
}
package com.envisioniot.enos.iot_mqtt_sdk.core;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
public interface IExecutorFactory {
void shutdownExecutorServices();
ExecutorService getPublishExecutor();
ExecutorService getConnectExecutor();
ScheduledExecutorService getTimeoutScheduler();
ExecutorService getCallbackExecutor();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.envisioniot.enos.iot_mqtt_sdk.core;

import java.lang.annotation.*;

@Inherited
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Sharable {

}
package com.envisioniot.enos.iot_mqtt_sdk.core;
import java.lang.annotation.*;
@Inherited
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Sharable {
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.envisioniot.enos.iot_mqtt_sdk.core.internals;

import com.envisioniot.enos.iot_mqtt_sdk.util.StringUtil;

public class Utils {

static String getRootMessage(Throwable error) {
if (error == null) {
return "";
}

if (error.getCause() == null) {
return StringUtil.isNotEmpty(error.getMessage()) ? error.getMessage() : error.getClass().getName();
}

return getRootMessage(error.getCause());
}


}
package com.envisioniot.enos.iot_mqtt_sdk.core.internals;
import com.envisioniot.enos.iot_mqtt_sdk.util.StringUtil;
public class Utils {
static String getRootMessage(Throwable error) {
if (error == null) {
return "";
}
if (error.getCause() == null) {
return StringUtil.isNotEmpty(error.getMessage()) ? error.getMessage() : error.getClass().getName();
}
return getRootMessage(error.getCause());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void main(String[] args) throws EnvisionException {
connection.downloadFileAsync(fileUri, FileCategory.FEATURE, new IFileCallback() {
@Override
public void onResponse(InputStream inputStream) throws IOException {
System.out.println("download feature ile asynchronously");
System.out.println("download feature file asynchronously");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[bufferLength];
int len;
Expand Down
Loading