List of usage examples for com.squareup.okhttp OkHttpClient setAuthenticator
public OkHttpClient setAuthenticator(Authenticator authenticator)
From source file:org.apache.nifi.processors.standard.InvokeHTTP.java
License:Apache License
private void setAuthenticator(OkHttpClient okHttpClient, ProcessContext context) { final String authUser = trimToEmpty(context.getProperty(PROP_BASIC_AUTH_USERNAME).getValue()); final String proxyUsername = trimToEmpty(context.getProperty(PROP_PROXY_USER).getValue()); // If the username/password properties are set then check if digest auth is being used if (!authUser.isEmpty() && "true".equalsIgnoreCase(context.getProperty(PROP_DIGEST_AUTH).getValue())) { final String authPass = trimToEmpty(context.getProperty(PROP_BASIC_AUTH_PASSWORD).getValue()); /*//from w w w . j a va 2 s .c om * Currently OkHttp doesn't have built-in Digest Auth Support. The ticket for adding it is here: * https://github.com/square/okhttp/issues/205#issuecomment-154047052 * Once added this should be refactored to use the built in support. For now, a third party lib is needed. */ final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>(); com.burgstaller.okhttp.digest.Credentials credentials = new com.burgstaller.okhttp.digest.Credentials( authUser, authPass); final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials); MultiAuthenticator authenticator = new MultiAuthenticator.Builder().with("Digest", digestAuthenticator) .build(); if (!proxyUsername.isEmpty()) { final String proxyPassword = context.getProperty(PROP_PROXY_PASSWORD).getValue(); authenticator.setProxyUsername(proxyUsername); authenticator.setProxyPassword(proxyPassword); } okHttpClient.interceptors().add(new AuthenticationCacheInterceptor(authCache)); okHttpClient.setAuthenticator(new CachingAuthenticatorDecorator(authenticator, authCache)); } else { // Add proxy authentication only if (!proxyUsername.isEmpty()) { final String proxyPassword = context.getProperty(PROP_PROXY_PASSWORD).getValue(); MultiAuthenticator authenticator = new MultiAuthenticator.Builder().build(); authenticator.setProxyUsername(proxyUsername); authenticator.setProxyPassword(proxyPassword); okHttpClient.setAuthenticator(authenticator); } } }
From source file:org.eyeseetea.malariacare.network.NetworkUtils.java
License:Open Source License
/** * Pushes data to DHIS Server//from w w w . ja v a 2s .c om * @param data */ public JSONObject pushData(JSONObject data) throws Exception { Response response = null; final String DHIS_URL = getDhisURL() + DHIS_PUSH_API; OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient(); client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout client.setReadTimeout(30, TimeUnit.SECONDS); // socket timeout client.setWriteTimeout(30, TimeUnit.SECONDS); // write timeout client.setRetryOnConnectionFailure(false); // Cancel retry on failure BasicAuthenticator basicAuthenticator = new BasicAuthenticator(); client.setAuthenticator(basicAuthenticator); Log.d(TAG, "Url" + DHIS_URL + ""); RequestBody body = RequestBody.create(JSON, data.toString()); Request request = new Request.Builder() .header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL) .post(body).build(); response = client.newCall(request).execute(); if (!response.isSuccessful()) { Log.e(TAG, "pushData (" + response.code() + "): " + response.body().string()); throw new IOException(response.message()); } return parseResponse(response.body().string()); }
From source file:org.eyeseetea.malariacare.network.NetworkUtils.java
License:Open Source License
/** * Pull data from DHIS Server// www .j a v a 2 s. com * @param data */ public JSONObject getData(String data) throws Exception { Response response = null; final String DHIS_URL = getDhisURL() + DHIS_PULL_API + data; OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient(); BasicAuthenticator basicAuthenticator = new BasicAuthenticator(); client.setAuthenticator(basicAuthenticator); Log.d(TAG, "Url" + DHIS_URL + ""); Request request = new Request.Builder() .header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL) .get().build(); response = client.newCall(request).execute(); if (!response.isSuccessful()) { Log.e(TAG, "getData (" + response.code() + "): " + response.body().string()); throw new IOException(response.message()); } return parseResponse(response.body().string()); }
From source file:org.eyeseetea.malariacare.network.NetworkUtils.java
License:Open Source License
/** * Call to DHIS Server/*from w w w. j av a2 s. c om*/ * @param data * @param url */ public Response executeCall(JSONObject data, String url, String method) throws IOException { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext); final String DHIS_URL = sharedPreferences.getString(applicationContext.getString(R.string.dhis_url), applicationContext.getString(R.string.login_info_dhis_default_server_url)) + url; OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient(); client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout client.setReadTimeout(30, TimeUnit.SECONDS); // socket timeout client.setWriteTimeout(30, TimeUnit.SECONDS); // write timeout client.setRetryOnConnectionFailure(false); // Cancel retry on failure BasicAuthenticator basicAuthenticator = new BasicAuthenticator(); client.setAuthenticator(basicAuthenticator); Request.Builder builder = new Request.Builder() .header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL); switch (method) { case "POST": RequestBody postBody = RequestBody.create(JSON, data.toString()); builder.post(postBody); break; case "PUT": RequestBody putBody = RequestBody.create(JSON, data.toString()); builder.put(putBody); break; case "PATCH": RequestBody patchBody = RequestBody.create(JSON, data.toString()); builder.patch(patchBody); break; case "GET": builder.get(); break; } Request request = builder.build(); return client.newCall(request).execute(); }
From source file:org.eyeseetea.malariacare.network.PushClient.java
License:Open Source License
/** * Pushes data to DHIS Server// w w w . j av a 2 s. c o m * @param data */ private JSONObject pushData(JSONObject data) throws Exception { Response response = null; final String DHIS_URL = getDhisURL(); OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient(); BasicAuthenticator basicAuthenticator = new BasicAuthenticator(); client.setAuthenticator(basicAuthenticator); RequestBody body = RequestBody.create(JSON, data.toString()); Request request = new Request.Builder() .header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL) .post(body).build(); response = client.newCall(request).execute(); if (!response.isSuccessful()) { Log.e(TAG, "pushData (" + response.code() + "): " + response.body().string()); throw new IOException(response.message()); } return parseResponse(response.body().string()); }
From source file:org.eyeseetea.malariacare.network.ServerAPIController.java
License:Open Source License
/** * Call to DHIS Server/*from ww w. j av a 2s.co m*/ * @param data * @param url */ static Response executeCall(JSONObject data, String url, String method) throws IOException { final String DHIS_URL = url; OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient(); BasicAuthenticator basicAuthenticator = new BasicAuthenticator(); client.setAuthenticator(basicAuthenticator); Request.Builder builder = new Request.Builder() .header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL); switch (method) { case "POST": RequestBody postBody = RequestBody.create(JSON, data.toString()); builder.post(postBody); break; case "PUT": RequestBody putBody = RequestBody.create(JSON, data.toString()); builder.put(putBody); break; case "PATCH": RequestBody patchBody = RequestBody.create(JSON, data.toString()); builder.patch(patchBody); break; case "GET": builder.get(); break; } Request request = builder.build(); return client.newCall(request).execute(); }
From source file:org.ohmage.dagger.OhmageModule.java
License:Apache License
@Provides @Singleton//ww w . j av a 2 s . c o m OkHttpClient providesOkHttpClient(@ForApplication Context context) { // Create an HTTP client that uses a cache on the file system. OkHttpClient okHttpClient = new OkHttpClient(); try { HttpResponseCache cache = new HttpResponseCache(context.getCacheDir(), 1024); okHttpClient.setResponseCache(cache); } catch (IOException e) { e.printStackTrace(); } // Add an authenticator to handle 401 error by updating the token okHttpClient.setAuthenticator(new OhmageAuthenticator()); return okHttpClient; }
From source file:retrofit.MagnetRestAdapter.java
License:Apache License
private MagnetRestAdapter(OkHttpClient client, BaseUrl baseUrl, List<Converter.Factory> converterFactories, /*List<CallAdapter.Factory> adapterFactories,*/ Executor callbackExecutor) { Log.d(TAG, "MagnetRestAdapter is created"); this.client = client; this.baseUrl = baseUrl; this.converterFactories = converterFactories; //this.adapterFactories = adapterFactories; this.requestManager = new RequestManager(client); CallAdapter.Factory adapterFactory = new MagnetCallAdapter.Factory(this, requestManager); this.adapterFactories = Arrays.asList(adapterFactory); this.callbackExecutor = callbackExecutor; this.requestInterceptor = new RequestInterceptor(this, requestManager); client.interceptors().add(requestInterceptor); client.setAuthenticator(new MaxRestAuthenticator()); }