Skip to content

Commit

Permalink
[dubbo-6756]ssl support load from classpath (apache#6756)
Browse files Browse the repository at this point in the history
* ssl support load from classpath

* delete no need import

* check dubbo compile bug
  • Loading branch information
owen200008 committed Mar 11, 2021
1 parent d225d64 commit 5602191
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,8 @@ public interface CommonConstants {

String CLUSTER_REDIS = "cluster";

/** Pseudo URL prefix for loading from the class path: "classpath:". */
String CLASSPATH_URL_PREFIX = "classpath:";

String DEFAULT_VERSION = "0.0.0";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.common.constants.CommonConstants;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -30,6 +31,9 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -228,4 +232,38 @@ public static void appendLines(File file, String[] lines) throws IOException {
writeLines(new FileOutputStream(file, true), lines);
}


/**
* use like spring code
* @param resourceLocation
* @return
*/
public static URL getURL(String resourceLocation) throws FileNotFoundException {
Assert.notNull(resourceLocation, "Resource location must not be null");
if (resourceLocation.startsWith(CommonConstants.CLASSPATH_URL_PREFIX)) {
String path = resourceLocation.substring(CommonConstants.CLASSPATH_URL_PREFIX.length());
ClassLoader cl = ClassUtils.getClassLoader();
URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));
if (url == null) {
String description = "class path resource [" + path + "]";
throw new FileNotFoundException(description +
" cannot be resolved to URL because it does not exist");
}
return url;
}
try {
// try URL
return new URL(resourceLocation);
}
catch (MalformedURLException ex) {
// no URL -> treat as file path
try {
return new File(resourceLocation).toURI().toURL();
}
catch (MalformedURLException ex2) {
throw new FileNotFoundException("Resource location [" + resourceLocation +
"] is neither a URL not a well-formed file path");
}
}
}
}
28 changes: 14 additions & 14 deletions dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.IOUtils;
import org.apache.dubbo.config.support.Parameter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -120,9 +120,9 @@ public void setClientTrustCertCollectionPath(String clientTrustCertCollectionPat
this.clientTrustCertCollectionPath = clientTrustCertCollectionPath;
}

public InputStream getServerKeyCertChainPathStream() throws FileNotFoundException {
public InputStream getServerKeyCertChainPathStream() throws IOException {
if (serverKeyCertChainPath != null) {
serverKeyCertChainPathStream = new FileInputStream(serverKeyCertChainPath);
serverKeyCertChainPathStream = IOUtils.getURL(serverKeyCertChainPath).openStream();
}
return serverKeyCertChainPathStream;
}
Expand All @@ -131,9 +131,9 @@ public void setServerKeyCertChainPathStream(InputStream serverKeyCertChainPathSt
this.serverKeyCertChainPathStream = serverKeyCertChainPathStream;
}

public InputStream getServerPrivateKeyPathStream() throws FileNotFoundException {
public InputStream getServerPrivateKeyPathStream() throws IOException {
if (serverPrivateKeyPath != null) {
serverPrivateKeyPathStream = new FileInputStream(serverPrivateKeyPath);
serverPrivateKeyPathStream = IOUtils.getURL(serverPrivateKeyPath).openStream();
}
return serverPrivateKeyPathStream;
}
Expand All @@ -142,9 +142,9 @@ public void setServerPrivateKeyPathStream(InputStream serverPrivateKeyPathStream
this.serverPrivateKeyPathStream = serverPrivateKeyPathStream;
}

public InputStream getServerTrustCertCollectionPathStream() throws FileNotFoundException {
public InputStream getServerTrustCertCollectionPathStream() throws IOException {
if (serverTrustCertCollectionPath != null) {
serverTrustCertCollectionPathStream = new FileInputStream(serverTrustCertCollectionPath);
serverTrustCertCollectionPathStream = IOUtils.getURL(serverTrustCertCollectionPath).openStream();
}
return serverTrustCertCollectionPathStream;
}
Expand All @@ -153,9 +153,9 @@ public void setServerTrustCertCollectionPathStream(InputStream serverTrustCertCo
this.serverTrustCertCollectionPathStream = serverTrustCertCollectionPathStream;
}

public InputStream getClientKeyCertChainPathStream() throws FileNotFoundException {
public InputStream getClientKeyCertChainPathStream() throws IOException {
if (clientKeyCertChainPath != null) {
clientKeyCertChainPathStream = new FileInputStream(clientKeyCertChainPath);
clientKeyCertChainPathStream = IOUtils.getURL(clientKeyCertChainPath).openStream();
}
return clientKeyCertChainPathStream;
}
Expand All @@ -164,9 +164,9 @@ public void setClientKeyCertChainPathStream(InputStream clientKeyCertChainPathSt
this.clientKeyCertChainPathStream = clientKeyCertChainPathStream;
}

public InputStream getClientPrivateKeyPathStream() throws FileNotFoundException {
public InputStream getClientPrivateKeyPathStream() throws IOException {
if (clientPrivateKeyPath != null) {
clientPrivateKeyPathStream = new FileInputStream(clientPrivateKeyPath);
clientPrivateKeyPathStream = IOUtils.getURL(clientPrivateKeyPath).openStream();
}
return clientPrivateKeyPathStream;
}
Expand All @@ -175,9 +175,9 @@ public void setClientPrivateKeyPathStream(InputStream clientPrivateKeyPathStream
this.clientPrivateKeyPathStream = clientPrivateKeyPathStream;
}

public InputStream getClientTrustCertCollectionPathStream() throws FileNotFoundException {
public InputStream getClientTrustCertCollectionPathStream() throws IOException {
if (clientTrustCertCollectionPath != null) {
clientTrustCertCollectionPathStream = new FileInputStream(clientTrustCertCollectionPath);
clientTrustCertCollectionPathStream = IOUtils.getURL(clientTrustCertCollectionPath).openStream();
}
return clientTrustCertCollectionPathStream;
}
Expand Down

0 comments on commit 5602191

Please sign in to comment.