@Override public S3EventNotification getTestEvent() throws Exception { /* * Upload a test resoruce to the mock S3 */ String payload = IOUtils.toString( new InputStreamReader(this.getClass().getResourceAsStream("basic_input.log"), "UTF-8")); this.client.putObject(S3_BUCKET, "basic_input.log", payload); /* * Create a S3EventNotification event */ S3ObjectEntity objEntity = new S3ObjectEntity("basic_input.log", 1L, null, null); S3BucketEntity bucketEntity = new S3BucketEntity(S3_BUCKET, null, null); S3Entity entity = new S3Entity(null, bucketEntity, objEntity, null); S3EventNotificationRecord rec = new S3EventNotificationRecord(null, null, null, "1970-01-01T00:00:00.000Z", null, null, null, entity, null); List<S3EventNotificationRecord> notifications = new ArrayList<S3EventNotificationRecord>(2); notifications.add(rec); return new S3EventNotification(notifications); }
private S3EventNotification getTestEvent(String bucket, boolean doPut) throws Exception { /* * Upload a test resoruce to the mock S3 */ if (doPut) { String payload = IOUtils.toString( new InputStreamReader(this.getClass().getResourceAsStream("basic_input.log"), "UTF-8")); this.client.putObject(bucket, "basic_input.log", payload); } /* * Create a S3EventNotification event */ S3ObjectEntity objEntity = new S3ObjectEntity("basic_input.log", 1L, null, null); S3BucketEntity bucketEntity = new S3BucketEntity(bucket, null, null); S3Entity entity = new S3Entity(null, bucketEntity, objEntity, null); S3EventNotificationRecord rec = new S3EventNotificationRecord(null, null, null, "1970-01-01T00:00:00.000Z", null, null, null, entity, null); List<S3EventNotificationRecord> notifications = new ArrayList<S3EventNotificationRecord>(2); notifications.add(rec); return new S3EventNotification(notifications); }
@Test public void testS3MessageS3FileNoExists() throws Exception { setLogLevel(UploadDownloadHelperServiceImpl.class, LogLevel.OFF); uploadDownloadServiceTestHelper.createDatabaseEntitiesForUploadDownloadTesting(); UploadSingleInitiationResponse resultUploadSingleInitiationResponse = uploadDownloadService.initiateUploadSingle(uploadDownloadServiceTestHelper .createUploadSingleInitiationRequest(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, NAMESPACE, BDEF_NAME_2, FORMAT_USAGE_CODE_2, FORMAT_FILE_TYPE_CODE_2, FORMAT_VERSION_2, TARGET_S3_KEY)); String filePath = resultUploadSingleInitiationResponse.getSourceBusinessObjectData().getStorageUnits().get(0).getStorageFiles().get(0).getFilePath(); S3Entity s3Entity = new S3Entity(null, null, new S3ObjectEntity(filePath, 0L, null, null), null); List<S3EventNotificationRecord> records = new ArrayList<>(); records.add(new S3EventNotificationRecord(null, null, null, null, null, null, null, s3Entity, null)); S3EventNotification s3EventNotification = new S3EventNotification(records); setLogLevel(UploadDownloadServiceImpl.class, LogLevel.OFF); setLogLevel(HerdJmsMessageListener.class, LogLevel.OFF); // Try to process an S3 JMS message, when source S3 file does not exist. herdJmsMessageListener.processMessage(jsonHelper.objectToJson(s3EventNotification), null); }
@Test public void testProcessSQS() throws Exception { final HttpRequestBase request = mock(HttpRequestBase.class); final S3ObjectInputStream stream = new S3ObjectInputStream(Resources .asByteSource( Resources.getResource("fixtures/s3_object.txt.gz")) .openStream(), request); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding("gzip"); final S3Object object = new S3Object(); object.setObjectMetadata(metadata); object.setObjectContent(stream); when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))).thenReturn(object); message.setBody(FixtureHelpers.fixture("fixtures/sqs_records.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, times(10)).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); verify(request, never()).abort(); assertThat(actual).isTrue(); }
public S3EventIterator(Context context, List<S3EventNotificationRecord> records, AmazonS3ClientFactory s3ClientFactory) { this.records = records; this.context = context; this.client = s3ClientFactory.newInstance(); this.config = new RetryConfigBuilder() .retryOnSpecificExceptions(SocketTimeoutException.class, UncheckedIOException.class) .withMaxNumberOfTries(3).withDelayBetweenTries(100, ChronoUnit.MILLIS) .withExponentialBackoff().build(); }
@Override public void handler(SNSEvent event, Context context) throws HandlerException { if (!initialized) { init(context); } this.source = this.sources.get(0); this.inputFiles = new ArrayList<String>(0); for (SNSRecord record : event.getRecords()) { /* * Parse SNS as a S3 notification */ String json = record.getSNS().getMessage(); S3EventNotification s3Event = S3EventNotification.parseJson(json); /* * Validate the S3 file matches the regex */ List<S3EventNotificationRecord> toProcess = new ArrayList<S3EventNotificationRecord>(s3Event.getRecords()); for (S3EventNotificationRecord s3Record : s3Event.getRecords()) { String s3Path = String.format("s3://%s/%s", s3Record.getS3().getBucket().getName(), s3Record.getS3().getObject().getKey()); try { this.source = SourceUtils.getSource(s3Path, this.sources); } catch (SourceNotFoundException e) { logger.warn("skipping processing " + s3Path); toProcess.remove(s3Record); } } if (toProcess.size() == 0) { logger.warn("Nothing to process"); return; } this.inputFiles.addAll(toProcess.stream().map(m -> { return m.getS3().getObject().getKey(); }).collect(Collectors.toList())); this.recordIterator = new S3EventIterator(context, toProcess, s3ClientFactory); super.process(context); } }
public void handler(S3EventNotification event, Context context) throws HandlerException { if (!initialized) { init(context); } /* * Validate the S3 file matches the regex */ List<S3EventNotificationRecord> toProcess = new ArrayList<S3EventNotificationRecord>(event.getRecords()); for (S3EventNotificationRecord record : event.getRecords()) { String s3Path = String.format("s3://%s/%s", record.getS3().getBucket().getName(), record.getS3().getObject().getKey()); try { this.source = SourceUtils.getSource(s3Path, this.sources); } catch (SourceNotFoundException e) { logger.warn("Skipping processing " + s3Path); toProcess.remove(record); } } if (toProcess.size() == 0) { logger.warn("Nothing to process"); return; } this.recordIterator = new S3EventIterator(context, toProcess, s3ClientFactory); super.process(context); }
public static S3EventNotification getS3Notification(String key, String bucket, long size) { S3ObjectEntity objEntity = new S3ObjectEntity(key, size, null, null); S3BucketEntity bucketEntity = new S3BucketEntity(bucket, null, null); S3Entity entity = new S3Entity(null, bucketEntity, objEntity, null); String timestamp = formatter.print(System.currentTimeMillis()); S3EventNotificationRecord rec = new S3EventNotificationRecord(null, null, null, timestamp, null, null, null, entity, null); List<S3EventNotificationRecord> notifications = new ArrayList<S3EventNotificationRecord>(1); notifications.add(rec); return new S3EventNotification(notifications); }
@Test public void testS3MessageWithWrongFormat() throws Exception { // Create and persist database entities required for testing. businessObjectDefinitionServiceTestHelper.createDatabaseEntitiesForBusinessObjectDefinitionTesting(); storageDaoTestHelper.createStorageEntity(StorageEntity.SAMPLE_DATA_FILE_STORAGE, Arrays.asList(new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_BUCKET_NAME), S3_BUCKET_NAME))); // Create a business object definition. BusinessObjectDefinitionCreateRequest request = new BusinessObjectDefinitionCreateRequest(NAMESPACE, BDEF_NAME, DATA_PROVIDER_NAME, BDEF_DESCRIPTION, BDEF_DISPLAY_NAME, businessObjectDefinitionServiceTestHelper.getNewAttributes()); businessObjectDefinitionService.createBusinessObjectDefinition(request); String fileName = "test1.csv"; String filePath = NAMESPACE + "/" + BDEF_NAME + fileName; long fileSize = 1024L; S3Entity s3Entity = new S3Entity(null, null, new S3ObjectEntity(filePath, fileSize, null, null), null); List<S3EventNotificationRecord> records = new ArrayList<>(); records.add(new S3EventNotificationRecord(null, null, null, null, null, null, null, s3Entity, null)); S3EventNotification s3EventNotification = new S3EventNotification(records); try { sampleDataJmsMessageListener.processMessage(jsonHelper.objectToJson(s3EventNotification), null); } catch (IllegalArgumentException ex) { //this exception should be caught inside the processMessage method fail(); } }
@Test public void testS3MessageNoKey() throws Exception { S3Entity s3Entity = new S3Entity(null, null, new S3ObjectEntity("key_does_not_exist", 0L, null, null), null); List<S3EventNotificationRecord> records = new ArrayList<>(); records.add(new S3EventNotificationRecord(null, null, null, null, null, null, null, s3Entity, null)); S3EventNotification s3EventNotification = new S3EventNotification(records); setLogLevel(UploadDownloadServiceImpl.class, LogLevel.OFF); setLogLevel(HerdJmsMessageListener.class, LogLevel.OFF); herdJmsMessageListener.processMessage(jsonHelper.objectToJson(s3EventNotification), null); }
private File retrieveS3File(String sqsdMessageBody) throws UnsupportedEncodingException { File localFile = null; if(!sqsdMessageBody.isEmpty()){ AmazonS3 s3 = new AmazonS3Client(); List<S3EventNotificationRecord> records = S3EventNotification.parseJson(sqsdMessageBody).getRecords(); S3EventNotificationRecord firstRecord = records.get(0); String bucketName = firstRecord.getS3().getBucket().getName(); String objectRegion = firstRecord.getAwsRegion(); Region s3Region = Region.getRegion(Regions.fromName(objectRegion)); s3.setRegion(s3Region); // Object key may have spaces or unicode non-ASCII characters. String keyName = firstRecord.getS3().getObject().getKey().replace('+', ' '); keyName = URLDecoder.decode(keyName, "UTF-8"); localFile = new File(keyName); System.out.println("Downloading file: " + objectRegion + "/" + bucketName + "/" + keyName); s3.getObject(new GetObjectRequest(bucketName, keyName), localFile); if(!localFile.canRead()){ localFile = null; } } return localFile; }
@Override protected AmazonS3Object doForward(S3EventNotificationRecord a) { final S3Entity s3 = a.getS3(); final S3ObjectEntity object = s3.getObject(); return new AmazonS3Object(a.getAwsRegion(), s3.getBucket().getName(), object.getKey(), object.getSizeAsLong(), object.geteTag(), object.getVersionId()); }
@Override protected S3EventNotificationRecord doBackward(AmazonS3Object b) { final S3BucketEntity bucket = new S3BucketEntity(b.getBucketName(), null, null); final S3ObjectEntity object = new S3ObjectEntity(b.getKey(), b.getSize(), b.getETag().orElse(null), b.getVersionId().orElse(null), null); final S3Entity s3 = new S3Entity(null, bucket, object, null); return new S3EventNotificationRecord(null, null, null, null, null, null, null, s3, null); }
@Test public void testConverter() throws Exception { final S3EventNotificationRecord record = MAPPER.readValue( FixtureHelpers.fixture("fixtures/amazon_event_record.json"), S3EventNotificationRecord.class); final AmazonS3Object actual = converter.convert(record); assertThat(actual.getRegion()).isEqualTo("us-east-1"); assertThat(actual.getBucketName()).isEqualTo("bucket-name"); assertThat(actual.getKey()).isEqualTo("object-key"); assertThat(actual.getSize()).isEqualTo(100); assertThat(actual.getETag().get()).isEqualTo("object eTag"); }
@Test public void testProcessNullMessage() throws Exception { final boolean actual = processor.test(null); verify(broadcaster, never()).isEmpty(); verify(broadcaster, never()).broadcast(any(OutboundEvent.class)); verify(s3, never()).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isFalse(); }
@Test public void testProcessNoConnections() throws Exception { when(broadcaster.isEmpty()).thenReturn(true); final boolean actual = processor.test(message); verify(broadcaster).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3, never()).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isFalse(); }
@Test public void testProcessSNSParseFailure() throws Exception { when(broadcaster.isEmpty()).thenReturn(false); final boolean actual = processor.test(message); verify(broadcaster).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3, never()).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isTrue(); }
@Test public void testProcessS3EventFetchFailure() throws Exception { when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))) .thenThrow(new AmazonServiceException("error")); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isFalse(); }
@Test public void testProcessNoConnectionsAfterParse() throws Exception { when(broadcaster.isEmpty()).thenReturn(false, true); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3, never()).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isFalse(); }
@Test public void testProcessS3ZeroSizeFailure() throws Exception { when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))) .thenThrow(new AmazonS3ZeroSizeException()); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isTrue(); }
@Test public void testProcessS3ConstraintFailure() throws Exception { when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))) .thenThrow(new AmazonS3ConstraintException()); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isTrue(); }
@Test public void testProcessSNS() throws Exception { final HttpRequestBase request = mock(HttpRequestBase.class); final S3ObjectInputStream stream = new S3ObjectInputStream(Resources .asByteSource( Resources.getResource("fixtures/s3_object.txt.gz")) .openStream(), request); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding("gzip"); final S3Object object = new S3Object(); object.setObjectMetadata(metadata); object.setObjectContent(stream); when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))).thenReturn(object); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, times(10)).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); verify(request, never()).abort(); assertThat(actual).isTrue(); }
@Test public void testProcessNoConnectionsDuringDownload() throws Exception { final HttpRequestBase request = mock(HttpRequestBase.class); final S3ObjectInputStream stream = new S3ObjectInputStream(Resources .asByteSource( Resources.getResource("fixtures/s3_object.txt.gz")) .openStream(), request); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding("gzip"); final S3Object object = new S3Object(); object.setObjectMetadata(metadata); object.setObjectContent(stream); when(broadcaster.test(anyString())).thenReturn(false, false, false, false, true); when(s3.fetch(any(S3EventNotificationRecord.class))).thenReturn(object); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, times(5)).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); verify(request).abort(); assertThat(actual).isFalse(); }
@Before public void setUp() { reset(mockS3); final S3BucketEntity bucket = new S3BucketEntity("bucket-name", null, null); final S3ObjectEntity object = new S3ObjectEntity("object-key", 100L, "object eTag", "object version", null); final S3Entity s3 = new S3Entity(null, bucket, object, null); record = new S3EventNotificationRecord("us-east-1", null, "aws:s3", "1970-01-01T00:00:00.000Z", "2.0", null, null, s3, null); }
public void auditValidatedFile(S3Event event,Context ctx) throws Exception{ Connection conn = new com.mysql.jdbc.Driver().connect(props.getProperty("url"), props); List<S3EventNotificationRecord> notificationRecords = event.getRecords(); PreparedStatement ps = conn.prepareStatement(props.getProperty("sql.auditValidatedFile")); for(S3EventNotificationRecord record : notificationRecords){ String fileURL = record.getS3().getBucket().getName()+"/"+record.getS3().getObject().getKey(); ps.setString(1, fileURL); ps.setString(2, "VALIDATED"); ps.setString(3,"VALIDATED"); ps.addBatch(); } ps.executeBatch(); ps.close(); conn.close(); }
@Override public SNSEvent getTestEvent() throws Exception { /* * Upload a test resoruce to the mock S3 */ String payload = IOUtils.toString( new InputStreamReader(this.getClass().getResourceAsStream("basic_input.log"), "UTF-8")); this.client.putObject(S3_BUCKET, "basic_input.log", payload); /* * Create a S3EventNotification event */ S3ObjectEntity objEntity = new S3ObjectEntity("basic_input.log", 1L, null, null); S3BucketEntity bucketEntity = new S3BucketEntity(S3_BUCKET, null, null); S3Entity entity = new S3Entity(null, bucketEntity, objEntity, null); S3EventNotificationRecord rec = new S3EventNotificationRecord(null, null, null, "1970-01-01T00:00:00.000Z", null, null, null, entity, null); List<S3EventNotificationRecord> notifications = new ArrayList<S3EventNotificationRecord>(2); notifications.add(rec); /* * Wrap as an SNS Event */ S3EventNotification event = new S3EventNotification(notifications); SNSEvent.SNS sns = new SNSEvent.SNS(); sns.setMessage(event.toJson()); SNSEvent snsEvent = new SNSEvent(); ArrayList<SNSRecord> snsRecords = new ArrayList<SNSRecord>(1); SNSRecord snsRecord = new SNSRecord(); snsRecord.setEventSource("aws:sns"); snsRecord.setEventVersion("1.0"); snsRecord.setEventSubscriptionArn("arn"); snsRecord.setSns(sns); snsRecords.add(snsRecord); snsEvent.setRecords(snsRecords); return snsEvent; }
private SNSEvent getTestEvent(String bucket, boolean doPut) throws Exception { /* * Upload a test resoruce to the mock S3 */ if (doPut) { String payload = IOUtils.toString( new InputStreamReader(this.getClass().getResourceAsStream("basic_input.log"), "UTF-8")); this.client.putObject(bucket, "basic_input.log", payload); } /* * Create a S3EventNotification event */ S3ObjectEntity objEntity = new S3ObjectEntity("basic_input.log", 1L, null, null); S3BucketEntity bucketEntity = new S3BucketEntity(bucket, null, null); S3Entity entity = new S3Entity(null, bucketEntity, objEntity, null); S3EventNotificationRecord rec = new S3EventNotificationRecord(null, null, null, "1970-01-01T00:00:00.000Z", null, null, null, entity, null); List<S3EventNotificationRecord> notifications = new ArrayList<S3EventNotificationRecord>(2); notifications.add(rec); /* * Wrap as an SNS Event */ S3EventNotification event = new S3EventNotification(notifications); SNSEvent.SNS sns = new SNSEvent.SNS(); sns.setMessage(event.toJson()); SNSEvent snsEvent = new SNSEvent(); ArrayList<SNSRecord> snsRecords = new ArrayList<SNSRecord>(1); SNSRecord snsRecord = new SNSRecord(); snsRecord.setEventSource("aws:sns"); snsRecord.setEventVersion("1.0"); snsRecord.setEventSubscriptionArn("arn"); snsRecord.setSns(sns); snsRecords.add(snsRecord); snsEvent.setRecords(snsRecords); return snsEvent; }
public String handleRequest(S3Event s3event, Context context) { _logger = context.getLogger(); _logger.log("Received S3 Event: " + s3event.toJson()); try { S3EventNotificationRecord record = s3event.getRecords().get(0); String bucket = record.getS3().getBucket().getName(); String extractBucket = "extracts." + bucket; // Object key may have spaces or unicode non-ASCII characters. String key = URLDecoder.decode(record.getS3().getObject().getKey().replace('+', ' '), "UTF-8"); // Short-circuit ignore .extract files because they have already been extracted, this prevents an endless loop if (key.toLowerCase().endsWith(".extract")) { _logger.log("Ignoring extract file " + key); return "Ignored"; } AmazonS3 s3Client = new AmazonS3Client(); S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucket, key)); try (InputStream objectData = s3Object.getObjectContent()) { String extractJson = doTikaStuff(bucket, key, objectData); byte[] extractBytes = extractJson.getBytes(Charset.forName("UTF-8")); int extractLength = extractBytes.length; ObjectMetadata metaData = new ObjectMetadata(); metaData.setContentLength(extractLength); _logger.log("Saving extract file to S3"); InputStream inputStream = new ByteArrayInputStream(extractBytes); s3Client.putObject(extractBucket, key + ".extract", inputStream, metaData); } } catch (IOException | TransformerConfigurationException | SAXException e) { _logger.log("Exception: " + e.getLocalizedMessage()); throw new RuntimeException(e); } return "Success"; }
@Test public void testS3Message() throws Exception { // Create and persist database entities required for testing. businessObjectDefinitionServiceTestHelper.createDatabaseEntitiesForBusinessObjectDefinitionTesting(); storageDaoTestHelper.createStorageEntity(StorageEntity.SAMPLE_DATA_FILE_STORAGE, Arrays.asList(new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_BUCKET_NAME), S3_BUCKET_NAME))); // Create a business object definition. BusinessObjectDefinitionCreateRequest request = new BusinessObjectDefinitionCreateRequest(NAMESPACE, BDEF_NAME, DATA_PROVIDER_NAME, BDEF_DESCRIPTION, BDEF_DISPLAY_NAME, businessObjectDefinitionServiceTestHelper.getNewAttributes()); businessObjectDefinitionService.createBusinessObjectDefinition(request); // Get the business object definition entity. BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDao.getBusinessObjectDefinitionByKey(new BusinessObjectDefinitionKey(NAMESPACE, BDEF_NAME)); assertNotNull(businessObjectDefinitionEntity); String fileName = "test1.csv"; String filePath = NAMESPACE + "/" + BDEF_NAME + "/" + fileName; long fileSize = 1024L; S3Entity s3Entity = new S3Entity(null, null, new S3ObjectEntity(filePath, fileSize, null, null), null); List<S3EventNotificationRecord> records = new ArrayList<>(); records.add(new S3EventNotificationRecord(null, null, null, null, null, null, null, s3Entity, null)); S3EventNotification s3EventNotification = new S3EventNotification(records); sampleDataJmsMessageListener.processMessage(jsonHelper.objectToJson(s3EventNotification), null); BusinessObjectDefinitionKey businessObjectDefinitionKey = new BusinessObjectDefinitionKey(NAMESPACE, BDEF_NAME); BusinessObjectDefinition updatedBusinessObjectDefinition = businessObjectDefinitionService.getBusinessObjectDefinition(businessObjectDefinitionKey, false); List<SampleDataFile> sampleDataFiles = Arrays.asList(new SampleDataFile(NAMESPACE + "/" + BDEF_NAME + "/", fileName)); // Validate the returned object. assertEquals(new BusinessObjectDefinition(updatedBusinessObjectDefinition.getId(), NAMESPACE, BDEF_NAME, DATA_PROVIDER_NAME, BDEF_DESCRIPTION, NO_BDEF_SHORT_DESCRIPTION, BDEF_DISPLAY_NAME, businessObjectDefinitionServiceTestHelper.getNewAttributes(), NO_DESCRIPTIVE_BUSINESS_OBJECT_FORMAT, sampleDataFiles, businessObjectDefinitionEntity.getCreatedBy(), businessObjectDefinitionEntity.getUpdatedBy(), HerdDateUtils.getXMLGregorianCalendarValue(businessObjectDefinitionEntity.getUpdatedOn()), NO_BUSINESS_OBJECT_DEFINITION_CHANGE_EVENTS), updatedBusinessObjectDefinition); }
@Test public void testS3MessageWithDashCharacterName() throws Exception { String namespace = "testnamespace-1"; String businessObjectDefinitionName = "testbdefname-1"; // Create and persist database entities required for testing. businessObjectDefinitionServiceTestHelper.createDatabaseEntitiesForBusinessObjectDefinitionTesting(namespace, DATA_PROVIDER_NAME); storageDaoTestHelper.createStorageEntity(StorageEntity.SAMPLE_DATA_FILE_STORAGE, Arrays.asList(new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_BUCKET_NAME), S3_BUCKET_NAME))); // Create a business object definition. BusinessObjectDefinitionCreateRequest request = new BusinessObjectDefinitionCreateRequest(namespace, businessObjectDefinitionName, DATA_PROVIDER_NAME, BDEF_DESCRIPTION, BDEF_DISPLAY_NAME, businessObjectDefinitionServiceTestHelper.getNewAttributes()); businessObjectDefinitionService.createBusinessObjectDefinition(request); // Get the business object definition entity. BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDao.getBusinessObjectDefinitionByKey(new BusinessObjectDefinitionKey(namespace, businessObjectDefinitionName)); assertNotNull(businessObjectDefinitionEntity); String fileName = "test1.csv"; String filePath = namespace + "/" + businessObjectDefinitionName + "/" + fileName; long fileSize = 1024L; S3Entity s3Entity = new S3Entity(null, null, new S3ObjectEntity(filePath, fileSize, null, null), null); List<S3EventNotificationRecord> records = new ArrayList<>(); records.add(new S3EventNotificationRecord(null, null, null, null, null, null, null, s3Entity, null)); S3EventNotification s3EventNotification = new S3EventNotification(records); sampleDataJmsMessageListener.processMessage(jsonHelper.objectToJson(s3EventNotification), null); BusinessObjectDefinitionKey businessObjectDefinitionKey = new BusinessObjectDefinitionKey(namespace, businessObjectDefinitionName); BusinessObjectDefinition updatedBusinessObjectDefinition = businessObjectDefinitionService.getBusinessObjectDefinition(businessObjectDefinitionKey, false); List<SampleDataFile> sampleDataFiles = Arrays.asList(new SampleDataFile(namespace + "/" + businessObjectDefinitionName + "/", fileName)); // Validate the returned object. assertEquals( new BusinessObjectDefinition(updatedBusinessObjectDefinition.getId(), namespace, businessObjectDefinitionName, DATA_PROVIDER_NAME, BDEF_DESCRIPTION, NO_BDEF_SHORT_DESCRIPTION, BDEF_DISPLAY_NAME, businessObjectDefinitionServiceTestHelper.getNewAttributes(), NO_DESCRIPTIVE_BUSINESS_OBJECT_FORMAT, sampleDataFiles, businessObjectDefinitionEntity.getCreatedBy(), businessObjectDefinitionEntity.getUpdatedBy(), HerdDateUtils.getXMLGregorianCalendarValue(businessObjectDefinitionEntity.getUpdatedOn()), NO_BUSINESS_OBJECT_DEFINITION_CHANGE_EVENTS), updatedBusinessObjectDefinition); }
@Test public void testS3Message() throws Exception { setLogLevel(UploadDownloadHelperServiceImpl.class, LogLevel.OFF); uploadDownloadServiceTestHelper.createDatabaseEntitiesForUploadDownloadTesting(); UploadSingleInitiationResponse resultUploadSingleInitiationResponse = uploadDownloadService.initiateUploadSingle(uploadDownloadServiceTestHelper.createUploadSingleInitiationRequest()); String filePath = resultUploadSingleInitiationResponse.getSourceBusinessObjectData().getStorageUnits().get(0).getStorageFiles().get(0).getFilePath(); S3Entity s3Entity = new S3Entity(null, null, new S3ObjectEntity(filePath, 0L, null, null), null); List<S3EventNotificationRecord> records = new ArrayList<>(); records.add(new S3EventNotificationRecord(null, null, null, null, null, null, null, s3Entity, null)); S3EventNotification s3EventNotification = new S3EventNotification(records); setLogLevel(UploadDownloadServiceImpl.class, LogLevel.OFF); setLogLevel(HerdJmsMessageListener.class, LogLevel.DEBUG); herdJmsMessageListener.processMessage(jsonHelper.objectToJson(s3EventNotification), null); }
@Override public String handleRequest(S3Event s3Event, Context context) { byte[] buffer = new byte[1024]; try { for (S3EventNotificationRecord record: s3Event.getRecords()) { String srcBucket = record.getS3().getBucket().getName(); // Object key may have spaces or unicode non-ASCII characters. String srcKey = record.getS3().getObject().getKey() .replace('+', ' '); srcKey = URLDecoder.decode(srcKey, "UTF-8"); // Detect file type Matcher matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey); if (!matcher.matches()) { System.out.println("Unable to detect file type for key " + srcKey); return ""; } String extension = matcher.group(1).toLowerCase(); if (!"zip".equals(extension)) { System.out.println("Skipping non-zip file " + srcKey + " with extension " + extension); return ""; } System.out.println("Extracting zip file " + srcBucket + "/" + srcKey); // Download the zip from S3 into a stream AmazonS3 s3Client = new AmazonS3Client(); S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey)); ZipInputStream zis = new ZipInputStream(s3Object.getObjectContent()); ZipEntry entry = zis.getNextEntry(); while(entry != null) { String fileName = entry.getName(); String mimeType = FileMimeType.fromExtension(FilenameUtils.getExtension(fileName)).mimeType(); System.out.println("Extracting " + fileName + ", compressed: " + entry.getCompressedSize() + " bytes, extracted: " + entry.getSize() + " bytes, mimetype: " + mimeType); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int len; while ((len = zis.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } InputStream is = new ByteArrayInputStream(outputStream.toByteArray()); ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(outputStream.size()); meta.setContentType(mimeType); s3Client.putObject(srcBucket, FilenameUtils.getFullPath(srcKey) + fileName, is, meta); is.close(); outputStream.close(); entry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); //delete zip file when done System.out.println("Deleting zip file " + srcBucket + "/" + srcKey + "..."); s3Client.deleteObject(new DeleteObjectRequest(srcBucket, srcKey)); System.out.println("Done deleting"); } return "Ok"; } catch (IOException e) { throw new RuntimeException(e); } }