Java Code Examples for io.undertow.util.QueryParameterUtils#parseQueryString()

The following examples show how to use io.undertow.util.QueryParameterUtils#parseQueryString() . 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: Parameters.java    From PYX-Reloaded with Apache License 2.0 7 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 2
Source File: SessionTpidBindHandler.java    From mxisd with GNU Affero General Public License v3.0 5 votes vote down vote up
@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 3
Source File: StoreInviteHandler.java    From mxisd with GNU Affero General Public License v3.0 5 votes vote down vote up
@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()));
}