1 /*
2  * Copyright 2012-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.services.sqs.internal;
16
17 import java.net.URI;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import com.amazonaws.Request;
22 import com.amazonaws.handlers.AbstractRequestHandler;
23
24 public class SQSRequestHandler extends AbstractRequestHandler {
25
26     private static final Map<String, String> nonstandardEndpointMap = new HashMap<String, String>();
27     static {
28         nonstandardEndpointMap.put("queue.amazonaws.com""sqs.us-east-1.amazonaws.com");
29         nonstandardEndpointMap.put("us-west-1.queue.amazonaws.com""sqs.us-west-1.amazonaws.com");
30         nonstandardEndpointMap.put("us-west-2.queue.amazonaws.com""sqs.us-west-2.amazonaws.com");
31         nonstandardEndpointMap.put("eu-west-1.queue.amazonaws.com""sqs.eu-west-1.amazonaws.com");
32         nonstandardEndpointMap.put("ap-southeast-1.queue.amazonaws.com""sqs.ap-southeast-1.amazonaws.com");
33         nonstandardEndpointMap.put("ap-northeast-1.queue.amazonaws.com""sqs.ap-northeast-1.amazonaws.com");
34         nonstandardEndpointMap.put("sa-east-1.queue.amazonaws.com""sqs.sa-east-1.amazonaws.com");
35         nonstandardEndpointMap.put("us-gov-west-1.queue.amazonaws.com""sqs.us-gov-west-1.amazonaws.com");
36         nonstandardEndpointMap.put("ap-southeast-2.queue.amazonaws.com""sqs.ap-southeast-2.amazonaws.com");
37     }
38
39     @Override
40     public void beforeRequest(Request<?> request) {
41         URI endpoint = request.getEndpoint();
42
43         // If the request is using a non-standard endpoint, then
44         // alter it to use the corresponding, standard endpoint
45         if (nonstandardEndpointMap.containsKey(endpoint.getHost())) {
46             String newHost = nonstandardEndpointMap.get(endpoint.getHost());
47             String newEndpoint = endpoint.toString().replaceFirst(endpoint.getHost(), newHost);
48             request.setEndpoint(URI.create(newEndpoint));
49         }
50     }
51 }
52