Справочник Пользователя для Red Hat Web Application Framework 6.1

Скачать
Страница из 230
118
Chapter 10. Kernel Tutorial
msg[1].setRefersTo(anchor);
msg[2] = msg[0].replyTo(from,body);
msg[3] = msg[0].replyTo(from,body);
msg[4] = msg[2].replyTo(from,body);
msg[5] = msg[2].replyTo(from,body);
msg[6] = msg[3].replyTo(from,body);
Note that the two root-level messages (0 and 1) explicitly set the reference to our anchor object, but
the replies do not. This information is automatically transferred to the replies when they are created,
as are the subject and other properties, in the same way as
Message.reply()
.
In addition,
ThreadedMessage.replyTo()
also sets a special sort key that determines the correct
position of the message in the tree. Details of the sort keys are not important, but they are generated
so that all common messages can be retrieved in the correct order using a single order by clause.
A special query provided by the messaging package is used to reconstruct the tree structure shown
above:
Session session = SessionManager.getSession();
DataQuery query = session.retrieveQuery
("com.arsdigita.messaging.getMessageTree");
query.addEqualsFilter("object", anchor);
while (query.next() {
ThreadedMessage msg = new ThreadedMessage
((BigDecimal) query.get("id"));
System.out.println("message " + msg.toString());
}
The
addEqualsFilter
is required to retrieve only those messages that refer to the same
ACSObject
.
10.3. Security Service FAQ
This section addresses common questions developers have when implementing a security service
through the kernel.
1. How do I determine whether the user is logged in?
KernelHelper.getKernelRequestContext(request).getUserContext().isLoggedIn()
returns true if the user is logged in, false otherwise.
2. How do I get the ID for the current user?
KernelHelper.getKernelRequestContext(request).getUserContext().getUserID()
returns the current user’s ID. This method throws an exception if the user is not logged in, so check
with
isLoggedIn()
first. If the calling code needs a
User
object for the current user, it should call
getUser()
instead.
3. How do I get the
User
object for the current user?
KernelHelper.getKernelRequestContext(request).getUserContext().getUser()
re-
turns a
User
object for the current user. This method throws an exception if the user is not logged in,
so check with
isLoggedIn()
first. This method throws DataObjectNotFoundException if the user
doesn’t exist; calling code should catch this exception and handle it appropriately. The
User
object
provided by this method is cached for the current thread, so multiple calls to
getUser()
incur at most
one database hit.