CPD Results

The following document contains the results of PMD's CPD 4.2.2.

Duplications

File Line
org/abstracthorizon/danube/http/auth/JAASAuthenticatedHTTPContext.java 194
org/abstracthorizon/danube/http/auth/JAASAuthenticator.java 186
    }

    /**
     * Obtains subject object from base 64 encoded username and password
     * @param base64 base 64 encoded username and password
     * @return subject or <code>null</code>
     */
    protected Subject authorise(String base64) {
        AuthData authData = null;
        // TODO Maybe we need to keep queried AuthData even if it is expired
        synchronized (cachedAuth) {
            long now = System.currentTimeMillis() - minScanPeriod;
            if (lastScan + minScanPeriod < now) {
                Iterator<AuthData> it = cachedAuth.values().iterator();
                while (it.hasNext()) {
                    authData = it.next();
                    if (authData.lastAccessed + cacheTimeout < now) {
                        it.remove();
                    }
                }
                lastScan = System.currentTimeMillis();
            }
            authData = cachedAuth.get(base64);
        }

        if (authData != null) {
            authData.lastAccessed = System.currentTimeMillis();
            return authData.subject;
        }

        String userPass = Base64.decode(base64);
        int i = userPass.indexOf(':');
        if (i < 0) {
            return null;
        }

        final String user = userPass.substring(0, i);
        final char[] pass = userPass.substring(i+1).toCharArray();

        LoginContext loginContext; // = getLoginContext();
        try {
            // if (loginContext == null) {
                loginContext = new LoginContext(getLoginContextName(), new CallbackHandler() {

                    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                        for (int i = 0; i < callbacks.length; i++) {
                            if (callbacks[i] instanceof TextOutputCallback) {
                            } else if (callbacks[i] instanceof ConfirmationCallback) {
                            } else if (callbacks[i] instanceof NameCallback) {
                                NameCallback nameCallback = (NameCallback)callbacks[i];
                                nameCallback.setName(user);
                            } else if (callbacks[i] instanceof PasswordCallback) {
                                PasswordCallback passwordCallback = (PasswordCallback)callbacks[i];
                                passwordCallback.setPassword(pass);
                            } else {
                                throw new UnsupportedCallbackException
                                 (callbacks[i], "Unrecognized Callback");
                            }
                          }
                    }
                });
                if (loginContext == null) {
                    return null;
                } else {
                    setLoginContext(loginContext);
                }
            // }
            logger.debug("Trying to authenticate user " + user);
            loginContext.login();
            logger.debug("Successfully authenticated user " + user);
        } catch (LoginException e) {
            logger.debug("Exception trying to get LoginContext " + getLoginContextName(), e);
            return null;
        }
        Subject subject = loginContext.getSubject();
        synchronized (cachedAuth) {
            authData = new AuthData();
            authData.lastAccessed = System.currentTimeMillis();
            authData.subject = subject;
            cachedAuth.put(base64, authData);
        }
        return subject;
    }

    /**
     * Returns wrapped handler
     * @return wrapped handler
     */
    public ConnectionHandler getHandler() {

File Line
org/abstracthorizon/danube/http/auth/JAASAuthenticatedHTTPContext.java 107
org/abstracthorizon/danube/http/auth/JAASAuthenticator.java 119
    }

    /**
     * This method creates sets context path to be same as context path
     * up to here plus this component's path. Component's path is reset
     * to &quot;<code>/<code>&quot;
     *
     * @param connection socket connection
     * @throws ConnectionException
     */
    public void handleConnection(final Connection connection) throws ConnectionException {
        Subject subject = null;
        Session session = null;
        boolean fromSession = false;
        HTTPSessionManager sessionManager = getSessionManager();
        if (sessionManager != null) {
            session = (Session)sessionManager.findSession(connection, false);
            if (session != null) {
                subject = (Subject)session.getAttributes().get(AUTHORIZATION_DATA_ATTRIBUTE);
                if (subject != null) {
                    fromSession = true;
                }
            }
        }

        HTTPConnection httpConnection = (HTTPConnection)connection.adapt(HTTPConnection.class);
        if (subject == null) {

            String authHeader = httpConnection.getRequestHeaders().getOnly(AUTHORIZATION_REQUEST_HEADER);
            if (authHeader != null) {
                if (authHeader.startsWith("Basic ")) {
                    String base64 = authHeader.substring(6);
                    subject = authorise(base64);
                }
            }
        }

        if (subject != null) {
            if (!fromSession && (session != null)) {
                session.getAttributes().put(AUTHORIZATION_DATA_ATTRIBUTE, subject);
            }
            try {
                Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {

File Line
org/abstracthorizon/danube/http/auth/JAASAuthenticatedHTTPContext.java 276
org/abstracthorizon/danube/http/auth/JAASAuthenticator.java 284
    }

    /**
     * Returns session manaager
     * @return http session manager
     */
    public HTTPSessionManager getSessionManager() {
        if (sessionManager == null) {
            sessionManager = new SimpleSessionManager();
        }
        return sessionManager;
    }

    /**
     * Sets session manager
     * @param sessionManager http session manager
     */
    public void setSessionManager(HTTPSessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    /**
     * Returns realm to be used. If not set then component path will be used.
     * @return realm
     */
    public String getRealm() {
        return realm;
    }

    /**
     * Sets realm.
     *
     * @param realm realm
     */
    public void setRealm(String realm) {
        this.realm = realm;
    }

    /**
     * Returns login context name
     * @return login context name
     */
    public String getLoginContextName() {
        return loginContextName;
    }

    /**
     * Sets login context name
     * @param loginContextName login context name
     */
    public void setLoginContextName(String loginContextName) {
        this.loginContextName = loginContextName;
    }

    /**
     * Returns login context
     * @return login context
     */
    public LoginContext getLoginContext() {
        return loginContext;
    }

    /**
     * Sets login context
     * @param loginContext login context
     */
    public void setLoginContext(LoginContext loginContext) {
        this.loginContext = loginContext;
    }

    /**
     * Returns cache timeout
     * @return cache timeout
     */
    public int getCacheTimeout() {
        return cacheTimeout;
    }

    /**
     * Sets cache timeout
     * @param cacheTimeout cache timeout
     */
    public void setCacheTimeout(int cacheTimeout) {
        this.cacheTimeout = cacheTimeout;
    }

    /**
     * Return minimum scan period
     * @return minimum scan period
     */
    public int getMinimumScanPeriod() {
        return minScanPeriod;
    }

    /**
     * Sets minimum scan period
     * @param minScanPeriod minimum scan period
     */
    public void setMinimumScanPeriod(int minScanPeriod) {
        this.minScanPeriod = minScanPeriod;
    }