Java Code Examples for org.apache.http.client.methods.HttpPost#getMethod()

The following examples show how to use org.apache.http.client.methods.HttpPost#getMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SKFSClient.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
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);
}
 
Example 2
Source File: RestCreateFidoPolicy.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
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);
}
 
Example 3
Source File: RestCreateFidoPolicy.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
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);
}
 
Example 4
Source File: SKFSClient.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
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);
}