Search in sources :

Example 1 with QueryMap

use of io.apiman.gateway.engine.beans.util.QueryMap in project apiman-plugins by apiman.

the class JsonpPolicyTest method shouldNotSaveCallbackFunctionNameInContextWhenNotPresent.

@Test
public void shouldNotSaveCallbackFunctionNameInContextWhenNotPresent() throws Exception {
    // given
    JsonpConfigBean config = new JsonpConfigBean();
    config.setCallbackParamName("testParam");
    QueryMap queryParams = new QueryMap();
    ApiRequest request = new ApiRequest();
    request.setQueryParams(queryParams);
    IPolicyChain<ApiRequest> chain = mock(IPolicyChain.class);
    // when
    jsonpPolicy.doApply(request, sContext, config, chain);
    // then
    assertNull(sContext.getAttribute("callbackFunctionName", null));
    verify(chain).doApply(request);
}
Also used : JsonpConfigBean(io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean) QueryMap(io.apiman.gateway.engine.beans.util.QueryMap) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) Test(org.junit.Test)

Example 2 with QueryMap

use of io.apiman.gateway.engine.beans.util.QueryMap in project apiman by apiman.

the class GatewayServlet method parseApiRequestQueryParams.

/**
 * Parses the query string into a map.
 * @param queryString
 */
protected static final QueryMap parseApiRequestQueryParams(String queryString) {
    QueryMap rval = new QueryMap();
    if (queryString != null) {
        try {
            // $NON-NLS-1$
            String[] pairSplit = queryString.split("&");
            for (String paramPair : pairSplit) {
                // $NON-NLS-1$
                int idx = paramPair.indexOf("=");
                String key, value;
                if (idx != -1) {
                    // $NON-NLS-1$
                    key = URLDecoder.decode(paramPair.substring(0, idx), "UTF-8");
                    // $NON-NLS-1$
                    value = URLDecoder.decode(paramPair.substring(idx + 1), "UTF-8");
                } else {
                    // $NON-NLS-1$
                    key = URLDecoder.decode(paramPair, "UTF-8");
                    value = null;
                }
                rval.add(key, value);
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    return rval;
}
Also used : QueryMap(io.apiman.gateway.engine.beans.util.QueryMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with QueryMap

use of io.apiman.gateway.engine.beans.util.QueryMap in project apiman by apiman.

the class GatewayServlet method readRequest.

/**
 * Reads a {@link ApiRequest} from information found in the inbound
 * portion of the http request.
 * @param request the undertow http server request
 * @return a valid {@link ApiRequest}
 * @throws IOException
 */
protected ApiRequest readRequest(HttpServletRequest request) throws Exception {
    // parseApiRequestPath(request);
    ApiRequestPathInfo pathInfo = getEngine().getApiRequestPathParser().parseEndpoint(request.getPathInfo(), wrapMultiMap(request));
    if (pathInfo.orgId == null) {
        // $NON-NLS-1$
        throw new Exception(Messages.i18n.format("GatewayServlet.InvalidApiEndpoint"));
    }
    QueryMap queryParams = parseApiRequestQueryParams(request.getQueryString());
    String apiKey = getApiKey(request, queryParams);
    ApiRequest srequest = GatewayThreadContext.getApiRequest();
    srequest.setApiKey(apiKey);
    srequest.setApiOrgId(pathInfo.orgId);
    srequest.setApiId(pathInfo.apiId);
    srequest.setApiVersion(pathInfo.apiVersion);
    srequest.setUrl(request.getRequestURL().toString());
    srequest.setDestination(pathInfo.resource);
    srequest.setQueryParams(queryParams);
    readHeaders(srequest, request);
    srequest.setRawRequest(request);
    srequest.setRemoteAddr(request.getRemoteAddr());
    srequest.setTransportSecure(request.isSecure());
    return srequest;
}
Also used : QueryMap(io.apiman.gateway.engine.beans.util.QueryMap) ApiRequestPathInfo(io.apiman.common.util.ApimanPathUtils.ApiRequestPathInfo) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with QueryMap

use of io.apiman.gateway.engine.beans.util.QueryMap in project apiman by apiman.

the class GatewayServletTest method testParseApiRequestQueryParams.

/**
 * Test method for {@link io.apiman.gateway.platforms.servlet.GatewayServlet#parseApiRequestQueryParams(String)}
 */
@Test
public void testParseApiRequestQueryParams() {
    QueryMap paramMap = GatewayServlet.parseApiRequestQueryParams(null);
    Assert.assertNotNull(paramMap);
    paramMap = GatewayServlet.parseApiRequestQueryParams("param1");
    Assert.assertNull(paramMap.get("param1"));
    paramMap = GatewayServlet.parseApiRequestQueryParams("param1=value1");
    Assert.assertEquals("value1", paramMap.get("param1"));
    paramMap = GatewayServlet.parseApiRequestQueryParams("param1=value1&param2");
    Assert.assertEquals("value1", paramMap.get("param1"));
    Assert.assertNull(paramMap.get("param2"));
    paramMap = GatewayServlet.parseApiRequestQueryParams("param1=value1&param2=value2");
    Assert.assertEquals("value1", paramMap.get("param1"));
    Assert.assertEquals("value2", paramMap.get("param2"));
    paramMap = GatewayServlet.parseApiRequestQueryParams("param1=value1&param2=value2&param3=value3");
    Assert.assertEquals("value1", paramMap.get("param1"));
    Assert.assertEquals("value2", paramMap.get("param2"));
    Assert.assertEquals("value3", paramMap.get("param3"));
    paramMap = GatewayServlet.parseApiRequestQueryParams("param1=hello%20world&param2=hello+world&param3=hello%20world&strangekeywith%3D%2B%26%3F=strangevaluewith%3D%2B%26%3F");
    Assert.assertEquals("hello world", paramMap.get("param1"));
    Assert.assertEquals("hello world", paramMap.get("param2"));
    Assert.assertEquals("hello world", paramMap.get("param3"));
    Assert.assertEquals("strangevaluewith=+&?", paramMap.get("strangekeywith=+&?"));
}
Also used : QueryMap(io.apiman.gateway.engine.beans.util.QueryMap) Test(org.junit.Test)

Example 5 with QueryMap

use of io.apiman.gateway.engine.beans.util.QueryMap in project apiman-plugins by apiman.

the class JsonpPolicyTest method shouldRemoveCallbackParamNameFromRequest.

@Test
public void shouldRemoveCallbackParamNameFromRequest() throws Exception {
    // given
    JsonpConfigBean config = new JsonpConfigBean();
    config.setCallbackParamName("testParam");
    QueryMap queryParams = new QueryMap();
    queryParams.put("testParam", "testFunction");
    ApiRequest request = new ApiRequest();
    request.setQueryParams(queryParams);
    IPolicyChain<ApiRequest> chain = mock(IPolicyChain.class);
    // when
    jsonpPolicy.doApply(request, sContext, config, chain);
    // then
    assertNull(request.getQueryParams().get("testParam"));
    verify(chain).doApply(request);
}
Also used : JsonpConfigBean(io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean) QueryMap(io.apiman.gateway.engine.beans.util.QueryMap) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) Test(org.junit.Test)

Aggregations

QueryMap (io.apiman.gateway.engine.beans.util.QueryMap)6 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)4 Test (org.junit.Test)4 JsonpConfigBean (io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ApiRequestPathInfo (io.apiman.common.util.ApimanPathUtils.ApiRequestPathInfo)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1