Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Add caching strategie
Browse files Browse the repository at this point in the history
Took 7 minutes
  • Loading branch information
itsmefox committed Aug 21, 2019
1 parent c85bf6f commit 55beca9
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>foxhttp</artifactId>
<version>1.3.6</version>
<version>1.3.7</version>


<name>GroundWork - FoxHttp</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import ch.viascom.groundwork.foxhttp.authorization.DefaultAuthorizationStrategy;
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationStrategy;
import ch.viascom.groundwork.foxhttp.cache.FoxHttpCacheStrategy;
import ch.viascom.groundwork.foxhttp.cache.NoCacheStrategy;
import ch.viascom.groundwork.foxhttp.component.FoxHttpComponent;
import ch.viascom.groundwork.foxhttp.cookie.DefaultCookieStore;
import ch.viascom.groundwork.foxhttp.cookie.FoxHttpCookieStore;
Expand Down Expand Up @@ -51,9 +53,10 @@ public class FoxHttpClient {
//Interceptors
private FoxHttpInterceptorStrategy foxHttpInterceptorStrategy = new DefaultInterceptorStrategy();

//@Getter
@Getter
@Setter
//Caching
//private FoxHttpCacheStrategy foxHttpCacheStrategy;
private FoxHttpCacheStrategy foxHttpCacheStrategy = new NoCacheStrategy();

@Getter
@Setter
Expand Down
84 changes: 51 additions & 33 deletions src/main/java/ch/viascom/groundwork/foxhttp/FoxHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void setFoxHttpClient(FoxHttpClient foxHttpClient) throws FoxHttpRequestE
this.foxHttpClient = foxHttpClient;
// Copy configuration from client to request
try {
if(this.getFoxHttpPlaceholderStrategy() == null) {
if (this.getFoxHttpPlaceholderStrategy() == null) {
this.setFoxHttpPlaceholderStrategy(this.foxHttpClient.getFoxHttpPlaceholderStrategy().getClass().getDeclaredConstructor().newInstance());
}
this.getFoxHttpPlaceholderStrategy().getPlaceholderMap().putAll(this.foxHttpClient.getFoxHttpPlaceholderStrategy().getPlaceholderMap());
Expand Down Expand Up @@ -250,42 +250,60 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
setRequestBodyStream();
}

foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "sendRequest()");
connection.connect();
int responseCode;

foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "========= Response =========");
//Check for caching
if (foxHttpClient.getFoxHttpCacheStrategy().isCachingEnabled() && foxHttpClient.getFoxHttpCacheStrategy().isCachingAvailable(this, foxHttpClient)) {
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "sendRequest()");
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "========= Cache - Response =========");
foxHttpResponse = foxHttpClient.getFoxHttpCacheStrategy().loadDataFromCache(this, foxHttpClient);
responseCode = foxHttpResponse.getResponseCode();
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "responseCode(" + responseCode + ")");

int responseCode = ((HttpURLConnection) connection).getResponseCode();
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "responseCode(" + responseCode + ")");

//Execute interceptor
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "executeResponseCodeInterceptor()");
FoxHttpInterceptorExecutor.executeResponseCodeInterceptor(new FoxHttpResponseCodeInterceptorContext(responseCode, this, foxHttpClient));

if (!skipResponseBody) {
InputStream is;
if (responseCode >= HttpURLConnection.HTTP_OK && responseCode < HttpURLConnection.HTTP_MULT_CHOICE) {
//On success response code
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "getResponseBody(success)");
is = connection.getInputStream();
//Execute interceptor
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "executeResponseCodeInterceptor()");
FoxHttpInterceptorExecutor.executeResponseCodeInterceptor(new FoxHttpResponseCodeInterceptorContext(responseCode, this, foxHttpClient));
} else {
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "sendRequest()");
connection.connect();

foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "========= Response =========");

responseCode = ((HttpURLConnection) connection).getResponseCode();
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "responseCode(" + responseCode + ")");

//Execute interceptor
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "executeResponseCodeInterceptor()");
FoxHttpInterceptorExecutor.executeResponseCodeInterceptor(new FoxHttpResponseCodeInterceptorContext(responseCode, this, foxHttpClient));

if (!skipResponseBody) {
InputStream is;
if (responseCode >= HttpURLConnection.HTTP_OK && responseCode < HttpURLConnection.HTTP_MULT_CHOICE) {
//On success response code
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "getResponseBody(success)");
is = connection.getInputStream();
} else {
//On error response code
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "getResponseBody(error)");
is = ((HttpURLConnection) connection).getErrorStream();
}

foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "createFoxHttpResponse()");
foxHttpResponse = new FoxHttpResponse(is, this, responseCode, foxHttpClient);
} else {
//On error response code
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "getResponseBody(error)");
is = ((HttpURLConnection) connection).getErrorStream();
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "createFoxHttpResponse()");
foxHttpResponse = new FoxHttpResponse(null, this, responseCode, foxHttpClient);
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "No body return because skipResponseBody is active!");
}

foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "createFoxHttpResponse()");
foxHttpResponse = new FoxHttpResponse(is, this, responseCode, foxHttpClient);
} else {
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "createFoxHttpResponse()");
foxHttpResponse = new FoxHttpResponse(null, this, responseCode, foxHttpClient);
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.INFO, "No body return because skipResponseBody is active!");
//Process response headers
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "processResponseHeader()");
processResponseHeader();
if (foxHttpClient.getFoxHttpCacheStrategy().isCachingEnabled()) {
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "saveDataToCache()");
foxHttpClient.getFoxHttpCacheStrategy().saveDataToCache(foxHttpResponse, this, foxHttpClient);
}
}

//Process response headers
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "processResponseHeader()");
processResponseHeader();

//Execute interceptor
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "executeResponseInterceptor()");
FoxHttpInterceptorExecutor.executeResponseInterceptor(new FoxHttpResponseInterceptorContext(responseCode, foxHttpResponse, this, foxHttpClient));
Expand Down Expand Up @@ -335,8 +353,8 @@ private void setHeaderIfNotExist(HeaderTypes type, String value, URLConnection c
}

private void processAuthorizationStrategy() throws FoxHttpRequestException {
List<FoxHttpAuthorization> foxHttpAuthorizations = foxHttpClient.getFoxHttpAuthorizationStrategy()
.getAuthorization(connection, authScope, foxHttpClient, foxHttpPlaceholderStrategy);
List<FoxHttpAuthorization> foxHttpAuthorizations = foxHttpClient.getFoxHttpAuthorizationStrategy().getAuthorization(connection, authScope, foxHttpClient,
foxHttpPlaceholderStrategy);
FoxHttpAuthorizationContext authorizationContext = new FoxHttpAuthorizationContext(connection, this, foxHttpClient);
for (FoxHttpAuthorization foxHttpAuthorization : foxHttpAuthorizations) {
foxHttpClient.getFoxHttpLogger().log(FoxHttpLoggerLevel.DEBUG, "-> doAuthorization(" + foxHttpAuthorization + ")");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package ch.viascom.groundwork.foxhttp.cache;

import ch.viascom.groundwork.foxhttp.FoxHttpClient;
import ch.viascom.groundwork.foxhttp.FoxHttpRequest;
import ch.viascom.groundwork.foxhttp.FoxHttpResponse;

/**
* @author [email protected]
*/
public interface FoxHttpCacheStrategy {

boolean isCachingEnabled();

void setCachingEnabled(boolean enabled);

boolean isCachingAvailable(FoxHttpRequest request, FoxHttpClient client);

FoxHttpResponse loadDataFromCache(FoxHttpRequest request, FoxHttpClient client);

void saveDataToCache(FoxHttpResponse response, FoxHttpRequest request, FoxHttpClient client);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ch.viascom.groundwork.foxhttp.cache;

import ch.viascom.groundwork.foxhttp.FoxHttpClient;
import ch.viascom.groundwork.foxhttp.FoxHttpRequest;
import ch.viascom.groundwork.foxhttp.FoxHttpResponse;
import java.io.InputStream;

public class NoCacheStrategy implements FoxHttpCacheStrategy {

@Override
public boolean isCachingEnabled() {
return false;
}

@Override
public void setCachingEnabled(boolean enabled) {

}

@Override
public boolean isCachingAvailable(FoxHttpRequest request, FoxHttpClient client) {
return false;
}

@Override
public FoxHttpResponse loadDataFromCache(FoxHttpRequest request, FoxHttpClient client) {
return null;
}

@Override
public void saveDataToCache(FoxHttpResponse response, FoxHttpRequest request, FoxHttpClient client) {

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.viascom.groundwork.foxhttp;

import ch.viascom.groundwork.foxhttp.builder.FoxHttpRequestBuilder;
import ch.viascom.groundwork.foxhttp.models.User;
import ch.viascom.groundwork.foxhttp.parser.GsonParser;
import ch.viascom.groundwork.foxhttp.parser.XStreamParser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package ch.viascom.groundwork.foxhttp;

import ch.viascom.groundwork.foxhttp.body.request.*;
import static org.fest.assertions.api.Assertions.assertThat;

import ch.viascom.groundwork.foxhttp.body.request.FoxHttpRequestBody;
import ch.viascom.groundwork.foxhttp.body.request.RequestByteArrayBody;
import ch.viascom.groundwork.foxhttp.body.request.RequestMultipartBody;
import ch.viascom.groundwork.foxhttp.body.request.RequestObjectBody;
import ch.viascom.groundwork.foxhttp.body.request.RequestServiceResultBody;
import ch.viascom.groundwork.foxhttp.body.request.RequestUrlEncodedFormBody;
import ch.viascom.groundwork.foxhttp.builder.FoxHttpClientBuilder;
import ch.viascom.groundwork.foxhttp.builder.FoxHttpRequestBuilder;
import ch.viascom.groundwork.foxhttp.exception.FoxHttpRequestException;
import ch.viascom.groundwork.foxhttp.log.FoxHttpLoggerLevel;
import ch.viascom.groundwork.foxhttp.log.SystemOutFoxHttpLogger;
import ch.viascom.groundwork.foxhttp.models.PostResponse;
import ch.viascom.groundwork.foxhttp.models.User;
import ch.viascom.groundwork.foxhttp.parser.GenericParser;
import ch.viascom.groundwork.foxhttp.parser.GsonParser;
import ch.viascom.groundwork.foxhttp.type.ContentType;
import ch.viascom.groundwork.foxhttp.type.RequestType;
import ch.viascom.groundwork.serviceresult.ServiceResult;
import ch.viascom.groundwork.serviceresult.ServiceResultStatus;
import ch.viascom.groundwork.serviceresult.util.Metadata;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.Charset;

import static org.fest.assertions.api.Assertions.assertThat;
import org.junit.Test;

/**
* @author [email protected]
Expand Down Expand Up @@ -65,7 +70,6 @@ public void postURLEncodedFormRequest() throws Exception {
requestBuilder.setRequestBody(requestBody);
FoxHttpResponse foxHttpResponse = requestBuilder.build().execute();


assertThat(foxHttpResponse.getResponseCode()).isEqualTo(200);
assertThat(foxHttpResponse.getByteArrayOutputStreamBody().size()).isGreaterThan(0);

Expand All @@ -82,13 +86,13 @@ public void postMultiPartRequest() throws Exception {

RequestMultipartBody requestBody = new RequestMultipartBody(Charset.forName("UTF-8"));
requestBody.addFormField("filename", "test.data");
requestBody.addInputStreamPart("file", "file.json", new ByteArrayInputStream("{\"name\":\"FoxHttp\"}".getBytes()), "QUOTED-PRINTABLE", ContentType.DEFAULT_TEXT.getMimeType());
requestBody.addInputStreamPart("file", "file.json", new ByteArrayInputStream("{\"name\":\"FoxHttp\"}".getBytes()), "QUOTED-PRINTABLE",
ContentType.DEFAULT_TEXT.getMimeType());

FoxHttpRequestBuilder requestBuilder = new FoxHttpRequestBuilder(endpoint + "post", RequestType.POST, clientBuilder.build());
requestBuilder.setRequestBody(requestBody);
FoxHttpResponse foxHttpResponse = requestBuilder.build().execute();


assertThat(foxHttpResponse.getResponseCode()).isEqualTo(200);
assertThat(foxHttpResponse.getByteArrayOutputStreamBody().size()).isGreaterThan(0);

Expand Down Expand Up @@ -116,7 +120,8 @@ public void postServiceResultRequest() throws Exception {

PostResponse postResponse = foxHttpResponse.getParsedBody(PostResponse.class);

assertThat(postResponse.getData()).isEqualTo("{\"status\":\"failed\",\"type\":\"ch.viascom.app.models.AppUser\",\"content\":{\"username\":\"[email protected]\",\"firstname\":\"Fox\",\"lastname\":\"Http\"},\"hash\":\"FAKE-HASH\",\"destination\":\"\",\"metadata\":{\"isActive\":{\"type\":\"java.lang.Boolean\",\"content\":true}}}");
assertThat(postResponse.getData()).isEqualTo(
"{\"status\":\"failed\",\"type\":\"ch.viascom.app.models.AppUser\",\"content\":{\"username\":\"[email protected]\",\"firstname\":\"Fox\",\"lastname\":\"Http\"},\"hash\":\"FAKE-HASH\",\"destination\":\"\",\"metadata\":{\"isActive\":{\"type\":\"java.lang.Boolean\",\"content\":true}}}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void getRequest() throws Exception {
assertThat(foxHttpResponse.getResponseCode()).isEqualTo(200);
assertThat(foxHttpResponse.getByteArrayOutputStreamBody().size()).isGreaterThan(0);
assertThat(foxHttpResponse.getFoxHttpRequest().getRequestType()).isEqualTo(RequestType.GET);
assertThat(foxHttpResponse.getFoxHttpRequest().getUrl()).isEqualTo(new URL(endpoint + "get"));
assertThat(foxHttpResponse.getFoxHttpRequest().getUrl()).isEqualTo(new URL(sslEndpoint + "get"));

GetResponse getResponse = foxHttpResponse.getParsedBody(GetResponse.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public interface FoxHttpInterfaceTest {

@GET("get")
GetResponse get(@Query(value = "key", allowOptional = true) String key);
GetResponse get(@Query(value = "key", allowOptional = true) String key) throws FoxHttpException;

@GET(value = "{host}get", completePath = true)
@Header(name = "foo", value = "bar")
Expand Down

0 comments on commit 55beca9

Please sign in to comment.