com.networknt.oauth.user.handler.Oauth2UserGetHandler Maven / Gradle / Ivy
package com.networknt.oauth.user.handler;
import com.hazelcast.map.IMap;
import com.hazelcast.query.PagingPredicate;
import com.hazelcast.query.Predicates;
import com.hazelcast.query.impl.predicates.LikePredicate;
import com.networknt.config.Config;
import com.networknt.oauth.cache.CacheStartupHookProvider;
import com.networknt.oauth.cache.model.User;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Deque;
import java.util.Map;
/**
* Created by stevehu on 2017-01-03.
* The current implementation creates a new PagingPredicate for each request which is not very
* efficient. We should create a map of PagingPredicate object with user_id as key from JWT. In
* that case, the same user navigate from page to page will use the same object without creating
* a new one each request.
* TODO implement above once people report performance issue.
*/
public class Oauth2UserGetHandler extends UserAuditHandler implements HttpHandler {
static Logger logger = LoggerFactory.getLogger(Oauth2UserGetHandler.class);
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
IMap users = CacheStartupHookProvider.hz.getMap("users");
Deque userIdDeque = exchange.getQueryParameters().get("userId");
String userId = userIdDeque == null? "%" : userIdDeque.getFirst() + "%";
int page = Integer.valueOf(exchange.getQueryParameters().get("page").getFirst()) - 1;
Deque pageSizeDeque = exchange.getQueryParameters().get("pageSize");
int pageSize = pageSizeDeque == null? 10 : Integer.valueOf(pageSizeDeque.getFirst());
LikePredicate likePredicate = new LikePredicate("userId", userId);
PagingPredicate pagingPredicate = Predicates.pagingPredicate(likePredicate, pageSize);
pagingPredicate.setPage(page);
Collection values = users.values(pagingPredicate);
for (User value : values) {
value.setPassword(null);
}
exchange.getResponseHeaders().add(Headers.CONTENT_TYPE, "application/json");
exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(values));
processAudit(exchange);
}
}