List of usage examples for com.amazonaws.auth AWS4Signer sign
@Override public void sign(SignableRequest<?> request, AWSCredentials credentials)
From source file:com.comcast.cmb.common.controller.AdminServletBase.java
License:Apache License
protected String httpPOST(String baseUrl, String urlString, AWSCredentials awsCredentials) { URL url;//from ww w . j a v a 2 s.c om 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: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);//from w w w . java 2 s . co 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);//from w w w. j a va 2 s . c o m signer.sign(new SignableHttpRequest(hr, hc), creds); }