mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	Implemented a simple fethcer to replace httpclient
This commit is contained in:
		
							parent
							
								
									39f55e25c1
								
							
						
					
					
						commit
						d86f5f905b
					
				| @ -67,8 +67,8 @@ | |||||||
|                 <artifactId>maven-compiler-plugin</artifactId> |                 <artifactId>maven-compiler-plugin</artifactId> | ||||||
|                 <version>3.1</version> |                 <version>3.1</version> | ||||||
|                 <configuration> |                 <configuration> | ||||||
|                     <source>1.7</source> |                     <source>1.8</source> | ||||||
|                     <target>1.7</target> |                     <target>1.8</target> | ||||||
|                 </configuration> |                 </configuration> | ||||||
|             </plugin> |             </plugin> | ||||||
|         </plugins> |         </plugins> | ||||||
|  | |||||||
| @ -0,0 +1,64 @@ | |||||||
|  | package no.finn.unleash.repository; | ||||||
|  | 
 | ||||||
|  | import java.io.BufferedReader; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.io.InputStreamReader; | ||||||
|  | import java.net.HttpURLConnection; | ||||||
|  | import java.net.MalformedURLException; | ||||||
|  | import java.net.URI; | ||||||
|  | import java.net.URL; | ||||||
|  | import java.nio.charset.StandardCharsets; | ||||||
|  | import java.util.Collection; | ||||||
|  | import no.finn.unleash.Toggle; | ||||||
|  | 
 | ||||||
