Skip to content

Instantly share code, notes, and snippets.

@uphy
Created February 23, 2017 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uphy/db6fb9d24882bdaf104bc7d8b81fbedb to your computer and use it in GitHub Desktop.
Save uphy/db6fb9d24882bdaf104bc7d8b81fbedb to your computer and use it in GitHub Desktop.
package jp.uphy.wso2apim;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.axis2.client.Stub;
import org.apache.commons.httpclient.Header;
import org.wso2.carbon.analytics.webservice.stub.AnalyticsWebServiceStub;
import org.wso2.carbon.apimgt.gateway.stub.APIGatewayAdminStub;
import org.wso2.carbon.registry.extensions.stub.ResourceAdminServiceStub;
public class Wso2Client {
static {
disableCertificate();
}
private String baseUrl;
private String user;
private String password;
private ResourceAdminServiceStub resourceAdminServiceStub;
private APIGatewayAdminStub apiGatewayAdminStub;
private AnalyticsWebServiceStub analyticsWebServiceStub;
public Wso2Client(String baseUrl, String user, String password) {
this.baseUrl = baseUrl;
this.user = user;
this.password = password;
try {
final List<Header> headers = new ArrayList<>();
headers.add(new Header("Authorization",
"Basic " + Base64.getEncoder().encodeToString(String.format("%s:%s", user, password).getBytes())));
this.resourceAdminServiceStub = new ResourceAdminServiceStub(this.baseUrl + "ResourceAdminService");
this.apiGatewayAdminStub = new APIGatewayAdminStub(this.baseUrl + "APIGatewayAdmin");
this.analyticsWebServiceStub = new AnalyticsWebServiceStub(
"https://localhost:9444/services/AnalyticsWebService");
for (Stub stub : Arrays.asList(this.resourceAdminServiceStub, this.apiGatewayAdminStub,
this.analyticsWebServiceStub)) {
stub._getServiceClient().getOptions().setProperty("HTTP_HEADERS", headers);
}
} catch (Throwable e) {
throw new IllegalStateException(e);
}
}
public String getResourceTextContent(String path) throws IOException {
try {
return resourceAdminServiceStub.getTextContent(path);
} catch (Throwable e) {
throw new IOException(e);
}
}
public void updateTextContent(String path, String content) throws IOException {
try {
resourceAdminServiceStub.updateTextContent(path, content);
} catch (Throwable e) {
throw new IOException(e);
}
}
static synchronized void disableCertificate() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
} };
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
SSLContext.setDefault(sc);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (GeneralSecurityException e) {
throw new IllegalStateException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment