Search in sources :

Example 1 with ApiEhcache

use of nl.nn.adapterframework.http.rest.ApiEhcache in project iaf by ibissource.

the class EtagHandlerPipe method doPipe.

@Override
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    if (input == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "got null input");
    }
    if (!(input instanceof String)) {
        throw new PipeRunException(this, getLogPrefix(session) + "got an invalid type as input, expected String, got " + input.getClass().getName());
    }
    String uriPatternSessionKey = null;
    ParameterValueList pvl = null;
    ParameterList parameterList = getParameterList();
    if (parameterList != null) {
        ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session);
        try {
            pvl = prc.getValues(getParameterList());
            if (pvl != null) {
                for (int i = 0; i < parameterList.size(); i++) {
                    Parameter parameter = parameterList.getParameter(i);
                    if ("uriPattern".equalsIgnoreCase(parameter.getName()))
                        uriPatternSessionKey = (String) parameter.getValue(pvl, prc);
                }
            }
        } catch (ParameterException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception extracting parameters", e);
        }
    }
    // hash over data genereren, uit cache lezen en teruggeven, in cache updaten, verwijderen uit cache, cache naar disk wegschrijven, cache legen
    String cacheKey = null;
    if (uriPatternSessionKey != null && !uriPatternSessionKey.isEmpty())
        cacheKey = getRestPath() + "_" + uriPatternSessionKey.toLowerCase();
    else
        cacheKey = getRestPath() + "_" + getUriPattern();
    if (cache != null && cache.containsKey(cacheKey)) {
        Object returnCode = false;
        if (getAction().equalsIgnoreCase("generate")) {
            cache.put(cacheKey, RestListenerUtils.formatEtag(getRestPath(), getUriPattern(), input.hashCode()));
            returnCode = true;
        } else if (getAction().equalsIgnoreCase("get")) {
            returnCode = cache.get(cacheKey);
        } else if (getAction().equalsIgnoreCase("set")) {
            cache.put(cacheKey, input.toString());
            returnCode = true;
        } else if (getAction().equalsIgnoreCase("delete")) {
            returnCode = cache.remove(cacheKey);
        } else if (getAction().equalsIgnoreCase("flush")) {
            if (cache instanceof ApiEhcache) {
                ((ApiEhcache) cache).flush();
                returnCode = true;
            }
        } else if (getAction().equalsIgnoreCase("clear")) {
            cache.clear();
            returnCode = true;
        } else {
            throw new PipeRunException(this, getLogPrefix(session) + "action not found [" + getAction() + "]");
        }
        if (log.isDebugEnabled())
            log.debug("found eTag cacheKey [" + cacheKey + "] with action [" + getAction() + "]");
        return new PipeRunResult(getForward(), returnCode);
    } else {
        PipeForward pipeForward = findForward("exception");
        String msg;
        if (cache == null)
            msg = "failed to locate cache";
        else
            msg = "failed to locate eTag [" + cacheKey + "] in cache";
        if (pipeForward == null) {
            throw new PipeRunException(this, getLogPrefix(session) + msg);
        }
        return new PipeRunResult(pipeForward, "");
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ParameterList(nl.nn.adapterframework.parameters.ParameterList) Parameter(nl.nn.adapterframework.parameters.Parameter) ParameterException(nl.nn.adapterframework.core.ParameterException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) ApiEhcache(nl.nn.adapterframework.http.rest.ApiEhcache) PipeForward(nl.nn.adapterframework.core.PipeForward)

Aggregations

ParameterException (nl.nn.adapterframework.core.ParameterException)1 PipeForward (nl.nn.adapterframework.core.PipeForward)1 PipeRunException (nl.nn.adapterframework.core.PipeRunException)1 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)1 ApiEhcache (nl.nn.adapterframework.http.rest.ApiEhcache)1 Parameter (nl.nn.adapterframework.parameters.Parameter)1 ParameterList (nl.nn.adapterframework.parameters.ParameterList)1 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)1 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)1