Java Code Examples for io.undertow.server.HttpServerExchange#addQueryParam()

The following examples show how to use io.undertow.server.HttpServerExchange#addQueryParam() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PathTemplateHandler.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    PathTemplateMatcher.PathMatchResult<HttpHandler> match = pathTemplateMatcher.match(exchange.getRelativePath());
    if (match == null) {
        next.handleRequest(exchange);
        return;
    }
    exchange.putAttachment(PATH_TEMPLATE_MATCH, new PathTemplateMatch(match.getMatchedTemplate(), match.getParameters()));
    exchange.putAttachment(io.undertow.util.PathTemplateMatch.ATTACHMENT_KEY, new io.undertow.util.PathTemplateMatch(match.getMatchedTemplate(), match.getParameters()));
    if (rewriteQueryParameters) {
        for (Map.Entry<String, String> entry : match.getParameters().entrySet()) {
            exchange.addQueryParam(entry.getKey(), entry.getValue());
        }
    }
    match.getValue().handleRequest(exchange);
}
 
Example 2
Source File: PathTemplateHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    PathTemplateMatcher.PathMatchResult<HttpHandler> match = pathTemplateMatcher.match(exchange.getRelativePath());
    if (match == null) {
        next.handleRequest(exchange);
        return;
    }
    exchange.putAttachment(PATH_TEMPLATE_MATCH, new PathTemplateMatch(match.getMatchedTemplate(), match.getParameters()));
    exchange.putAttachment(io.undertow.util.PathTemplateMatch.ATTACHMENT_KEY, new io.undertow.util.PathTemplateMatch(match.getMatchedTemplate(), match.getParameters()));
    if (rewriteQueryParameters) {
        for (Map.Entry<String, String> entry : match.getParameters().entrySet()) {
            exchange.addQueryParam(entry.getKey(), entry.getValue());
        }
    }
    match.getValue().handleRequest(exchange);
}
 
Example 3
Source File: QueryParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_form_array() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.ARRAY.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.QUERY.name().toLowerCase(),
			QueryParameterStyle.FORM.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addQueryParam(PARAM_NAME, "3,4,5");
	
	checkArray(exchange, parameter);
}
 
Example 4
Source File: QueryParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_form_object_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	schema.setProperties(PROPS);
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.QUERY.name().toLowerCase(),
			QueryParameterStyle.FORM.name().toLowerCase(),
			true,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addQueryParam(ROLE, "admin");
	exchange.addQueryParam(FIRST_NAME, "Alex");
	
	checkMap(exchange, parameter, 3);
}
 
Example 5
Source File: QueryParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_form_object_no_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	schema.setProperties(PROPS);
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.QUERY.name().toLowerCase(),
			QueryParameterStyle.FORM.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addQueryParam(PARAM_NAME, "role,admin,firstName,Alex");
	
	checkMap(exchange, parameter, 2);
}
 
Example 6
Source File: QueryParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_spacedelimited_array() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.ARRAY.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.QUERY.name().toLowerCase(),
			QueryParameterStyle.SPACEDELIMITED.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addQueryParam(PARAM_NAME, "3 4 5");
	
	checkArray(exchange, parameter);
}
 
Example 7
Source File: QueryParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_pipedelimited_array() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.ARRAY.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.QUERY.name().toLowerCase(),
			QueryParameterStyle.PIPEDELIMITED.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addQueryParam(PARAM_NAME, "3|4|5");
	
	checkArray(exchange, parameter);
}
 
Example 8
Source File: QueryParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_deepobject() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	schema.setProperties(PROPS);
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.QUERY.name().toLowerCase(),
			QueryParameterStyle.DEEPOBJECT.name().toLowerCase(),
			true,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addQueryParam(String.format("%s[%s]", PARAM_NAME, ROLE), ADMIN);
	exchange.addQueryParam(String.format("%s[%s]", PARAM_NAME, FIRST_NAME), ALEX);
	
	checkMap(exchange, parameter, 3);
}
 
