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

The following examples show how to use io.undertow.server.HttpServerExchange#addPathParam() . 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: 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 2
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 3
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_matrix_object_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	schema.setProperties(PROPS);
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.MATRIX.name().toLowerCase(),
			true,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, "");
	
	// we cannot rely on the parsing result at call in this scenario
	// so, we use requestURI which has the original request path
	exchange.setRequestURI(";role=admin;firstName=Alex");
	
	checkMap(exchange, parameter, 3);
}
 
Example 4
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_matrix_object_no_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.MATRIX.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	exchange.addPathParam(PARAM_NAME, "");
	exchange.addPathParam(PARAM_NAME, "role,admin,firstName,Alex");
	
	checkMap(exchange, parameter, 2);
}
 
Example 5
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_simple_array() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.ARRAY.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.SIMPLE.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, "3,4,5");
	
	checkArray(exchange, parameter);
}
 
Example 6
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_simple_object_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.SIMPLE.name().toLowerCase(),
			true,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, "role=admin,firstName=Alex");
	
	checkMap(exchange, parameter, 2);
}
 
Example 7
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_simple_object_no_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.SIMPLE.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, "role,admin,firstName,Alex");
	
	checkMap(exchange, parameter, 2);
}
 
Example 8
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_label_array_explode() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.ARRAY.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.LABEL.name().toLowerCase(),
			true,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, ".3.4.5");
	
	checkArray(exchange, parameter);
}
 
Example 9
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_label_array_no_explode() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.ARRAY.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.LABEL.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, ".3,4,5");
	
	checkArray(exchange, parameter);
}
 
Example 10
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_label_object_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.LABEL.name().toLowerCase(),
			true,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, ".role=admin.firstName=Alex");
	
	checkMap(exchange, parameter, 2);
}
 
Example 11
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_label_object_no_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.OBJECT.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.LABEL.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, ".role,admin,firstName,Alex");
	
	checkMap(exchange, parameter, 2);
}
 
Example 12
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
/**
* undertow consider path parts following ';' as path parameters and tries to parse them too.
* the below code simulates the parsing results in light-4j handler chains.	
*/
@Test
public void test_matrix_primitive() {
	Schema schema = new PojoSchema();
	schema.setType("string");
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.MATRIX.name().toLowerCase(),
			true,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, "5");
	exchange.addPathParam(PARAM_NAME, "");
	
	checkString(exchange, parameter, "5");
}
 
Example 13
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_matrix_array_no_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.ARRAY.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.MATRIX.name().toLowerCase(),
			false,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, "3,4,5");
	exchange.addPathParam(PARAM_NAME, "");
	
	checkArray(exchange, parameter);
}
 
Example 14
Source File: PathParameterDeserializerTest.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void test_matrix_array_exploade() {
	Schema schema = new PojoSchema();
	schema.setType(ValueType.ARRAY.name().toLowerCase());
	
	Parameter parameter = new PoJoParameter(PARAM_NAME,
			ParameterType.PATH.name().toLowerCase(),
			PathParameterStyle.MATRIX.name().toLowerCase(),
			true,
			schema);
	
	HttpServerExchange exchange = new HttpServerExchange(null);
	
	exchange.addPathParam(PARAM_NAME, "3;id=4;id=5");
	exchange.addPathParam(PARAM_NAME, "");
	
	checkArray(exchange, parameter);
}
 
Example 15
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 16
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.addPathParam(key, value);
}
 
Example 17
Source File: PathParameterSessionConfig.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void setSessionId(final HttpServerExchange exchange, final String sessionId) {
    exchange.getPathParameters().remove(name);
    exchange.addPathParam(name, sessionId);
    UndertowLogger.SESSION_LOGGER.tracef("Setting path parameter session id %s on %s", sessionId, exchange);
}
 
Example 18
Source File: HttpRequestParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
final void handlePathParameters(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' || next == '?') {
            if (nextQueryParam == null) {
                if (queryParamPos != stringBuilder.length()) {
                    exchange.addPathParam(decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true), "");
                }
            } else {
                exchange.addPathParam(nextQueryParam, decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true));
            }
            exchange.setRequestURI(exchange.getRequestURI() + ';' + stringBuilder.toString(), state.parseState > HOST_DONE);
            state.stringBuilder.setLength(0);
            state.pos = 0;
            state.nextQueryParam = null;
            state.mapCount = 0;
            state.urlDecodeRequired = false;
            if (next == '?') {
                state.state = ParseState.QUERY_PARAMETERS;
                handleQueryParameters(buffer, state, exchange);
            } else {
                state.state = ParseState.VERSION;
            }
            return;
        } else if (next == '\r' || next == '\n') {
            throw UndertowMessages.MESSAGES.failedToParsePath();
        } else {
            if (decode && (next == '+' || next == '%' || next > 127)) {
                urlDecodeRequired = true;
            }
            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);
                }
                exchange.addPathParam(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.addPathParam(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.mapCount = mapCount;
    state.urlDecodeRequired = urlDecodeRequired;
}
 
Example 19
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.addPathParam(key, value);
}
 
Example 20
Source File: PathParameterSessionConfig.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void setSessionId(final HttpServerExchange exchange, final String sessionId) {
    exchange.getPathParameters().remove(name);
    exchange.addPathParam(name, sessionId);
    UndertowLogger.SESSION_LOGGER.tracef("Setting path parameter session id %s on %s", sessionId, exchange);
}