下面列出了org.apache.http.client.methods.HttpPost#getMethod() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static String callSKFSRestApi(String requestURI, JsonObjectBuilder payload){
JsonObjectBuilder svcinfoBuilder = Json.createObjectBuilder()
.add("did", SKFSDID)
.add("protocol", PROTOCOL)
.add("authtype", Constants.AUTHORIZATION_HMAC);
JsonObject body = Json.createObjectBuilder()
.add("svcinfo", svcinfoBuilder)
.add("payload", payload).build();
String contentType = MediaType.APPLICATION_JSON;
HttpPost request = new HttpPost(requestURI);
request.setEntity(new StringEntity(body.toString(), ContentType.APPLICATION_JSON));
String payloadHash = (body == null)? "" : calculateHash(body.getJsonObject("payload").toString());
String currentDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date());
String requestToHmac = request.getMethod() + "\n"
+ payloadHash + "\n"
+ contentType + "\n"
+ currentDate + "\n"
+ Constants.API_VERSION + "\n"
+ request.getURI().getPath();
String hmac = calculateHMAC(SECRETKEY, requestToHmac);
request.addHeader("Date", currentDate);
request.addHeader("Authorization", "HMAC " + ACCESSKEY + ":" + hmac);
request.addHeader("strongkey-api-version", Constants.API_VERSION);
request.addHeader("strongkey-content-sha256", payloadHash);
request.addHeader("Content-Type", contentType);
return callServer(request);
}
public static void create(String REST_URI,
String did,
String accesskey,
String secretkey,
Long startdate,
Long enddate,
String certificateProfileName,
Integer version,
String status,
String notes,
String policy) throws Exception
{
String apiversion = "2.0";
CreateFidoPolicyRequest cfpr = new CreateFidoPolicyRequest();
cfpr.setStartDate(startdate);
if (enddate != null)
cfpr.setEndDate(enddate);
cfpr.setCertificateProfileName(certificateProfileName);
cfpr.setVersion(version);
cfpr.setStatus(status);
cfpr.setNotes(notes);
cfpr.setPolicy(policy);
ObjectWriter ow = new ObjectMapper().writer();
String json = ow.writeValueAsString(cfpr);
ContentType mimetype = ContentType.APPLICATION_JSON;
StringEntity body = new StringEntity(json, mimetype);
String currentDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date());
String contentSHA = common.calculateSha256(json);
String resourceLoc = REST_URI + Constants.REST_SUFFIX + did + Constants.CREATE_POLICY_ENDPOINT;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(resourceLoc);
httpPost.setEntity(body);
String requestToHmac = httpPost.getMethod() + "\n"
+ contentSHA + "\n"
+ mimetype.getMimeType() + "\n"
+ currentDate + "\n"
+ apiversion + "\n"
+ httpPost.getURI().getPath();
String hmac = common.calculateHMAC(secretkey, requestToHmac);
httpPost.addHeader("Authorization", "HMAC " + accesskey + ":" + hmac);
httpPost.addHeader("strongkey-content-sha256", contentSHA);
httpPost.addHeader("Content-Type", mimetype.getMimeType());
httpPost.addHeader("Date", currentDate);
httpPost.addHeader("strongkey-api-version", apiversion);
// Make API rest call and get response from the server
System.out.println("\nCalling create policy @ " + resourceLoc);
CloseableHttpResponse response = httpclient.execute(httpPost);
String result;
try {
StatusLine responseStatusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
EntityUtils.consume(entity);
switch (responseStatusLine.getStatusCode()) {
case 200:
System.out.println(" Response : " + result);
break;
case 401:
System.out.println("Error during create policy : 401 HMAC Authentication Failed");
return;
case 404:
System.out.println("Error during create policy : 404 Resource not found");
return;
case 400:
case 500:
default:
System.out.println("Error during create policy : " + responseStatusLine.getStatusCode() + " " + result);
return;
}
} finally {
response.close();
}
System.out.println("Result of create policy: " + result);
}
public static void create(String REST_URI,
String did,
String accesskey,
String secretkey,
Long startdate,
Long enddate,
String certificateProfileName,
Integer version,
String status,
String notes,
String policy) throws Exception
{
String apiversion = "2.0";
CreateFidoPolicyRequest cfpr = new CreateFidoPolicyRequest();
cfpr.setStartDate(startdate);
if (enddate != null)
cfpr.setEndDate(enddate);
cfpr.setCertificateProfileName(certificateProfileName);
cfpr.setVersion(version);
cfpr.setStatus(status);
cfpr.setNotes(notes);
cfpr.setPolicy(policy);
ObjectWriter ow = new ObjectMapper().writer();
String json = ow.writeValueAsString(cfpr);
ContentType mimetype = ContentType.APPLICATION_JSON;
StringEntity body = new StringEntity(json, mimetype);
String currentDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date());
String contentSHA = common.calculateSha256(json);
String resourceLoc = REST_URI + Constants.REST_SUFFIX + did + Constants.CREATE_POLICY_ENDPOINT;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(resourceLoc);
httpPost.setEntity(body);
String requestToHmac = httpPost.getMethod() + "\n"
+ contentSHA + "\n"
+ mimetype.getMimeType() + "\n"
+ currentDate + "\n"
+ apiversion + "\n"
+ httpPost.getURI().getPath();
String hmac = common.calculateHMAC(secretkey, requestToHmac);
httpPost.addHeader("Authorization", "HMAC " + accesskey + ":" + hmac);
httpPost.addHeader("strongkey-content-sha256", contentSHA);
httpPost.addHeader("Content-Type", mimetype.getMimeType());
httpPost.addHeader("Date", currentDate);
httpPost.addHeader("strongkey-api-version", apiversion);
// Make API rest call and get response from the server
System.out.println("\nCalling create policy @ " + resourceLoc);
CloseableHttpResponse response = httpclient.execute(httpPost);
String result;
try {
StatusLine responseStatusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
EntityUtils.consume(entity);
switch (responseStatusLine.getStatusCode()) {
case 200:
System.out.println(" Response : " + result);
break;
case 401:
System.out.println("Error during create policy : 401 HMAC Authentication Failed");
return;
case 404:
System.out.println("Error during create policy : 404 Resource not found");
return;
case 400:
case 500:
default:
System.out.println("Error during create policy : " + responseStatusLine.getStatusCode() + " " + result);
return;
}
} finally {
response.close();
}
System.out.println("Result of create policy: " + result);
}
private static String callSKFSRestApi(String requestURI, JsonObjectBuilder payload) {
JsonObjectBuilder svcinfoBuilder = Json.createObjectBuilder()
.add("did", SKFSDID)
.add("protocol", PROTOCOL);
if (AUTHTYPE.equalsIgnoreCase(Constants.AUTHORIZATION_HMAC)) {
svcinfoBuilder.add("authtype", Constants.AUTHORIZATION_HMAC);
} else {
svcinfoBuilder
.add("authtype", Constants.AUTHORIZATION_PASSWORD)
.add("svcusername", SVCUSERNAME)
.add("svcpassword", SVCPASSWORD);
}
JsonObject body = Json.createObjectBuilder()
.add("svcinfo", svcinfoBuilder)
.add("payload", payload).build();
String contentType = MediaType.APPLICATION_JSON;
HttpPost request = new HttpPost(requestURI);
request.setEntity(new StringEntity(body.toString(), ContentType.APPLICATION_JSON));
// Build HMAC
if (AUTHTYPE.equalsIgnoreCase(Constants.AUTHORIZATION_HMAC)) {
String payloadHash = (body == null)? "" : calculateHash(body.getJsonObject("payload").toString());
String currentDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date());
String requestToHmac = request.getMethod() + "\n"
+ payloadHash + "\n"
+ contentType + "\n"
+ currentDate + "\n"
+ Constants.API_VERSION + "\n"
+ request.getURI().getPath();
String hmac = calculateHMAC(SECRETKEY, requestToHmac);
request.addHeader("Date", currentDate);
request.addHeader("Authorization", "HMAC " + ACCESSKEY + ":" + hmac);
request.addHeader("strongkey-api-version", Constants.API_VERSION);
request.addHeader("strongkey-content-sha256", payloadHash);
}
request.addHeader("Content-Type", contentType);
return callServer(request);
}