Search in sources :

Example 26 with CalFacadeException

use of org.bedework.calfacade.exc.CalFacadeException in project bw-calendar-engine by Bedework.

the class CalSvc method getCal.

/* Currently this gets a local calintf only. Later we need to use a par to
   * get calintf from a table.
   */
Calintf getCal() throws CalFacadeException {
    if (cali != null) {
        return cali;
    }
    final long start = System.currentTimeMillis();
    try {
        final long beforeGetIntf = System.currentTimeMillis() - start;
        cali = CalintfFactory.getIntf(CalintfFactory.hibernateClass);
        final long afterGetIntf = System.currentTimeMillis() - start;
        cali.open(pars.getWebMode(), pars.getForRestore(), // Just for the user interactions
        pars.getIndexRebuild());
        postNotification(SysEvent.makeTimedEvent("Login: about to obtain calintf", beforeGetIntf));
        postNotification(SysEvent.makeTimedEvent("Login: calintf obtained", afterGetIntf));
        postNotification(SysEvent.makeTimedEvent("Login: intf opened", System.currentTimeMillis() - start));
        cali.beginTransaction();
        postNotification(SysEvent.makeTimedEvent("Login: transaction started", System.currentTimeMillis() - start));
        String runAsUser = pars.getUser();
        if (pars.getCalSuite() != null) {
            final BwCalSuite cs = cali.getCalSuite(pars.getCalSuite());
            if (cs == null) {
                error("******************************************************");
                error("Unable to fetch calendar suite " + pars.getCalSuite());
                error("Is the database correctly initialised?");
                error("******************************************************");
                throw new CalFacadeException(CalFacadeException.unknownCalsuite, pars.getCalSuite());
            }
            getCalSuitesHandler().set(new BwCalSuiteWrapper(cs));
            /* For administrative use we use the account of the admin group the user
         * is a direct member of
         *
         * For public clients we use the calendar suite owning group.
         */
            if (!pars.getPublicAdmin()) {
                runAsUser = cs.getGroup().getOwnerHref();
            }
        }
        postNotification(SysEvent.makeTimedEvent("Login: before get dirs", System.currentTimeMillis() - start));
        final Directories dir = getDirectories();
        /* Get ourselves a user object */
        String authenticatedUser = pars.getAuthUser();
        if (authenticatedUser != null) {
            final String sv = authenticatedUser;
            if (dir.isPrincipal(authenticatedUser)) {
                authenticatedUser = dir.accountFromPrincipal(authenticatedUser);
            }
            if (authenticatedUser == null) {
                error("Failed with Authenticated user " + sv);
                return null;
            }
            if (authenticatedUser.endsWith("/")) {
                getLogger().warn("Authenticated user " + authenticatedUser + " ends with \"/\"");
            }
        }
        postNotification(SysEvent.makeTimedEvent("Login: before user fetch", System.currentTimeMillis() - start));
        // synchronized (synchlock) {
        final Users users = (Users) getUsersHandler();
        if (runAsUser == null) {
            runAsUser = authenticatedUser;
        }
        BwPrincipal currentPrincipal;
        final BwPrincipal authPrincipal;
        PrivilegeSet maxAllowedPrivs = null;
        boolean subscriptionsOnly = getSystemProperties().getUserSubscriptionsOnly();
        boolean userMapHit = false;
        boolean addingUser = false;
        boolean addingRunAsUser = false;
        if (pars.getForRestore()) {
            authenticated = true;
            currentPrincipal = dir.caladdrToPrincipal(pars.getAuthUser());
            authPrincipal = currentPrincipal;
            subscriptionsOnly = false;
        } else if (authenticatedUser == null) {
            authenticated = false;
            // Unauthenticated use
            currentPrincipal = unauthUsers.get(runAsUser);
            if (currentPrincipal == null) {
                currentPrincipal = users.getUser(runAsUser);
            } else {
                userMapHit = true;
            }
            if (currentPrincipal == null) {
                // XXX Should we set this one up?
                currentPrincipal = BwPrincipal.makeUserPrincipal();
            }
            currentPrincipal.setUnauthenticated(true);
            if (!userMapHit) {
                unauthUsers.put(runAsUser, currentPrincipal);
            }
            authPrincipal = currentPrincipal;
            maxAllowedPrivs = PrivilegeSet.readOnlyPrivileges;
        } else {
            authenticated = true;
            currentPrincipal = unauthUsers.get(authenticatedUser);
            if (currentPrincipal == null) {
                currentPrincipal = users.getUser(authenticatedUser);
            } else {
                userMapHit = true;
            }
            if (currentPrincipal == null) {
                /* Add the user to the database. Presumably this is first logon
             */
                getLogger().debug("Add new user " + authenticatedUser);
                /*
            currentPrincipal = addUser(authenticatedUser);
            if (currentPrincipal == null) {
              error("Failed to find user after adding: " + authenticatedUser);
            }
            */
                currentPrincipal = getFakeUser(authenticatedUser);
                addingUser = true;
            }
            authPrincipal = currentPrincipal;
            if (authenticatedUser.equals(runAsUser)) {
                getLogger().debug("Authenticated user " + authenticatedUser + " logged on");
            } else {
                currentPrincipal = unauthUsers.get(runAsUser);
                if (currentPrincipal == null) {
                    currentPrincipal = users.getUser(runAsUser);
                } else {
                    userMapHit = true;
                }
                if (currentPrincipal == null) {
                    // throw new CalFacadeException("User " + runAsUser + " does not exist.");
                    /* Add the user to the database. Presumably this is first logon
               */
                    getLogger().debug("Add new run-as-user " + runAsUser);
                    // currentPrincipal = addUser(runAsUser);
                    currentPrincipal = getFakeUser(runAsUser);
                    addingRunAsUser = true;
                }
                getLogger().debug("Authenticated user " + authenticatedUser + " logged on - running as " + runAsUser);
            }
            if (!userMapHit && (currentPrincipal != null)) {
                currentPrincipal.setGroups(dir.getAllGroups(currentPrincipal));
                authUsers.put(currentPrincipal.getAccount(), currentPrincipal);
            }
            postNotification(SysEvent.makeTimedEvent("Login: after get Groups", System.currentTimeMillis() - start));
            if (pars.getService()) {
                subscriptionsOnly = false;
            } else {
                final BwPrincipalInfo bwpi = dir.getDirInfo(currentPrincipal);
                currentPrincipal.setPrincipalInfo(bwpi);
                if (pars.getPublicAdmin() || (bwpi != null && bwpi.getHasFullAccess())) {
                    subscriptionsOnly = false;
                }
                postNotification(SysEvent.makeTimedEvent("Login: got Dirinfo", System.currentTimeMillis() - start));
            }
        }
        principalInfo = new SvciPrincipalInfo(this, currentPrincipal, authPrincipal, maxAllowedPrivs, subscriptionsOnly);
        cali.init(pars.getLogId(), configs, principalInfo, null, pars.getPublicAdmin(), pars.getPublicSubmission(), pars.getSessionsless(), pars.getDontKill());
        if (addingUser) {
            // Do the real work of setting up user
            addUser(authenticatedUser);
        }
        if (addingRunAsUser) {
            // Do the real work of setting up user
            addUser(runAsUser);
        }
        if (!currentPrincipal.getUnauthenticated()) {
            if (pars.getService()) {
                postNotification(SysEvent.makePrincipalEvent(SysEvent.SysCode.SERVICE_USER_LOGIN, currentPrincipal, System.currentTimeMillis() - start));
            } else if (!creating) {
                users.logon(currentPrincipal);
                postNotification(SysEvent.makePrincipalEvent(SysEvent.SysCode.USER_LOGIN, currentPrincipal, System.currentTimeMillis() - start));
            }
        } else {
        // If we have a runAsUser it's a public client. Pretend we authenticated
        // WHY?          currentPrincipal.setUnauthenticated(runAsUser == null);
        }
        if (pars.getPublicAdmin() || pars.isGuest()) {
            if (debug) {
                trace("PublicAdmin: " + pars.getPublicAdmin() + " user: " + runAsUser);
            }
        /* We may be running as a different user. The preferences we want to see
           * are those of the user we are running as - i.e. the 'run.as' user
           * not those of the authenticated user.
           * /

          BwCalSuiteWrapper suite = getCalSuitesHandler().get();
          BwPrincipal user;

          if (suite != null) {
            // Use this user
            user = users.getPrincipal(suite.getGroup().getOwnerHref());
          } else if (runAsUser == null) {
            // Unauthenticated CalDAV for example?
            user = currentPrincipal;
          } else {
            // No calendar suite set up

            // XXX This is messy
            if (runAsUser.startsWith("/")) {
              user = users.getPrincipal(runAsUser);
            } else {
              user = users.getUser(runAsUser);
            }
          }

          if (!user.equals(principalInfo.getPrincipal())) {
            user.setGroups(getDirectories().getAllGroups(user));
            user.setPrincipalInfo(getDirectories().getDirInfo(user));
            ((SvciPrincipalInfo)principalInfo).setPrincipal(user);
          }

           */
        }
        return cali;
    // }
    } catch (final CalFacadeException cfe) {
        error(cfe);
        throw cfe;
    } catch (final Throwable t) {
        error(t);
        throw new CalFacadeException(t);
    } finally {
        if (cali != null) {
            cali.endTransaction();
            cali.close();
        // cali.flushAll();
        }
    }
}
Also used : BwCalSuite(org.bedework.calfacade.svc.BwCalSuite) PrivilegeSet(org.bedework.access.PrivilegeSet) BwString(org.bedework.calfacade.BwString) BwCalSuiteWrapper(org.bedework.calfacade.svc.wrappers.BwCalSuiteWrapper) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) Directories(org.bedework.calfacade.ifs.Directories) BwPrincipal(org.bedework.calfacade.BwPrincipal) BwPrincipalInfo(org.bedework.calfacade.BwPrincipalInfo)

