Skip to content

Instantly share code, notes, and snippets.

@huskercane
Created November 6, 2014 01:36
Show Gist options
  • Select an option

  • Save huskercane/c0a0e5663edb047e176b to your computer and use it in GitHub Desktop.

Select an option

Save huskercane/c0a0e5663edb047e176b to your computer and use it in GitHub Desktop.
Java class to trust all certs and hosts, needs wink and apache http 1.4
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.apache.wink.client.handlers.BasicAuthSecurityHandler;
import org.apache.wink.client.httpclient.ApacheHttpClientConfig;
public class WinkClient{
public static void main(String [] args) throws Exception {
String userName = "zztop";
String password = "somethingcool";
String host = "110.14.6.14";
SSLContextBuilder sslBuilder = new SSLContextBuilder();
sslBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
RestClient client = getRestClient(userName, password, httpclient);
resource = getFolderURI(host, client);
// resource.accept(MediaType.APPLICATION_JSON);
// resource.contentType(MediaType.APPLICATION_JSON);
ClientResponse post = resource.put("rohit");
System.out.println(post.getMessage());
System.out.println(post.getStatusCode());
System.out.println(post);
}
}
/**
* @param host
* @param client
* @return
*/
private static Resource getFolderURI(String host, RestClient client) {
UriBuilder builder;
Resource resource;
builder = UriBuilder.fromUri("https://" + host).path("folder").path("{folderName}").queryParam("dcPath", "{dcPath}").queryParam("dsName", "{dsName}");
URI build = builder.build("myiso.iso", "ha-datacenter", "iscsiDataStore1");
System.out.println(build.toString());
resource = client.resource(build);
return resource;
}
/**
* @param userName
* @param password
* @param httpclient
* @return
*/
private static RestClient getRestClient(String userName, String password, CloseableHttpClient httpclient) {
ApacheHttpClientConfig config = new ApacheHttpClientConfig(httpclient);
config.setBypassHostnameVerification(true);
BasicAuthSecurityHandler basicAuthHandler = new BasicAuthSecurityHandler();
basicAuthHandler.setUserName(userName);
basicAuthHandler.setPassword(password);
config.handlers(basicAuthHandler);
RestClient client = new RestClient(config);
return client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment