Example usage for com.amazonaws.auth AWS4Signer setServiceName

List of usage examples for com.amazonaws.auth AWS4Signer setServiceName

Introduction

In this page you can find the example usage for com.amazonaws.auth AWS4Signer setServiceName.

Prototype

@Override
public void setServiceName(String serviceName) 

Source Link

Document

Sets the service name that this signer should use when calculating request signatures.

Usage

From source file:com.comcast.cmb.common.controller.AdminServletBase.java

License:Apache License

protected String httpPOST(String baseUrl, String urlString, AWSCredentials awsCredentials) {

    URL url;//w  w w . j  av  a2 s.c  o  m
    HttpURLConnection conn;
    BufferedReader br;
    String line;
    String doc = "";

    try {

        String urlPost = urlString.substring(0, urlString.indexOf("?"));
        url = new URL(urlPost);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");

        CreateQueueRequest createQueueRequest = new CreateQueueRequest("test");
        Request<CreateQueueRequest> request = new CreateQueueRequestMarshaller().marshall(createQueueRequest);
        //set parameters from url
        String parameterString = urlString.substring(urlString.indexOf("?") + 1);
        String[] parameterArray = parameterString.split("&");
        Map<String, String> requestParameters = new HashMap<String, String>();
        for (int i = 0; i < parameterArray.length; i++) {
            requestParameters.put(parameterArray[i].substring(0, parameterArray[i].indexOf("=")),
                    parameterArray[i].substring(parameterArray[i].indexOf("=") + 1));
        }
        request.setParameters(requestParameters);
        //get endpoint from url
        URI uri = new URI(baseUrl);
        request.setEndpoint(uri);
        String resourcePath = urlString.substring(baseUrl.length(), urlString.indexOf("?"));
        request.setResourcePath(resourcePath);

        AWS4Signer aws4Signer = new AWS4Signer();
        String host = uri.getHost();
        aws4Signer.setServiceName(host);
        aws4Signer.sign(request, awsCredentials);

        //set headers for real request
        for (Entry<String, String> entry : request.getHeaders().entrySet()) {
            conn.setRequestProperty(entry.getKey(), entry.getValue());
        }

        // Send post request
        conn.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        StringBuffer bodyStringBuffer = new StringBuffer();
        for (Entry<String, String> entry : requestParameters.entrySet()) {
            bodyStringBuffer.append(entry.getKey() + "=" + entry.getValue() + "&");
        }
        String bodyString = "";
        if (bodyStringBuffer.length() > 0) {
            bodyString = bodyStringBuffer.substring(0, bodyStringBuffer.length() - 1);
        }
        wr.writeBytes(bodyString);
        wr.flush();
        wr.close();

        br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        while ((line = br.readLine()) != null) {
            doc += line;
        }

        br.close();

        logger.info("event=http_get url=" + urlString);

    } catch (Exception ex) {
        logger.error("event=http_get url=" + urlString, ex);
    }

    return doc;
}

From source file:com.eucalyptus.ws.handlers.IoInternalHmacHandler.java

License:Open Source License

private void sign(final FullHttpRequest request) {
    final AWS4Signer signer = new AWS4Signer();
    signer.setRegionName("eucalyptus");
    signer.setServiceName("internal");
    signer.sign(wrapRequest(request), credentials());
}

From source file:com.streamsets.pipeline.lib.aws.AwsUtil.java

License:Apache License

public static AwsRequestSigningApacheInterceptor getAwsSigV4Interceptor(String awsServiceName,
        AwsRegion awsRegion, String otherEndpoint, CredentialValue awsAccessKeyId,
        CredentialValue awsSecretAccessKey) throws StageException {
    AWS4Signer signer = new AWS4Signer();
    signer.setServiceName(awsServiceName);

    if (awsRegion == AwsRegion.OTHER) {
        if (otherEndpoint == null || otherEndpoint.isEmpty()) {
            return null;
        }// ww w.  jav  a  2 s  .c  om
        signer.setRegionName(otherEndpoint);
    } else {
        signer.setRegionName(awsRegion.getId());
    }

    return new AwsRequestSigningApacheInterceptor(awsServiceName, signer,
            AwsUtil.getCredentialsProvider(awsAccessKeyId, awsSecretAccessKey));
}

From source file:org.springframework.vault.authentication.AwsIamAuthentication.java

License:Apache License

private static String getSignedHeaders(AwsIamAuthenticationOptions options) {

    Map<String, String> headers = createIamRequestHeaders(options);

    AWS4Signer signer = new AWS4Signer();

    DefaultRequest<String> request = new DefaultRequest<>("sts");

    request.setContent(new ByteArrayInputStream(REQUEST_BODY.getBytes()));
    request.setHeaders(headers);//w  ww . ja v  a  2s.  c o m
    request.setHttpMethod(HttpMethodName.POST);
    request.setEndpoint(options.getEndpointUri());

    signer.setServiceName(request.getServiceName());
    signer.sign(request, options.getCredentialsProvider().getCredentials());

    Map<String, Object> map = new LinkedHashMap<>();

    for (Entry<String, String> entry : request.getHeaders().entrySet()) {
        map.put(entry.getKey(), Collections.singletonList(entry.getValue()));
    }

    try {
        return OBJECT_MAPPER.writeValueAsString(map);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Cannot serialize headers to JSON", e);
    }
}

From source file:zipkin.autoconfigure.storage.elasticsearch.aws.ElasticsearchAwsRequestSigner.java

License:Apache License

@Override
public void process(HttpRequest hr, HttpContext hc) {
    AWSCredentials creds = credentialsProvider.getCredentials();
    AWS4Signer signer = new AWS4Signer();
    signer.setServiceName("es");
    signer.setRegionName(region);// w w w.  ja v  a 2 s  .  c o m
    signer.sign(new SignableHttpRequest(hr, hc), creds);
}