Skip to content

Commit

Permalink
Extract FilterId concept
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Panov committed Jun 29, 2015
1 parent 7e423a2 commit 442ec65
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 31 deletions.
84 changes: 84 additions & 0 deletions zuul-netflix/src/main/java/com/netflix/zuul/FilterId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.netflix.zuul;

import java.util.UUID;

import org.junit.After;
import org.junit.Test;

import net.jcip.annotations.Immutable;
import net.jcip.annotations.ThreadSafe;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@Immutable
@ThreadSafe
public final class FilterId {

private String value;

private FilterId(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}

public static class Builder {
private String applicationName = ZuulApplicationInfo.getApplicationName();
private String filterType;
private String filterName;

public Builder applicationName(String applicationName) {
this.applicationName = applicationName;
return this;
}

public Builder filterType(String filterType) {
this.filterType = filterType;
return this;
}

public Builder filterName(String filterName) {
this.filterName = filterName;
return this;
}

public FilterId build() {
return new FilterId(applicationName + ":" + filterName + ":" + filterType);
}
}


public static class UnitTest {

String zuulAppName = ZuulApplicationInfo.getApplicationName();

@After
public void setZuulAppNameBack() {
ZuulApplicationInfo.setApplicationName(zuulAppName);
}

@Test
public void filterId() {
FilterId filterId = new Builder().applicationName("app")
.filterType("com.acme.None")
.filterName("none")
.build();
assertThat(filterId.toString(), is("app:none:com.acme.None"));
}

@Test
public void defaultApplicationName() {
String applicationName = UUID.randomUUID().toString();
ZuulApplicationInfo.setApplicationName(applicationName);

FilterId filterId = new Builder().filterType("com.acme.None")
.filterName("none")
.build();
assertThat(filterId.toString(), is(applicationName + ":none:com.acme.None"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;

import com.netflix.zuul.FilterId;
import com.netflix.zuul.ZuulApplicationInfo;
import com.netflix.zuul.ZuulFilter;

import org.junit.Test;

import net.jcip.annotations.ThreadSafe;
Expand All @@ -28,6 +32,7 @@
/**
* Representation of a ZuulFilter for representing and storing in a database
*/

@ThreadSafe
public class FilterInfo implements Comparable<FilterInfo>{

Expand All @@ -40,12 +45,13 @@ public class FilterInfo implements Comparable<FilterInfo>{
private final String application_name;
private int revision;
private Date creationDate = new Date();

/* using AtomicBoolean so we can pass it into EndpointScriptMonitor */
private final AtomicBoolean isActive = new AtomicBoolean();
private final AtomicBoolean isCanary = new AtomicBoolean();

/**
* Constructor
* Constructors
*/
public FilterInfo(String filter_id, String filter_code, String filter_type, String filter_name, String disablePropertyName, String filter_order, String application_name) {
this.filter_id = filter_id;
Expand All @@ -59,6 +65,32 @@ public FilterInfo(String filter_id, String filter_code, String filter_type, Stri
isCanary.set(false);
}

public FilterInfo(String filterCode, String filterName, ZuulFilter filter) {
this.filter_code = filterCode;
this.filter_type = filter.filterType();
this.filter_name = filterName;
this.filter_disablePropertyName = filter.disablePropertyName();
this.filter_order = "" + filter.filterOrder();
this.application_name = ZuulApplicationInfo.getApplicationName();
isActive.set(false);
isCanary.set(false);
this.filter_id = buildFilterId();
}

public FilterInfo(String filter_id, int revision, Date creationDate, boolean isActive, boolean isCanary, String filter_code, String filter_type, String filter_name, String disablePropertyName, String filter_order, String application_name) {
this.filter_id = filter_id;
this.revision = revision;
this.creationDate = new Date(creationDate.getTime());
this.isActive.set(isActive);
this.isCanary.set(isCanary);
this.filter_code = filter_code;
this.filter_name = filter_name;
this.filter_type = filter_type;
this.filter_order = filter_order;
this.filter_disablePropertyName = disablePropertyName;
this.application_name = application_name;
}

/**
*
* @return the filter name; the class name of the filter
Expand Down Expand Up @@ -92,7 +124,6 @@ public String getFilterType() {
return filter_type;
}


@Override
public String toString() {
return "FilterInfo{" +
Expand All @@ -115,21 +146,6 @@ public String getApplication_name() {
return application_name;
}

public FilterInfo(String filter_id, int revision, Date creationDate, boolean isActive, boolean isCanary, String filter_code, String filter_type, String filter_name, String disablePropertyName, String filter_order, String application_name) {
this.filter_id = filter_id;
this.revision = revision;
this.creationDate = new Date(creationDate.getTime());
this.isActive.set(isActive);
this.isCanary.set(isCanary);
this.filter_code = filter_code;
this.filter_name = filter_name;
this.filter_type = filter_type;
this.filter_order = filter_order;
this.filter_disablePropertyName = disablePropertyName;
this.application_name = application_name;

}

/**
*
* @return the revision of this filter
Expand Down Expand Up @@ -163,7 +179,6 @@ public boolean isCanary() {
return isCanary.get();
}


/**
*
* @return unique key for the filter
Expand All @@ -188,7 +203,15 @@ public String getFilterOrder() {
* @return key is application_name:filter_name:filter_type
*/
public static String buildFilterID(String application_name, String filter_type, String filter_name) {
return application_name + ":" + filter_name + ":" + filter_type;
return new FilterId.Builder().applicationName(application_name)
.filterType(filter_type)
.filterName(filter_name)
.build()
.toString();
}

public String buildFilterId() {
return buildFilterID(application_name, filter_type, filter_name);
}

@Override
Expand Down Expand Up @@ -258,7 +281,5 @@ public void creationDateIsCopiedInConstructor() {
date.setTime(0);
assertThat(filterInfo.getCreationDate().getTime(), is(originalCreationTime));
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ public FilterInfo verifyFilter(String sFilterCode) throws CompilationFailedExcep
checkZuulFilterInstance(instance);
ZuulFilter filter = (ZuulFilter) instance;


String filter_id = FilterInfo.buildFilterID(ZuulApplicationInfo.getApplicationName(), filter.filterType(), groovyClass.getSimpleName());

return new FilterInfo(filter_id, sFilterCode, filter.filterType(), groovyClass.getSimpleName(), filter.disablePropertyName(), "" + filter.filterOrder(), ZuulApplicationInfo.getApplicationName());
return new FilterInfo(sFilterCode, groovyClass.getSimpleName(), filter);
}

Object instanciateClass(Class groovyClass) throws InstantiationException, IllegalAccessException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@
*/
package com.netflix.zuul.scriptManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import com.netflix.astyanax.AstyanaxContext;
import com.netflix.astyanax.Keyspace;
import com.netflix.astyanax.model.Column;
import com.netflix.astyanax.model.ColumnList;
import com.netflix.astyanax.model.Row;
import com.netflix.astyanax.model.Rows;
import com.netflix.zuul.FilterId;
import com.netflix.zuul.ZuulApplicationInfo;
import com.netflix.zuul.dependency.cassandra.hystrix.HystrixCassandraGetRowsByKeys;
import com.netflix.zuul.dependency.cassandra.hystrix.HystrixCassandraGetRowsByQuery;
import com.netflix.zuul.dependency.cassandra.hystrix.HystrixCassandraPut;
import com.netflix.zuul.event.ZuulEvent;
import net.jcip.annotations.ThreadSafe;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
Expand All @@ -35,11 +47,22 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import net.jcip.annotations.ThreadSafe;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.anyList;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* from zuul_filters
Expand Down Expand Up @@ -318,8 +341,10 @@ public FilterInfo addFilter(String filtercode, String filter_type, String filter
}

public static String buildFilterID(String filter_type, String filter_name) {
return FilterInfo.buildFilterID(ZuulApplicationInfo.getApplicationName(), filter_type, filter_name);

return new FilterId.Builder().filterType(filter_type)
.filterName(filter_name)
.build()
.toString();
}

@Override
Expand Down

0 comments on commit 442ec65

Please sign in to comment.