@Override public void insertOrUpdatePreferences(EasitApplicationPreferences preferences, EasitAccount user) throws Exception { // Convert the prefs to String strPrefs = localPrefsToServerPrefs(preferences); // Prepare request and send it DefaultHttpClient client = new DefaultHttpClient(); StringEntity input = new StringEntity(strPrefs); input.setContentEncoding("UTF8"); input.setContentType("application/json"); HttpPost postRequest = new HttpPost( environment.getProperty("flowManager.url") + "/oldpreferences/" + user.getUserToken()); postRequest.setEntity(input); HttpResponse response = client.execute(postRequest); // NOT Correct answer if (response.getStatusLine().getStatusCode() != 200) { logger.info("ERROR:"); logger.info( "URL target" + environment.getProperty("flowManager.url") + "/oldpreferences/" + user.getUserToken()); throw new Exception("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } // Clear connection client.getConnectionManager().shutdown(); }
/** * Get the user preferences from the GPII * * @param user * @return * @throws Exception */ private String getPreferencesFromServer(EasitAccount user) throws Exception { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet( environment.getProperty("flowManager.url") + environment.getProperty("flowManager.preferences")); // add the access token to the request header request.addHeader("Authorization", "Bearer " + user.getAccessToken()); HttpResponse response = client.execute(request); // NOT Correct answer if (response.getStatusLine().getStatusCode() != 200) { throw new Exception("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } // Correct answer BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } logger.info("User preferences:" + result); return result.toString(); }