private PublisherSvcApi configureService() { SessionManager session = new SessionManager(getActivity()); String username = session.getUserDetails().get(SessionManager.KEY_USERNAME); String password = session.getUserDetails().get(SessionManager.KEY_PASSWORD); return new SecuredRestBuilder() .setLoginEndpoint(UserSvcApi.SERVICE_URL + UserSvcApi.TOKEN_PATH) .setUsername(username) .setPassword(password) .setClientId(UserSvcApi.CLIENT_ID) .setClient(new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(UserSvcApi.SERVICE_URL) .setLogLevel(RestAdapter.LogLevel.FULL) .build() .create(PublisherSvcApi.class); }
private MyClientGetInterface createRetrofitClient(String url) { // prepare apache client config with timeout RequestConfig configWithTimeout = RequestConfig.custom() .setSocketTimeout(MY_DEAR_TIMEOUT) .build(); // build apache client with timeout HttpClient httpClient = HttpClientBuilder.create(). setDefaultRequestConfig(configWithTimeout) .build(); // feed retrofit with the newly created rest client RestAdapter restAdapter = new RestAdapter.Builder() .setClient(new ApacheClient(httpClient )) .setEndpoint(url) .build(); return restAdapter.create(MyClientGetInterface.class); }
/** * This test creates a Video and attempts to add it to the video service * without logging in. The test checks to make sure that the request is * denied and the client redirected to the login page. * * @throws Exception */ @Test public void testRedirectToLoginWithoutAuth() throws Exception { ErrorRecorder error = new ErrorRecorder(); VideoSvcApi videoService = new RestAdapter.Builder() .setClient( new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(TEST_URL).setLogLevel(LogLevel.FULL) .setErrorHandler(error).build().create(VideoSvcApi.class); try { // This should fail because we haven't logged in! videoService.addVideo(video); fail("Yikes, the security setup is horribly broken and didn't require the user to login!!"); } catch (Exception e) { // Ok, our security may have worked, ensure that // we got redirected to the login page assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, error.getError() .getResponse().getStatus()); } }
private void notifySubscription( final Context context, final long pubId, final long subId, final String subUsername, final boolean result) { SessionManager session = new SessionManager(context); String username = session.getUserDetails().get(SessionManager.KEY_USERNAME); String password = session.getUserDetails().get(SessionManager.KEY_PASSWORD); final SubscriptionResult subscriptionResult = new SubscriptionResult(subId, result); final PublisherSvcApi publisherSvc = new SecuredRestBuilder() .setLoginEndpoint(UserSvcApi.SERVICE_URL + UserSvcApi.TOKEN_PATH) .setUsername(username) .setPassword(password) .setClientId(UserSvcApi.CLIENT_ID) .setClient(new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(UserSvcApi.SERVICE_URL) .setLogLevel(RestAdapter.LogLevel.FULL) .build() .create(PublisherSvcApi.class); Thread t = new Thread(new Runnable() { @Override public void run() { boolean answer = publisherSvc.confirmSubscription(pubId, subscriptionResult); if (answer) { if (result) { handler.post(new ShowToastMessage(subUsername + " is now subscribed to your alerts")); Intent intent = new Intent(UIUpdaterReceiver.ACTION_UPDATE_UI); LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } else { handler.post(new ShowToastMessage(subUsername + " will not receive your alerts")); } } else { handler.post(new ShowToastMessage("Error while processing the confirmation")); } } }); t.start(); }
private void downloadAlerts(final long pubId) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); final long subId = preferences.getLong(MobileClient.SUBSCRIBER_ID, 0); SessionManager session = new SessionManager(this); final String username = session.getUserDetails().get(SessionManager.KEY_USERNAME); String password = session.getUserDetails().get(SessionManager.KEY_PASSWORD); final SubscriberSvcApi subscriberSvc = new SecuredRestBuilder() .setLoginEndpoint(UserSvcApi.SERVICE_URL + UserSvcApi.TOKEN_PATH) .setUsername(username) .setPassword(password) .setClientId(UserSvcApi.CLIENT_ID) .setClient(new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(UserSvcApi.SERVICE_URL) .setLogLevel(RestAdapter.LogLevel.FULL) .build() .create(SubscriberSvcApi.class); Thread t = new Thread(new Runnable() { @Override public void run() { publisherAlerts = subscriberSvc.getAlertsByUser(subId, pubId); AlertsActivity.this.runOnUiThread(new Runnable() { @Override public void run() { loadAlerts(); AlertsActivity.this.setListAdapter(alertsAdapter); loadEmptyContent(); progress.dismiss(); } }); } }); t.start(); }
public void downloadContacts() { contactsAdapter.flushList(); final long subId = user.getId(); SessionManager session = new SessionManager(getActivity()); final String username = session.getUserDetails().get(SessionManager.KEY_USERNAME); String password = session.getUserDetails().get(SessionManager.KEY_PASSWORD); final UserSvcApi userSvc = new SecuredRestBuilder() .setLoginEndpoint(UserSvcApi.SERVICE_URL + UserSvcApi.TOKEN_PATH) .setUsername(username) .setPassword(password) .setClientId(UserSvcApi.CLIENT_ID) .setClient(new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(UserSvcApi.SERVICE_URL) .setLogLevel(RestAdapter.LogLevel.FULL) .build() .create(UserSvcApi.class); Thread t = new Thread(new Runnable() { @Override public void run() { ContactsFragment.this.contactsAccepted = userSvc.getContactsAccepted(subId); ContactsFragment.this.contactsPending = userSvc.getContactsPending(subId); ContactsFragment.this.getActivity().runOnUiThread(new Runnable() { @Override public void run() { loadContacts(); showHeaderView(); ContactsFragment.this.setListAdapter(contactsAdapter); } }); } }); t.start(); }
public void revokeSubscription(final MobileClient subscriber) { SessionManager session = new SessionManager(getActivity()); final String username = session.getUserDetails().get(SessionManager.KEY_USERNAME); String password = session.getUserDetails().get(SessionManager.KEY_PASSWORD); final PublisherSvcApi publisherSvc = new SecuredRestBuilder() .setLoginEndpoint(UserSvcApi.SERVICE_URL + UserSvcApi.TOKEN_PATH) .setUsername(username) .setPassword(password) .setClientId(UserSvcApi.CLIENT_ID) .setClient(new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(UserSvcApi.SERVICE_URL) .setLogLevel(RestAdapter.LogLevel.FULL) .build() .create(PublisherSvcApi.class); Thread t = new Thread(new Runnable() { @Override public void run() { final boolean result = publisherSvc.revokeSubscription(user.getId(), subscriber.getId()); ContactsFragment.this.getActivity().runOnUiThread(new Runnable() { @Override public void run() { if (result) { Toast.makeText(ContactsFragment.this.getActivity(), subscriber.getFullName() + " will no longer receive alerts from you", Toast.LENGTH_LONG) .show(); contactsAdapter.removeItem(subscriber); } } }); } }); t.start(); }
private <T> T createRetrofitClient(Class<T> iface){ RestAdapter restAdapter = new RestAdapter.Builder() // java's url connection does not support custom verbs.... // using apache client instead .setClient(new ApacheClient()) .setEndpoint(serverUrl) .build(); return restAdapter.create(iface); }
private <T> T createRetrofitClient(Class<T> iface){ RestAdapter restAdapter = new RestAdapter.Builder() // java's url connection does not support non standard verbs. // using apache client instead... .setClient(new ApacheClient()) .setEndpoint(serverUrl) .build(); return restAdapter.create(iface); }
/** * This test creates a Video and attempts to add it to the video service * without logging in. The test checks to make sure that the request is * denied and the client redirected to the login page. * * @throws Exception */ @Test public void testDenyVideoAddWithoutLogin() throws Exception { ErrorRecorder error = new ErrorRecorder(); VideoSvcApi videoService = new RestAdapter.Builder() .setClient( new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(TEST_URL).setLogLevel(LogLevel.FULL) .setErrorHandler(error).build().create(VideoSvcApi.class); try { // This should fail because we haven't logged in! videoService.addVideo(video); fail("Yikes, the security setup is horribly broken and didn't require the user to login!!"); } catch (Exception e) { // Ok, our security may have worked, ensure that // we got redirected to the login page assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, error.getError() .getResponse().getStatus()); } // Now, let's login and ensure that the Video wasn't added videoService.login("coursera", "changeit"); // We should NOT get back the video that we added above! Collection<Video> videos = videoService.getVideoList(); assertFalse(videos.contains(video)); }
public static synchronized VideoSvcApi init(String server, String user, String pass) { videoSvc_ = new SecuredRestBuilder() .setLoginEndpoint(server + VideoSvcApi.TOKEN_PATH) .setUsername(user) .setPassword(pass) .setClientId(CLIENT_ID) .setClient( new ApacheClient(new EasyHttpClient())) .setEndpoint(server).setLogLevel(LogLevel.FULL).build() .create(VideoSvcApi.class); return videoSvc_; }
public Client.Provider build() { return new Client.Provider() { public Client get() { return new ApacheClient(httpClientBuilder.build()); } }; }
public FDroidEndPoint getEndPoint() { return new RestAdapter.Builder() .setConverter(new SimpleXMLConverter()) .setErrorHandler(new FDroidErrorHandler()) .setEndpoint(appMarketHost) .setClient(new ApacheClient()) .setLogLevel(RestAdapter.LogLevel.FULL) .build().create(FDroidEndPoint.class); }
private RestAdapter makeRestAdapter(User user) { AuthInterceptor requestInterceptor = new AuthInterceptor(user); return new RestAdapter.Builder() .setRequestInterceptor(requestInterceptor) .setErrorHandler(new Dhis2ErrorHandler()) .setEndpoint(dhis2BaseUrl) .setClient(new ApacheClient()) .setLogLevel(RestAdapter.LogLevel.FULL) .build(); }
private void publishAlert(final GlucoseAlert glucose) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext()); final long pubId = preferences.getLong(MobileClient.PUBLISHER_ID, 0); SessionManager session = new SessionManager(getContext()); String username = session.getUserDetails().get(SessionManager.KEY_USERNAME); String password = session.getUserDetails().get(SessionManager.KEY_PASSWORD); final PublisherSvcApi publisherSvc = new SecuredRestBuilder() .setLoginEndpoint(UserSvcApi.SERVICE_URL + UserSvcApi.TOKEN_PATH) .setUsername(username) .setPassword(password) .setClientId(UserSvcApi.CLIENT_ID) .setClient(new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(UserSvcApi.SERVICE_URL) .setLogLevel(RestAdapter.LogLevel.FULL) .build() .create(PublisherSvcApi.class); Thread t = new Thread(new Runnable() { @Override public void run() { glucose.setPublisherId(pubId); final boolean result = publisherSvc.notifyAlert(pubId, glucose); ((PublisherActivity) getContext()).runOnUiThread(new Runnable() { @Override public void run() { if (result) { Toast.makeText(getContext(), "The alert was successfully notified to your contacts", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(getContext(), "You don't have contacts to notifiy the alert", Toast.LENGTH_LONG) .show(); } } }); } }); t.start(); }
private void doSubscriptionRequest(final String pubUsername) { SessionManager session = new SessionManager(getActivity()); String username = session.getUserDetails().get(SessionManager.KEY_USERNAME); String password = session.getUserDetails().get(SessionManager.KEY_PASSWORD); final SubscriberSvcApi subscriberSvc = new SecuredRestBuilder() .setLoginEndpoint(UserSvcApi.SERVICE_URL + UserSvcApi.TOKEN_PATH) .setUsername(username) .setPassword(password) .setClientId(UserSvcApi.CLIENT_ID) .setClient(new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) .setEndpoint(UserSvcApi.SERVICE_URL) .setLogLevel(RestAdapter.LogLevel.FULL) .build() .create(SubscriberSvcApi.class); Thread t = new Thread(new Runnable() { @Override public void run() { final RequestSubscriptionResult answer = subscriberSvc.doSubscriptionByUsername(subscriberId, pubUsername); RequestSubscriptionFragment.this.getActivity().runOnUiThread( new Runnable() { @Override public void run() { switch (answer) { case STATUS_PENDING: showToastMessage("A previous request sent to " + pubUsername + " is pending confirmation"); pubUsernameEditTxt.setText(""); break; case STATUS_ALREADY_SUBSCRIBED: showToastMessage("You are already subscribed to " + pubUsername); pubUsernameEditTxt.setText(""); break; case STATUS_OK: showToastMessage("The request was sent to " + pubUsername); pubUsernameEditTxt.setText(""); break; default: showToastMessage("Answer is " + answer); } } private void showToastMessage(String message) { Toast.makeText(RequestSubscriptionFragment.this.getActivity(), message, Toast.LENGTH_LONG).show(); } } ); } }); t.start(); }
@Override protected void onTryUpdate(int reason) throws RetryException { RestAdapter adapter = new RestAdapter.Builder() .setEndpoint("http://h.bilibili.com") .setExecutors(mExecutor, mMainExecutor) .setClient(new ApacheClient(mClient)) .build(); BiliWallpaperService service = adapter.create(BiliWallpaperService.class); List<Wallpaper> wallpapers = getWallpapers(service); if (wallpapers == null) { throw new RetryException(); } if (wallpapers.isEmpty()) { Log.w(TAG, "No wallpapers returned from API."); scheduleUpdate(System.currentTimeMillis() + UPDATE_TIME_MILLIS); return; } wallpapers.remove(0); // first item is banner place holder final Wallpaper wallpaper = selectWallpaper(wallpapers); final Wallpaper selectedPaper = getDetail(service, wallpaper); if (selectedPaper == null) { Log.w(TAG, "No details returned for selected paper from API. id=" + wallpaper.il_id); throw new RetryException(); } WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext()); int minimumHeight = manager.getDesiredMinimumHeight(); final Resolution pic = selectResolution(selectedPaper, minimumHeight); publishArtwork(new Artwork.Builder() .imageUri((Uri.parse(pic.il_file.replaceFirst("_m\\.", "_l\\.")))) .title(pic.title) .token(String.valueOf(wallpaper.il_id)) .byline(wallpaper.author_name + ", " + DateFormat.format("yyyy-MM-dd", wallpaper.posttime * 1000) + "\n" + wallpaper.type) .viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(wallpaper.author_url))) .build()); scheduleUpdate(System.currentTimeMillis() + UPDATE_TIME_MILLIS); }
/** * Configures an Apache HttpClient to use for communication with HP Haven OnDemand. If not provided a sensible * default will be used * @param httpClient The HttpClient to use * @return this */ public Builder<E, T> setHttpClient(final HttpClient httpClient) { client = new ApacheClient(httpClient); return this; }
/** * Connects to Ardoq with token authentication, and a custom client * * @param endpoint * @param token * @param client */ public ArdoqClient(final String endpoint, final String token, final HttpClient client) { this.restAdapter = initAdapter(endpoint, getRequestInterceptor(endpoint, token), new ApacheClient(client)); }
/** * Connects to your Ardoq installation with token authentication. * * @param endpoint The Ardoq installation you wish to connect to (e.g. https://app.ardoq.com) * @param token The token generated via Profile -> APIS token that you wish to authenticate with * @param connectionTimeoutSeconds HttpClient connection timeout in seconds (defaults to 15s) * @param readTimeoutSeconds HttpClient read timeout in seconds (defaults to 20s) */ public ArdoqClient(final String endpoint, final String token, final long connectionTimeoutSeconds, final long readTimeoutSeconds) { ApacheClient client = getHttpClient(connectionTimeoutSeconds, readTimeoutSeconds); this.restAdapter = initAdapter(endpoint, getRequestInterceptor(endpoint, token), client); }
/** * Connects to your Ardoq installation with token authentication and a custom request configuration. * * @param endpoint The Ardoq installation you wish to connect to (e.g. https://app.ardoq.com) * @param token The token generated via Profile -> APIS token that you wish to authenticate with * @param defaultRequestConfig User provided org.apache.http.client.config.RequestConfig (for setting i.e. proxy settings etc.) */ public ArdoqClient(final String endpoint, final String token, final RequestConfig defaultRequestConfig) { ApacheClient client = new ApacheClient(HttpClientBuilder.create().setDefaultRequestConfig(defaultRequestConfig).build()); this.restAdapter = initAdapter(endpoint, getRequestInterceptor(endpoint, token), client); }
/** * Connects to Ardoq with username and password, and a custom HTTP client * **We Strongly suggest that you connect with a token instead** * * @param endpoint The Ardoq installation you wish to connect to (e.g. https://app.ardoq.com) * @param username Your username * @param password Your password */ public ArdoqClient(final String endpoint, final String username, final String password, final HttpClient client) { this.restAdapter = initAdapter(endpoint, getRequestInterceptorBasicAuth(endpoint, username, password), new ApacheClient(client)); }
/** * Connects to Ardoq with username and password and a custom request configuration * **We Strongly suggest that you connect with a token instead** * * @param endpoint The Ardoq installation you wish to connect to (e.g. https://app.ardoq.com) * @param username Your username * @param password Your password * @param defaultRequestConfig User provided org.apache.http.client.config.RequestConfig (for setting i.e. proxy settings etc.) */ public ArdoqClient(final String endpoint, final String username, final String password, final RequestConfig defaultRequestConfig) { ApacheClient client = new ApacheClient(HttpClientBuilder.create().setDefaultRequestConfig(defaultRequestConfig).build()); this.restAdapter = initAdapter(endpoint, getRequestInterceptorBasicAuth(endpoint, username, password), client); }
/** * Connects to Ardoq with username and password * **We Strongly suggest that you connect with a token instead** * * @param endpoint The Ardoq installation you wish to connect to (e.g. https://app.ardoq.com) * @param username Your username * @param password Your password * @param connectionTimeoutSeconds HttpClient connection timeout in seconds (defaults to 15s) * @param readTimeoutSeconds HttpClient read timeout in seconds (defaults to 20s) */ public ArdoqClient(final String endpoint, final String username, final String password, final long connectionTimeoutSeconds, final long readTimeoutSeconds) { ApacheClient httpClient = getHttpClient(connectionTimeoutSeconds, readTimeoutSeconds); this.restAdapter = initAdapter(endpoint, getRequestInterceptorBasicAuth(endpoint, username, password), httpClient); }