public void execute(Context context) { // pre-processing with filters for (Filter filter : filters) { if (filter.getClass() == AuthenticationFilter.class) { filter.execute(context.getUserRequest()); } else if (filter.getClass() == DebugFilter.class) { filter.execute(context.getPageRequest()); } } // main processing target.execute(context.getPageRequest()); }
public String toString() { String str = ""; for (Filter filter : filters) { str += filter.getClass().getCanonicalName() + ":"; } return str; }
public <A> QueryWhere<T> where(Filter filter) { HashMap<String, Object> fieldMap = New.hashMap(); for (Field f : filter.getClass().getDeclaredFields()) { f.setAccessible(true); try { Object obj = f.get(filter); if (obj == from.getAlias()) { List<TableDefinition.FieldDefinition> fields = from.getAliasDefinition().getFields(); String name = f.getName(); for (TableDefinition.FieldDefinition field : fields) { String n = name + "." + field.field.getName(); Object o = field.field.get(obj); fieldMap.put(n, o); } } fieldMap.put(f.getName(), f.get(filter)); } catch (Exception e) { throw new RuntimeException(e); } } Token filterCode = new ClassReader().decompile(filter, fieldMap, "where"); // String filterQuery = filterCode.toString(); conditions.add(filterCode); return new QueryWhere<T>(this); }
@Override public JSONObject serialize(Filter bean) throws JSONException { if (bean == null) { return null; } if (bean instanceof Selection) { return selectionJsonHandler.serialize((Selection) bean); } if (bean instanceof StringQuery) { JSONObject ret = (JSONObject) JsonSerializer.serialize(bean); return new JSONObject().put("query", ret); } if (bean instanceof AndOr) { AndOr andOr = (AndOr) bean; String operation = andOr.getOperation().name(); List<JSONObject> filters = convertToJson(andOr.filters); return new JSONObject().put(operation, new JSONArray(filters)); } if (bean instanceof BoolFilter) { BoolFilter bool = (BoolFilter) bean; JSONObject ret = new JSONObject(); if (bool.getMust() != null) { ret.put("must", new JSONArray(convertToJson(bool.getMust()))); } if (bool.getMust_not() != null) { ret.put("must_not", new JSONArray(convertToJson(bool.getMust_not()))); } if (bool.getShould() != null) { ret.put("should", new JSONArray(convertToJson(bool.getShould()))); } return new JSONObject().put("bool", ret); } if (bean instanceof Ids) { Ids ids = (Ids) bean; JSONObject ret = new JSONObject(); if (ids.getValues() != null) { ret.put("values", new JSONArray(ids.getValues())); } if (ids.getExcludes() != null) { ret.put("excludes", new JSONArray(ids.getExcludes())); } return new JSONObject().put("ids", ret); } if (bean instanceof Query) { return queryJsonHandler.serialize((Query) bean); } if (bean instanceof QueryFilter) { return new JSONObject() .put("query", queryJsonHandler.serialize(((QueryFilter) bean).getQuery())); } throw new UnsupportedOperationException(bean.getClass() + " is not supported"); }
@Override public DBObject createFilter(final Filter<K, T> filter, final MongoStore<K, T> store) { if (filter instanceof FilterList) { FilterList<K, T> filterList = (FilterList<K, T>) filter; return transformListFilter(filterList, store); } else if (filter instanceof SingleFieldValueFilter) { SingleFieldValueFilter<K, T> fieldFilter = (SingleFieldValueFilter<K, T>) filter; return transformFieldFilter(fieldFilter, store); } else if (filter instanceof MapFieldValueFilter) { MapFieldValueFilter<K, T> mapFilter = (MapFieldValueFilter<K, T>) filter; return transformMapFilter(mapFilter, store); } else { LOG.warn( "MongoDB remote filter not yet implemented for " + filter.getClass().getCanonicalName()); return null; } }
/** * Returns true under the following conditions: * * <ol> * <li>If this filter is the same type as the given filter. * <li>If this filter type is a descendant of the given filter's type. * <li>If the given filter's type is a descendant of this filter's type. * </ol> * * If the given filter is not of type {@link TypeFilter} then false is returned. * * <p>If the given filter is null then false is returned. * * @param filter the other filter */ @Override public boolean isAllowed(Filter filter) { // if its null then just return if (filter == null) return false; // check for the same instance if (this == filter) return true; // make sure the given filter is a TypeFilter if (filter instanceof TypeFilter) { // because the TypeFilter class is abstract, this.getClass() should never return // TypeFilter, but should return the type of the class that extends TypeFilter // then check the types if (this.getClass().isInstance(filter) || filter.getClass().isInstance(this)) { // if they are the same type then return true // if the given filter is a descendant type of this filter type then return true // if this type is a descendant of the given filter's type then return true return true; } } // otherwise return false return false; }
public FilterModel(Filter filter) { String typeName = filter.getClass().getSimpleName(); FilterType type = FilterType.valueOf(typeName); this.type = typeName; switch (type) { case ColumnCountGetFilter: this.limit = ((ColumnCountGetFilter) filter).getLimit(); break; case ColumnPaginationFilter: this.limit = ((ColumnPaginationFilter) filter).getLimit(); this.offset = ((ColumnPaginationFilter) filter).getOffset(); break; case ColumnPrefixFilter: this.value = Base64.encodeBytes(((ColumnPrefixFilter) filter).getPrefix()); break; case ColumnRangeFilter: this.minColumn = Base64.encodeBytes(((ColumnRangeFilter) filter).getMinColumn()); this.minColumnInclusive = ((ColumnRangeFilter) filter).getMinColumnInclusive(); this.maxColumn = Base64.encodeBytes(((ColumnRangeFilter) filter).getMaxColumn()); this.maxColumnInclusive = ((ColumnRangeFilter) filter).getMaxColumnInclusive(); break; case DependentColumnFilter: { DependentColumnFilter dcf = (DependentColumnFilter) filter; this.family = Base64.encodeBytes(dcf.getFamily()); byte[] qualifier = dcf.getQualifier(); if (qualifier != null) { this.qualifier = Base64.encodeBytes(qualifier); } this.op = dcf.getOperator().toString(); this.comparator = new WritableByteArrayComparableModel(dcf.getComparator()); this.dropDependentColumn = dcf.dropDependentColumn(); } break; case FilterList: this.op = ((FilterList) filter).getOperator().toString(); this.filters = new ArrayList<FilterModel>(); for (Filter child : ((FilterList) filter).getFilters()) { this.filters.add(new FilterModel(child)); } break; case FirstKeyOnlyFilter: case KeyOnlyFilter: break; case InclusiveStopFilter: this.value = Base64.encodeBytes(((InclusiveStopFilter) filter).getStopRowKey()); break; case MultipleColumnPrefixFilter: this.prefixes = new ArrayList<String>(); for (byte[] prefix : ((MultipleColumnPrefixFilter) filter).getPrefix()) { this.prefixes.add(Base64.encodeBytes(prefix)); } break; case PageFilter: this.value = Long.toString(((PageFilter) filter).getPageSize()); break; case PrefixFilter: this.value = Base64.encodeBytes(((PrefixFilter) filter).getPrefix()); break; case FamilyFilter: case QualifierFilter: case RowFilter: case ValueFilter: this.op = ((CompareFilter) filter).getOperator().toString(); this.comparator = new WritableByteArrayComparableModel(((CompareFilter) filter).getComparator()); break; case RandomRowFilter: this.chance = ((RandomRowFilter) filter).getChance(); break; case SingleColumnValueExcludeFilter: case SingleColumnValueFilter: { SingleColumnValueFilter scvf = (SingleColumnValueFilter) filter; this.family = Base64.encodeBytes(scvf.getFamily()); byte[] qualifier = scvf.getQualifier(); if (qualifier != null) { this.qualifier = Base64.encodeBytes(qualifier); } this.op = scvf.getOperator().toString(); this.comparator = new WritableByteArrayComparableModel(scvf.getComparator()); if (scvf.getFilterIfMissing()) { this.ifMissing = true; } if (scvf.getLatestVersionOnly()) { this.latestVersion = true; } } break; case SkipFilter: this.filters = new ArrayList<FilterModel>(); this.filters.add(new FilterModel(((SkipFilter) filter).getFilter())); break; case TimestampsFilter: this.timestamps = ((TimestampsFilter) filter).getTimestamps(); break; case WhileMatchFilter: this.filters = new ArrayList<FilterModel>(); this.filters.add(new FilterModel(((WhileMatchFilter) filter).getFilter())); break; default: throw new RuntimeException("unhandled filter type " + type); } }