Search in sources :

Example 6 with ApiResponse

use of io.apiman.gateway.engine.beans.ApiResponse in project apiman by apiman.

the class PolicyChainTest method shouldEndChainImmediatelyWhenSkipCalled.

@Test
public void shouldEndChainImmediatelyWhenSkipCalled() {
    IPolicy skipPolicy = spy(new IPolicy() {

        @Override
        public Object parseConfiguration(String jsonConfiguration) throws ConfigurationParseException {
            return null;
        }

        @Override
        public void apply(ApiRequest request, IPolicyContext context, Object config, IPolicyChain<ApiRequest> chain) {
            chain.doSkip(request);
        }

        @Override
        public void apply(ApiResponse response, IPolicyContext context, Object config, IPolicyChain<ApiResponse> chain) {
            chain.doSkip(response);
        }
    });
    PolicyWithConfiguration pwcSkip = new PolicyWithConfiguration(skipPolicy, null);
    policies.add(pwcSkip);
    policies.add(pwcTwo);
    requestChain = new RequestChain(policies, mockContext);
    requestChain.bodyHandler(mockBodyHandler);
    requestChain.endHandler(mockEndHandler);
    requestChain.doApply(mockRequest);
    requestChain.end();
    verify(mockEndHandler, times(1)).handle((Void) null);
    // Should only be called once, as the second is skipped
    verify(skipPolicy, times(1)).apply(mockRequest, mockContext, null, requestChain);
    verify(policyOne, never()).apply(mockRequest, mockContext, null, requestChain);
}
Also used : IPolicy(io.apiman.gateway.engine.policy.IPolicy) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) ConfigurationParseException(io.apiman.gateway.engine.beans.exceptions.ConfigurationParseException) RequestChain(io.apiman.gateway.engine.policy.RequestChain) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) PolicyWithConfiguration(io.apiman.gateway.engine.policy.PolicyWithConfiguration) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) Test(org.junit.Test)

Example 7 with ApiResponse

use of io.apiman.gateway.engine.beans.ApiResponse in project apiman by apiman.

the class EchoBackEndApi method invoke.

/**
 * @see io.apiman.test.policies.IPolicyTestBackEndApi#invoke(io.apiman.gateway.engine.beans.ApiRequest, byte[])
 */
@Override
public PolicyTestBackEndApiResponse invoke(ApiRequest request, byte[] requestBody) {
    try {
        EchoResponse echoResponse = new EchoResponse();
        if (requestBody != null) {
            echoResponse.setBodyLength(new Long(requestBody.length));
            echoResponse.setBodySha1(DigestUtils.sha1Hex(requestBody));
        }
        echoResponse.setCounter(counter++);
        echoResponse.setHeaders(request.getHeaders());
        echoResponse.setMethod(request.getType());
        echoResponse.setResource(request.getDestination());
        echoResponse.setUri("urn:" + request.getDestination());
        ApiResponse apiResponse = new ApiResponse();
        String errorCode = request.getHeaders().get("X-Echo-ErrorCode");
        if (errorCode != null) {
            int ec = Integer.parseInt(errorCode);
            String errorMsg = request.getHeaders().get("X-Echo-ErrorMessage");
            apiResponse.setCode(ec);
            apiResponse.setMessage(errorMsg);
        } else {
            apiResponse.setCode(200);
            apiResponse.setMessage("OK");
        }
        apiResponse.getHeaders().put("Date", new Date().toString());
        apiResponse.getHeaders().put("Server", "apiman.policy-test");
        apiResponse.getHeaders().put("Content-Type", "application/json");
        String responseBody = normalize(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(echoResponse));
        apiResponse.getHeaders().put("Content-Length", String.valueOf(responseBody.length()));
        PolicyTestBackEndApiResponse response = new PolicyTestBackEndApiResponse(apiResponse, responseBody);
        return response;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) IOException(java.io.IOException) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) Date(java.util.Date)

Example 8 with ApiResponse

use of io.apiman.gateway.engine.beans.ApiResponse 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;
    }
}
Also used : IDataPolicy(io.apiman.gateway.engine.policy.IDataPolicy) ISignalReadStream(io.apiman.gateway.engine.io.ISignalReadStream) Optional.ofNullable(java.util.Optional.ofNullable) CacheConnectorInterceptor(io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor) IOException(java.io.IOException) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) CachingConfig(io.apiman.gateway.engine.policies.config.CachingConfig) IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) IConnectorInterceptor(io.apiman.gateway.engine.policy.IConnectorInterceptor) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) ComponentNotFoundException(io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException) IReadWriteStream(io.apiman.gateway.engine.io.IReadWriteStream) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) ICacheStoreComponent(io.apiman.gateway.engine.components.ICacheStoreComponent) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) CachedResponse(io.apiman.gateway.engine.impl.CachedResponse) IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) ComponentNotFoundException(io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) IOException(java.io.IOException) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) ICacheStoreComponent(io.apiman.gateway.engine.components.ICacheStoreComponent) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse)

Example 9 with ApiResponse

use of io.apiman.gateway.engine.beans.ApiResponse 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;
}
Also used : CachingResourcesSettingsEntry(io.apiman.gateway.engine.policies.config.CachingResourcesSettingsEntry) IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) IOException(java.io.IOException) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) ComponentNotFoundException(io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException) ICacheStoreComponent(io.apiman.gateway.engine.components.ICacheStoreComponent)

Example 10 with ApiResponse

use of io.apiman.gateway.engine.beans.ApiResponse in project apiman by apiman.

the class HttpApiFactory method buildResponse.

public static ApiResponse buildResponse(HttpClientResponse response, Set<String> suppressHeaders) {
    ApiResponse apimanResponse = new ApiResponse();
    apimanResponse.setCode(response.statusCode());
    apimanResponse.setMessage(response.statusMessage() == null ? "" : response.statusMessage());
    multimapToMap(apimanResponse.getHeaders(), response.headers(), suppressHeaders);
    return apimanResponse;
}
Also used : ApiResponse(io.apiman.gateway.engine.beans.ApiResponse)

Aggregations

ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)21 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)7 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)5 PolicyTestBackEndApiResponse (io.apiman.test.policies.PolicyTestBackEndApiResponse)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 AbstractStream (io.apiman.gateway.engine.io.AbstractStream)4 ISignalWriteStream (io.apiman.gateway.engine.io.ISignalWriteStream)4 Test (org.junit.Test)4 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)3 IOException (java.io.IOException)3 IApiRequestExecutor (io.apiman.gateway.engine.IApiRequestExecutor)2 IEngine (io.apiman.gateway.engine.IEngine)2 IEngineResult (io.apiman.gateway.engine.IEngineResult)2 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)2 ComponentNotFoundException (io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException)2 IBufferFactoryComponent (io.apiman.gateway.engine.components.IBufferFactoryComponent)2 ICacheStoreComponent (io.apiman.gateway.engine.components.ICacheStoreComponent)2 ByteBuffer (io.apiman.gateway.engine.io.ByteBuffer)2 JsonpConfigBean (io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean)2 IApiConnectionResponse (io.apiman.gateway.engine.IApiConnectionResponse)1