This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/main/java/at/co/blogspot/javaskeleton/WebClientDevWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package at.co.blogspot.javaskeleton; | ||
|
||
import java.io.IOException; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLException; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.X509TrustManager; | ||
import javax.net.ssl.SSLSocket; | ||
import javax.net.ssl.SSLSession; | ||
|
||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.conn.ClientConnectionManager; | ||
import org.apache.http.conn.scheme.Scheme; | ||
import org.apache.http.conn.scheme.SchemeRegistry; | ||
import org.apache.http.conn.ssl.SSLSocketFactory; | ||
import org.apache.http.conn.ssl.X509HostnameVerifier; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
import org.apache.log4j.Logger; | ||
|
||
import de.perdoctus.synolib.RequestExecutor; | ||
|
||
/* | ||
* http://javaskeleton.blogspot.co.at/2010/07/avoiding-peer-not-authenticated-with.html | ||
* This code is public domain: you are free to use, link and/or modify it in any way you want, for all purposes including commercial applications. | ||
*/ | ||
public class WebClientDevWrapper { | ||
|
||
|
||
private static final Logger LOG = Logger.getLogger(RequestExecutor.class); | ||
|
||
|
||
public static HttpClient wrapClient(HttpClient base) { | ||
return wrapClient(base, 443); | ||
} | ||
|
||
public static HttpClient wrapClient(HttpClient base, int port) { | ||
try { | ||
SSLContext ctx = SSLContext.getInstance("TLS"); | ||
X509TrustManager tm = new X509TrustManager() { | ||
|
||
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { | ||
} | ||
|
||
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { | ||
} | ||
|
||
public X509Certificate[] getAcceptedIssuers() { | ||
return null; | ||
} | ||
}; | ||
X509HostnameVerifier verifier = new X509HostnameVerifier() { | ||
|
||
@Override | ||
public void verify(String string, SSLSocket ssls) throws IOException { | ||
} | ||
|
||
@Override | ||
public void verify(String string, X509Certificate xc) throws SSLException { | ||
} | ||
|
||
@Override | ||
public void verify(String string, String[] strings, String[] strings1) throws SSLException { | ||
} | ||
|
||
@Override | ||
public boolean verify(String string, SSLSession ssls) { | ||
return true; | ||
} | ||
}; | ||
ctx.init(null, new TrustManager[]{tm}, null); | ||
SSLSocketFactory ssf = new SSLSocketFactory(ctx); | ||
ssf.setHostnameVerifier(verifier); | ||
ClientConnectionManager ccm = base.getConnectionManager(); | ||
SchemeRegistry sr = ccm.getSchemeRegistry(); | ||
sr.register(new Scheme("https", ssf, port)); | ||
return new DefaultHttpClient(ccm, base.getParams()); | ||
} catch (Exception ex) { | ||
LOG.error("Error enabling https-connections", ex); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters