Java Code Examples for io.undertow.util.QueryParameterUtils

The following examples show how to use io.undertow.util.QueryParameterUtils. These examples are extracted from open source projects. 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 Project: quarkus-http   Source File: RequestPathAttribute.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    int pos = newValue.indexOf('?');
    exchange.setResolvedPath("");
    if (pos == -1) {
        exchange.setRelativePath(newValue);
        exchange.setRequestURI(newValue);
        exchange.setRequestPath(newValue);
    } else {
        final String path = newValue.substring(0, pos);
        exchange.setRequestPath(path);
        exchange.setRelativePath(path);
        exchange.setRequestURI(newValue);

        final String newQueryString = newValue.substring(pos);
        exchange.setQueryString(newQueryString);
        exchange.getQueryParameters().putAll(QueryParameterUtils.parseQueryString(newQueryString.substring(1), QueryParameterUtils.getQueryParamEncoding(exchange)));
    }
}
 
Example 2
Source Project: quarkus-http   Source File: RequestURLAttribute.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    int pos = newValue.indexOf('?');
    if (pos == -1) {
        exchange.setRelativePath(newValue);
        exchange.setRequestURI(newValue);
        exchange.setRequestPath(newValue);
        exchange.setResolvedPath("");
    } else {
        final String path = newValue.substring(0, pos);
        exchange.setRelativePath(path);
        exchange.setRequestURI(path);
        exchange.setRequestPath(path);
        exchange.setResolvedPath("");
        final String newQueryString = newValue.substring(pos);
        exchange.setQueryString(newQueryString);
        exchange.getQueryParameters().putAll(QueryParameterUtils.parseQueryString(newQueryString.substring(1), QueryParameterUtils.getQueryParamEncoding(exchange)));
    }

}
 
Example 3
Source Project: lams   Source File: RequestPathAttribute.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    int pos = newValue.indexOf('?');
    exchange.setResolvedPath("");
    if (pos == -1) {
        exchange.setRelativePath(newValue);
        exchange.setRequestURI(newValue);
        exchange.setRequestPath(newValue);
    } else {
        final String path = newValue.substring(0, pos);
        exchange.setRequestPath(path);
        exchange.setRelativePath(path);
        exchange.setRequestURI(newValue);

        final String newQueryString = newValue.substring(pos);
        exchange.setQueryString(newQueryString);
        exchange.getQueryParameters().putAll(QueryParameterUtils.parseQueryString(newQueryString.substring(1), QueryParameterUtils.getQueryParamEncoding(exchange)));
    }
}
 
Example 4
Source Project: lams   Source File: RequestURLAttribute.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    int pos = newValue.indexOf('?');
    if (pos == -1) {
        exchange.setRelativePath(newValue);
        exchange.setRequestURI(newValue);
        exchange.setRequestPath(newValue);
        exchange.setResolvedPath("");
    } else {
        final String path = newValue.substring(0, pos);
        exchange.setRelativePath(path);
        exchange.setRequestURI(path);
        exchange.setRequestPath(path);
        exchange.setResolvedPath("");
        final String newQueryString = newValue.substring(pos);
        exchange.setQueryString(newQueryString);
        exchange.getQueryParameters().putAll(QueryParameterUtils.parseQueryString(newQueryString.substring(1), QueryParameterUtils.getQueryParamEncoding(exchange)));
    }

}
 
Example 5
Source Project: PYX-Reloaded   Source File: Parameters.java    License: Apache License 2.0 6 votes vote down vote up
public static Parameters fromExchange(HttpServerExchange exchange) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    InputStream in = exchange.getInputStream();
    byte[] buffer = new byte[8 * 1024];

    int read;
    while ((read = in.read(buffer)) != -1)
        out.write(buffer, 0, read);

    Parameters params = new Parameters();
    Map<String, Deque<String>> rawParams = QueryParameterUtils.parseQueryString(new String(out.toByteArray()), "UTF-8");
    for (Map.Entry<String, Deque<String>> entry : rawParams.entrySet())
        params.put(entry.getKey(), entry.getValue().getFirst());

    return params;
}
 
Example 6
@Override
public void handleRequest(HttpServerExchange exchange) {
    BindRequest bindReq = new BindRequest();
    bindReq.setSid(getQueryParameter(exchange, BindRequest.Keys.SessionID));
    bindReq.setSecret(getQueryParameter(exchange, BindRequest.Keys.Secret));
    bindReq.setUserId(getQueryParameter(exchange, BindRequest.Keys.UserID));

    String reqContentType = getContentType(exchange).orElse("application/octet-stream");
    if (StringUtils.equals("application/x-www-form-urlencoded", reqContentType)) {
        String body = getBodyUtf8(exchange);
        Map<String, Deque<String>> parms = QueryParameterUtils.parseQueryString(body, StandardCharsets.UTF_8.name());
        bindReq.setSid(getQueryParameter(parms, BindRequest.Keys.SessionID));
        bindReq.setSecret(getQueryParameter(parms, BindRequest.Keys.Secret));
        bindReq.setUserId(getQueryParameter(parms, BindRequest.Keys.UserID));
    } else if (StringUtils.equals("application/json", reqContentType)) {
        bindReq = parseJsonTo(exchange, BindRequest.class);
    } else {
        log.warn("Unknown encoding in 3PID session bind: {}", reqContentType);
        log.warn("The request will most likely fail");
    }

    try {
        SingleLookupReply lookup = mgr.bind(bindReq.getSid(), bindReq.getSecret(), bindReq.getUserId());
        JsonObject response = signMgr.signMessageGson(GsonUtil.makeObj(new SingeLookupReplyJson(lookup)));
        respond(exchange, response);
    } catch (BadRequestException e) {
        log.info("requested session was not validated");

        JsonObject obj = new JsonObject();
        obj.addProperty("errcode", "M_SESSION_NOT_VALIDATED");
        obj.addProperty("error", e.getMessage());
        respond(exchange, HttpStatus.SC_BAD_REQUEST, obj);
    } finally {
        // If a user registers, there is no standard login event. Instead, this is the only way to trigger
        // resolution at an appropriate time. Meh at synapse/Riot!
        invMgr.lookupMappingsForInvites();
    }
}
 
Example 7
@Override
public void handleRequest(HttpServerExchange exchange) {
    String reqContentType = getContentType(exchange).orElse("application/octet-stream");
    JsonObject invJson = new JsonObject();

    if (StringUtils.startsWith(reqContentType, "application/json")) {
        invJson = parseJsonObject(exchange);
    }

    // Backward compatibility for pre-r0.1.0 implementations
    else if (StringUtils.startsWith(reqContentType, "application/x-www-form-urlencoded")) {
        String body = getBodyUtf8(exchange);
        Map<String, Deque<String>> parms = QueryParameterUtils.parseQueryString(body, StandardCharsets.UTF_8.name());
        for (Map.Entry<String, Deque<String>> entry : parms.entrySet()) {
            if (entry.getValue().size() == 0) {
                return;
            }

            if (entry.getValue().size() > 1) {
                throw new BadRequestException("key " + entry.getKey() + " has more than one value");
            }

            invJson.addProperty(entry.getKey(), entry.getValue().peekFirst());
        }
    } else {
        throw new BadRequestException("Unsupported Content-Type: " + reqContentType);
    }

    Type parmType = new TypeToken<Map<String, String>>() {
    }.getType();
    Map<String, String> parameters = GsonUtil.get().fromJson(invJson, parmType);
    StoreInviteRequest inv = GsonUtil.get().fromJson(invJson, StoreInviteRequest.class);
    _MatrixID sender = MatrixID.asAcceptable(inv.getSender());

    IThreePidInvite invite = new ThreePidInvite(sender, inv.getMedium(), inv.getAddress(), inv.getRoomId(), parameters);
    IThreePidInviteReply reply = invMgr.storeInvite(invite);

    // FIXME the key info must be set by the invitation manager in the reply object!
    respondJson(exchange, new ThreePidInviteReplyIO(reply, keyMgr.getPublicKeyBase64(keyMgr.getServerSigningKey().getId()), cfg.getPublicUrl()));
}