1
- package biz .aQute .aws ;
2
-
3
- import java .io .*;
4
- import java .net .*;
5
- import java .security .*;
6
- import java .text .*;
7
- import java .util .*;
8
-
9
- import javax .crypto .*;
10
- import javax .crypto .spec .*;
11
- import javax .xml .parsers .*;
1
+ package biz .aQute .aws .impl ;
2
+
3
+ import java .io .IOException ;
4
+ import java .io .UnsupportedEncodingException ;
5
+ import java .net .HttpURLConnection ;
6
+ import java .net .MalformedURLException ;
7
+ import java .net .URL ;
8
+ import java .net .URLEncoder ;
9
+ import java .security .NoSuchAlgorithmException ;
10
+ import java .text .ParseException ;
11
+ import java .text .SimpleDateFormat ;
12
+ import java .util .Date ;
13
+ import java .util .Locale ;
14
+ import java .util .Map ;
15
+
16
+ import javax .crypto .Mac ;
17
+ import javax .crypto .spec .SecretKeySpec ;
18
+ import javax .xml .parsers .DocumentBuilder ;
19
+ import javax .xml .parsers .DocumentBuilderFactory ;
20
+ import javax .xml .parsers .ParserConfigurationException ;
12
21
13
22
import aQute .lib .base64 .Base64 ;
14
- import biz .aQute .aws .s3 .*;
15
- import biz .aQute .aws .ses .*;
16
- import biz .aQute .aws .sqs .*;
23
+ import biz .aQute .aws .s3 .S3Impl ;
24
+ import biz .aQute .aws .sqs .SQSImpl ;
17
25
18
26
/**
19
27
* https://email.us-east-1.amazonaws.com/ ?Action=SendEmail
33
41
* &AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE
34
42
* &Signature=RhU864jFu893mg7g9N9j9nr6h7EXAMPLE &Algorithm=HMACSHA256
35
43
*/
36
- public class AWS {
44
+
45
+ public class AWSImpl {
37
46
final private String accessKey ;
38
47
final private String secretKey ;
39
48
final private Mac mac ;
40
49
final private SecretKeySpec secret ;
41
- final private static SimpleDateFormat awsDateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'" , Locale .ENGLISH );
42
- static SimpleDateFormat httpDateFormat = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss Z" , Locale .ENGLISH );
50
+ final private static SimpleDateFormat awsDateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'" ,
51
+ Locale .ENGLISH );
52
+ static SimpleDateFormat httpDateFormat = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss Z" ,
53
+ Locale .ENGLISH );
43
54
final private static DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
55
+ final String region ;
44
56
static {
45
57
dbf .setNamespaceAware (false );
46
58
}
47
59
48
- public AWS (String accessKey , String secretKey ) throws NoSuchAlgorithmException {
60
+ public AWSImpl (String accessKey , String secretKey , String region ) throws NoSuchAlgorithmException {
49
61
this .accessKey = accessKey ;
62
+ this .region = region ;
50
63
this .mac = Mac .getInstance ("HmacSHA256" );
51
64
this .secret = new SecretKeySpec (secretKey .getBytes (), "HmacSHA256" );
52
65
this .secretKey = secretKey ;
@@ -67,12 +80,12 @@ public HttpURLConnection sign() throws Exception {
67
80
synchronized (secret ) {
68
81
69
82
switch (protocol .signature ) {
70
- case 2 :
71
- return signVersion2 (protocol );
72
- case 3 :
73
- return signVersion3 ();
74
- default :
75
- throw new IllegalArgumentException ("Invalid signature version" );
83
+ case 2 :
84
+ return signVersion2 (protocol );
85
+ case 3 :
86
+ return signVersion3 ();
87
+ default :
88
+ throw new IllegalArgumentException ("Invalid signature version" );
76
89
}
77
90
}
78
91
}
@@ -122,7 +135,7 @@ private StringBuilder buildRequest() {
122
135
StringBuilder sb = new StringBuilder ();
123
136
String del = "" ;
124
137
125
- for (Map .Entry <String ,Object > parameter : arguments .entrySet ()) {
138
+ for (Map .Entry <String , Object > parameter : arguments .entrySet ()) {
126
139
Object value = parameter .getValue ();
127
140
if (value != null ) {
128
141
sb .append (del ) //
@@ -147,32 +160,25 @@ private String encodeUrl(String value) {
147
160
try {
148
161
// TODO Not very efficient now
149
162
return URLEncoder .encode (value , "utf-8" ).replace ("+" , "%20" ).replace ("*" , "%2A" ).replace ("%7E" , "~" );
150
- }
151
- catch (final UnsupportedEncodingException ex ) {
163
+ } catch (final UnsupportedEncodingException ex ) {
152
164
throw new RuntimeException (ex );
153
165
}
154
166
}
155
167
156
168
private byte [] hmacSha256 (final String str ) {
157
169
try {
158
170
mac .init (secret );
159
- }
160
- catch (final Exception e ) {
171
+ } catch (final Exception e ) {
161
172
throw new RuntimeException (e );
162
173
}
163
174
return mac .doFinal (str .getBytes ());
164
175
}
165
176
166
- public S3 s3 () {
167
- return new S3 (accessKey , secretKey );
168
- }
169
-
170
- public SES ses () {
171
- return new SES (new Protocol (this , "https://email.us-east-1.amazonaws.com/" , null , 3 ));
177
+ public S3Impl s3 () {
178
+ return new S3Impl (accessKey , secretKey );
172
179
}
173
-
174
- public SQS sqs (String region ) {
175
- return new SQS (new Protocol (this , region , SQS .version , 2 ));
180
+ public SQSImpl sqs () {
181
+ return new SQSImpl (new Protocol (this , region , SQSImpl .version , 2 ));
176
182
}
177
183
178
184
}
0 commit comments