forked from instana/robot-shop
-
Notifications
You must be signed in to change notification settings - Fork 4
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
15 changed files
with
553 additions
and
337 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,72 @@ | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>steveww</groupId> | ||
<artifactId>shipping</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
<name>Spark Java Sample</name> | ||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.3.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.instana</groupId> | ||
<artifactId>shipping</artifactId> | ||
<version>1.0</version> | ||
<name>shipping service</name> | ||
<description>Shipping calculations</description> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sparkjava</groupId> | ||
<artifactId>spark-core</artifactId> | ||
<version>2.7.2</version> | ||
<groupId>org.springframework.retry</groupId> | ||
<artifactId>spring-retry</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>1.7.25</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>c3p0</groupId> | ||
<artifactId>c3p0</artifactId> | ||
<version>0.9.1.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.19</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-dbutils</groupId> | ||
<artifactId>commons-dbutils</artifactId> | ||
<version>1.7</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.8.2</version> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.5</version> | ||
<version>4.5.12</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>org.steveww.spark.Main</mainClass> | ||
</manifest> | ||
</archive> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
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
75 changes: 75 additions & 0 deletions
75
shipping/src/main/java/com/instana/robotshop/shipping/CartHelper.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,75 @@ | ||
package com.instana.robotshop.shipping; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.params.BasicHttpParams; | ||
import org.apache.http.params.HttpConnectionParams; | ||
import org.apache.http.params.HttpParams; | ||
|
||
public class CartHelper { | ||
private static final Logger logger = LoggerFactory.getLogger(CartHelper.class); | ||
|
||
private String baseUrl; | ||
|
||
public CartHelper(String baseUrl) { | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
// TODO - Remove deprecated calls | ||
public String addToCart(String id, String data) { | ||
logger.info("add shipping to cart {}", id); | ||
StringBuilder buffer = new StringBuilder(); | ||
|
||
CloseableHttpClient httpClient = null; | ||
try { | ||
// set timeout to 5 secs | ||
HttpParams httpParams = new BasicHttpParams(); | ||
HttpConnectionParams.setConnectionTimeout(httpParams, 5000); | ||
|
||
httpClient = HttpClients.createDefault(); | ||
HttpPost postRequest = new HttpPost(baseUrl + id); | ||
StringEntity payload = new StringEntity(data); | ||
payload.setContentType("application/json"); | ||
postRequest.setEntity(payload); | ||
CloseableHttpResponse res = httpClient.execute(postRequest); | ||
|
||
if (res.getStatusLine().getStatusCode() == 200) { | ||
BufferedReader in = new BufferedReader(new InputStreamReader(res.getEntity().getContent())); | ||
String line; | ||
while ((line = in.readLine()) != null) { | ||
buffer.append(line); | ||
} | ||
} else { | ||
logger.warn("Failed with code {}", res.getStatusLine().getStatusCode()); | ||
} | ||
try { | ||
res.close(); | ||
} catch(IOException e) { | ||
logger.warn("httpresponse", e); | ||
} | ||
} catch(Exception e) { | ||
logger.warn("http client exception", e); | ||
} finally { | ||
if (httpClient != null) { | ||
try { | ||
httpClient.close(); | ||
} catch(IOException e) { | ||
logger.warn("httpclient", e); | ||
} | ||
} | ||
} | ||
|
||
// this will be empty on error | ||
return buffer.toString(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
shipping/src/main/java/com/instana/robotshop/shipping/City.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,85 @@ | ||
package com.instana.robotshop.shipping; | ||
|
||
import javax.persistence.Table; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Column; | ||
|
||
/* | ||
* Bean for City | ||
*/ | ||
@Entity | ||
@Table(name = "cities") | ||
public class City { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private long uuid; | ||
|
||
@Column(name = "country_code") | ||
private String code; | ||
private String city; | ||
private String name; | ||
private String region; | ||
private double latitude; | ||
private double longitude; | ||
|
||
public long getUuid() { | ||
return this.uuid; | ||
} | ||
|
||
public String getCode() { | ||
return this.code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCity() { | ||
return this.city; | ||
} | ||
|
||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getRegion() { | ||
return this.region; | ||
} | ||
|
||
public void setRegion(String code) { | ||
this.region = region; | ||
} | ||
|
||
public double getLatitude() { | ||
return this.latitude; | ||
} | ||
|
||
public void setLatitude(double latitude) { | ||
this.latitude = latitude; | ||
} | ||
|
||
public double getLongitude() { | ||
return this.longitude; | ||
} | ||
|
||
public void setLongitude(double longitude) { | ||
this.longitude = longitude; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Country: %s City: %s Region: %s Coords: %f %f", this.code, this.city, this.region, this.latitude, this.longitude); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
shipping/src/main/java/com/instana/robotshop/shipping/CityRepository.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,17 @@ | ||
package com.instana.robotshop.shipping; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface CityRepository extends CrudRepository<City, Long> { | ||
List<City> findByCode(String code); | ||
|
||
@Query( | ||
value = "select c from City c where c.code = ?1 and c.city like ?2%" | ||
) | ||
List<City> match(String code, String text); | ||
|
||
City findById(long id); | ||
} |
Oops, something went wrong.