Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a caching policy #165

Merged
merged 3 commits into from
Aug 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,19 @@ public static Class<?> loadClass(String classname) {

return c;
}

/**
* Squishy way to find a setter method.
* @param theClass
*/
public static Method findSetter(Class<?> onClass, Class<?> targetClass) {
Method[] methods = onClass.getMethods();
for (Method method : methods) {
Class<?>[] ptypes = method.getParameterTypes();
if (method.getName().startsWith("set") && ptypes.length == 1 && ptypes[0] == targetClass) { //$NON-NLS-1$
return method;
}
}
return null;
}
}
12 changes: 12 additions & 0 deletions distro/data/src/main/resources/data/all-policyDefs.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
}
]
},
{
"name" : "Caching Policy",
"description" : "Allows caching of API responses in the Gateway to reduce overall traffic to the back-end API.",
"policyImpl" : "class:io.apiman.gateway.engine.policies.CachingPolicy",
"icon" : "hdd-o",
"templates" : [
{
"language" : null,
"template" : "API responses will be cached for @{ttl} seconds."
}
]
},
{
"name" : "Ignored Resources Policy",
"description" : "Requests satisfying the provided regular expression will be ignored.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
POST /policyDefs ${apiman.suite.api-username}/${apiman.suite.api-password}
Content-Type: application/json

{
"name" : "Caching Policy",
"description" : "Allows caching of API responses in the Gateway to reduce overall traffic to the back-end API.",
"policyImpl" : "class:io.apiman.gateway.engine.policies.CachingPolicy",
"icon" : "hdd-o",
"templates" : [
{
"language" : null,
"template" : "API responses will be cached for @{ttl} seconds."
}
]
}
----
200
Content-Type: application/json

{
"id" : "CachingPolicy"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<test name="Create Ignored Resources Policy Def">data/policyDefs/004_ignoredresources-policydef.resttest</test>
<test name="Create Authorization Policy Def">data/policyDefs/005_authorization-policydef.resttest</test>
<test name="Create Quota Policy Def">data/policyDefs/006_quota-policydef.resttest</test>
<test name="Create Caching Policy Def">data/policyDefs/007_caching-policydef.resttest</test>
</testGroup>

<!-- Gateways -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<column name="template"
value="Requests matching any of the @{pathsToIgnore.size()} regular expressions provided will receive a 404 error code."/>
</insert>
<insert tableName="pd_templates">
<column name="policydef_id" value="CachingPolicy"/>
<column name="language"/>
<column name="template"
value="API responses will be cached for @{ttl} seconds."/>
</insert>
<insert tableName="pd_templates">
<column name="policydef_id" value="AuthorizationPolicy"/>
<column name="language"/>
Expand Down Expand Up @@ -205,6 +211,16 @@
<column name="plugin_id"/>
<column name="policy_impl" value="class:io.apiman.gateway.engine.policies.IgnoredResourcesPolicy"/>
</insert>
<insert tableName="policydefs">
<column name="id" value="CachingPolicy"/>
<column name="description" value="Allows caching of API responses in the Gateway to reduce overall traffic to the back-end API."/>
<column name="form"/>
<column name="form_type" value="Default"/>
<column name="icon" value="hdd-o"/>
<column name="name" value="Caching Policy"/>
<column name="plugin_id"/>
<column name="policy_impl" value="class:io.apiman.gateway.engine.policies.CachingPolicy"/>
</insert>
<insert tableName="policydefs">
<column name="id" value="AuthorizationPolicy"/>
<column name="description"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@
<local-cache name="shared-state" batching="true">
<file-store passivation="false" purge="false" />
</local-cache>
<local-cache name="caching" batching="true">
<file-store passivation="false" purge="false" />
</local-cache>
</cache-container>
<cache-container name="web" aliases="standard-session-cache" default-cache="local-web"
module="org.jboss.as.clustering.web.infinispan">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ INSERT INTO pd_templates (policydef_id, language, template) VALUES ('TransferQuo

INSERT INTO pd_templates (policydef_id, language, template) VALUES ('IgnoredResourcesPolicy', NULL, 'Requests matching any of the @{pathsToIgnore.size()} regular expressions provided will receive a 404 error code.');

INSERT INTO pd_templates (policydef_id, language, template) VALUES ('CachingPolicy', NULL, 'API responses will be cached for @{ttl} seconds.');

INSERT INTO pd_templates (policydef_id, language, template) VALUES ('AuthorizationPolicy', NULL, 'Appropriate authorization roles are required. There are @{rules.size()} authorization rules defined.');

-- Changeset c:/Users/ewittman/git/apiman/apiman/distro/ddl/src/main/liquibase/current/050-apiman-manager-api.db.data.changelog.xml::1434686531709-3::apiman
Expand Down Expand Up @@ -156,6 +158,8 @@ INSERT INTO policydefs (id, description, form, form_type, icon, name, plugin_id,

INSERT INTO policydefs (id, description, form, form_type, icon, name, plugin_id, policy_impl) VALUES ('IgnoredResourcesPolicy', 'Requests satisfying the provided regular expression will be ignored.', NULL, 'Default', 'eye-slash', 'Ignored Resources Policy', NULL, 'class:io.apiman.gateway.engine.policies.IgnoredResourcesPolicy');

INSERT INTO policydefs (id, description, form, form_type, icon, name, plugin_id, policy_impl) VALUES ('CachingPolicy', 'Allows caching of API responses in the Gateway to reduce overall traffic to the back-end API.', NULL, 'Default', 'hdd-o', 'Cachine Policy', NULL, 'class:io.apiman.gateway.engine.policies.CachingPolicy');

INSERT INTO policydefs (id, description, form, form_type, icon, name, plugin_id, policy_impl) VALUES ('AuthorizationPolicy', 'Enables fine grained authorization to API resources based on authenticated user roles.', NULL, 'Default', 'users', 'Authorization Policy', NULL, 'class:io.apiman.gateway.engine.policies.AuthorizationPolicy');

-- Changeset c:/Users/ewittman/git/apiman/apiman/distro/ddl/src/main/liquibase/current/050-apiman-manager-api.db.data.changelog.xml::1434686531709-5::apiman
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ INSERT INTO pd_templates (policydef_id, language, template) VALUES ('TransferQuo

INSERT INTO pd_templates (policydef_id, language, template) VALUES ('IgnoredResourcesPolicy', NULL, 'Requests matching any of the @{pathsToIgnore.size()} regular expressions provided will receive a 404 error code.');

INSERT INTO pd_templates (policydef_id, language, template) VALUES ('CachingPolicy', NULL, 'API responses will be cached for @{ttl} seconds.');

INSERT INTO pd_templates (policydef_id, language, template) VALUES ('AuthorizationPolicy', NULL, 'Appropriate authorization roles are required. There are @{rules.size()} authorization rules defined.');

-- Changeset c:/Users/ewittman/git/apiman/apiman/distro/ddl/src/main/liquibase/current/050-apiman-manager-api.db.data.changelog.xml::1434686531709-3::apiman
Expand Down Expand Up @@ -154,6 +156,8 @@ INSERT INTO policydefs (id, description, form, form_type, icon, name, plugin_id,

INSERT INTO policydefs (id, description, form, form_type, icon, name, plugin_id, policy_impl) VALUES ('IgnoredResourcesPolicy', 'Requests satisfying the provided regular expression will be ignored.', NULL, 'Default', 'eye-slash', 'Ignored Resources Policy', NULL, 'class:io.apiman.gateway.engine.policies.IgnoredResourcesPolicy');

INSERT INTO policydefs (id, description, form, form_type, icon, name, plugin_id, policy_impl) VALUES ('CachingPolicy', 'Allows caching of API responses in the Gateway to reduce overall traffic to the back-end API.', NULL, 'Default', 'hdd-o', 'Cachine Policy', NULL, 'class:io.apiman.gateway.engine.policies.CachingPolicy');

INSERT INTO policydefs (id, description, form, form_type, icon, name, plugin_id, policy_impl) VALUES ('AuthorizationPolicy', 'Enables fine grained authorization to API resources based on authenticated user roles.', NULL, 'Default', 'users', 'Authorization Policy', NULL, 'class:io.apiman.gateway.engine.policies.AuthorizationPolicy');

-- Changeset c:/Users/ewittman/git/apiman/apiman/distro/ddl/src/main/liquibase/current/050-apiman-manager-api.db.data.changelog.xml::1434686531709-5::apiman
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ apiman-gateway.policy-factory=io.apiman.gateway.engine.policy.PolicyFactoryImpl
apiman-gateway.components.ISharedStateComponent=io.apiman.gateway.engine.ispn.InfinispanSharedStateComponent
apiman-gateway.components.IRateLimiterComponent=io.apiman.gateway.engine.ispn.InfinispanRateLimiterComponent
apiman-gateway.components.IPolicyFailureFactoryComponent=io.apiman.gateway.platforms.servlet.PolicyFailureFactoryComponent
apiman-gateway.components.IBufferFactoryComponent=io.apiman.gateway.platforms.servlet.components.BufferFactoryComponentImpl
#apiman-gateway.metrics=io.apiman.gateway.engine.impl.InMemoryMetrics
apiman-gateway.components.IBufferFactoryComponent=io.apiman.gateway.engine.impl.ByteBufferFactoryComponent
apiman-gateway.components.ICacheStoreComponent=io.apiman.gateway.engine.ispn.InfinispanCacheStoreComponent

# ---------------------------------------------------------------------
# Elasticsearch Metrics Settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@
<local-cache name="shared-state" batching="true">
<file-store passivation="false" purge="false" />
</local-cache>
<local-cache name="caching" batching="true">
<file-store passivation="false" purge="false" />
</local-cache>
</cache-container>
<cache-container name="web" default-cache="passivation" module="org.wildfly.clustering.web.infinispan">
<local-cache name="passivation" batching="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.annotate.JsonIgnore;

/**
* The response sent back to a caller when a managed service is
* invoked.
Expand All @@ -28,11 +30,12 @@
public class ServiceResponse implements IServiceObject, Serializable {

private static final long serialVersionUID = -7245095046846226241L;

private int code;
private String message;
private Map<String, String> headers = new HeaderHashMap();
private Map<String, Object> attributes = new HashMap<>();
@JsonIgnore
private transient Map<String, Object> attributes = new HashMap<>();

/**
* Constructor.
Expand All @@ -55,7 +58,7 @@ public Map<String, String> getHeaders() {
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}

/**
* @return the code
*/
Expand Down Expand Up @@ -97,15 +100,15 @@ public Map<String, Object> getAttributes() {
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
/**

/**
* @param name Name of attribute
* @param value Value of attribute
*/
public void setAttribute(String name, Object value) {
this.attributes.put(name, value);
}

/**
* @param name Name of attribute
* @return Attribute if present; else null.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2015 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apiman.gateway.engine;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation that allows a component to indicate that it depends on other
* components.
*
* @author eric.wittmann@redhat.com
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface DependsOnComponents {

Class<? extends IComponent> [] value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@
import io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException;
import io.apiman.gateway.engine.policy.IPolicyContext;

import java.util.Collection;

/**
* A registry to discover and lookup components used by the policy implementations. All
* of the components in the registry are made available via the {@link IPolicyContext} for
* of the components in the registry are made available via the {@link IPolicyContext} for
* lookup by the policy implementations. This interface is internal to the policy engine
* and should never be made available to the policy impls.
*
* @author eric.wittmann@redhat.com
*/
public interface IComponentRegistry {


/**
* @return a list of all registered components
*/
public Collection<IComponent> getComponents();

/**
* Gets a specific type of component from the registry.
* @param componentType the component type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.apiman.gateway.engine;


/**
* Components that require initialization should implement this interface.
*
Expand All @@ -26,5 +27,4 @@ public interface IRequiresInitialization {
* Called by the engine to initialize the component.
*/
public void initialize();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2015 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apiman.gateway.engine.components;

import io.apiman.gateway.engine.IComponent;
import io.apiman.gateway.engine.async.IAsyncResultHandler;
import io.apiman.gateway.engine.io.ISignalReadStream;
import io.apiman.gateway.engine.io.ISignalWriteStream;

import java.io.IOException;

/**
* A component that provides a way to cache content. The cache store supports
* caching of serializeable objects in addition to the binary content associated
* with it (if any).
*
* @author eric.wittmann@redhat.com
*/
public interface ICacheStoreComponent extends IComponent {

/**
* Put an entry into the cache. The entry will not include any binary
* content - just the JSON object.
* @param cacheKey
* @param jsonObject
* @param timeToLive
* @throws IOException
*/
public <T> void put(String cacheKey, T jsonObject, long timeToLive) throws IOException;

/**
* Open a cache store entry so that data can be streamed into it.
* @param cacheKey
* @param jsonObject
* @param timeToLive
* @throws IOException
*/
public <T> ISignalWriteStream putBinary(String cacheKey, T jsonObject, long timeToLive) throws IOException;

/**
* Gets a cache entry.
* @param cacheKey
* @param type
* @throws IOException
*/
public <T> void get(String cacheKey, Class<T> type, IAsyncResultHandler<T> handler);

/**
* Gets a cache entry with its binary data.
* @param cacheKey
* @throws IOException
*/
public <T> void getBinary(String cacheKey, Class<T> type, IAsyncResultHandler<ISignalReadStream<T>> handler);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apiman.gateway.platforms.servlet.components;
package io.apiman.gateway.engine.impl;

import io.apiman.gateway.engine.components.IBufferFactoryComponent;
import io.apiman.gateway.engine.io.ByteBuffer;
import io.apiman.gateway.engine.io.IApimanBuffer;
import io.apiman.gateway.platforms.servlet.io.ByteBuffer;

/**
* Implementation of {@link IBufferFactoryComponent} for servlets.
*
*
* @author Marc Savy <msavy@redhat.com>
*/
public class BufferFactoryComponentImpl implements IBufferFactoryComponent {
public class ByteBufferFactoryComponent implements IBufferFactoryComponent {

/**
* @see io.apiman.gateway.engine.components.IBufferFactoryComponent#createBuffer()
*/
@Override
public IApimanBuffer createBuffer() {
return new ByteBuffer(30);
return new ByteBuffer(1024);
}

/**
Expand Down