Example 9
Source File: ParameterHandler.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
	PathTemplateMatcher.PathMatchResult<String> result = pathTemplateMatcher.match(exchange.getRequestPath());
	
	if (result != null) {
		exchange.putAttachment(ATTACHMENT_KEY,
				new io.undertow.util.PathTemplateMatch(result.getMatchedTemplate(), result.getParameters()));
		for (Map.Entry<String, String> entry : result.getParameters().entrySet()) {
			exchange.addQueryParam(entry.getKey(), entry.getValue());
			
			exchange.addPathParam(entry.getKey(), entry.getValue());
		}
	}
	
	Handler.next(exchange, next);
}
 
Example 10
Source File: ParameterHandler.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
	PathTemplateMatcher.PathMatchResult<String> result = pathTemplateMatcher.match(exchange.getRequestPath());
	
	if (result != null) {
		exchange.putAttachment(ATTACHMENT_KEY,
				new io.undertow.util.PathTemplateMatch(result.getMatchedTemplate(), result.getParameters()));
		for (Map.Entry<String, String> entry : result.getParameters().entrySet()) {
			exchange.addQueryParam(entry.getKey(), entry.getValue());
			
			exchange.addPathParam(entry.getKey(), entry.getValue());
		}
	}
	
	Handler.next(exchange, next);
}
 
Example 11
Source File: ProteusHandler.java    From proteus with Apache License 2.0 5 votes vote down vote up
public void handleRouterRequest(HttpServerExchange exchange) throws Exception
{
    PathTemplateMatcher<RoutingMatch> matcher = matches.get(exchange.getRequestMethod());

    if (matcher == null) {
        handleNoMatch(exchange);

        return;
    }

    PathTemplateMatcher.PathMatchResult<RoutingMatch> match = matcher.match(exchange.getRelativePath());

    if (match == null) {
        handleNoMatch(exchange);

        return;
    }

    exchange.putAttachment(PathTemplateMatch.ATTACHMENT_KEY, match);

    for (Entry<String, String> entry : match.getParameters().entrySet()) {
        exchange.addQueryParam(entry.getKey(), entry.getValue());
    }

    for (HandlerHolder handler : match.getValue().predicatedHandlers) {
        if (handler.predicate.resolve(exchange)) {
            handler.handler.handleRequest(exchange);

            return;
        }
    }

    if (match.getValue().defaultHandler != null) {
        match.getValue().defaultHandler.handleRequest(exchange);
    } else {
        fallbackHandler.handleRequest(exchange);
    }
}
 
Example 12
Source File: Handler.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * On the first step of the request, match the request against the configured
 * paths. If the match is successful, store the chain id within the exchange.
 * Otherwise return false.
 *
 * @param httpServerExchange
 *            The current requests server exchange.
 * @return true if a handler has been defined for the given path.
 */
public static boolean start(HttpServerExchange httpServerExchange) {
	// Get the matcher corresponding to the current request type.
	PathTemplateMatcher<String> pathTemplateMatcher = methodToMatcherMap.get(httpServerExchange.getRequestMethod());
	if (pathTemplateMatcher != null) {
		// Match the current request path to the configured paths.
		PathTemplateMatcher.PathMatchResult<String> result = pathTemplateMatcher
				.match(httpServerExchange.getRequestPath());
		if (result != null) {
			// Found a match, configure and return true;
			// Add path variables to query params.
			httpServerExchange.putAttachment(ATTACHMENT_KEY,
					new io.undertow.util.PathTemplateMatch(result.getMatchedTemplate(), result.getParameters()));
			for (Map.Entry<String, String> entry : result.getParameters().entrySet()) {
				// the values shouldn't be added to query param. but this is left as it was to keep backward compatability
				httpServerExchange.addQueryParam(entry.getKey(), entry.getValue());
				
				// put values in path param map
				httpServerExchange.addPathParam(entry.getKey(), entry.getValue());
			}
			String id = result.getValue();
			httpServerExchange.putAttachment(CHAIN_ID, id);
			httpServerExchange.putAttachment(CHAIN_SEQ, 0);
			return true;
		}
	}
	return false;
}
 
Example 13
Source File: URLUtils.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
void handle(HttpServerExchange exchange, String key, String value) {
    exchange.addQueryParam(key, value);
}
 
Example 14
Source File: HttpRequestParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a path value
 *
 * @param buffer   The buffer
 * @param state    The current state
 * @param exchange The exchange builder
 * @return The number of bytes remaining
 */
@SuppressWarnings("unused")
final void handleQueryParameters(ByteBuffer buffer, ParseState state, HttpServerExchange exchange) throws BadRequestException {
    StringBuilder stringBuilder = state.stringBuilder;
    int queryParamPos = state.pos;
    int mapCount = state.mapCount;
    boolean urlDecodeRequired = state.urlDecodeRequired;
    String nextQueryParam = state.nextQueryParam;

    //so this is a bit funky, because it not only deals with parsing, but
    //also deals with URL decoding the query parameters as well, while also
    //maintaining a non-decoded version to use as the query string
    //In most cases these string will be the same, and as we do not want to
    //build up two separate strings we don't use encodedStringBuilder unless
    //we encounter an encoded character

    while (buffer.hasRemaining()) {
        char next = (char) (buffer.get() & 0xFF);
        if(!allowUnescapedCharactersInUrl && !ALLOWED_TARGET_CHARACTER[next]) {
            throw new BadRequestException(UndertowMessages.MESSAGES.invalidCharacterInRequestTarget(next));
        }
        if (next == ' ' || next == '\t') {
            final String queryString = stringBuilder.toString();
            exchange.setQueryString(queryString);
            if (nextQueryParam == null) {
                if (queryParamPos != stringBuilder.length()) {
                    exchange.addQueryParam(decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true), "");
                }
            } else {
                exchange.addQueryParam(nextQueryParam, decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true));
            }
            state.state = ParseState.VERSION;
            state.stringBuilder.setLength(0);
            state.pos = 0;
            state.nextQueryParam = null;
            state.urlDecodeRequired = false;
            state.mapCount = 0;
            return;
        } else if (next == '\r' || next == '\n') {
            throw UndertowMessages.MESSAGES.failedToParsePath();
        } else {
            if (decode && (next == '+' || next == '%' || next > 127)) { //+ is only a whitespace substitute in the query part of the URL
                urlDecodeRequired = true;
            } else if (next == '=' && nextQueryParam == null) {
                nextQueryParam = decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true);
                urlDecodeRequired = false;
                queryParamPos = stringBuilder.length() + 1;
            } else if (next == '&' && nextQueryParam == null) {
                if (++mapCount >= maxParameters) {
                    throw UndertowMessages.MESSAGES.tooManyQueryParameters(maxParameters);
                }
                if (queryParamPos != stringBuilder.length()) {
                    exchange.addQueryParam(decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true), "");
                }
                urlDecodeRequired = false;
                queryParamPos = stringBuilder.length() + 1;
            } else if (next == '&') {
                if (++mapCount >= maxParameters) {
                    throw UndertowMessages.MESSAGES.tooManyQueryParameters(maxParameters);
                }
                exchange.addQueryParam(nextQueryParam, decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true));
                urlDecodeRequired = false;
                queryParamPos = stringBuilder.length() + 1;
                nextQueryParam = null;
            }
            stringBuilder.append(next);

        }

    }
    state.pos = queryParamPos;
    state.nextQueryParam = nextQueryParam;
    state.urlDecodeRequired = urlDecodeRequired;
    state.mapCount = mapCount;
}
 
Example 15
Source File: URLUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
void handle(HttpServerExchange exchange, String key, String value) {
    exchange.addQueryParam(key, value);
}