private String authorize() { try { OkHttpClient.Builder builder = client.newBuilder(); builder.interceptors().remove(this); OkHttpClient clone = builder.build(); 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(); response = response.priorResponse() != null ? response.priorResponse() : response; response = response.networkResponse() != null ? response.networkResponse() : response; String token = response.header(LOCATION); if (token == null || token.isEmpty()) { throw new KubernetesClientException( "Unexpected response (" + response.code() + " " + response.message() + "), to the authorization request. Missing 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); } }
public static InputStream openUri(Context context, Uri uri, String reqContentTypeSubstring) throws OpenUriException { if (uri == null) { throw new IllegalArgumentException("Uri cannot be empty"); } String scheme = uri.getScheme(); if (scheme == null) { throw new OpenUriException(false, new IOException("Uri had no scheme")); } InputStream in = null; if ("content".equals(scheme)) { try { in = context.getContentResolver().openInputStream(uri); } catch (FileNotFoundException | SecurityException e) { throw new OpenUriException(false, e); } } else if ("file".equals(scheme)) { List<String> segments = uri.getPathSegments(); if (segments != null && segments.size() > 1 && "android_asset".equals(segments.get(0))) { AssetManager assetManager = context.getAssets(); StringBuilder assetPath = new StringBuilder(); for (int i = 1; i < segments.size(); i++) { if (i > 1) { assetPath.append("/"); } assetPath.append(segments.get(i)); } try { in = assetManager.open(assetPath.toString()); } catch (IOException e) { throw new OpenUriException(false, e); } } else { try { in = new FileInputStream(new File(uri.getPath())); } catch (FileNotFoundException e) { throw new OpenUriException(false, e); } } } else if ("http".equals(scheme) || "https".equals(scheme)) { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.SECONDS) .readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS) .build(); Request request; int responseCode = 0; String responseMessage = null; try { request = new Request.Builder().url(new URL(uri.toString())).build(); } catch (MalformedURLException e) { throw new OpenUriException(false, e); } try { Response response = client.newCall(request).execute(); responseCode = response.code(); responseMessage = response.message(); if (!(responseCode >= 200 && responseCode < 300)) { throw new IOException("HTTP error response."); } if (reqContentTypeSubstring != null) { String contentType = response.header("Content-Type"); if (contentType == null || !contentType.contains(reqContentTypeSubstring)) { throw new IOException( "HTTP content type '" + contentType + "' didn't match '" + reqContentTypeSubstring + "'."); } } in = response.body().byteStream(); } catch (IOException e) { if (responseCode > 0) { throw new OpenUriException(500 <= responseCode && responseCode < 600, responseMessage, e); } else { throw new OpenUriException(false, e); } } } if (in == null) { throw new OpenUriException(false, "Null input stream for URI: " + uri, null); } return in; }