use of io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException in project apiman by apiman.
the class ConfigDrivenComponentRegistry method createAndRegisterComponent.
/**
* Creates the component and registers it in the registry.
* @param componentType the component type
* @return the component
* @throws ComponentNotFoundException when a policy tries to get a component from
* the context but the component doesn't exist or is otherwise not available.
*/
public <T extends IComponent> T createAndRegisterComponent(Class<T> componentType) throws ComponentNotFoundException {
try {
synchronized (components) {
Class<? extends T> componentClass = engineConfig.getComponentClass(componentType, pluginRegistry);
Map<String, String> componentConfig = engineConfig.getComponentConfig(componentType);
T component = create(componentClass, componentConfig);
components.put(componentType, component);
// Because components are lazily created, we need to initialize them here
// if necessary.
DependsOnComponents annotation = componentClass.getAnnotation(DependsOnComponents.class);
if (annotation != null) {
Class<? extends IComponent>[] value = annotation.value();
for (Class<? extends IComponent> theC : value) {
Method setter = ReflectionUtils.findSetter(componentClass, theC);
if (setter != null) {
IComponent injectedComponent = getComponent(theC);
try {
setter.invoke(component, new Object[] { injectedComponent });
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
}
if (component instanceof IRequiresInitialization) {
((IRequiresInitialization) component).initialize();
}
return component;
}
} catch (Exception e) {
throw new ComponentNotFoundException(componentType.getName());
}
}
use of io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException in project apiman by apiman.
the class CachingPolicy method responseDataHandler.
/**
* @see io.apiman.gateway.engine.policies.AbstractMappedDataPolicy#responseDataHandler(io.apiman.gateway.engine.beans.ApiResponse, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object)
*/
@Deprecated
@Override
protected IReadWriteStream<ApiResponse> responseDataHandler(final ApiResponse response, IPolicyContext context, CachingConfig policyConfiguration) {
// Possibly cache the response for future posterity.
// Check the response code against list in config (empty/null list means cache all).
final boolean shouldCache = (context.getAttribute(SHOULD_CACHE_ATTR, Boolean.FALSE) && ofNullable(policyConfiguration.getStatusCodes()).map(statusCodes -> statusCodes.isEmpty() || statusCodes.contains(String.valueOf(response.getCode()))).orElse(true));
if (shouldCache) {
try {
String cacheId = context.getAttribute(CACHE_ID_ATTR, null);
ICacheStoreComponent cache = context.getComponent(ICacheStoreComponent.class);
final ISignalWriteStream writeStream = cache.putBinary(cacheId, response, policyConfiguration.getTtl());
return new AbstractStream<ApiResponse>() {
@Override
public ApiResponse getHead() {
return response;
}
@Override
protected void handleHead(ApiResponse head) {
}
@Override
public void write(IApimanBuffer chunk) {
writeStream.write(chunk);
super.write(chunk);
}
@Override
public void end() {
writeStream.end();
super.end();
}
};
} catch (ComponentNotFoundException | IOException e) {
// TODO log error
return null;
}
} else {
return null;
}
}
use of io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException in project apiman by apiman.
the class CachingResourcesPolicy method responseDataHandler.
/**
* @see io.apiman.gateway.engine.policies.AbstractMappedDataPolicy#responseDataHandler(io.apiman.gateway.engine.beans.ApiResponse, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object)
*/
@Override
protected IReadWriteStream<ApiResponse> responseDataHandler(final ApiResponse response, IPolicyContext context, CachingResourcesConfig policyConfiguration) {
if (response == null) {
// if the response is empty because of a policy failure before we end here and return null
return null;
}
List<CachingResourcesSettingsEntry> possibleMatchingCachingEntries = context.getAttribute(CACHE_POSSIBLE_MATCHING_ENTRIES, new ArrayList<CachingResourcesSettingsEntry>());
boolean isAMatch = false;
for (CachingResourcesSettingsEntry entry : possibleMatchingCachingEntries) {
isAMatch = isAMatch || matchesPolicyEntryVsActualValue(entry.getStatusCode(), String.valueOf(response.getCode()));
}
// Possibly cache the response for future posterity.
final boolean shouldCache = context.getAttribute(SHOULD_CACHE_ATTR, Boolean.FALSE) && isAMatch;
if (shouldCache) {
try {
String cacheId = context.getAttribute(CACHE_ID_ATTR, null);
ICacheStoreComponent cache = context.getComponent(ICacheStoreComponent.class);
final ISignalWriteStream writeStream = cache.putBinary(cacheId, response, policyConfiguration.getTtl());
return new AbstractStream<ApiResponse>() {
@Override
public ApiResponse getHead() {
return response;
}
@Override
protected void handleHead(ApiResponse head) {
}
@Override
public void write(IApimanBuffer chunk) {
writeStream.write(chunk);
super.write(chunk);
}
@Override
public void end() {
writeStream.end();
super.end();
}
};
} catch (ComponentNotFoundException | IOException e) {
throw new RuntimeException(e);
}
}
return null;
}
Aggregations