use of com.zimbra.cs.session.Session in project zm-mailbox by Zimbra.
the class ImapFolder method handleAddedMessages.
@Override
public void handleAddedMessages(int changeId, ImapSession.AddedItems added) {
boolean debug = ZimbraLog.imap.isDebugEnabled();
added.sort();
boolean recent = true;
for (Session s : mailbox.getListeners(Session.Type.IMAP)) {
// added messages are only \Recent if we're the first IMAP session notified about them
ImapSession i4session = (ImapSession) s;
if (i4session == session) {
break;
} else if (i4session.isWritable() && i4session.getFolderId() == folderId) {
recent = false;
break;
}
}
List<Integer> renumber = new ArrayList<Integer>();
if (added.numbered != null) {
// if messages have acceptable UIDs, just add 'em
StringBuilder addlog = debug ? new StringBuilder(" ** adding messages (ntfn) ") : null;
for (ImapMessage i4msg : added.numbered) {
if (cache(i4msg, recent)) {
if (debug) {
addlog.append(i4msg).append(' ');
}
i4msg.setAdded(true);
dirtyMessage(i4msg, changeId);
} else {
renumber.add(i4msg.msgId);
}
}
if (debug) {
ZimbraLog.imap.debug(addlog);
}
}
if (added.unnumbered != null || renumber.size() > 0) {
// a higher UID than the message(s) which were added previously."
if (added.unnumbered != null) {
for (ImapMessage i4msg : added.unnumbered) {
renumber.add(i4msg.msgId);
}
}
try {
ZimbraLog.imap.debug(" ** moved; changing imap uid (ntfn): %s", renumber);
// notification will take care of adding to mailbox
getMailbox().resetImapUid(null, renumber);
} catch (ServiceException e) {
if (debug) {
ZimbraLog.imap.debug(" ** moved; imap uid change failed; msg hidden (ntfn): %s", renumber);
}
}
}
}
use of com.zimbra.cs.session.Session in project zm-mailbox by Zimbra.
the class Auth method doResponse.
private Element doResponse(Element request, AuthToken at, ZimbraSoapContext zsc, Map<String, Object> context, Account acct, boolean csrfSupport, TrustedDeviceToken td, String deviceId) throws ServiceException {
Element response = zsc.createElement(AccountConstants.AUTH_RESPONSE);
at.encodeAuthResp(response, false);
/*
* bug 67078
* also return auth token cookie in http header
*/
HttpServletRequest httpReq = (HttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST);
HttpServletResponse httpResp = (HttpServletResponse) context.get(SoapServlet.SERVLET_RESPONSE);
boolean rememberMe = request.getAttributeBool(AccountConstants.A_PERSIST_AUTH_TOKEN_COOKIE, false);
at.encode(httpResp, false, ZimbraCookie.secureCookie(httpReq), rememberMe);
response.addAttribute(AccountConstants.E_LIFETIME, at.getExpires() - System.currentTimeMillis(), Element.Disposition.CONTENT);
boolean isCorrectHost = Provisioning.onLocalServer(acct);
if (isCorrectHost) {
Session session = updateAuthenticatedAccount(zsc, at, context, true);
if (session != null)
ZimbraSoapContext.encodeSession(response, session.getSessionId(), session.getSessionType());
}
Server localhost = Provisioning.getInstance().getLocalServer();
String referMode = localhost.getAttr(Provisioning.A_zimbraMailReferMode, "wronghost");
// if (!isCorrectHost || LC.zimbra_auth_always_send_refer.booleanValue()) {
if (Provisioning.MAIL_REFER_MODE_ALWAYS.equals(referMode) || (Provisioning.MAIL_REFER_MODE_WRONGHOST.equals(referMode) && !isCorrectHost)) {
response.addAttribute(AccountConstants.E_REFERRAL, acct.getAttr(Provisioning.A_zimbraMailHost), Element.Disposition.CONTENT);
}
Element prefsRequest = request.getOptionalElement(AccountConstants.E_PREFS);
if (prefsRequest != null) {
Element prefsResponse = response.addUniqueElement(AccountConstants.E_PREFS);
GetPrefs.handle(prefsRequest, prefsResponse, acct);
}
Element attrsRequest = request.getOptionalElement(AccountConstants.E_ATTRS);
if (attrsRequest != null) {
Element attrsResponse = response.addUniqueElement(AccountConstants.E_ATTRS);
Set<String> attrList = AttributeManager.getInstance().getAttrsWithFlag(AttributeFlag.accountInfo);
for (Iterator it = attrsRequest.elementIterator(AccountConstants.E_ATTR); it.hasNext(); ) {
Element e = (Element) it.next();
String name = e.getAttribute(AccountConstants.A_NAME);
if (name != null && attrList.contains(name)) {
Object v = acct.getUnicodeMultiAttr(name);
if (v != null) {
ToXML.encodeAttr(attrsResponse, name, v);
}
}
}
}
Element requestedSkinEl = request.getOptionalElement(AccountConstants.E_REQUESTED_SKIN);
String requestedSkin = requestedSkinEl != null ? requestedSkinEl.getText() : null;
String skin = SkinUtil.chooseSkin(acct, requestedSkin);
ZimbraLog.webclient.debug("chooseSkin() returned " + skin);
if (skin != null) {
response.addElement(AccountConstants.E_SKIN).setText(skin);
}
boolean csrfCheckEnabled = false;
if (httpReq.getAttribute(Provisioning.A_zimbraCsrfTokenCheckEnabled) != null) {
csrfCheckEnabled = (Boolean) httpReq.getAttribute(Provisioning.A_zimbraCsrfTokenCheckEnabled);
}
if (csrfSupport && csrfCheckEnabled) {
String accountId = at.getAccountId();
long authTokenExpiration = at.getExpires();
int tokenSalt = (Integer) httpReq.getAttribute(CsrfFilter.CSRF_SALT);
String token = CsrfUtil.generateCsrfToken(accountId, authTokenExpiration, tokenSalt, at);
Element csrfResponse = response.addUniqueElement(HeaderConstants.E_CSRFTOKEN);
csrfResponse.addText(token);
httpResp.setHeader(Constants.CSRF_TOKEN, token);
}
if (td != null) {
td.encode(httpResp, response, ZimbraCookie.secureCookie(httpReq));
}
if (deviceId != null) {
response.addUniqueElement(AccountConstants.E_DEVICE_ID).setText(deviceId);
}
if (acct.isIsMobileGatewayProxyAccount()) {
response.addAttribute(AccountConstants.A_ZMG_PROXY, true);
}
return response;
}
use of com.zimbra.cs.session.Session in project zm-mailbox by Zimbra.
the class GetInfo method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Account account = getRequestedAccount(zsc);
if (!canAccessAccount(zsc, account)) {
throw ServiceException.PERM_DENIED("can not access account");
}
// figure out the subset of data the caller wants (default to all data)
String secstr = request.getAttribute(AccountConstants.A_SECTIONS, null);
Set<Section> sections;
if (secstr != null) {
sections = EnumSet.noneOf(Section.class);
for (String sec : Splitter.on(',').omitEmptyStrings().trimResults().split(secstr)) {
sections.add(Section.lookup(sec));
}
} else {
sections = EnumSet.allOf(Section.class);
}
String rightsStr = request.getAttribute(AccountConstants.A_RIGHTS, null);
Set<Right> rights = null;
if (rightsStr != null) {
RightManager rightMgr = RightManager.getInstance();
rights = Sets.newHashSet();
for (String right : Splitter.on(',').omitEmptyStrings().trimResults().split(rightsStr)) {
rights.add(rightMgr.getUserRight(right));
}
}
Element response = zsc.createElement(AccountConstants.GET_INFO_RESPONSE);
response.addAttribute(AccountConstants.E_VERSION, BuildInfo.FULL_VERSION, Element.Disposition.CONTENT);
response.addAttribute(AccountConstants.E_ID, account.getId(), Element.Disposition.CONTENT);
response.addAttribute(AccountConstants.E_NAME, account.getUnicodeName(), Element.Disposition.CONTENT);
try {
response.addAttribute(AccountConstants.E_CRUMB, zsc.getAuthToken().getCrumb(), Element.Disposition.CONTENT);
} catch (AuthTokenException e) {
// shouldn't happen
ZimbraLog.account.warn("can't generate crumb", e);
}
long lifetime = zsc.getAuthToken().getExpires() - System.currentTimeMillis();
response.addAttribute(AccountConstants.E_LIFETIME, lifetime, Element.Disposition.CONTENT);
Provisioning prov = Provisioning.getInstance();
// bug 53770, return if the request is using a delegated authtoken issued to an admin account
AuthToken authToken = zsc.getAuthToken();
if (authToken.isDelegatedAuth()) {
Account admin = prov.get(AccountBy.id, authToken.getAdminAccountId());
if (admin != null) {
boolean isAdmin = AdminAccessControl.isAdequateAdminAccount(admin);
if (isAdmin) {
response.addAttribute(AccountConstants.E_ADMIN_DELEGATED, true, Element.Disposition.CONTENT);
}
}
}
try {
Server server = prov.getLocalServer();
if (server != null) {
response.addAttribute(AccountConstants.A_DOCUMENT_SIZE_LIMIT, server.getFileUploadMaxSize());
}
Config config = prov.getConfig();
if (config != null) {
long maxAttachSize = config.getMtaMaxMessageSize();
if (maxAttachSize == 0) {
maxAttachSize = -1;
/* means unlimited */
}
response.addAttribute(AccountConstants.A_ATTACHMENT_SIZE_LIMIT, maxAttachSize);
}
} catch (ServiceException e) {
}
if (sections.contains(Section.MBOX) && Provisioning.onLocalServer(account)) {
response.addAttribute(AccountConstants.E_REST, UserServlet.getRestUrl(account), Element.Disposition.CONTENT);
try {
Mailbox mbox = getRequestedMailbox(zsc);
response.addAttribute(AccountConstants.E_QUOTA_USED, mbox.getSize(), Element.Disposition.CONTENT);
Session s = (Session) context.get(SoapEngine.ZIMBRA_SESSION);
if (s instanceof SoapSession) {
// we have a valid session; get the stats on this session
response.addAttribute(AccountConstants.E_PREVIOUS_SESSION, ((SoapSession) s).getPreviousSessionTime(), Element.Disposition.CONTENT);
response.addAttribute(AccountConstants.E_LAST_ACCESS, ((SoapSession) s).getLastWriteAccessTime(), Element.Disposition.CONTENT);
response.addAttribute(AccountConstants.E_RECENT_MSGS, ((SoapSession) s).getRecentMessageCount(), Element.Disposition.CONTENT);
} else {
// we have no session; calculate the stats from the mailbox and the other SOAP sessions
long lastAccess = mbox.getLastSoapAccessTime();
response.addAttribute(AccountConstants.E_PREVIOUS_SESSION, lastAccess, Element.Disposition.CONTENT);
response.addAttribute(AccountConstants.E_LAST_ACCESS, lastAccess, Element.Disposition.CONTENT);
response.addAttribute(AccountConstants.E_RECENT_MSGS, mbox.getRecentMessageCount(), Element.Disposition.CONTENT);
}
} catch (ServiceException e) {
}
}
doCos(account, response);
Map<String, Object> attrMap = account.getUnicodeAttrs();
Locale locale = Provisioning.getInstance().getLocale(account);
if (sections.contains(Section.PREFS)) {
Element prefs = response.addUniqueElement(AccountConstants.E_PREFS);
GetPrefs.doPrefs(account, prefs, attrMap, null);
}
if (sections.contains(Section.ATTRS)) {
Element attrs = response.addUniqueElement(AccountConstants.E_ATTRS);
doAttrs(account, locale.toString(), attrs, attrMap);
}
if (sections.contains(Section.ZIMLETS)) {
Element zimlets = response.addUniqueElement(AccountConstants.E_ZIMLETS);
doZimlets(zimlets, account);
}
if (sections.contains(Section.PROPS)) {
Element props = response.addUniqueElement(AccountConstants.E_PROPERTIES);
doProperties(props, account);
}
if (sections.contains(Section.IDENTS)) {
Element ids = response.addUniqueElement(AccountConstants.E_IDENTITIES);
doIdentities(ids, account);
}
if (sections.contains(Section.SIGS)) {
Element sigs = response.addUniqueElement(AccountConstants.E_SIGNATURES);
doSignatures(sigs, account);
}
if (sections.contains(Section.DSRCS)) {
Element ds = response.addUniqueElement(AccountConstants.E_DATA_SOURCES);
doDataSources(ds, account);
}
if (sections.contains(Section.CHILDREN)) {
Element ca = response.addUniqueElement(AccountConstants.E_CHILD_ACCOUNTS);
doChildAccounts(ca, account, zsc.getAuthToken());
}
if (rights != null && !rights.isEmpty()) {
Element eRights = response.addUniqueElement(AccountConstants.E_RIGHTS);
doDiscoverRights(eRights, account, rights);
}
GetAccountInfo.addUrls(response, account);
for (GetInfoExt extension : extensions) {
extension.handle(zsc, response);
}
return response;
}
use of com.zimbra.cs.session.Session in project zm-mailbox by Zimbra.
the class Folder method getImapRECENT.
/** Returns the number of messages in the folder that would be considered
* \Recent in an IMAP session. If there is currently a READ-WRITE IMAP
* session open on the folder, by definition all other IMAP connections
* will see no \Recent messages. <i>(Note that as such, this method
* should <u>not</u> be called by IMAP sessions that have this folder
* selected.)</i> Otherwise, it is the number of messages/chats/contacts
* added to the folder, moved to the folder, or edited in the folder
* since the last such IMAP session. */
int getImapRECENT() throws ServiceException {
// no contents means no \Recent items (duh)
if (getSize() == 0) {
return 0;
}
// if there's a READ-WRITE IMAP session active on the folder, by definition there are no \Recent messages
for (Session s : mMailbox.getListeners(Session.Type.IMAP)) {
ImapSession i4session = (ImapSession) s;
if (i4session.getFolderId() == mId && i4session.isWritable()) {
return 0;
}
}
// if no active sessions, use a cached value if possible
if (imapRECENT >= 0) {
return imapRECENT;
}
// final option is to calculate the number of \Recent messages
markItemModified(Change.SIZE);
imapRECENT = DbMailItem.countImapRecent(this, getImapRECENTCutoff());
return imapRECENT;
}
use of com.zimbra.cs.session.Session in project zm-mailbox by Zimbra.
the class ImapSessionManager method duplicateExistingSession.
private static List<ImapMessage> duplicateExistingSession(int folderId, List<Session> sessionList) {
for (Session session : sessionList) {
ImapSession i4listener = (ImapSession) session;
if (i4listener.getFolderId() == folderId) {
// FIXME: may want to prefer loaded folders over paged-out folders
synchronized (i4listener) {
ImapFolder i4selected;
try {
i4selected = i4listener.getImapFolder();
} catch (ImapSessionClosedException e) {
return null;
}
if (i4selected == null) {
// cache miss
return null;
}
// found a matching session, so just copy its contents!
ZimbraLog.imap.info("copying message data from existing session: %s", i4listener.getPath());
final List<ImapMessage> i4list = new ArrayList<ImapMessage>(i4selected.getSize());
i4selected.traverse(new Function<ImapMessage, Void>() {
@Override
public Void apply(ImapMessage i4msg) {
if (!i4msg.isExpunged()) {
i4list.add(new ImapMessage(i4msg));
}
return null;
}
});
// XXX: watch out for deadlock between this and the SessionCache
if (!i4listener.isInteractive()) {
i4listener.unregister();
}
return i4list;
}
}
}
return null;
}
Aggregations