/
S3ExecutionFactory.java
303 lines (241 loc) · 13.6 KB
/
S3ExecutionFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags and
* the COPYRIGHT.txt file distributed with this work.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teiid.translator.amazon.s3;
import java.nio.charset.Charset;
import javax.resource.cci.ConnectionFactory;
import org.teiid.core.BundleUtil;
import org.teiid.core.types.DataTypeManager;
import org.teiid.language.Call;
import org.teiid.metadata.BaseColumn.NullType;
import org.teiid.metadata.MetadataFactory;
import org.teiid.metadata.Procedure;
import org.teiid.metadata.ProcedureParameter;
import org.teiid.metadata.ProcedureParameter.Type;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.metadata.Table;
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.ExecutionFactory;
import org.teiid.translator.ProcedureExecution;
import org.teiid.translator.Translator;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.TranslatorProperty;
import org.teiid.translator.TypeFacility;
import org.teiid.translator.WSConnection;
@Translator(name="amazon-s3", description="Amazon S3 Translator, reads contents of files or writes to them")
public class S3ExecutionFactory extends ExecutionFactory<ConnectionFactory, WSConnection> {
public static BundleUtil UTIL = BundleUtil.getBundleUtil(S3ExecutionFactory.class);
public static final String GETTEXTFILE = "getTextFile"; //$NON-NLS-1$
public static final String GETFILE = "getFile"; //$NON-NLS-1$
public static final String SAVEFILE = "saveFile"; //$NON-NLS-1$
public static final String DELETEFILE = "deleteFile"; //$NON-NLS-1$
public static final String LISTBUCKET = "list"; //$NON-NLS-1$
private Charset encoding = Charset.defaultCharset();
private String awsAccessKey;
private String awsSecretKey;
private String bucket;
private String region;
private String encryption;
private String encryptionKey;
public S3ExecutionFactory() {
setTransactionSupport(TransactionSupport.NONE);
setSourceRequiredForMetadata(false);
}
@TranslatorProperty(display="File Encoding",advanced=true)
public String getEncoding() {
return encoding.name();
}
public void setEncoding(String encoding) {
this.encoding = Charset.forName(encoding);
}
@TranslatorProperty(display="Amazon Access Key",advanced=true)
public String getAccesskey() {
return awsAccessKey;
}
public void setAccesskey(String value) {
this.awsAccessKey = value;
}
@TranslatorProperty(display="Amazon Secret Key",advanced=true)
public String getSecretkey() {
return awsSecretKey;
}
public void setSecretkey(String value) {
this.awsSecretKey = value;
}
@TranslatorProperty(display="Amazon Region",advanced=true)
public String getRegion() {
return region;
}
public void setRegion(String value) {
this.region = value;
}
@TranslatorProperty(display="Amazon Bucket",advanced=true)
public String getBucket() {
return bucket;
}
public void setBucket(String value) {
this.bucket = value;
}
@TranslatorProperty(display="Server Side Customer Encryption Algorithm Used",advanced=true)
public String getEncryption() {
return encryption;
}
public void setEncryption(String value) {
this.encryption = value;
}
@TranslatorProperty(display="Server Side Customer Encryption Key to be used to decrypt the object",advanced=true)
public String getEncryptionkey() {
return encryptionKey;
}
public void setEncryptionkey(String value) {
this.encryptionKey = value;
}
@Override
public ProcedureExecution createProcedureExecution(final Call command,
final ExecutionContext executionContext, final RuntimeMetadata metadata,
final WSConnection conn) throws TranslatorException {
return new S3ProcedureExecution(command, this, metadata, executionContext, conn);
}
@Override
public void getMetadata(MetadataFactory metadataFactory, WSConnection connection) throws TranslatorException {
addGetTextFileMethod(metadataFactory);
addGetFileMethod(metadataFactory);
saveFile(metadataFactory);
deleteFile(metadataFactory);
listBucket(metadataFactory);
Table t = metadataFactory.addTable("Bucket");
t.setVirtual(true);
t.setSupportsUpdate(false);
metadataFactory.addColumn("Key", DataTypeManager.DefaultDataTypes.STRING, t);
metadataFactory.addColumn("LastModified", DataTypeManager.DefaultDataTypes.STRING, t);
metadataFactory.addColumn("ETag", DataTypeManager.DefaultDataTypes.STRING, t);
metadataFactory.addColumn("Size", DataTypeManager.DefaultDataTypes.STRING, t);
metadataFactory.addColumn("StorageClass", DataTypeManager.DefaultDataTypes.STRING, t);
metadataFactory.addColumn("NextContinuationToken", DataTypeManager.DefaultDataTypes.STRING, t);
t.setSelectTransformation("select b.* from (exec list()) as a, " +
" XMLTABLE(XMLNAMESPACES(DEFAULT 'http://s3.amazonaws.com/doc/2006-03-01/'), '/ListBucketResult/Contents' \n" +
" PASSING XMLPARSE(CONTENT a.result WELLFORMED) COLUMNS Key string, LastModified string," +
" ETag string, Size string, StorageClass string, \n" +
" NextContinuationToken string PATH '../NextContinuationToken') as b;");
}
private void saveFile(MetadataFactory metadataFactory) {
Procedure p = metadataFactory.addProcedure(SAVEFILE);
p.setAnnotation("Saves the given value to the given bucket. Any existing file will be overriden."); //$NON-NLS-1$
ProcedureParameter param = metadataFactory.addProcedureParameter("name", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("The name of the object to save"); //$NON-NLS-1$
addCommonParameters(metadataFactory, p);
param = metadataFactory.addProcedureParameter("contents", TypeFacility.RUNTIME_NAMES.OBJECT, Type.In, p); //$NON-NLS-1$
param.setAnnotation("The contents to save. Can be one of CLOB, BLOB, or XML"); //$NON-NLS-1$
}
private void listBucket(MetadataFactory metadataFactory) {
Procedure p = metadataFactory.addProcedure(LISTBUCKET);
p.setAnnotation("Lists the Objects in a given bucket."); //$NON-NLS-1$
ProcedureParameter param;
param = metadataFactory.addProcedureParameter("bucket", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("The name of the bucket in Amazon S3"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("region", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("region in which the bucket exists on Amazon S3"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("accesskey", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Security Access Key, if not provided will use translator configured keys."); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("secretkey", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Security Secret Key, if not provided will use translator configured keys."); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("nexttoken", TypeFacility.RUNTIME_NAMES.STRING, //$NON-NLS-1$
Type.In, p);
param.setAnnotation("If the response is truncated, Amazon S3 returns this parameter with a "
+ "continuation token that you can specify as the continuation-token in your next request "
+ "to retrieve the next set of keys"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
metadataFactory.addProcedureResultSetColumn("result", TypeFacility.RUNTIME_NAMES.CLOB, p); //$NON-NLS-1$
}
private void deleteFile(MetadataFactory metadataFactory) {
Procedure p = metadataFactory.addProcedure(DELETEFILE);
p.setAnnotation("Delete the given file from bucket."); //$NON-NLS-1$
ProcedureParameter param = metadataFactory.addProcedureParameter("name", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Name of the file"); //$NON-NLS-1$
addCommonParameters(metadataFactory, p);
}
private void addCommonParameters(MetadataFactory metadataFactory, Procedure p) {
ProcedureParameter param;
param = metadataFactory.addProcedureParameter("bucket", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("The name of the bucket in Amazon S3"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("region", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("region in which the bucket exists on Amazon S3"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("endpoint", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Endpoint point of the Object, if provided this overwirtes the name and bucket properties"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("accesskey", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Security Access Key, if not provided will use translator configured keys."); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("secretkey", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Security Secret Key, if not provided will use translator configured keys."); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
}
private void addGetTextFileMethod(MetadataFactory metadataFactory) {
Procedure p = metadataFactory.addProcedure(GETTEXTFILE);
p.setAnnotation("Returns text files that match the given path as CLOBs"); //$NON-NLS-1$
ProcedureParameter param = metadataFactory.addProcedureParameter("name", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("The name of the file to return. Currently the patterns like *.<ext> are not supported"); //$NON-NLS-1$
addCommonParameters(metadataFactory, p);
param = metadataFactory.addProcedureParameter("encryption", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Server side encryption algorithm used"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("encryptionkey", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Server side encryption key to decrypt the object"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("stream", TypeFacility.RUNTIME_NAMES.BOOLEAN, Type.In, p); //$NON-NLS-1$
param.setAnnotation("If the result should be streamed."); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param.setDefaultValue("false"); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("file", TypeFacility.RUNTIME_NAMES.CLOB, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("endpoint", TypeFacility.RUNTIME_NAMES.STRING, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("lastModified", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("etag", TypeFacility.RUNTIME_NAMES.STRING, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("size", TypeFacility.RUNTIME_NAMES.LONG, p); //$NON-NLS-1$
}
private void addGetFileMethod(MetadataFactory metadataFactory) {
Procedure p = metadataFactory.addProcedure(GETFILE);
p.setAnnotation("Returns file that match the given path as BLOB"); //$NON-NLS-1$
ProcedureParameter param = metadataFactory.addProcedureParameter("name", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("The name of the file to return. Currently the patterns like *.<ext> are not supported"); //$NON-NLS-1$
addCommonParameters(metadataFactory, p);
param = metadataFactory.addProcedureParameter("encryption", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Server side encryption algorithm used"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("encryptionkey", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
param.setAnnotation("Server side encryption key to decrypt the object"); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param = metadataFactory.addProcedureParameter("stream", TypeFacility.RUNTIME_NAMES.BOOLEAN, Type.In, p); //$NON-NLS-1$
param.setAnnotation("If the result should be streamed."); //$NON-NLS-1$
param.setNullType(NullType.Nullable);
param.setDefaultValue("false"); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("file", TypeFacility.RUNTIME_NAMES.BLOB, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("endpoint", TypeFacility.RUNTIME_NAMES.STRING, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("lastModified", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("etag", TypeFacility.RUNTIME_NAMES.STRING, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("size", TypeFacility.RUNTIME_NAMES.LONG, p); //$NON-NLS-1$
}
@Override
public boolean areLobsUsableAfterClose() {
return false;
}
}