List of usage examples for com.squareup.okhttp OkHttpClient newCall
public Call newCall(Request request)
From source file:io.fabric8.openshift.client.internal.OpenShiftOAuthInterceptor.java
License:Apache License
private String authorize() { try {/*from w w w. j ava 2 s . co m*/ OkHttpClient clone = client.clone(); clone.interceptors().remove(this); String credential = Credentials.basic(config.getUsername(), new String(config.getPassword())); URL url = new URL(URLUtils.join(config.getMasterUrl(), AUTHORIZE_PATH)); Response response = clone .newCall(new Request.Builder().get().url(url).header(AUTHORIZATION, credential).build()) .execute(); response.body().close(); String token = response.priorResponse().networkResponse().header(LOCATION); token = token.substring(token.indexOf(BEFORE_TOKEN) + BEFORE_TOKEN.length()); token = token.substring(0, token.indexOf(AFTER_TOKEN)); return token; } catch (Exception e) { throw KubernetesClientException.launderThrowable(e); } }
From source file:io.github.baoliandeng.core.LocalVpnService.java
License:Open Source License
@Override public synchronized void run() { try {// w w w. j a v a 2 s . co m Log.d(Constant.TAG, "VPNService work thread is running... " + ID); ProxyConfig.AppInstallID = getAppInstallID(); ProxyConfig.AppVersion = getVersionName(); writeLog("Android version: %s", Build.VERSION.RELEASE); writeLog("App version: %s", ProxyConfig.AppVersion); waitUntilPreapred(); Client.Provider.Stub stub = new Client.Provider.Stub() { public boolean Verbose() { return ProxyConfig.IS_DEBUG; } public String Model() { return model; } public String Device() { return device; } public String Version() { return version; } public String AppName() { return "Lantern"; } public String SettingsDir() { return getFilesDir().getPath(); } public void Protect(long fd) { protect((int) fd); } }; Client.Configure(stub); Client.Start(stub); ChinaIpMaskManager.loadFromFile(getResources().openRawResource(R.raw.ipmask)); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("https://myhosts.sinaapp.com/blacklist.txt").build(); try { Response response = client.newCall(request).execute(); String body = response.body().string(); m_Blacklist = body.split("\\r?\\n"); } catch (IOException e) { Log.e(Constant.TAG, "Unable to fetch blacklist"); } runVPN(); } catch (InterruptedException e) { Log.e(Constant.TAG, "Exception", e); } catch (Exception e) { e.printStackTrace(); writeLog("Fatal error: %s", e.toString()); } writeLog("BaoLianDeng terminated."); dispose(); }
From source file:io.kodokojo.bdd.stage.AccessRestWhen.java
License:Open Source License
public SELF try_to_access_to_list_of_brick_available() { UserInfo currentUser = currentUsers.get(currentUserLogin); OkHttpClient httpClient = new OkHttpClient(); Request.Builder builder = new Request.Builder().url(getBaseUrl() + "/api/v1/brick").get(); builder = HttpUserSupport.addBasicAuthentification(currentUser, builder); Response response = null;//from www. j a v a 2s.com try { response = httpClient.newCall(builder.build()).execute(); Gson gson = new GsonBuilder().create(); JsonParser parser = new JsonParser(); String payload = response.body().string(); if (LOGGER.isTraceEnabled()) { LOGGER.trace(payload); } JsonArray root = (JsonArray) parser.parse(payload); for (JsonElement el : root) { brickAvailable.add(gson.fromJson(el, BrickConfigDto.class)); } } catch (IOException e) { fail(e.getMessage()); } finally { if (response != null) { IOUtils.closeQuietly(response.body()); } } return self(); }
From source file:io.kodokojo.bdd.stage.ApplicationThen.java
License:Open Source License
public SELF add_user_$_to_project_configuration_as_user_$_will_fail(@Quoted String usernameToAdd, @Quoted String usernameOwner) { UserInfo currentUser = currentUsers.get(usernameOwner); UserInfo userToAdd = currentUsers.get(usernameToAdd); String json = "[\n" + " \"" + userToAdd.getIdentifier() + "\"\n" + " ]"; OkHttpClient httpClient = new OkHttpClient(); RequestBody body = RequestBody.create(com.squareup.okhttp.MediaType.parse("application/json"), json.getBytes());/* w ww . ja v a 2 s . c o m*/ Request.Builder builder = new Request.Builder() .url(getApiBaseUrl() + "/projectconfig/" + projectConfigurationId + "/user").put(body); Request request = HttpUserSupport.addBasicAuthentification(currentUser, builder).build(); Response response = null; try { response = httpClient.newCall(request).execute(); assertThat(response.code()).isEqualTo(403); } catch (IOException e) { fail(e.getMessage()); } finally { if (response != null) { IOUtils.closeQuietly(response.body()); } } return self(); }
From source file:io.kodokojo.bdd.stage.ApplicationThen.java
License:Open Source License
private void getUserDetails(String username, boolean complete) { OkHttpClient httpClient = new OkHttpClient(); UserInfo requesterUserInfo = currentUsers.get(currentUserLogin); UserInfo targetUserInfo = currentUsers.get(username); String url = getApiBaseUrl() + "/user"; if (!requesterUserInfo.getIdentifier().equals(targetUserInfo.getIdentifier())) { url += "/" + targetUserInfo.getIdentifier(); }//from w w w .ja va2 s. c o m Request.Builder builder = new Request.Builder().get().url(url); Request request = HttpUserSupport.addBasicAuthentification(requesterUserInfo, builder).build(); Response response = null; try { response = httpClient.newCall(request).execute(); assertThat(response.code()).isEqualTo(200); JsonParser parser = new JsonParser(); String body = response.body().string(); JsonObject json = (JsonObject) parser.parse(body); assertThat(json.getAsJsonPrimitive("name").getAsString()).isNotEmpty(); if (complete) { assertThat(json.getAsJsonPrimitive("password").getAsString()).isNotEmpty(); assertThat(json.getAsJsonPrimitive("email").getAsString()).isNotEmpty(); assertThat(json.getAsJsonPrimitive("sshPublicKey").getAsString()).isNotEmpty(); } else { assertThat(json.getAsJsonPrimitive("password").getAsString()).isEmpty(); } if (StringUtils.isNotBlank(projectConfigurationId)) { assertThat(json.has("projectConfigurationIds")); JsonArray projectConfigurationIds = json.getAsJsonArray("projectConfigurationIds"); assertThat(projectConfigurationIds).isNotEmpty(); boolean foundCurrentProjectConfigurationId = false; Iterator<JsonElement> projectConfigIt = projectConfigurationIds.iterator(); while (projectConfigIt.hasNext()) { JsonObject projectConfig = (JsonObject) projectConfigIt.next(); assertThat(projectConfig.has("projectConfigurationId")); foundCurrentProjectConfigurationId = projectConfigurationId .equals(projectConfig.getAsJsonPrimitive("projectConfigurationId").getAsString()); } assertThat(foundCurrentProjectConfigurationId).isTrue(); } } catch (IOException e) { fail("Unable to get User details on Url " + url, e); } finally { if (response != null) { try { response.body().close(); } catch (IOException e) { fail("Fail to close Http response.", e); } } } }
From source file:io.kodokojo.bdd.stage.ApplicationThen.java
License:Open Source License
private ProjectConfigDto getProjectConfigurationFromCurrentProjectConfigId() { OkHttpClient httpClient = new OkHttpClient(); UserInfo requesterUserInfo = currentUsers.get(currentUserLogin); Request.Builder builder = new Request.Builder() .url(getApiBaseUrl() + "/projectconfig/" + projectConfigurationId).get(); Request request = HttpUserSupport.addBasicAuthentification(requesterUserInfo, builder).build(); Response response = null;//from w w w . j a va 2s . co m try { response = httpClient.newCall(request).execute(); assertThat(response.code()).isEqualTo(200); String bodyResponse = response.body().string(); Gson gson = new GsonBuilder().create(); ProjectConfigDto projectConfigDto = gson.fromJson(bodyResponse, ProjectConfigDto.class); assertThat(projectConfigDto).isNotNull(); assertThat(projectConfigDto.getAdmins().get(0).getUsername()) .isEqualTo(requesterUserInfo.getUsername()); assertThat(projectConfigDto.getUsers()).isNotEmpty(); assertThat(projectConfigDto.getStackConfigs()).isNotEmpty(); JsonParser parser = new JsonParser(); JsonObject json = (JsonObject) parser.parse(bodyResponse); assertThat(json.has("name")).isTrue(); assertThat(json.has("identifier")).isTrue(); assertThat(json.has("admins")).isTrue(); JsonArray admins = json.getAsJsonArray("admins"); assertThat(admins).isNotEmpty(); jsonUserAreValid(admins.iterator()); JsonArray users = json.getAsJsonArray("users"); jsonUserAreValid(users.iterator()); assertThat(users).isNotEmpty(); JsonArray stackConfigs = json.getAsJsonArray("stackConfigs"); assertThat(stackConfigs).isNotEmpty(); Iterator<JsonElement> stackConfigurationIt = stackConfigs.iterator(); while (stackConfigurationIt.hasNext()) { JsonObject stackConf = (JsonObject) stackConfigurationIt.next(); jsonStackConfigIsValid(stackConf); } return projectConfigDto; } catch (IOException e) { fail(e.getMessage()); } finally { if (response != null) { closeQuietly(response.body()); } } return null; }
From source file:io.kodokojo.bdd.stage.ApplicationWhen.java
License:Open Source License
public SELF retrive_a_new_id() { OkHttpClient httpClient = new OkHttpClient(); RequestBody emptyBody = RequestBody.create(null, new byte[0]); String baseUrl = getBaseUrl(); Request request = new Request.Builder().post(emptyBody).url(baseUrl + "/api/v1/user").build(); Response response = null;//from ww w .j a v a 2 s . c o m try { response = httpClient.newCall(request).execute(); if (response.code() != 200) { fail("Invalid HTTP code status " + response.code() + " expected 200"); } newUserId = response.body().string(); } catch (IOException e) { fail(e.getMessage(), e); } finally { if (response != null) { IOUtils.closeQuietly(response.body()); } } return self(); }
From source file:io.kodokojo.bdd.stage.ApplicationWhen.java
License:Open Source License
private SELF create_user(String email, boolean success) { if (isBlank(email)) { throw new IllegalArgumentException("email must be defined."); }//from w ww .j av a2 s.c om if (success && StringUtils.isBlank(newUserId)) { retrive_a_new_id(); } OkHttpClient httpClient = new OkHttpClient(); RequestBody body = RequestBody.create(MediaType.parse("application/json"), ("{\"email\": \"" + email + "\"}").getBytes()); String baseUrl = getBaseUrl(); Request.Builder builder = new Request.Builder().post(body) .url(baseUrl + "/api/v1/user" + (newUserId != null ? "/" + newUserId : "")); if (isNotBlank(currentUserLogin)) { UserInfo currentUser = currentUsers.get(currentUserLogin); if (currentUser != null) { builder = HttpUserSupport.addBasicAuthentification(currentUser, builder); } } Request request = builder.build(); Response response = null; try { response = httpClient.newCall(request).execute(); if (success) { assertThat(response.code()).isEqualTo(201); JsonParser parser = new JsonParser(); String bodyResponse = response.body().string(); JsonObject json = (JsonObject) parser.parse(bodyResponse); String currentUsername = json.getAsJsonPrimitive("username").getAsString(); String currentUserPassword = json.getAsJsonPrimitive("password").getAsString(); String currentUserEmail = json.getAsJsonPrimitive("email").getAsString(); String currentUserIdentifier = json.getAsJsonPrimitive("identifier").getAsString(); String currentUserEntityIdentifier = json.getAsJsonPrimitive("entityIdentifier").getAsString(); currentUsers.put(currentUsername, new UserInfo(currentUsername, currentUserIdentifier, currentUserEntityIdentifier, currentUserPassword, currentUserEmail)); if (isBlank(currentUserLogin)) { currentUserLogin = currentUsername; } Attachment privateKey = Attachment.plainText(bodyResponse).withTitle(currentUsername + " response"); currentStep.addAttachment(privateKey); } else { assertThat(response.code()).isNotEqualTo(201); } response.body().close(); } catch (IOException e) { if (success) { fail(e.getMessage(), e); } } finally { if (response != null) { IOUtils.closeQuietly(response.body()); } } return self(); }
From source file:io.kodokojo.bdd.stage.ApplicationWhen.java
License:Open Source License
private String getProjectConfiguration(String username, String projectConfigurationId) { UserInfo userInfo = currentUsers.get(username); Request.Builder builder = new Request.Builder().get() .url(getApiBaseUrl() + "/projectconfig/" + projectConfigurationId); Request request = HttpUserSupport.addBasicAuthentification(userInfo, builder).build(); Response response = null;/* w w w. java 2 s . c o m*/ try { OkHttpClient httpClient = new OkHttpClient(); response = httpClient.newCall(request).execute(); assertThat(response.code()).isEqualTo(200); String body = response.body().string(); return body; } catch (IOException e) { fail(e.getMessage()); } finally { if (response != null) { IOUtils.closeQuietly(response.body()); } } return null; }
From source file:io.kodokojo.bdd.stage.brickauthenticator.JenkinsUserAuthenticator.java
License:Open Source License
@Override public boolean authenticate(OkHttpClient httpClient, String url, UserInfo userInfo) { Request.Builder builder = new Request.Builder().url(url + "/me/configure"); builder = HttpUserSupport.addBasicAuthentification(userInfo, builder); Request request = builder.build(); Response response = null;/*from w ww.java 2 s.c o m*/ try { response = httpClient.newCall(request).execute(); assertThat(response.code()).isBetween(200, 299); return true; } catch (IOException e) { fail(e.getMessage()); } finally { if (response != null) { IOUtils.closeQuietly(response.body()); } } return false; }