use of io.apiman.gateway.engine.async.IAsyncHandler in project apiman by apiman.
the class AbstractCacheStoreComponent method getBinary.
/**
* @see ICacheStoreComponent#getBinary(String, Class, IAsyncResultHandler)
*/
@Override
public <T> void getBinary(final String cacheKey, final Class<T> type, final IAsyncResultHandler<ISignalReadStream<T>> handler) {
try {
final CacheEntry cacheEntry = getStore().get(cacheKey, CacheEntry.class);
// Did the fetch succeed? If not, return null.
if (null == cacheEntry) {
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
return;
}
// Is the cache entry expired? If so return null.
if (System.currentTimeMillis() > cacheEntry.getExpiresOn()) {
// Cache item has expired. Return null instead of the cached data.
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
return;
}
try {
@SuppressWarnings("unchecked") final T head = (T) JSON_MAPPER.readValue(cacheEntry.getHead(), type);
final String b64Data = cacheEntry.getData();
final IApimanBuffer data = bufferFactory.createBuffer(Base64.decodeBase64(b64Data));
final ISignalReadStream<T> rval = new ISignalReadStream<T>() {
IAsyncHandler<IApimanBuffer> bodyHandler;
IAsyncHandler<Void> endHandler;
boolean finished = false;
boolean aborted = false;
@Override
public void bodyHandler(IAsyncHandler<IApimanBuffer> bodyHandler) {
this.bodyHandler = bodyHandler;
}
@Override
public void endHandler(IAsyncHandler<Void> endHandler) {
this.endHandler = endHandler;
}
@Override
public T getHead() {
return head;
}
@Override
public boolean isFinished() {
return finished;
}
@Override
public void abort(Throwable t) {
finished = true;
aborted = true;
}
@Override
public void transmit() {
if (!aborted) {
bodyHandler.handle(data);
endHandler.handle(null);
}
finished = true;
}
};
handler.handle(AsyncResultImpl.create(rval));
} catch (Throwable e) {
LOGGER.error("Error reading binary cache entry with key: {}", cacheKey, e);
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
}
} catch (Throwable e) {
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
}
}
use of io.apiman.gateway.engine.async.IAsyncHandler in project apiman by apiman.
the class ApimanPolicyTest method send.
public PolicyTestResponse send(final PolicyTestRequest ptRequest) throws PolicyFailureError, Throwable {
final Set<Throwable> errorHolder = new HashSet<>();
final Set<PolicyFailure> failureHolder = new HashSet<>();
final Set<ApiResponse> responseHolder = new HashSet<>();
final StringBuilder responseBody = new StringBuilder();
IEngine engine = tester.getEngine();
ApiRequest srequest = tester.createApiRequest();
// $NON-NLS-1$
srequest.setUrl("http://localhost:8080" + ptRequest.resource());
srequest.setDestination(ptRequest.resource());
srequest.setType(ptRequest.method().name());
srequest.getHeaders().putAll(ptRequest.headers());
srequest.getQueryParams().putAll(ptRequest.queryParams());
IApiRequestExecutor executor = engine.executor(srequest, new IAsyncResultHandler<IEngineResult>() {
@Override
public void handle(IAsyncResult<IEngineResult> result) {
if (result.isError()) {
errorHolder.add(result.getError());
} else {
IEngineResult engineResult = result.getResult();
if (engineResult.isFailure()) {
failureHolder.add(engineResult.getPolicyFailure());
} else {
responseHolder.add(engineResult.getApiResponse());
engineResult.bodyHandler(new IAsyncHandler<IApimanBuffer>() {
@Override
public void handle(IApimanBuffer result) {
responseBody.append(new String(result.getBytes()));
}
});
engineResult.endHandler(new IAsyncHandler<Void>() {
@Override
public void handle(Void result) {
}
});
}
}
}
});
executor.streamHandler(new IAsyncHandler<ISignalWriteStream>() {
@Override
public void handle(ISignalWriteStream stream) {
if (ptRequest.body() != null) {
ByteBuffer buffer = new ByteBuffer(ptRequest.body());
stream.write(buffer);
}
stream.end();
}
});
// Push any context attributes into the Policy Context.
IPolicyContext policyContext = getContext(executor);
Map<String, Object> contextAttributes = ptRequest.contextAttributes();
for (Entry<String, Object> entry : contextAttributes.entrySet()) {
policyContext.setAttribute(entry.getKey(), entry.getValue());
}
// Execute the request.
executor.execute();
if (!errorHolder.isEmpty()) {
throw errorHolder.iterator().next();
}
if (!failureHolder.isEmpty()) {
throw new PolicyFailureError(failureHolder.iterator().next());
}
if (!responseHolder.isEmpty()) {
ApiResponse response = responseHolder.iterator().next();
return new PolicyTestResponse(response, responseBody.toString());
}
// $NON-NLS-1$
throw new Exception("No response found from request!");
}
use of io.apiman.gateway.engine.async.IAsyncHandler in project apiman by apiman.
the class EsCacheStoreComponent method getBinary.
/**
* @see io.apiman.gateway.engine.components.ICacheStoreComponent#getBinary(java.lang.String, java.lang.Class, io.apiman.gateway.engine.async.IAsyncResultHandler)
*/
@Override
public <T> void getBinary(final String cacheKey, final Class<T> type, final IAsyncResultHandler<ISignalReadStream<T>> handler) {
try {
GetResponse response = getClient().get(new GetRequest(getFullIndexName()).id(cacheKey), RequestOptions.DEFAULT);
// Did the GET succeed? If not, return null.
if (!response.isExists()) {
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
return;
}
// Is the cache entry expired? If so return null.
String sourceAsString = response.getSourceAsString();
CacheEntry cacheEntry = JSON_MAPPER.readValue(sourceAsString, CacheEntry.class);
if (System.currentTimeMillis() > cacheEntry.getExpiresOn()) {
// Cache item has expired. Return null instead of the cached data.
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
return;
}
try {
final T head = (T) JSON_MAPPER.reader(type).readValue(cacheEntry.getHead());
String b64Data = cacheEntry.getData();
final IApimanBuffer data = bufferFactory.createBuffer(Base64.decodeBase64(b64Data));
ISignalReadStream<T> rval = new ISignalReadStream<T>() {
IAsyncHandler<IApimanBuffer> bodyHandler;
IAsyncHandler<Void> endHandler;
boolean finished = false;
boolean aborted = false;
@Override
public void bodyHandler(IAsyncHandler<IApimanBuffer> bodyHandler) {
this.bodyHandler = bodyHandler;
}
@Override
public void endHandler(IAsyncHandler<Void> endHandler) {
this.endHandler = endHandler;
}
@Override
public T getHead() {
return head;
}
@Override
public boolean isFinished() {
return finished;
}
@Override
public void abort(Throwable t) {
finished = true;
aborted = true;
}
@Override
public void transmit() {
if (!aborted) {
bodyHandler.handle(data);
endHandler.handle(null);
}
finished = true;
}
};
handler.handle(AsyncResultImpl.create(rval));
} catch (Throwable e) {
LOGGER.error(e, "Error attempting to stream cached binary on key {0}", cacheKey);
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
}
} catch (Throwable e) {
LOGGER.error(e, "Error attempting to GET cached binary on key {0}", cacheKey);
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
}
}
Aggregations