Skip to content

Commit 06d0603

Browse files
committed
caching example
1 parent f532971 commit 06d0603

File tree

5 files changed

+172
-50
lines changed

5 files changed

+172
-50
lines changed

Book-RESTful-Service/pom.xml

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,64 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.howtoprogram</groupId>
7-
<artifactId>book-rest-service</artifactId>
8-
<version>0.1.0</version>
6+
<groupId>com.howtoprogram</groupId>
7+
<artifactId>book-rest-service</artifactId>
8+
<version>0.1.0</version>
99

10-
<parent>
11-
<groupId>org.springframework.boot</groupId>
12-
<artifactId>spring-boot-starter-parent</artifactId>
13-
<version>1.3.5.RELEASE</version>
14-
</parent>
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.3.5.RELEASE</version>
14+
</parent>
1515

16-
<dependencies>
17-
<dependency>
18-
<groupId>org.springframework.boot</groupId>
19-
<artifactId>spring-boot-starter-web</artifactId>
20-
</dependency>
21-
<dependency>
22-
<groupId>org.springframework.boot</groupId>
23-
<artifactId>spring-boot-starter-test</artifactId>
24-
<scope>test</scope>
25-
</dependency>
26-
<dependency>
27-
<groupId>com.jayway.jsonpath</groupId>
28-
<artifactId>json-path</artifactId>
29-
<scope>test</scope>
30-
</dependency>
31-
</dependencies>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-test</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.jayway.jsonpath</groupId>
28+
<artifactId>json-path</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.json</groupId>
33+
<artifactId>json</artifactId>
34+
<version>20160212</version>
35+
</dependency>
36+
</dependencies>
3237

33-
<properties>
34-
<java.version>1.8</java.version>
35-
</properties>
38+
<properties>
39+
<java.version>1.8</java.version>
40+
</properties>
3641

3742

38-
<build>
39-
<plugins>
40-
<plugin>
41-
<groupId>org.springframework.boot</groupId>
42-
<artifactId>spring-boot-maven-plugin</artifactId>
43-
</plugin>
44-
</plugins>
45-
</build>
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-maven-plugin</artifactId>
48+
</plugin>
49+
</plugins>
50+
</build>
4651

47-
<repositories>
48-
<repository>
49-
<id>spring-releases</id>
50-
<url>https://repo.spring.io/libs-release</url>
51-
</repository>
52-
</repositories>
53-
<pluginRepositories>
54-
<pluginRepository>
55-
<id>spring-releases</id>
56-
<url>https://repo.spring.io/libs-release</url>
57-
</pluginRepository>
58-
</pluginRepositories>
52+
<repositories>
53+
<repository>
54+
<id>spring-releases</id>
55+
<url>https://repo.spring.io/libs-release</url>
56+
</repository>
57+
</repositories>
58+
<pluginRepositories>
59+
<pluginRepository>
60+
<id>spring-releases</id>
61+
<url>https://repo.spring.io/libs-release</url>
62+
</pluginRepository>
63+
</pluginRepositories>
5964
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
import java.io.IOException;
3+
import java.io.OutputStreamWriter;
4+
import java.net.HttpURLConnection;
5+
import java.net.URL;
6+
7+
import org.json.JSONObject;
8+
9+
public class FcmTest {
10+
11+
public static final String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
12+
13+
public static void main(String[] args) throws IOException {
14+
try {
15+
String userDeviceIdKey = "EE8CED92CE4F85AF4172926FEDDD3568A5AE28CF37BB8F89645A9D5784ACD6C8";
16+
String authKey = "AAAA0sDON-U:APA91bH1YWRLme0Q4e_"
17+
+ "1aS_5pUcH2W44BodoA7Ad3zxdsFDnkUPbP4m4UFB2gT13Q2iKZsOJTuwL79khDEn8cftqSEaF_"
18+
+ "0Wdf7awiIa7sf6US8jgNwHEygCTaOT9Zjw0cWG3x5J0WSOEhc1d7uwHCGoss-lejgp6pQ";
19+
String fcmUrl = API_URL_FCM;
20+
21+
URL url = new URL(fcmUrl);
22+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
23+
24+
conn.setUseCaches(false);
25+
conn.setDoInput(true);
26+
conn.setDoOutput(true);
27+
28+
conn.setRequestMethod("POST");
29+
conn.setRequestProperty("Authorization", "key=" + authKey);
30+
conn.setRequestProperty("Content-Type", "application/json");
31+
32+
JSONObject json = new JSONObject();
33+
json.put("to", userDeviceIdKey.trim());
34+
JSONObject info = new JSONObject();
35+
info.put("title", "Notificatoin Title"); // Notification title
36+
info.put("body", "Hello Test notification"); // Notification body
37+
json.put("notification", info);
38+
39+
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
40+
wr.write(json.toString());
41+
wr.flush();
42+
conn.getInputStream();
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
}

okhttp-examples/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
/target/
2+
/.settings/
3+
/.classpath/
4+
/.project/

okhttp-examples/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<groupId>junit</groupId>
2525
<artifactId>junit</artifactId>
2626
<version>4.8.1</version>
27-
<scope>test</scope>
2827
</dependency>
2928
</dependencies>
3029
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.howtoprogram.okhttp.cache;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import org.junit.Assert;
7+
8+
import okhttp3.Cache;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.Request;
11+
import okhttp3.Response;
12+
13+
public class CacheOkHttp {
14+
15+
private final OkHttpClient client;
16+
17+
public CacheOkHttp(File cacheDirectory) throws Exception {
18+
int cacheSize = 10 * 1024 * 1024; // 10 MiB
19+
Cache cache = new Cache(cacheDirectory, cacheSize);
20+
client = new OkHttpClient.Builder().cache(cache).build();
21+
}
22+
23+
public static void main(String args[]) throws Exception {
24+
25+
File cacheLoc = new File("D:\\tmp\\okhttp-cache");
26+
Assert.assertTrue(cacheLoc.exists());
27+
CacheOkHttp cacheOkHttp = new CacheOkHttp(cacheLoc);
28+
cacheOkHttp.run();
29+
}
30+
31+
public void run() throws Exception {
32+
Request request = new Request.Builder().url("http://httpbin.org/cache/10").build();
33+
34+
Response response1 = client.newCall(request).execute();
35+
if (!response1.isSuccessful()) {
36+
throw new IOException("Unexpected code " + response1);
37+
}
38+
39+
String response1Body = response1.body().string();
40+
System.out.println("Response 1 response: " + response1);
41+
System.out.println("Response 1 cache response: " + response1.cacheResponse());
42+
System.out.println("Response 1 network response: " + response1.networkResponse());
43+
44+
Response response2 = client.newCall(request).execute();
45+
if (!response2.isSuccessful()) {
46+
throw new IOException("Unexpected code " + response2);
47+
}
48+
49+
String response2Body = response2.body().string();
50+
System.out.println("Response 2 response: " + response2);
51+
System.out.println("Response 2 cache response: " + response2.cacheResponse());
52+
System.out.println("Response 2 network response: " + response2.networkResponse());
53+
System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body));
54+
55+
Thread.sleep(11000);
56+
57+
Response response3 = client.newCall(request).execute();
58+
if (!response3.isSuccessful()) {
59+
throw new IOException("Unexpected code " + response3);
60+
}
61+
System.out.println(response3.cacheControl().maxStaleSeconds());
62+
63+
System.out.println("Response 3 response: " + response3);
64+
System.out.println("response 3 cache response: " + response3.cacheResponse());
65+
System.out.println("Response 3 network response: " + response3.networkResponse());
66+
67+
}
68+
}

0 commit comments

Comments
 (0)