Example 27 with CalFacadeException

use of org.bedework.calfacade.exc.CalFacadeException in project bw-calendar-engine by Bedework.

the class CalSvc method getUserDirectories.

/* (non-Javadoc)
   * @see org.bedework.calsvci.CalSvcI#getUserDirectories()
   */
@Override
public Directories getUserDirectories() throws CalFacadeException {
    if (userGroups != null) {
        return userGroups;
    }
    try {
        userGroups = (Directories) CalFacadeUtil.getObject(getSystemProperties().getUsergroupsClass(), Directories.class);
        userGroups.init(getGroupsCallBack(), configs);
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
    return userGroups;
}
Also used : CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 28 with CalFacadeException

use of org.bedework.calfacade.exc.CalFacadeException in project bw-calendar-engine by Bedework.

the class CalSvc method getAdminDirectories.

/* (non-Javadoc)
   * @see org.bedework.calsvci.CalSvcI#getAdminDirectories()
   */
@Override
public Directories getAdminDirectories() throws CalFacadeException {
    if (adminGroups != null) {
        return adminGroups;
    }
    try {
        adminGroups = (Directories) CalFacadeUtil.getObject(getSystemProperties().getAdmingroupsClass(), Directories.class);
        adminGroups.init(getGroupsCallBack(), configs);
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
    return adminGroups;
}
Also used : CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 29 with CalFacadeException

use of org.bedework.calfacade.exc.CalFacadeException in project bw-calendar-engine by Bedework.

the class CalSvc method getEncrypter.

PwEncryptionIntf getEncrypter() throws CalFacadeException {
    if (pwEncrypt != null) {
        return pwEncrypt;
    }
    try {
        String pwEncryptClass = "org.bedework.util.security.PwEncryptionDefault";
        // String pwEncryptClass = getSysparsHandler().get().getPwEncryptClass();
        pwEncrypt = (PwEncryptionIntf) CalFacadeUtil.getObject(pwEncryptClass, PwEncryptionIntf.class);
        String privKeys = null;
        String pubKeys = null;
        GenKeysMBean gk = (GenKeysMBean) MBeanUtil.getMBean(GenKeysMBean.class, GenKeysMBean.serviceName);
        if (gk != null) {
            privKeys = gk.getPrivKeyFileName();
            pubKeys = gk.getPublicKeyFileName();
        }
        if (privKeys == null) {
            throw new CalFacadeException("Unable to get keyfile locations. Is genkeys service installed?");
        }
        pwEncrypt.init(privKeys, pubKeys);
        return pwEncrypt;
    } catch (CalFacadeException cfe) {
        cfe.printStackTrace();
        throw cfe;
    } catch (Throwable t) {
        t.printStackTrace();
        throw new CalFacadeException(t);
    }
}
Also used : GenKeysMBean(org.bedework.util.security.keys.GenKeysMBean) BwString(org.bedework.calfacade.BwString) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 30 with CalFacadeException

use of org.bedework.calfacade.exc.CalFacadeException in project bw-calendar-engine by Bedework.

the class CalSvc method init.

private void init(final CalSvcIPars parsParam, final boolean creating) throws CalFacadeException {
    pars = (CalSvcIPars) parsParam.clone();
    this.creating = creating;
    debug = getLogger().isDebugEnabled();
    final long start = System.currentTimeMillis();
    fixUsers();
    try {
        if (configs == null) {
            // Try again - failed at static init?
            configs = new CalSvcFactoryDefault().getSystemConfig();
        }
        open();
        beginTransaction();
        if (userGroups != null) {
            userGroups.init(getGroupsCallBack(), configs);
        }
        if (adminGroups != null) {
            adminGroups.init(getGroupsCallBack(), configs);
        }
        final SystemProperties sp = getSystemProperties();
        if (tzserverUri == null) {
            tzserverUri = sp.getTzServeruri();
            if (tzserverUri == null) {
                throw new CalFacadeException("No timezones server URI defined");
            }
            Timezones.initTimezones(tzserverUri);
            Timezones.setSystemDefaultTzid(sp.getTzid());
        }
        /* Some checks on parameter validity
       */
        // BwUser =
        tzstore = new TimeZonesStoreImpl(this);
        /* Nominate our timezone registry */
        System.setProperty("net.fortuna.ical4j.timezone.registry", "org.bedework.icalendar.TimeZoneRegistryFactoryImpl");
        if (!creating) {
            final String tzid = getPrefsHandler().get().getDefaultTzid();
            if (tzid != null) {
                Timezones.setThreadDefaultTzid(tzid);
            }
            // if (pars.getCaldav() && !pars.isGuest()) {
            if (!pars.isGuest()) {
            /* Ensure scheduling resources exist */
            // getCal().getSpecialCalendar(getPrincipal(), BwCalendar.calTypeInbox,
            // true, PrivilegeDefs.privAny);
            // getCal().getSpecialCalendar(getPrincipal(), BwCalendar.calTypeOutbox,
            // true, PrivilegeDefs.privAny);
            }
        }
        if ((pars.getPublicAdmin() || pars.getAllowSuperUser()) && (pars.getAuthUser() != null)) {
            ((SvciPrincipalInfo) principalInfo).setSuperUser(getSysparsHandler().isRootUser(principalInfo.getAuthPrincipal()));
        }
        postNotification(SysEvent.makePrincipalEvent(SysEvent.SysCode.USER_SVCINIT, getPrincipal(), System.currentTimeMillis() - start));
    } catch (final CalFacadeException cfe) {
        rollbackTransaction();
        cfe.printStackTrace();
        throw cfe;
    } catch (final Throwable t) {
        rollbackTransaction();
        t.printStackTrace();
        throw new CalFacadeException(t);
    } finally {
        try {
            endTransaction();
        } catch (final Throwable ignored) {
        }
        try {
            close();
        } catch (final Throwable ignored) {
        }
    }
}
Also used : CalSvcFactoryDefault(org.bedework.calsvci.CalSvcFactoryDefault) BasicSystemProperties(org.bedework.calfacade.configs.BasicSystemProperties) SystemProperties(org.bedework.calfacade.configs.SystemProperties) BwString(org.bedework.calfacade.BwString) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)298 BwCalendar (org.bedework.calfacade.BwCalendar)55 BwEvent (org.bedework.calfacade.BwEvent)55 EventInfo (org.bedework.calfacade.svc.EventInfo)37 WebdavException (org.bedework.webdav.servlet.shared.WebdavException)32 ArrayList (java.util.ArrayList)28 BwString (org.bedework.calfacade.BwString)26 BwDateTime (org.bedework.calfacade.BwDateTime)24 IndexException (org.bedework.util.indexing.IndexException)23 BwPrincipal (org.bedework.calfacade.BwPrincipal)22 TreeSet (java.util.TreeSet)19 BwAttendee (org.bedework.calfacade.BwAttendee)18 CalFacadeAccessException (org.bedework.calfacade.exc.CalFacadeAccessException)16 Calendar (net.fortuna.ical4j.model.Calendar)15 DateTime (net.fortuna.ical4j.model.DateTime)15 Period (net.fortuna.ical4j.model.Period)13 BwCategory (org.bedework.calfacade.BwCategory)13 StringReader (java.io.StringReader)12 CoreEventInfo (org.bedework.calcorei.CoreEventInfo)12 BwEventProxy (org.bedework.calfacade.BwEventProxy)12