|  | public final class HttpToggleFetcher implements ToggleFetcher { | ||||||
|  |     public static final int CONNECT_TIMEOUT = 10000; | ||||||
|  |     private String etag = null; | ||||||
|  | 
 | ||||||
|  |     private final URL toggleUrl; | ||||||
|  | 
 | ||||||
|  |     public HttpToggleFetcher(URI repo) { | ||||||
|  |         try { | ||||||
|  |             toggleUrl = repo.toURL(); | ||||||
|  |         } catch (MalformedURLException ex) { | ||||||
|  |             throw new IllegalArgumentException("Invalid repo uri", ex); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public ToggleResponse fetchToggles() throws ToggleException { | ||||||
|  |         HttpURLConnection connection = null; | ||||||
|  |         try { | ||||||
|  |             connection = (HttpURLConnection) toggleUrl.openConnection(); | ||||||
|  |             connection.setConnectTimeout(CONNECT_TIMEOUT); | ||||||
|  |             connection.setReadTimeout(CONNECT_TIMEOUT); | ||||||
|  |             connection.setRequestProperty("If-None-Match", etag); | ||||||
|  |             connection.connect(); | ||||||
|  | 
 | ||||||
|  |             int responseCode = connection.getResponseCode(); | ||||||
|  |             if(responseCode < 300) { | ||||||
|  |                 return getToggleResponse(connection); | ||||||
|  |             } else { | ||||||
|  |                 return new ToggleResponse(ToggleResponse.Status.NOT_CHANGED); | ||||||
|  |             } | ||||||
|  |         } catch (IOException e) { | ||||||
|  |             throw new ToggleException("Could not fetch toggles", e); | ||||||
|  |         } finally { | ||||||
|  |             if(connection != null) { | ||||||
|  |                 connection.disconnect(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private ToggleResponse getToggleResponse(HttpURLConnection request) throws IOException { | ||||||
|  |         etag = request.getHeaderField("ETag"); | ||||||
|  | 
 | ||||||
|  |         try(BufferedReader reader = new BufferedReader( | ||||||
|  |                 new InputStreamReader((InputStream) request.getContent(), StandardCharsets.UTF_8))) { | ||||||
|  | 
 | ||||||
|  |             Collection<Toggle> toggles = JsonToggleParser.fromJson(reader); | ||||||
|  |             return new ToggleResponse(ToggleResponse.Status.CHANGED, toggles); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,5 +1,6 @@ | |||||||
| package no.finn.unleash.repository; | package no.finn.unleash.repository; | ||||||
| 
 | 
 | ||||||
|  | import java.io.Reader; | ||||||
| import java.util.Collection; | import java.util.Collection; | ||||||
| import com.google.gson.Gson; | import com.google.gson.Gson; | ||||||
| import com.google.gson.GsonBuilder; | import com.google.gson.GsonBuilder; | ||||||
| @ -22,4 +23,9 @@ public final class JsonToggleParser { | |||||||
|         Gson gson = new GsonBuilder().create(); |         Gson gson = new GsonBuilder().create(); | ||||||
|         return gson.fromJson(jsonString,ToggleCollection.class).getFeatures(); |         return gson.fromJson(jsonString,ToggleCollection.class).getFeatures(); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     public static Collection<Toggle> fromJson(Reader reader) { | ||||||
|  |         Gson gson = new GsonBuilder().create(); | ||||||
|  |         return gson.fromJson(reader,ToggleCollection.class).getFeatures(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -4,4 +4,8 @@ public class ToggleException extends RuntimeException { | |||||||
|     public ToggleException(String message) { |     public ToggleException(String message) { | ||||||
|         super(message); |         super(message); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     public ToggleException(String message, Throwable cause) { | ||||||
|  |         super(message, cause); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -0,0 +1,5 @@ | |||||||
|  | package no.finn.unleash.repository; | ||||||
|  | 
 | ||||||
|  | public interface ToggleFetcher { | ||||||
|  |    ToggleResponse fetchToggles() throws ToggleException; | ||||||
|  | } | ||||||
| @ -0,0 +1,30 @@ | |||||||
|  | package no.finn.unleash.repository; | ||||||
|  | 
 | ||||||
|  | import java.util.Collection; | ||||||
|  | import java.util.Collections; | ||||||
|  | import no.finn.unleash.Toggle; | ||||||
|  | 
 | ||||||
|  | public final class ToggleResponse { | ||||||
|  |     enum Status {NOT_CHANGED, CHANGED} | ||||||
|  | 
 | ||||||
|  |     private final Status status; | ||||||
|  |     private final Collection<Toggle> getToggles; | ||||||
|  | 
 | ||||||
|  |     public ToggleResponse(Status status, Collection<Toggle> getToggles) { | ||||||
|  |         this.status = status; | ||||||
|  |         this.getToggles = getToggles; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public ToggleResponse(Status status) { | ||||||
|  |         this.status = status; | ||||||
|  |         this.getToggles = Collections.emptyList(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Status getStatus() { | ||||||
|  |         return status; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Collection<Toggle> getGetToggles() { | ||||||
|  |         return getToggles; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,23 @@ | |||||||
|  | package no.finn.unleash.repository; | ||||||
|  | 
 | ||||||
|  | import java.net.URI; | ||||||
|  | import org.junit.Ignore; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.*; | ||||||
|  | 
 | ||||||
|  | public class HttpToggleFetcherTest { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     @Ignore | ||||||
|  |     public void explore() { | ||||||
|  |         HttpToggleFetcher httpToggleFetcher = new HttpToggleFetcher(URI.create("http://localhost:4242/features")); | ||||||
|  | 
 | ||||||
|  |         ToggleResponse toggleResponse = httpToggleFetcher.fetchToggles(); | ||||||
|  |         toggleResponse = httpToggleFetcher.fetchToggles(); | ||||||
|  |         System.out.println("toggleResponse = " + toggleResponse); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -1,53 +0,0 @@ | |||||||
| module.exports = [ |  | ||||||
|     { |  | ||||||
|         "id": 1, |  | ||||||
|         "created": "2014-08-01 12:22:00", |  | ||||||
|         "type": "feature-create", |  | ||||||
|         "user": "John, Doe", |  | ||||||
|         "comment": "Optional comment", |  | ||||||
|         "data": { |  | ||||||
|             "name": "com.example.feature", |  | ||||||
|             "status": "off", |  | ||||||
|             "strategy": "default", |  | ||||||
|             "description": "Feature description" |  | ||||||
|         } |  | ||||||
|     }, |  | ||||||
|     { |  | ||||||
|         "id": 2, |  | ||||||
|         "created": "2014-09-02 15:23:11", |  | ||||||
|         "type": "feature-update", |  | ||||||
|         "user": "User name", |  | ||||||
|         "comment": "Optional comment", |  | ||||||
|         "data": { |  | ||||||
|             "name": "com.example.feature", |  | ||||||
|             "status": "on" |  | ||||||
|         } |  | ||||||
|     }, |  | ||||||
|     { |  | ||||||
|         "id": 3, |  | ||||||
|         "created": "1970-01-01 00:00:00", |  | ||||||
|         "type": "strategy-create", |  | ||||||
|         "user": "User name", |  | ||||||
|         "comment": "Optional comment", |  | ||||||
|         "data": { |  | ||||||
|             "name": "strategyA", |  | ||||||
|             "parameters_template": { |  | ||||||
|                 "users": "example values", |  | ||||||
|                 "target_age": "number" |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     }, |  | ||||||
|     { |  | ||||||
|         "id": 4, |  | ||||||
|         "created": "1970-01-01 00:00:00", |  | ||||||
|         "type": "strategy-update", |  | ||||||
|         "user": "localhost.localdomain", |  | ||||||
|         "comment": "commit message goes here", |  | ||||||
|         "data": { |  | ||||||
|             "name": "strategyA", |  | ||||||
|             "parameters_template": { |  | ||||||
|                 "users": "new default example values" |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| ]; |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user