All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tangosol.coherence.rest.util.RestHelper Maven / Gradle / Ivy

There is a newer version: 24.09
Show newest version
/*
 * Copyright (c) 2000, 2020, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */
package com.tangosol.coherence.rest.util;

import com.tangosol.util.Base;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.mvel2.ParserContext;

/**
 * A collection of utility methods used by Coherence REST.
 *
 * @author ic  2011.12.17
 */
public abstract class RestHelper
    {

    // ---- public API ------------------------------------------------------

    /**
     * Return a map containing query parameters from UriInfo instance.
     *
     * @param uriInfo  UriInfo for invoked resource method
     *
     * @return Map containing all query parameters from provided UriInfo
     */
    public static Map getQueryParameters(UriInfo uriInfo)
        {
        MultivaluedMap mapQueryParams = uriInfo.getQueryParameters();

        Map mapParams = new HashMap(mapQueryParams.size());
        for (String sKey : mapQueryParams.keySet())
            {
            List listValues = mapQueryParams.get(sKey);
            mapParams.put(sKey, listValues.size() == 1 ? listValues.get(0) : listValues);
            }
        return mapParams;
        }

    /**
     * Resolve the size of a result set Coherence REST resource will return.
     *
     * @param cParamMax     max result set size extracted from URL or -1 if
     *                      not submitted
     * @param cQueryMax     max result set size configured for direct query
     *                      or -1 if not configured
     * @param cResourceMax  max result set size configured for this resource
     *                      or -1 if not configured
     *
     * @return max size of result set that this resource is allowed to return,
     *          or -1 if not configured nor provided by user
     */
    public static int resolveMaxResults(int cParamMax, int cQueryMax, int cResourceMax)
        {
        int cMax = cResourceMax;
        if (cQueryMax >= 0)
            {
            cMax = cQueryMax;
            }
        if (cParamMax >= 0)
            {
            cMax = cMax >= 0 ? Math.min(cParamMax, cMax) : cParamMax;
            }

        return cMax;
        }

    /**
     * Return an instance of {@link ParserContext} to be used for evaluation.
     *
     * @return an instance of {@link ParserContext} to be used for evaluation
     */
    public static ParserContext getMvelParserContext()
        {
        ClassLoader contextLoader = Base.getContextClassLoader();
        return s_mapParserContextByLoader.computeIfAbsent(contextLoader, RestHelper::makeParserContext);
        }

    private static ParserContext makeParserContext(ClassLoader contextLoader)
        {
        ParserContext ctx = new ParserContext();
        ctx.addPackageImport("java.util");
        // set the context ClassLoader so that Mvel and ASM uses the correct ClassLoader
        // for optimizations
        ctx.getParserConfiguration().setClassLoader(contextLoader);
        return ctx;
        }

    /**
     * Mapping(Weak reference) used to associate a ClassLoader with an instance of ParserContext.
     */
    private static WeakHashMap s_mapParserContextByLoader = new WeakHashMap<>();
    }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy