Example usage for org.apache.wicket Application get

List of usage examples for org.apache.wicket Application get

Introduction

In this page you can find the example usage for org.apache.wicket Application get.

Prototype

public static Application get() 

Source Link

Document

Get Application for current thread.

Usage

From source file:org.wicketstuff.security.components.SecureComponentHelper.java

License:Apache License

/**
 * Gets the {@link ActionFactory}.//from  www .  j a v  a  2s.  co  m
 * 
 * @return the actionfactory
 * @throws WicketRuntimeException
 *             if the ActionFactory is not found.
 */
private static ActionFactory getActionFactory() {
    Application application = Application.get();
    if (application instanceof WaspApplication) {
        WaspApplication app = (WaspApplication) application;
        return app.getActionFactory();
    }
    throw new WicketRuntimeException(application + " is not a " + WaspApplication.class);
}

From source file:org.wicketstuff.security.log.AuthorizationMessageSource.java

License:Apache License

/**
 * @see org.apache.wicket.validation.IErrorMessageSource#getMessage(java.lang.String, java.util.Map) 
 *///w w w .ja  v a 2 s. c  o  m
@Override
public final String getMessage(String key, Map<String, Object> vars) {
    Localizer localizer = Application.get().getResourceSettings().getLocalizer();
    // Note: It is important that the default value of "" is provided
    // to getString() not to throw a MissingResourceException or to
    // return a default string like "[Warning: String ..."
    String message = localizer.getString(key, getComponent(), "");
    if (Strings.isEmpty(message)) {
        return null;
    } else {
        return new MapVariableInterpolator(message, mergeVariables(vars),
                Application.get().getResourceSettings().getThrowExceptionOnMissingResource()).toString();
    }
}

From source file:org.wicketstuff.security.login.http.HttpAuthenticationLoginPage.java

License:Apache License

/**
 * Delegates authentication. Subclasses should first try there custom authentication scheme
 * before letting super handle the call. Subclasses should either return a boolean value (see
 * {@link #handleBasicAuthentication(WebRequest, WebResponse, String, String)} ) if processing
 * should continue or throw an exception.
 * /*from   ww w. java 2  s  .c o  m*/
 * @param request
 * @param response
 * @param scheme
 *            the authentication scheme like "Basic" or "Digest"
 * @param param
 *            the parameters after the scheme from the header
 * @throws LoginException
 *             if the user could not be logged in.
 * @throws RestartResponseAtInterceptPageException
 *             to an {@link AccessDeniedPage} if the scheme is not supported
 */
protected void handleAuthentication(WebRequest request, WebResponse response, String scheme, String param)
        throws LoginException {
    if (!handleBasicAuthentication(request, response, scheme, param))
        return;
    log.error("Unsupported Http authentication type: " + scheme);
    throw new RestartResponseAtInterceptPageException(
            Application.get().getApplicationSettings().getAccessDeniedPage());
}

From source file:org.wicketstuff.security.login.http.HttpAuthenticationLoginPage.java

License:Apache License

/**
 * Handles authentication for the "Basic" scheme. If the scheme is not the basic scheme true is
 * returned so another implementation may try it. In general authentication attempts by the next
 * scheme should only proceed if the scheme was of the wrong type. False will generally be
 * returned when a) the user has been authenticated or b) the scheme is correct but another
 * problem arises, like missing additional headers.
 * // ww w . j av a 2s  . c  o m
 * @param request
 * @param response
 * @param scheme
 * @param param
 *            username:password in base 64
 * @return true if authentication by another scheme should be attempted, false if authentication
 *         by another scheme should not be attempted.
 * @throws LoginException
 *             If the supplied credentials do not grant enough credits for the requested
 *             resource
 * @throws RestartResponseAtInterceptPageException
 *             to the home page if the login was successfull but when there is no page to
 *             continue to.
 */
protected boolean handleBasicAuthentication(WebRequest request, WebResponse response, String scheme,
        String param) throws LoginException {
    if (!"Basic".equalsIgnoreCase(scheme))
        return true;
    if (param == null) {
        log.error("Username, password not supplied");
        return false;
    }
    byte[] decoded = Base64.decodeBase64(param.getBytes());
    String[] split = new String(decoded).split(":");
    if (split == null || split.length != 2)
        throw new LoginException("Could not decrypt username / password");
    Object loginContext = getBasicLoginContext(split[0], split[1]);
    Session session = Session.get();
    if (session instanceof WaspSession) {
        if (!isAuthenticated())
            ((WaspSession) session).login(loginContext);

        continueToOriginalDestination();
        // or
        throw new RestartResponseAtInterceptPageException(Application.get().getHomePage());
    } else
        log.error("Unable to find WaspSession");
    return false;
}

From source file:org.wicketstuff.security.login.http.HttpAuthenticationLoginPage.java

License:Apache License

/**
 * Check if already someone is authenticated to prevent duplicate logins. By default this checks
 * if the home page is authenticated.//from  w w  w  . j  a va2 s  . com
 * 
 * @return true if the user is already authenticated, false otherwise
 */
protected boolean isAuthenticated() {
    WaspAuthorizationStrategy strategy = (WaspAuthorizationStrategy) Session.get().getAuthorizationStrategy();
    return strategy.isClassAuthenticated(Application.get().getHomePage());
}

From source file:org.wicketstuff.security.models.SecureCompoundPropertyModel.java

License:Apache License

/**
 * Shortcut to the {@link ActionFactory}.
 * /*from w  w  w  .ja  v a2s  . com*/
 * @return the factory
 */
protected final ActionFactory getActionFactory() {
    return ((WaspApplication) Application.get()).getActionFactory();
}

From source file:org.wicketstuff.security.pages.BasePage.java

License:Apache License

/**
 * Shortcut to the application.//from   w  ww. j a  v a 2  s . c o m
 * 
 * @return the wasp application
 */
protected final WaspWebApplication getWaspApplication() {
    return (WaspWebApplication) Application.get();
}

From source file:org.wicketstuff.security.pages.MockLoginPage.java

License:Apache License

/**
 * /*  w  ww.j  a v a  2s .  c  o m*/
 * @param username
 * @return true if the login was successful, false otherwise
 */
public boolean login(String username) {
    try {
        LoginContext context;
        if ("test".equals(username)) {
            context = new PrimaryLoginContext();
        } else if ("all".equals(username)) {
            context = new CustomLoginContext(new SimplePrincipal("all permissions"));
        } else {
            context = new CustomLoginContext(new SimplePrincipal(username));
        }
        ((WaspSession) Session.get()).login(context);
        continueToOriginalDestination();
        // or
        setResponsePage(Application.get().getHomePage());
        return true;
    } catch (LoginException e) {
        log.error(e.getMessage(), e);
    }
    return false;
}

From source file:org.wicketstuff.security.pages.SecondaryLoginPage.java

License:Apache License

/**
 * Login using a username.//from ww  w  .j a v a 2s .  c  o  m
 * 
 * @param username
 * @return true if the login was successful, false otherwise
 */
public boolean login(String username) {
    try {
        LoginContext context = new SecondaryLoginContext();
        ((WaspSession) Session.get()).login(context);
        continueToOriginalDestination();

        // or
        setResponsePage(Application.get().getHomePage());
        return true;
    } catch (LoginException e) {
        log.error(e.getMessage(), e);
    }
    return false;
}

From source file:org.wicketstuff.security.strategies.WaspAuthorizationStrategy.java

License:Apache License

/**
 * Shortcut to the actionfactory./*w  ww .  ja  v  a  2 s  . c  o  m*/
 * 
 * @return the actionfactory from the application
 */
protected final WaspActionFactory getActionFactory() {
    return ((WaspApplication) Application.get()).getActionFactory();
}