private ClientResponse put(final String uri, final String body) { ClientResponse clientResp = _client.put_json(_baseUrl.resolve(uri), authToken, body); if (clientResp.getStatus() == 401) { getAuthToken(); clientResp = _client.put_json(_baseUrl.resolve(uri), authToken, body); } return clientResp; }
private ClientResponse get(final String uri) { ClientResponse clientResp = _client.get_json(_baseUrl.resolve(uri), authToken); if (clientResp != null && clientResp.getStatus() == 401) { getAuthToken(); clientResp = _client.get_json(_baseUrl.resolve(uri), authToken); } return clientResp; }
@Override protected void onPostExecute(RESTClient result) { try { handler.onResponse(result.getResponse()); } catch (Exception error) { Log.e("RPC Handler", result.getResponse()); Log.e("RPC Handler", "Error on Processing Response"); handler.onException(error); } }
public void crud(String mediaType) { if (MediaType.APPLICATION_XML.equals(mediaType)) { bookStr = "<book><isbn>34523423423423</isbn><author>Amresh</author><publication>Willey</publication></book>"; pk = "34523423423423"; } else if (MediaType.APPLICATION_JSON.equals(mediaType)) { bookStr = "{book:{\"isbn\":\"2222\",\"author\":\"Kuldeep\", \"publication\":\"McGraw\"}}"; pk = "2222"; } else { fail("Incorrect Media Type:" + mediaType); return; } // Initialize REST Client restClient.initialize(WS_URL, mediaType); // Get Application Token applicationToken = restClient.getApplicationToken(); // Get Session Token sessionToken = restClient.getSessionToken(applicationToken); // Insert Record restClient.insertBook(sessionToken, bookStr); // Find Record String foundBook = restClient.findBook(sessionToken, pk); System.out.println("Before book " + foundBook); if (MediaType.APPLICATION_JSON.equals(mediaType)) { foundBook = "{book:" + foundBook + "}"; } System.out.println("found book:" + foundBook); // Update Record String updatedBook = restClient.updateBook(sessionToken, foundBook); System.out.println("updatedBook:" + updatedBook); // Get All Books String allBooks = restClient.getAllBooks(sessionToken); // Delete Record restClient.deleteBook(sessionToken, updatedBook, pk); // Close Session restClient.closeSession(sessionToken); // Close Application restClient.closeApplication(applicationToken); }
/** * @return user has sys admin priviledges or not * @throws ECSException */ public boolean isSystemAdmin() throws ECSException { ClientResponse clientResp = null; clientResp = _client.get_json(_baseUrl.resolve(URI_WHOAMI), authToken); if (clientResp.getStatus() != 200) { if (clientResp.getStatus() == 401 || clientResp.getStatus() == 302) { getAuthToken(); clientResp = _client.get_xml(_baseUrl.resolve(URI_WHOAMI), authToken); } if (clientResp.getStatus() != 200) { throw ECSException.exceptions.isSystemAdminFailed(_baseUrl, clientResp.getStatus()); } } String respBody = clientResp.getEntity(String.class); if (respBody.contains(ROLE_SYSTEM_ADMIN)) { return true; } else { return false; } }
public void run(String base_addr) { Alert alert = new Alert( "id1", "app1", System.currentTimeMillis(), "atos", new ArrayList<>(), new ArrayList<>()); RESTClient client = new RESTClient(base_addr); // System.out.println( "Basic " + new String( Base64.encodeBase64( "wp_admin:123456".getBytes() // ) ) ); // // String response = client.get( "user" ). // header( "TenantId", "atos" ). // header( "authorization", "Basic " + new String( Base64.encodeBase64( // "wp_admin:123456".getBytes() ) ) ). // send(); // // JsonObject o = new Gson().fromJson( response, JsonObject.class ); // // String session = o.get( "details" ).getAsJsonObject().get( "sessionId" ).getAsString(); // // System.out.println( session ); // client.post( "api/public/alert" ).cookie( "SESSION", session ).send( alert ); client .post("game-requirements/api/public/alert") .header("Content-Type", "application/json;charset=UTF-8") .send(alert); // System.out.println( client.post( "if/alert" ).cookie( "SESSION", user.getAuthorities(). // ).send( alert ) ); }
@Override protected RESTClient doInBackground(Object... params) { int index = 0; NameValuePair item; JSONObject post = new JSONObject(); RESTClient rest = new RESTClient(RPC_URL); try { if (params.length > 0) { rest.setUrl(rest.getUrl() + params[0]); ++index; for (; index < params.length; index++) { item = (NameValuePair) params[index]; // post.put(item.getName(), item.getValue()); rest.AddParam(item.getName(), item.getValue()); } // rest.setPost(post.toString()); rest.Execute(RESTClient.RequestMethod.GET); } } catch (Exception e) { Log.e("Reques Error", "" + e); } return rest; }
/** * @return authentication token * @throws ECSException */ public String getAuthToken() throws ECSException { _log.info("ECSApi:getAuthToken enter"); List<String> authTokenList = null; ClientResponse clientResp = null; clientResp = _client.get(_baseUrl.resolve(URI_LOGIN)); if (clientResp.getStatus() != 200) { throw ECSException.exceptions.unableToConnect(_baseUrl, clientResp.getStatus()); } MultivaluedMap<String, String> headers = clientResp.getHeaders(); authTokenList = headers.get("X-SDS-AUTH-TOKEN"); if (authTokenList.size() != 1) { throw ECSException.exceptions.invalidReturnParameters(_baseUrl); } authToken = authTokenList.get(0); _log.info("ECSApi:getAuthToken leave"); return authToken; }
/** Close client resources */ public void close() { _client.close(); }
/** * Get storage pools. * * @return storage pools * @throws ECSException */ public List<ECSStoragePool> getStoragePools() throws ECSException { _log.info("ECSApi:getStoragePools enter"); ClientResponse clientResp = null; clientResp = _client.get_json(_baseUrl.resolve(URI_STORAGE_POOL), authToken); if (clientResp.getStatus() != 200) { if (clientResp.getStatus() == 401 || clientResp.getStatus() == 302) { getAuthToken(); clientResp = _client.get_json(_baseUrl.resolve(URI_STORAGE_POOL), authToken); } if (clientResp.getStatus() != 200) { throw ECSException.exceptions.storageAccessFailed( _baseUrl.resolve(URI_STORAGE_POOL), clientResp.getStatus(), "getStoragePools"); } } JSONObject objectRepGroup = null; JSONArray arrayRepGroup = null; List<ECSStoragePool> ecsPools = new ArrayList<ECSStoragePool>(); JSONObject objRG = null; JSONArray aryVarray = null; Long storagepoolTotalCapacity = 0L, storagepoolFreeCapacity = 0L; ClientResponse clientRespVarray = null; try { objectRepGroup = clientResp.getEntity(JSONObject.class); arrayRepGroup = objectRepGroup.getJSONArray("data_service_vpool"); // run thru every replication group for (int i = 0; i < arrayRepGroup.length(); i++) { ECSStoragePool pool = new ECSStoragePool(); objRG = arrayRepGroup.getJSONObject(i); JSONObject objVarray = null; String vArrayId = null; URI uriEcsVarray = null; JSONObject objVarrayCap = null; String ecsVarray = null; // Get ECS vArray ID(=ECS StoragePool/cluster) and its capacity aryVarray = objRG.getJSONArray("varrayMappings"); for (int j = 0; j < aryVarray.length(); j++) { objVarray = aryVarray.getJSONObject(j); vArrayId = objVarray.getString("value"); // get total and free capacity for this ECS vArray ecsVarray = ECS_VARRAY_BASE + vArrayId + ".json"; uriEcsVarray = URI.create(ecsVarray); clientRespVarray = _client.get_json(_baseUrl.resolve(uriEcsVarray), authToken); if (clientRespVarray.getStatus() != 200) { if (clientRespVarray.getStatus() == 401 || clientRespVarray.getStatus() == 302) { getAuthToken(); clientRespVarray = _client.get_json(_baseUrl.resolve(uriEcsVarray), authToken); } if (clientRespVarray.getStatus() != 200) { throw ECSException.exceptions.storageAccessFailed( _baseUrl.resolve(uriEcsVarray), clientRespVarray.getStatus(), "get ECS vArray"); } } objVarrayCap = clientRespVarray.getEntity(JSONObject.class); storagepoolTotalCapacity += Integer.parseInt(objVarrayCap.getString("totalProvisioned_gb")); storagepoolFreeCapacity += Integer.parseInt(objVarrayCap.getString("totalFree_gb")); } // for each ECS varray pool.setName(objRG.getString("name")); pool.setId(objRG.getString("id")); pool.setTotalCapacity(storagepoolTotalCapacity); pool.setFreeCapacity(storagepoolFreeCapacity); ecsPools.add(pool); if (clientRespVarray != null) { clientRespVarray.close(); } } } catch (Exception e) { _log.error("discovery of Pools failed"); String response = String.format("%1$s", (clientResp == null) ? "" : clientResp); String response2 = String.format("%1$s", (clientRespVarray == null) ? "" : clientRespVarray); response = response + response2; throw ECSException.exceptions.getStoragePoolsFailed(response, e); } finally { _log.info("discovery of Pools finished"); if (clientResp != null) { clientResp.close(); } if (clientRespVarray != null) { clientRespVarray.close(); } } _log.info("ECSApi:getStoragePools leave"); return ecsPools; }
public void send(final String uri, final Map<String, String> headers) { String message = createMessage(uri, headers.get(REFERER)); client.put(message, headers); }