Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.justinefactory.cloud.communication.service;


import com.justinefactory.cloud.communication.transfer.CloudObjectContentGetter;
import com.justinefactory.deserialization.deserializers.Deserializer;
import com.justinefactory.domain.AwsInfo;
import com.justinefactory.reading.exceptions.AwsContentReadingException;
import com.justinefactory.reading.exceptions.ContentDeserializationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.lang.invoke.MethodHandles;

class CloudObjectTransferService<ContentType, DeserializedContent> {

private final CloudObjectContentGetter<ContentType> cloudObjectContentGetter;
private final Deserializer<ContentType, DeserializedContent> deserializer;
private static final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass());

CloudObjectTransferService(CloudObjectContentGetter<ContentType> cloudObjectContentGetter, Deserializer<ContentType, DeserializedContent> deserializer) {
logger.info("Transfer service - initialization");
this.cloudObjectContentGetter = cloudObjectContentGetter;
this.deserializer = deserializer;
}

public DeserializedContent getAndDeserializeCloudObject(AwsInfo info, Class<DeserializedContent> classToDeserialize) throws IOException, AwsContentReadingException, ContentDeserializationException {
ContentType input = cloudObjectContentGetter.getObjectContent(info);
logger.debug("Transfer Service - extracting content from cloud object - success.");
DeserializedContent deserializedContent = deserializer.deserialize(input, classToDeserialize);
logger.info("Transfer Service - finished with success.");
return deserializedContent;
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.justinefactory.cloud.communication.distribution.service;
package com.justinefactory.cloud.communication.service;

import com.amazonaws.services.s3.AmazonS3;
import com.justinefactory.cloud.communication.distribution.ServerObjectDownloadByUrlCreator;
import com.justinefactory.domain.AwsInfo;
import com.justinefactory.reading.exceptions.ContentReadingException;
import com.justinefactory.sending.service.ContentSendingService;
import com.justinefactory.stats.exceptions.StatsCalculatingException;
import com.justinefactory.writing.domain.ContentStorage;
import com.justinefactory.writing.domain.Content;
import com.justinefactory.writing.exceptions.ContentWritingException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandles;
import java.time.Duration;

public class CloudObjectsManagementService<RawContent, Content> {
public class CloudObjectsManagementService<RawContentType, ContentType> {

private final ContentSendingService<RawContent, Content, ContentStorage<String>> sendingService;
private final ContentSendingService<RawContentType, ContentType, Content<String>> sendingService;
private final ServerObjectDownloadByUrlCreator urlCreator;
private static final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass());

public CloudObjectsManagementService(ContentSendingService<RawContent, Content, ContentStorage<String>> sendingService, ServerObjectDownloadByUrlCreator urlCreator) {
public CloudObjectsManagementService(ContentSendingService<RawContentType, ContentType, Content<String>> sendingService, ServerObjectDownloadByUrlCreator urlCreator) {
logger.debug("Creating, sending and management of server objects - initialization");
this.sendingService = sendingService;
this.urlCreator = urlCreator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.justinefactory.cloud.communication.transfer;

import com.justinefactory.domain.AwsInfo;
import com.justinefactory.reading.exceptions.AwsContentReadingException;

import java.io.IOException;

public interface CloudObjectContentGetter<ContentType> {

ContentType getObjectContent(AwsInfo info) throws IOException, AwsContentReadingException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.justinefactory.cloud.communication.transfer;

import com.justinefactory.reading.exceptions.AwsContentReadingException;

public interface ContentExtractor<Object, ContentType> {

ContentType extractContent(Object object) throws AwsContentReadingException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.justinefactory.cloud.communication.transfer;

import com.amazonaws.services.s3.model.S3ObjectInputStream;

public interface ContentExtractorFromS3ObjectInputStream<ContentType> extends ContentExtractor<S3ObjectInputStream, ContentType> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.justinefactory.cloud.communication.transfer;

import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.util.IOUtils;
import com.justinefactory.reading.exceptions.AwsContentReadingException;
import com.justinefactory.writing.domain.JsonContent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;


public class JsonExtractorFromS3ObjectInputStream implements ContentExtractorFromS3ObjectInputStream<JsonContent> {

private final Logger logger = LogManager.getLogger(this.getClass());

@Override
public JsonContent extractContent(S3ObjectInputStream inputStream) throws AwsContentReadingException {
if (inputStream == null) {
throw new AwsContentReadingException("Trouble while processing inputStream - input was empty.");
}
try {
return new JsonContent(IOUtils.toString(inputStream));
} catch (IOException e) {
logger.warn("Trouble while extracting json object from inputStream {}", inputStream, e);
throw new AwsContentReadingException(e, "Trouble while extracting json object from inputStream " + inputStream);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.justinefactory.cloud.communication.transfer;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.justinefactory.domain.AwsInfo;
import com.justinefactory.reading.exceptions.AwsContentReadingException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;


public class S3CloudObjectContentGetter<ContentType> implements CloudObjectContentGetter<ContentType> {

private final AmazonS3 client;
private final ContentExtractorFromS3ObjectInputStream<ContentType> contentExtractor;
private final Logger logger = LogManager.getLogger(this.getClass());

public S3CloudObjectContentGetter(AmazonS3 awsClient, ContentExtractorFromS3ObjectInputStream<ContentType> contentExtractor) {
client = awsClient;
this.contentExtractor = contentExtractor;
}

@Override
public ContentType getObjectContent(AwsInfo info) throws IOException, AwsContentReadingException {
try (S3ObjectInputStream inputStream = client.getObject(new GetObjectRequest(info.getBucketName(), info.getKeyName())).getObjectContent()) {
return contentExtractor.extractContent(inputStream);
} catch (AmazonServiceException e) {
logger.warn("Trouble with Amazon S3 while processing get request of {}.", info.getURI());
throw new AmazonServiceException("Trouble with Amazon S3 while processing request on ." + info.getURI(), e);
} catch (SdkClientException e) {
logger.warn("Trouble with Amazon S3 connection while processing get request on {}.", info.getURI());
throw new SdkClientException("Trouble with Amazon S3 connection.", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.justinefactory.deserialization.deserializers;

import com.justinefactory.reading.exceptions.ContentDeserializationException;

import java.lang.reflect.Type;

public interface Deserializer<Object, ContentType> {

ContentType deserialize(Object object, Class<ContentType> type) throws ContentDeserializationException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.justinefactory.deserialization.deserializers;

import com.google.gson.Gson;
import com.justinefactory.reading.exceptions.ContentDeserializationException;
import com.justinefactory.writing.domain.JsonContent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class JsonDeserializer<ContentType> implements Deserializer<JsonContent, ContentType> {

private final Logger logger = LogManager.getLogger(this.getClass());

@Override
public ContentType deserialize(JsonContent jsonContent, Class<ContentType> classTypeToDeserializeJson) throws ContentDeserializationException {
Gson gson = new Gson();
try {
return gson.fromJson(jsonContent.getContent(), classTypeToDeserializeJson);
} catch (Throwable e) {
logger.warn("Trouble while performing deserialization of json: {}", jsonContent.getContent(), e);
throw new ContentDeserializationException(e, "Trouble while performing deserialization of json: " + jsonContent.getContent());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.justinefactory.reading.exceptions;

public class AwsContentReadingException extends ContentReadingException {

public AwsContentReadingException(Throwable cause) {
super(cause);
}

public AwsContentReadingException(Throwable cause, String message) {
super(cause, message);
}

public AwsContentReadingException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.justinefactory.reading.exceptions;

public class ContentDeserializationException extends ContentReadingException {

public ContentDeserializationException(Throwable cause, String message) {
super(cause, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.justinefactory.reading.parsers;

import com.justinefactory.reading.exceptions.ContentParsingException;
import com.justinefactory.writing.domain.Content;

public interface ContentParser<Line, Content> {
public interface ContentParser<RawContent, ContentType> {

public Content parseLine(Line line) throws ContentParsingException;
Content<ContentType> parse(RawContent rawContent) throws ContentParsingException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.justinefactory.reading.parsers;

public interface CsvLineParser<Line> extends ContentParser<String[], Line> {
import com.justinefactory.writing.domain.CsvContent;

public interface CsvLineParser<RawContent> extends ContentParser<CsvContent, RawContent> {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.justinefactory.reading.parsers;

import com.justinefactory.reading.exceptions.ContentParsingException;
import com.justinefactory.writing.domain.Content;
import com.justinefactory.writing.domain.PlainContent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -11,7 +13,15 @@ public class IntegerPlainContentParser implements PlainContentParser<Integer> {
private final static Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass());

@Override
public Integer parseLine(String rawLine) throws ContentParsingException {
public Content<Integer> parse(PlainContent strings) throws ContentParsingException {
Content<Integer> content = new Content<>();
for (String item : strings.getContent()) {
content.addContent(parseLine(item));
}
return content;
}

private Integer parseLine(String rawLine) throws ContentParsingException {
if (rawLine.isEmpty()) {
logger.warn("Parsing file line to Integer - failed. Line: {} does not equal required number of columns (1).", rawLine);
throw new ContentParsingException("Parsing file line to Integer - failed. The line " + rawLine + " does not equal required number of columns (1).");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.justinefactory.reading.parsers;

public interface PlainContentParser<Line> extends ContentParser<String, Line> {
import com.justinefactory.writing.domain.PlainContent;

public interface PlainContentParser<RawContent> extends ContentParser<PlainContent, RawContent> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@

import com.justinefactory.domain.ThreeElemContent;
import com.justinefactory.reading.exceptions.ContentParsingException;
import com.justinefactory.writing.domain.Content;
import com.justinefactory.writing.domain.CsvContent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.List;

public class ThreeElementCsvParser implements CsvLineParser<ThreeElemContent> {

private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass());

@Override
public ThreeElemContent parseLine(String[] csvLine) throws ContentParsingException {
public Content<ThreeElemContent> parse(CsvContent strings) throws ContentParsingException {
Content<ThreeElemContent> content = new Content<>();
for(String[] item : strings.getContent()){
content.addContent(parseLine(item));
}
return content;
}


private ThreeElemContent parseLine(String[] csvLine) throws ContentParsingException {
logger.debug("Parsing csvFile line to ThreeElemContent class.");
if (csvLine.length != 3) {
logger.warn("Parsing csvFile line to ThreeElemContent class - CSV line: {} does not equal required number of columns (3).", csvLine);
Expand All @@ -32,4 +44,6 @@ public ThreeElemContent parseLine(String[] csvLine) throws ContentParsingExcepti
}

}


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.justinefactory.reading.readers;

import com.justinefactory.reading.exceptions.ContentReadingException;
import com.justinefactory.writing.domain.ContentStorage;
import com.justinefactory.writing.domain.Content;

public interface ContentReader<RawContent> {
public interface ContentReader<ContentType> {

ContentStorage<RawContent> readContent() throws ContentReadingException;
ContentType readContent() throws ContentReadingException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import com.justinefactory.reading.exceptions.ContentReadingException;
import com.justinefactory.reading.exceptions.ReadingContentFromFileException;
import com.justinefactory.reading.exceptions.SourceFileIsEmptyException;
import com.justinefactory.writing.domain.ContentStorage;
import com.justinefactory.writing.domain.CsvContent;
import com.opencsv.CSVReader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Reader;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.justinefactory.util.FileNotEmptyAndExistChecker.checkIfFileExistsIsNotDirAndNotEmpty;

class CsvContentReader implements ContentReader<String[]> {
class CsvContentReader implements ContentReader<CsvContent> {

private final PathInfo fileData;
private final Logger logger = LogManager.getLogger(MethodHandles.lookup().lookupClass());
Expand All @@ -28,7 +26,7 @@ class CsvContentReader implements ContentReader<String[]> {


@Override
public ContentStorage<String[]> readContent() throws ContentReadingException {
public CsvContent readContent() throws ContentReadingException {
logger.debug("Reading data from file id {}", fileData.getId());
if (!checkIfFileExistsIsNotDirAndNotEmpty(fileData.getPath())) {
logger.warn("Reading data from file id {} failed. File does not exist or is empty.", fileData.getId());
Expand All @@ -38,7 +36,7 @@ public ContentStorage<String[]> readContent() throws ContentReadingException {
try (Reader reader = Files.newBufferedReader(fileData.getPath());
CSVReader csvReader = new CSVReader(reader)
) {
ContentStorage<String[]> rawContent = new ContentStorage<>(csvReader.readAll());
CsvContent rawContent = new CsvContent(csvReader.readAll());
logger.debug("Reading data from file id {} - success.", fileData.getId());
return rawContent;
} catch (Throwable e) {
Expand Down
Loading