Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
Used encapsulated system properties for AWS layer
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 9, 2017
1 parent 90fd3e5 commit eae1dc8
Show file tree
Hide file tree
Showing 22 changed files with 482 additions and 178 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/amihaiemil/charles/aws/AccessKeyId.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,30 @@
* @since 1.0.0
*/
public interface AccessKeyId extends SystemProperty {

/**
* Actual name of the aws access key id sys prop.
*/
String NAME = "aws.accessKeyId";

/**
* Fake for unit testing.
*/
public static final class Fake implements AccessKeyId {

private String value;

public Fake() {
this("testAccessKeyId");
}

public Fake(String value) {
this.value = value;
}

@Override
public String read() {
return this.value;
}
}
}
54 changes: 50 additions & 4 deletions src/main/java/com/amihaiemil/charles/aws/AmazonEsRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,46 @@ public final class AmazonEsRepository implements AwsEsRepository {
*/
private String indexName;

/**
* AWS access key.
*/
private AccessKeyId accesskey;

/**
* Aws secret key;
*/
private SecretKey secretKey;

/**
* Aws ES region.
*/
private Region reg;

/**
* ElasticSearch URL.
*/
private EsEndPoint esEdp;

/**
* ctor.
* @param indexName Name of the Es index where the pages will be exported.
* @param accessKey Aws access key.
* @param secretKey Aws secret key.
* @param reg AWS ElasticSearch region.
* @param es ElasticSearch URL.
*/
public AmazonEsRepository(String indexName) {
public AmazonEsRepository(
final String indexName,
final AccessKeyId accesskey,
final SecretKey secretKey,
final Region reg,
final EsEndPoint es
) {
this.indexName = indexName;
this.accesskey = accesskey;
this.secretKey = secretKey;
this.reg = reg;
this.esEdp = es;
}

@Override
Expand All @@ -82,13 +116,17 @@ public void export(List<WebPage> pages) throws DataExportException {
new AwsHttpHeaders<>(
new AwsPost<>(
new EsHttpRequest<>(
this.esEdp,
"_bulk",
new SimpleAwsResponseHandler(false),
new SimpleAwsErrorHandler(false)
),
new ByteArrayInputStream(data.getBytes())
), headers
)
),
this.accesskey,
this.secretKey,
this.reg
);
index.perform();
} catch (IOException e) {
Expand All @@ -108,11 +146,15 @@ public boolean exists() {
new SignedRequest<>(
new AwsHead<>(
new EsHttpRequest<>(
this.esEdp,
this.indexName,
new BooleanAwsResponseHandler(),
new SimpleAwsErrorHandler(false)
)
)
),
this.accesskey,
this.secretKey,
this.reg
);
boolean exists = false;
try {
Expand All @@ -134,11 +176,15 @@ public void delete() {
new SignedRequest<>(
new AwsDelete<>(
new EsHttpRequest<>(
this.esEdp,
this.indexName,
new SimpleAwsResponseHandler(false),
new SimpleAwsErrorHandler(false)
)
)
),
this.accesskey,
this.secretKey,
this.reg
);
deleteIndex.perform();
}
Expand Down
47 changes: 42 additions & 5 deletions src/main/java/com/amihaiemil/charles/aws/AmazonEsSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,46 @@ public final class AmazonEsSearch {
*/
private String indexName;

/**
* ElasticSearch URL.
*/
private EsEndPoint esEdp;

/**
* AWS access key.
*/
private AccessKeyId accesskey;

/**
* Aws secret key;
*/
private SecretKey secretKey;

/**
* Aws ES region.
*/
private Region reg;

/**
* Ctor.
* @param qry
* @param idxName
* @param es ElasticSearch URL.
* @param qry Search query.
* @param idxName Index Name.
*/
public AmazonEsSearch(SearchQuery qry, String idxName) {
public AmazonEsSearch(
final EsEndPoint es,
final AccessKeyId accesskey,
final SecretKey secretKey,
final Region reg,
final SearchQuery qry,
final String idxName
) {
this.esEdp = es;
this.query = qry;
this.indexName = idxName;
this.accesskey = accesskey;
this.secretKey = secretKey;
this.reg = reg;
}

/**
Expand All @@ -77,12 +109,17 @@ public SearchResultsPage search() {
new AwsHttpHeaders<>(
new AwsPost<>(
new EsHttpRequest<>(
this.esEdp,
this.indexName + "/_search",
new SearchResponseHandler(), new SimpleAwsErrorHandler(false)
new SearchResponseHandler(),
new SimpleAwsErrorHandler(false)
),
new ByteArrayInputStream(this.query.toJson().toString().getBytes())
), headers
)
),
this.accesskey,
this.secretKey,
this.reg
);
return search.perform();
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/amihaiemil/charles/aws/EsEndPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,29 @@
*/
public interface EsEndPoint extends SystemProperty {

/**
* Actual name of the aws es endpoint sys prop.
*/
String NAME = "aws.es.endpoint";

/**
* Fake for unit testing.
*/
public static final class Fake implements EsEndPoint {

private String value;

public Fake() {
this("testAccessEsEndpoint");
}

public Fake(String value) {
this.value = value;
}

@Override
public String read() {
return this.value;
}
}
}
26 changes: 25 additions & 1 deletion src/main/java/com/amihaiemil/charles/aws/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,36 @@
package com.amihaiemil.charles.aws;

/**
* AWS Region system property.
* AWS ElasticSearch Region system property.
* @author Sherif Waly ([email protected])
* @version $Id$
* @since 1.0.0
*
*/
public interface Region extends SystemProperty {
/**
* Actual name of the aws ES region sys prop.
*/
String NAME = "aws.es.region";

/**
* Fake for unit testing.
*/
public static final class Fake implements Region {

private String value;

public Fake() {
this("testAwsEsRegion");
}

public Fake(String value) {
this.value = value;
}

@Override
public String read() {
return this.value;
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/amihaiemil/charles/aws/SecretKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,29 @@
* @since 1.0.0
*/
public interface SecretKey extends SystemProperty {
/**
* Actual name of the aws secret key sys prop.
*/
String NAME = "aws.secretKey";

/**
* Fake for unit testing.
*/
public static final class Fake implements SecretKey {

private String value;

public Fake() {
this("testAwsSecretKey");
}

public Fake(String value) {
this.value = value;
}

@Override
public String read() {
return this.value;
}
}
}
13 changes: 6 additions & 7 deletions src/main/java/com/amihaiemil/charles/aws/StAccessKeyId.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
* @since 1.0.0
*/
public final class StAccessKeyId extends AbstractSystemProperty implements AccessKeyId {

/**
* Ctor.
* @param String name
*/
public StAccessKeyId(final String name) {
super(name);

/**
* Ctor.
*/
public StAccessKeyId() {
super(AccessKeyId.NAME);
}

}
13 changes: 2 additions & 11 deletions src/main/java/com/amihaiemil/charles/aws/StEsEndPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,9 @@ public final class StEsEndPoint extends AbstractSystemProperty implements EsEndP

/**
* Ctor.
* @param String name
*/
public StEsEndPoint(final String name) {
super(name);
public StEsEndPoint() {
super(EsEndPoint.NAME);
}

@Override
public String read() {
String endPoint = super.read();
if(!endPoint.endsWith("/")) {
endPoint += "/";
}
return endPoint;
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/amihaiemil/charles/aws/StRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
* @since 1.0.0
*/
public final class StRegion extends AbstractSystemProperty implements Region {

/**
* Ctor.
* @param String name
*/
public StRegion(final String name) {
super(name);
public StRegion() {
super(Region.NAME);
}

}
9 changes: 4 additions & 5 deletions src/main/java/com/amihaiemil/charles/aws/StSecretKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
* @since 1.0.0
*/
public final class StSecretKey extends AbstractSystemProperty implements SecretKey {

/**
* Ctor.
* @param String name
*/
public StSecretKey(final String name) {
super(name);
public StSecretKey() {
super(SecretKey.NAME);
}

}
Loading

0 comments on commit eae1dc8

Please sign in to comment.