public String processingGetTopLevelGroupRequest() throws IOException, KeyHandledException { debug("Entering the processingGetTopLevelGroupRequest method"); String jsonFormatted = ""; CloseableHttpClient httpclient = HttpClients.createDefault(); try { Date now = new Date(); DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String date = df.format(now); String dataToSign = "(request-target): get " + gatewayurl + "groups\n" + "host: " + gatewayhost + "\n" + "date: " + date; String hmac = generateAuthHeader(dataToSign, apisecret); String authorisation = "Signature keyId=\"" + apikey + "\",algorithm=\"hmac-sha256\",headers=\"(request-target) host date\",signature=\"" + hmac + "\""; debug("dataToSign is......" + dataToSign); debug("hmac is......" + hmac); HttpGet httpGet = new HttpGet(httpsUrlForGroups); httpGet.addHeader("Authorization", authorisation); httpGet.addHeader("Cache-Control", "no-cache"); httpGet.addHeader("date", date); //-------- Calling GET method ------------- CloseableHttpResponse response = httpclient.execute(httpGet); //------------------------------------------ try { HttpEntity entity = response.getEntity(); debug("response.getStatusLine is.........." + response.getStatusLine()); String json = EntityUtils.toString(entity); ObjectMapper mapper = new ObjectMapper(); Object jsonObj = mapper.readValue(json, Object.class); jsonFormatted = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(jsonObj); debug("json is..........." + jsonFormatted); EntityUtils.consume(entity); } finally { httpclient.close(); } } finally { httpclient.close(); } debug("Leaving the processingGetTopLevelGroupRequest method"); return jsonFormatted; } /** * This method is using for generate the Auth Header * * @param dataToSign * @param secret * @return * @throws KeyHandledException */ // TODO:: it should be moved in a specific Helper class public static String generateAuthHeader(String dataToSign, String secret) throws KeyHandledException { String hash = ""; try { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key); hash = Base64.encodeBase64String(sha256_HMAC.doFinal(dataToSign.getBytes())); } catch (Exception e) { throw new KeyHandledException(ErrorCodesEnum.GENERIC_ERROR, e.getMessage()); } return (hash); }