Example usage for com.amazonaws Request setEndpoint

List of usage examples for com.amazonaws Request setEndpoint

Introduction

In this page you can find the example usage for com.amazonaws Request setEndpoint.

Prototype

void setEndpoint(URI endpoint);

Source Link

Document

Sets the service endpoint (ex: "https://ec2.amazonaws.com") to which this request should be sent.

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;/*from w  ww  . j av a2 s . co 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.emc.vipr.services.s3.ViPRS3Client.java

License:Open Source License

/**
 * Overridden to specify the namespace via vHost or header. This choice is consistent with the
 * bucket convention used in the standard AWS client.  vHost buckets implies vHost namespace, otherwise the
 * namespace is specified in a header.//from  www .jav  a  2s  .  co m
 */
@Override
protected <X extends AmazonWebServiceRequest> Request<X> createRequest(String bucketName, String key,
        X originalRequest, HttpMethodName httpMethod) {
    Request<X> request = super.createRequest(bucketName, key, originalRequest, httpMethod);

    if (namespace != null) {
        // is this a vHost request?
        if (isVHostRequest(request)) {
            // then prepend the namespace and bucket into the request host
            request.setEndpoint(convertToVirtualHostEndpoint(namespace, bucketName));
        } else {
            // otherwise add a header for namespace
            if (!request.getHeaders().containsKey(ViPRConstants.NAMESPACE_HEADER))
                request.addHeader(ViPRConstants.NAMESPACE_HEADER, namespace);
        }
    }

    return request;
}

From source file:com.erudika.para.rest.Signer.java

License:Apache License

private Request<?> buildAWSRequest(String httpMethod, String endpoint, String resourcePath,
        Map<String, String> headers, Map<String, String> params, InputStream entity) {
    Request<?> r = new DefaultRequest<AmazonWebServiceRequest>(Config.PARA);

    if (!StringUtils.isBlank(httpMethod)) {
        r.setHttpMethod(HttpMethodName.valueOf(httpMethod));
    }/* w w w  . ja v  a2s  . co  m*/
    if (!StringUtils.isBlank(endpoint)) {
        if (!endpoint.startsWith("http")) {
            endpoint = "https://" + endpoint;
        }
        r.setEndpoint(URI.create(endpoint));
    }
    if (!StringUtils.isBlank(resourcePath)) {
        r.setResourcePath(SdkHttpUtils.urlEncode(resourcePath, true));
    }
    if (headers != null) {
        if (headers.containsKey("x-amz-date")) {
            overriddenDate = parseAWSDate(headers.get("x-amz-date"));
        }
        // we don't need these here, added by default
        headers.remove("host");
        headers.remove("x-amz-date");
        r.setHeaders(headers);
    }
    if (params != null) {
        for (Map.Entry<String, String> param : params.entrySet()) {
            r.addParameter(param.getKey(), param.getValue());
        }
    }
    if (entity != null) {
        r.setContent(entity);
    }
    return r;
}

From source file:com.github.sjones4.youcan.youare.YouAreClient.java

License:Open Source License

private <X, Y extends AmazonWebServiceRequest> Response<X> invoke(final Request<Y> request,
        final Unmarshaller<X, StaxUnmarshallerContext> unmarshaller, final ExecutionContext executionContext) {
    request.setEndpoint(endpoint);
    request.setTimeOffset(timeOffset);/*ww w .  j  ava  2 s . c  om*/

    executionContext.setCredentialsProvider(
            CredentialUtils.getCredentialsProvider(request.getOriginalRequest(), awsCredentialsProvider));

    StaxResponseHandler<X> responseHandler = new StaxResponseHandler<X>(unmarshaller);
    DefaultErrorResponseHandler errorResponseHandler = new DefaultErrorResponseHandler(exceptionUnmarshallers);
    return client.execute(request, responseHandler, errorResponseHandler, executionContext);
}

From source file:com.ivona.services.tts.IvonaSpeechCloudClient.java

License:Open Source License

private <Y> Request<Y> prepareRequest(Request<Y> request, ExecutionContext executionContext,
        boolean signRequest) {
    request.setEndpoint(endpoint);
    request.setTimeOffset(timeOffset);//from   ww w  . jav a 2  s .com

    AWSCredentials credentials = awsCredentialsProvider.getCredentials();

    AmazonWebServiceRequest originalRequest = request.getOriginalRequest();
    if (originalRequest != null && originalRequest.getRequestCredentials() != null) {
        credentials = originalRequest.getRequestCredentials();
    }
    if (signRequest) {
        // expiration date is not currently supported on service side, but presignRequest method requires
        // this argument so one with default value is provided.
        Date expirationDate = DateTime.now(DateTimeZone.UTC).plusMinutes(DEFAULT_GET_REQUEST_EXPIRATION_MINUTES)
                .toDate();
        signer.presignRequest(request, credentials, expirationDate);
    } else {
        executionContext.setSigner(signer);
        executionContext.setCredentials(credentials);
    }
    return request;
}