forked from stardogventures/starwizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,187 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
starwizard-core/src/main/java/io/stardog/starwizard/util/RequestUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.stardog.starwizard.util; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
public class RequestUtil { | ||
/** | ||
* Return a remote IP address, optionally picking up the X-Forwarded-For header used by AWS ELB | ||
* @param request request | ||
* @return the ip address, from either X-Forwarded-For or the direct connection | ||
*/ | ||
public static String getRemoteIp(HttpServletRequest request) { | ||
if (request == null) { | ||
return "127.0.0.1"; | ||
} | ||
|
||
String ip = request.getHeader("X-Forwarded-For") != null ? request.getHeader("X-Forwarded-For") : request.getRemoteAddr(); | ||
if (ip.contains(",")) { | ||
ip = ip.substring(0, ip.indexOf(",")); | ||
} | ||
return ip; | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>starwizard</artifactId> | ||
<groupId>io.stardog.starwizard</groupId> | ||
<version>0.2.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>starwizard-services</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.dropwizard</groupId> | ||
<artifactId>dropwizard-core</artifactId> | ||
<version>${dropwizard.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.stripe</groupId> | ||
<artifactId>stripe-java</artifactId> | ||
<version>7.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.6</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.maps</groupId> | ||
<artifactId>google-maps-services</artifactId> | ||
<version>0.9.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.amazonaws</groupId> | ||
<artifactId>aws-java-sdk-ssm</artifactId> | ||
<version>1.11.415</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-annotations</artifactId> | ||
<version>1.5.21</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.auto.value</groupId> | ||
<artifactId>auto-value-annotations</artifactId> | ||
<version>1.6.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.auto.value</groupId> | ||
<artifactId>auto-value</artifactId> | ||
<version>1.6.2</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
38 changes: 38 additions & 0 deletions
38
starwizard-services/src/main/java/io/stardog/starwizard/services/common/AsyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.stardog.starwizard.services.common; | ||
|
||
import io.dropwizard.lifecycle.Managed; | ||
|
||
import javax.inject.Singleton; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A shared service for executing lightweight asynchronous processes and managing their thread pool using Dropwizard | ||
* lifecycle. | ||
*/ | ||
@Singleton | ||
public class AsyncService implements Managed { | ||
private final ExecutorService executorService = Executors.newFixedThreadPool(10); | ||
|
||
@Override | ||
public void start() throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public void stop() throws Exception { | ||
executorService.shutdown(); | ||
executorService.awaitTermination(60, TimeUnit.SECONDS); | ||
executorService.shutdownNow(); | ||
} | ||
|
||
public ExecutorService getExecutorService() { | ||
return executorService; | ||
} | ||
|
||
public Future<?> submit(Runnable runnable) { | ||
return executorService.submit(runnable); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
starwizard-services/src/main/java/io/stardog/starwizard/services/common/TimeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.stardog.starwizard.services.common; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
|
||
/** | ||
* Injectable service for getting the current time and date - can override the concept of "now" for test purposes. | ||
*/ | ||
@Singleton | ||
public class TimeService { | ||
private final ZoneId timezone; | ||
private Instant now = null; | ||
|
||
@Inject | ||
public TimeService(@Named("timezone") ZoneId timezone) { | ||
this.timezone = timezone; | ||
} | ||
|
||
public void setToday(LocalDate today) { | ||
setNow(today.atStartOfDay(timezone).toInstant()); | ||
} | ||
|
||
public void setNow(Instant now) { | ||
this.now = now; | ||
} | ||
|
||
public Instant getNow() { | ||
return now != null ? now : Instant.now(); | ||
} | ||
|
||
public LocalDate getToday() { | ||
return toDate(getNow()); | ||
} | ||
|
||
public LocalDate toDate(Instant at) { | ||
return at.atZone(timezone).toLocalDate(); | ||
} | ||
|
||
public Instant toInstant(LocalDate date) { return date.atStartOfDay(timezone).toInstant(); } | ||
} |
9 changes: 9 additions & 0 deletions
9
starwizard-services/src/main/java/io/stardog/starwizard/services/geocode/GeocodeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.stardog.starwizard.services.geocode; | ||
|
||
import io.stardog.starwizard.services.geocode.data.Geopoint; | ||
|
||
import java.util.Optional; | ||
|
||
public interface GeocodeService { | ||
Optional<Geopoint> geocode(String string); | ||
} |
86 changes: 86 additions & 0 deletions
86
starwizard-services/src/main/java/io/stardog/starwizard/services/geocode/data/Geopoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.stardog.starwizard.services.geocode.data; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Geopoint is a data object that represents a particular point in space. It Jackson-serializes to/from the GeoJSON format. | ||
*/ | ||
@AutoValue | ||
@JsonPropertyOrder({"type", "coordinates"}) | ||
public abstract class Geopoint { | ||
public String getType() { | ||
return "Point"; | ||
} | ||
|
||
public abstract List<Double> getCoordinates(); | ||
|
||
@JsonIgnore | ||
public double getLng() { | ||
return getCoordinates().get(0); | ||
} | ||
|
||
@JsonIgnore | ||
public double getLat() { | ||
return getCoordinates().get(1); | ||
} | ||
|
||
/** | ||
* Return the distance, in meters, from this point to another Geopoint. | ||
* From: http://stackoverflow.com/questions/3694380/calculating-distance-between-two-points-using-latitude-longitude-what-am-i-doi | ||
* @param other another geopoint | ||
* @return distance in meters | ||
*/ | ||
@JsonIgnore | ||
public double getDistanceTo(Geopoint other) { | ||
double lat1 = getLat(); | ||
double lon1 = getLng(); | ||
double lat2 = other.getLat(); | ||
double lon2 = other.getLng(); | ||
double el1 = 0; | ||
double el2 = 0; | ||
|
||
final int R = 6371; // Radius of the earth | ||
|
||
Double latDistance = Math.toRadians(lat2 - lat1); | ||
Double lonDistance = Math.toRadians(lon2 - lon1); | ||
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) | ||
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) | ||
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); | ||
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | ||
double distance = R * c * 1000; // convert to meters | ||
|
||
double height = el1 - el2; | ||
|
||
distance = Math.pow(distance, 2) + Math.pow(height, 2); | ||
|
||
return Math.sqrt(distance); | ||
} | ||
|
||
public static Geopoint of(double lng, double lat) { | ||
if (Math.abs(lng) > 180) { | ||
throw new IllegalArgumentException("invalid longitude: " + lng); | ||
} | ||
if (Math.abs(lat) > 90) { | ||
throw new IllegalArgumentException("invalid latitude: " + lat); | ||
} | ||
return new AutoValue_Geopoint(ImmutableList.of(lng, lat)); | ||
} | ||
|
||
@JsonCreator | ||
public static Geopoint of(@JsonProperty("type") String type, @JsonProperty("coordinates") List<Double> coordinates) { | ||
if (!"Point".equals(type)) { | ||
throw new IllegalArgumentException("Must be type 'Point'"); | ||
} | ||
if (coordinates.size() != 2) { | ||
throw new IllegalArgumentException("Must have two coordinates"); | ||
} | ||
return Geopoint.of(coordinates.get(0), coordinates.get(1)); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ces/src/main/java/io/stardog/starwizard/services/geocode/exceptions/GeocodeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.stardog.starwizard.services.geocode.exceptions; | ||
|
||
public class GeocodeException extends RuntimeException { | ||
public GeocodeException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
Oops, something went wrong.