Search in sources :

Example 11 with RestResponse

use of org.glassfish.admingui.common.util.RestResponse in project Payara by payara.

the class RestApiHandlers method checkIfEndPointExist.

@Handler(id = "gf.checkIfEndPointExist", input = { @HandlerInput(name = "endpoint", type = String.class, required = true) }, output = { @HandlerOutput(name = "exists", type = Boolean.class) })
public static void checkIfEndPointExist(HandlerContext handlerCtx) {
    boolean result = false;
    RestResponse response = null;
    try {
        response = get((String) handlerCtx.getInputValue("endpoint"));
        result = response.isSuccess();
    } catch (Exception ex) {
        GuiUtil.getLogger().info("checkIfEnpointExist failed.");
        if (GuiUtil.getLogger().isLoggable(Level.FINE)) {
            ex.printStackTrace();
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }
    handlerCtx.setOutputValue("exists", result);
}
Also used : RestResponse(org.glassfish.admingui.common.util.RestResponse) Handler(com.sun.jsftemplating.annotation.Handler)

Example 12 with RestResponse

use of org.glassfish.admingui.common.util.RestResponse in project Payara by payara.

the class SecurityHandler method removeUser.

/**
 *	<p> This handler removes users for specified realm.
 *	@param	handlerCtx	The HandlerContext.
 */
@Handler(id = "removeUser", input = { @HandlerInput(name = "Realm", type = String.class, required = true), @HandlerInput(name = "configName", type = String.class, required = true), @HandlerInput(name = "selectedRows", type = List.class, required = true) }, output = { @HandlerOutput(name = "result", type = java.util.List.class) })
public static void removeUser(HandlerContext handlerCtx) {
    String error = null;
    String realmName = (String) handlerCtx.getInputValue("Realm");
    String configName = (String) handlerCtx.getInputValue("configName");
    try {
        List obj = (List) handlerCtx.getInputValue("selectedRows");
        List<Map> selectedRows = (List) obj;
        for (Map oneRow : selectedRows) {
            String user = (String) oneRow.get("name");
            String endpoint = GuiUtil.getSessionValue("REST_URL") + "/configs/config/" + configName + "/admin-service/jmx-connector/system.json";
            Map<String, Object> responseMap = RestUtil.restRequest(endpoint, null, "get", handlerCtx, false);
            Map<String, Object> valueMap = (Map<String, Object>) responseMap.get("data");
            valueMap = (Map<String, Object>) ((Map<String, Object>) valueMap.get("extraProperties")).get("entity");
            String authRealm = (String) valueMap.get("authRealmName");
            if (realmName.equals(authRealm) && user.equals(GuiUtil.getSessionValue("userName"))) {
                error = GuiUtil.getMessage(COMMON_BUNDLE, "msg.error.cannotDeleteCurrent");
                continue;
            } else {
                HashMap attrs = new HashMap<String, Object>();
                endpoint = GuiUtil.getSessionValue("REST_URL") + "/configs/config/" + configName + "/security-service/auth-realm/" + realmName + "/delete-user?target=" + configName;
                attrs.put("name", user);
                RestResponse response = RestUtil.delete(endpoint, attrs);
                if (!response.isSuccess()) {
                    GuiUtil.getLogger().severe("Remove user failed.  parent=" + endpoint + "; attrs =" + attrs);
                    error = GuiUtil.getMessage("msg.error.checkLog");
                }
            }
        }
        if (error != null) {
            GuiUtil.prepareAlert("error", error, null);
        }
    } catch (Exception ex) {
        GuiUtil.handleException(handlerCtx, ex);
    }
}
Also used : RestResponse(org.glassfish.admingui.common.util.RestResponse) Handler(com.sun.jsftemplating.annotation.Handler)

Example 13 with RestResponse

use of org.glassfish.admingui.common.util.RestResponse in project Payara by payara.

the class AdminConsoleAuthModule method validateRequest.

/**
 * <p>
 * This is where the validation happens...
 * </p>
 */
@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
    HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
    HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
    if (!isMandatory(messageInfo) && !request.getRequestURI().endsWith("/j_security_check")) {
        return doNothing(clientSubject);
    }
    HttpSession session = request.getSession(true);
    Subject savedClientSubject = (Subject) session.getAttribute(SAVED_SUBJECT);
    String savedUsername = (String) session.getAttribute(USER_NAME);
    if (savedClientSubject != null && savedUsername != null) {
        // Caller authenticated before, re-apply authentication for this request
        return notifyContainerAboutLogin(clientSubject, savedUsername);
    }
    // See if we've already calculated the serverName / serverPort
    if (session.getAttribute(REST_SERVER_NAME) == null) {
        saveServerHostPort(session);
    }
    // See if the username / password has been passed in...
    String username = request.getParameter("j_username");
    char[] password = request.getParameter("j_password") != null ? request.getParameter("j_password").toCharArray() : null;
    if (username == null || password == null || !request.getMethod().equalsIgnoreCase("post")) {
        // Credentials not passed in, show the login page
        return saveRequestAndForwardToLogin(session, request, response);
    }
    // Credentials provided, validte them via a REST based identity store
    RestResponse validationResult = validateCredentials(request, username, password);
    // Check to see if successful
    if (validationResult.isSuccess()) {
        notifyContainerAboutLogin(clientSubject, username);
        request.changeSessionId();
        // Get the "extraProperties" section from the validation result
        @SuppressWarnings("rawtypes") Map extraProperties = getExtraProperties(validationResult);
        // Save the Rest Token...
        if (extraProperties != null) {
            session.setAttribute(REST_TOKEN, extraProperties.get("token"));
        }
        // Save the Subject...
        session.setAttribute(SAVED_SUBJECT, clientSubject);
        // Save the userName
        session.setAttribute(USER_NAME, username);
        return redirectBack(session, request, response);
    }
    // If we reach this location an error has occurred
    return forwardToErrorPage(validationResult, request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) RestResponse(org.glassfish.admingui.common.util.RestResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Subject(javax.security.auth.Subject)

Aggregations

RestResponse (org.glassfish.admingui.common.util.RestResponse)13 Handler (com.sun.jsftemplating.annotation.Handler)7 Test (org.junit.Test)4 Map (java.util.Map)3 RestUtil.buildDefaultValueMap (org.glassfish.admingui.common.util.RestUtil.buildDefaultValueMap)3 RestUtil.getChildMap (org.glassfish.admingui.common.util.RestUtil.getChildMap)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)2 Subject (javax.security.auth.Subject)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 WebTarget (javax.ws.rs.client.WebTarget)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Response (javax.ws.rs.core.Response)1 RestUtil.buildChildEntityList (org.glassfish.admingui.common.util.RestUtil.buildChildEntityList)1