List of usage examples for com.squareup.okhttp OkHttpClient newCall
public Call newCall(Request request)
From source file:io.fabric8.devops.connector.DevOpsConnector.java
License:Apache License
protected void createGerritRepo(String repoName, String gerritUser, String gerritPwd, String gerritGitInitialCommit, String gerritGitRepoDescription) throws Exception { // lets add defaults if not env vars final String username = Strings.isNullOrBlank(gerritUser) ? "admin" : gerritUser; final String password = Strings.isNullOrBlank(gerritPwd) ? "secret" : gerritPwd; log.info("A Gerrit git repo will be created for this name : " + repoName); String gerritAddress = KubernetesHelper.getServiceURL(kubernetes, ServiceNames.GERRIT, namespace, "http", true);/*from w w w. ja v a 2 s . c o m*/ log.info("Found gerrit address: " + gerritAddress + " for namespace: " + namespace + " on Kubernetes address: " + kubernetes.getMasterUrl()); if (Strings.isNullOrBlank(gerritAddress)) { throw new Exception("No address for service " + ServiceNames.GERRIT + " in namespace: " + namespace + " on Kubernetes address: " + kubernetes.getMasterUrl()); } String GERRIT_URL = gerritAddress + "/a/projects/" + repoName; OkHttpClient client = new OkHttpClient(); if (isNotNullOrEmpty(gerritUser) && isNotNullOrEmpty(gerritPwd)) { client.setAuthenticator(new Authenticator() { @Override public Request authenticate(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); for (int i = 0, size = challenges.size(); i < size; i++) { Challenge challenge = challenges.get(i); if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue; String credential = Credentials.basic(username, password); return request.newBuilder().header("Authorization", credential).build(); } return null; } @Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException { return null; } }); } System.out.println("Requesting : " + GERRIT_URL); try { CreateRepositoryDTO createRepoDTO = new CreateRepositoryDTO(); createRepoDTO.setDescription(gerritGitRepoDescription); createRepoDTO.setName(repoName); createRepoDTO.setCreate_empty_commit(Boolean.valueOf(gerritGitInitialCommit)); RequestBody body = RequestBody.create(JSON, MAPPER.writeValueAsString(createRepoDTO)); Request request = new Request.Builder().post(body).url(GERRIT_URL).build(); Response response = client.newCall(request).execute(); System.out.println("responseBody : " + response.body().string()); } catch (Throwable t) { throw new RuntimeException(t); } finally { if (client != null && client.getConnectionPool() != null) { client.getConnectionPool().evictAll(); } } }
From source file:io.fabric8.docker.client.impl.BuildImage.java
License:Apache License
@Override public OutputHandle fromTar(String path) { try {/* w w w . j a v a 2 s . c om*/ StringBuilder sb = new StringBuilder(); sb.append(getOperationUrl()); sb.append(Q).append(DOCKER_FILE).append(EQUALS).append(dockerFile); if (alwaysRemoveIntermediate) { sb.append(A).append(ALWAYS_REMOVE_INTERMEDIATE).append(EQUALS).append(alwaysRemoveIntermediate); } else { sb.append(A).append(REMOVE_INTERMEDIATE_ON_SUCCESS).append(EQUALS) .append(removeIntermediateOnSuccess); } sb.append(A).append(PULL).append(EQUALS).append(pulling); sb.append(A).append(SUPRESS_VERBOSE_OUT).append(EQUALS).append(supressingVerboseOutput); sb.append(A).append(NOCACHE).append(EQUALS).append(noCache); if (cpuPeriodMicros >= 0) { sb.append(A).append(CPU_PERIOD).append(EQUALS).append(cpuPeriodMicros); } if (cpuQuotaMicros >= 0) { sb.append(A).append(CPU_QUOTA).append(EQUALS).append(cpuQuotaMicros); } if (cpuShares >= 0) { sb.append(A).append(CPU_SHARES).append(EQUALS).append(cpuShares); } if (cpus > 0) { sb.append(A).append(CPUS).append(EQUALS).append(cpus); } if (Utils.isNotNullOrEmpty(memorySize)) { sb.append(A).append(MEMORY).append(EQUALS).append(memorySize); } if (Utils.isNotNullOrEmpty(swapSize)) { sb.append(A).append(MEMSWAP).append(EQUALS).append(swapSize); } if (Utils.isNotNullOrEmpty(buildArgs)) { sb.append(A).append(BUILD_ARGS).append(EQUALS).append(buildArgs); } if (Utils.isNotNullOrEmpty(repositoryName)) { sb.append(A).append(REPOSITORY_NAME).append(EQUALS).append(repositoryName); } RequestBody body = RequestBody.create(MEDIA_TYPE_TAR, new File(path)); Request request = new Request.Builder() .header("X-Registry-Config", new String(Base64.encodeBase64( JSON_MAPPER.writeValueAsString(config.getAuthConfigs()).getBytes("UTF-8")), "UTF-8")) .post(body).url(sb.toString()).build(); OkHttpClient clone = client.clone(); clone.setReadTimeout(config.getImageBuildTimeout(), TimeUnit.MILLISECONDS); BuildImageHandle handle = new BuildImageHandle(out, config.getImageBuildTimeout(), TimeUnit.MILLISECONDS, listener); clone.newCall(request).enqueue(handle); return handle; } catch (Exception e) { throw DockerClientException.launderThrowable(e); } }
From source file:io.fabric8.docker.client.impl.EventOperationImpl.java
License:Apache License
@Override public OutputHandle list() { try {/*from w w w. ja v a 2s. c o m*/ StringBuilder sb = new StringBuilder(); sb.append(getOperationUrl()).append(Q).append(FILTERS).append(EQUALS) .append(JSON_MAPPER.writeValueAsString(filters)); if (Utils.isNotNullOrEmpty(since)) { sb.append(A).append(SINCE).append(EQUALS).append(since); } if (Utils.isNotNullOrEmpty(until)) { sb.append(A).append(UNTIL).append(EQUALS).append(until); } Request request = new Request.Builder().get().url(sb.toString()).build(); OkHttpClient clone = client.clone(); clone.setReadTimeout(0, TimeUnit.NANOSECONDS); EventHandle handle = new EventHandle(new PipedOutputStream(), config.getRequestTimeout(), TimeUnit.MILLISECONDS); clone.newCall(request).enqueue(handle); return handle; } catch (Exception e) { throw DockerClientException.launderThrowable(e); } }
From source file:io.fabric8.docker.client.impl.GetLogsOfContainer.java
License:Apache License
private OutputHandle doGetLogHandle(Boolean follow) { try {/*ww w. j a v a2s .c om*/ StringBuilder sb = new StringBuilder(); sb.append(URLUtils.join(getOperationUrl().toString())); sb.append("?").append(FOLLOW).append("=").append(follow ? TRUE : FLASE); sb.append("&").append(STDOUT).append("=").append(out != null || outPipe != null ? TRUE : FLASE); sb.append("&").append(STDERR).append("=").append(err != null || errPipe != null ? TRUE : FLASE); if (isNotNullOrEmpty(since)) { sb.append("&").append(SINCE).append("=").append(since); } if (lines > 0) { sb.append("&").append(TAIL).append("=").append(lines); } else { sb.append("&").append(TAIL).append("=").append(ALL); } if (timestampsEnabled) { sb.append("&").append(TIMESTAMPS).append("=").append(TRUE); } Request request = new Request.Builder().url(sb.toString()).get().build(); OkHttpClient clone = client.clone(); clone.setReadTimeout(0, TimeUnit.MILLISECONDS); final ContainerLogHandle handle = new ContainerLogHandle(out, err, outPipe, errPipe); clone.newCall(request).enqueue(handle); handle.waitUntilReady(); return handle; } catch (Throwable t) { throw DockerClientException.launderThrowable(t); } }
From source file:io.fabric8.docker.client.impl.ImageOperationImpl.java
License:Apache License
@Override public List<SearchResult> search(String term) { try {/*w ww . ja va 2 s. co m*/ StringBuilder sb = new StringBuilder().append(getOperationUrl(SEARCH_OPERATION)).append(Q).append(TERM) .append(EQUALS).append(term); Request request = new Request.Builder().get().url(new URL(sb.toString())).get().build(); Response response = null; try { OkHttpClient clone = client.clone(); clone.setReadTimeout(config.getImageSearchTimeout(), TimeUnit.MILLISECONDS); response = clone.newCall(request).execute(); assertResponseCodes(request, response, 200); } catch (Exception e) { throw requestException(request, e); } return JSON_MAPPER.readValue(response.body().byteStream(), IMAGE_SEARCH_RESULT_LIST); } catch (Exception e) { throw DockerClientException.launderThrowable(e); } }
From source file:io.fabric8.docker.client.impl.ImportImage.java
License:Apache License
@Override public OutputHandle asRepo(String src) { try {/*from w w w . ja va2s . co m*/ StringBuilder sb = new StringBuilder().append(getOperationUrl(CREATE_OPERATION)); if (Utils.isNotNullOrEmpty(tag)) { sb.append(Q).append(TAG).append(EQUALS).append(tag); } AuthConfig authConfig = RegistryUtils.getConfigForImage(name, config); Request request = new Request.Builder() .header("X-Registry-Auth", new String(Base64.encodeBase64(JSON_MAPPER .writeValueAsString(authConfig != null ? authConfig : new AuthConfig()) .getBytes("UTF-8")), "UTF-8")) .post(RequestBody.create(MEDIA_TYPE_TEXT, EMPTY)).url(sb.toString()).build(); OkHttpClient clone = client.clone(); clone.setReadTimeout(0, TimeUnit.MILLISECONDS); ImportImageHandle handle = new ImportImageHandle(out, config.getImagePushTimeout(), TimeUnit.MILLISECONDS, listener); clone.newCall(request).enqueue(handle); return handle; } catch (Exception e) { throw DockerClientException.launderThrowable(e); } }
From source file:io.fabric8.docker.client.impl.PullImage.java
License:Apache License
@Override public OutputHandle fromRegistry() { try {/* w ww .ja v a 2 s .c o m*/ StringBuilder sb = new StringBuilder().append(getOperationUrl(CREATE_OPERATION)).append(Q) .append(FROM_IMAGE).append(EQUALS).append(name); if (Utils.isNotNullOrEmpty(tag)) { sb.append(A).append(TAG).append(EQUALS).append(tag); } AuthConfig authConfig = RegistryUtils.getConfigForImage(name, config); Request request = new Request.Builder() .header("X-Registry-Auth", new String(Base64.encodeBase64(JSON_MAPPER .writeValueAsString(authConfig != null ? authConfig : new AuthConfig()) .getBytes("UTF-8")), "UTF-8")) .post(RequestBody.create(MEDIA_TYPE_TEXT, EMPTY)).url(sb.toString()).build(); OkHttpClient clone = client.clone(); clone.setReadTimeout(config.getImagePullTimeout(), TimeUnit.MILLISECONDS); PullImageHandle handle = new PullImageHandle(out, config.getImagePushTimeout(), TimeUnit.MILLISECONDS, listener); clone.newCall(request).enqueue(handle); return handle; } catch (Exception e) { throw DockerClientException.launderThrowable(e); } }
From source file:io.fabric8.docker.client.impl.PushImage.java
License:Apache License
@Override public OutputHandle toRegistry() { try {/*ww w . j a va 2 s . c om*/ StringBuilder sb = new StringBuilder().append(getOperationUrl(PUSH_OPERATION)); sb.append(Q).append(FORCE).append(EQUALS).append(force); if (Utils.isNotNullOrEmpty(tag)) { sb.append(A).append(TAG).append(EQUALS).append(tag); } AuthConfig authConfig = RegistryUtils.getConfigForImage(name, config); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, "{}"); Request request = new Request.Builder() .header("X-Registry-Auth", new String(Base64.encodeBase64(JSON_MAPPER .writeValueAsString(authConfig != null ? authConfig : new AuthConfig()) .getBytes("UTF-8")), "UTF-8")) .post(body).url(sb.toString()).build(); OkHttpClient clone = client.clone(); clone.setReadTimeout(config.getImagePushTimeout(), TimeUnit.MILLISECONDS); PushImageHandle handle = new PushImageHandle(out, config.getImagePushTimeout(), TimeUnit.MILLISECONDS, listener); clone.newCall(request).enqueue(handle); return handle; } catch (Exception e) { throw DockerClientException.launderThrowable(e); } }
From source file:io.fabric8.docker.client.utils.SSLUtils.java
License:Apache License
public static boolean isHttpsAvailable(Config config) { Config sslConfig = new ConfigBuilder(config) .withDockerUrl(URLUtils.withProtocol(config.getDockerUrl(), Config.HTTPS_PROTOCOL_PREFIX)) .withRequestTimeout(1000).withConnectionTimeout(1000).build(); OkHttpClient client = HttpClientUtils.createHttpClient(config); Response response = null;//from w w w . j ava 2 s .com try { Request request = new Request.Builder().get().url(sslConfig.getDockerUrl()).build(); response = client.newCall(request).execute(); return response.handshake() != null; } catch (Throwable t) { LOG.warn("SSL handshake failed. Falling back to insecure connection."); } finally { if (response != null) { try { response.body().close(); } catch (IOException e) { //ignore } } if (client != null && client.getConnectionPool() != null) { client.getConnectionPool().evictAll(); } } return false; }
From source file:io.fabric8.kubernetes.client.internal.SSLUtils.java
License:Apache License
public static boolean isHttpsAvailable(Config config) { Config sslConfig = new ConfigBuilder(config) .withMasterUrl(Config.HTTPS_PROTOCOL_PREFIX + config.getMasterUrl()).withRequestTimeout(1000) .withConnectionTimeout(1000).build(); OkHttpClient client = HttpClientUtils.createHttpClient(config); try {//from w w w. ja v a 2 s .c o m Request request = new Request.Builder().get().url(sslConfig.getMasterUrl()).build(); Response response = client.newCall(request).execute(); try (ResponseBody body = response.body()) { return response.isSuccessful(); } } catch (Throwable t) { LOG.warn("SSL handshake failed. Falling back to insecure connection."); } finally { if (client != null && client.getConnectionPool() != null) { client.getConnectionPool().evictAll(); } } return false; }