use of javax.portlet.PortletSession in project uPortal by Jasig.
the class SearchPortletController method performSearch.
@SuppressWarnings("unchecked")
@ActionMapping
public void performSearch(@RequestParam(value = "query") String query, ActionRequest request, ActionResponse response, @RequestParam(value = "ajax", required = false) final boolean ajax) throws IOException {
final PortletSession session = request.getPortletSession();
final String queryId = RandomStringUtils.randomAlphanumeric(32);
if (isTooManyQueries(session, queryId)) {
logger.debug("Rejecting search for '{}', exceeded max queries per minute for user", query);
if (!ajax) {
response.setRenderParameter("hitMaxQueries", Boolean.TRUE.toString());
response.setRenderParameter("query", query);
} else {
// For Ajax return to a nonexistent file to generate the 404 error since it was
// easier for the
// UI to have an error response.
final String contextPath = request.getContextPath();
response.sendRedirect(contextPath + AJAX_MAX_QUERIES_URL);
}
return;
}
// construct a new search query object from the string query
final SearchRequest queryObj = new SearchRequest();
queryObj.setQueryId(queryId);
queryObj.setSearchTerms(query);
setupSearchResultsObjInSession(session, queryId);
if (!isRestSearch(request)) {
/*
* TODO: For autocomplete I wish we didn't have to go through a whole render phase just
* to trigger the events-based features of the portlet, but atm I don't
* see a way around it, since..
*
* - (1) You can only start an event chain in the Action phase; and
* - (2) You can only return JSON in a Resource phase; and
* - (3) An un-redirected Action phase leads to a Render phase, not a
* Resource phase :(
*
* It would be awesome either (first choice) to do Action > Event > Resource,
* or Action > sendRedirect() followed by a Resource request.
*
* As it stands, this implementation will trigger a complete render on
* the portal needlessly.
*/
// send a search query event
response.setEvent(SearchConstants.SEARCH_REQUEST_QNAME, queryObj);
}
logger.debug("Query initiated for queryId {}, query {}", queryId, query);
response.setRenderParameter("queryId", queryId);
response.setRenderParameter("query", query);
}
use of javax.portlet.PortletSession in project uPortal by Jasig.
the class SearchPortletController method showJSONSearchResults.
/**
* Display AJAX autocomplete search results for the last query
*/
@ResourceMapping(value = "retrieveSearchJSONResults")
public ModelAndView showJSONSearchResults(PortletRequest request) {
PortletPreferences prefs = request.getPreferences();
int maxTextLength = Integer.parseInt(prefs.getValue(AUTOCOMPLETE_MAX_TEXT_LENGTH_PREF_NAME, "180"));
final Map<String, Object> model = new HashMap<>();
List<AutocompleteResultsModel> results = new ArrayList<>();
final PortletSession session = request.getPortletSession();
String queryId = (String) session.getAttribute(SEARCH_LAST_QUERY_ID);
if (queryId != null) {
final PortalSearchResults portalSearchResults = this.getPortalSearchResults(request, queryId);
if (portalSearchResults != null) {
final ConcurrentMap<String, List<Tuple<SearchResult, String>>> resultsMap = portalSearchResults.getResults();
results = collateResultsForAutoCompleteResponse(resultsMap, maxTextLength);
}
}
model.put("results", results);
model.put("count", results.size());
return new ModelAndView("json", model);
}
use of javax.portlet.PortletSession in project uPortal by Jasig.
the class PortletSessionAdministrativeRequestListener method administer.
/**
* @see
* org.apache.pluto.spi.optional.AdministrativeRequestListener#administer(javax.portlet.PortletRequest,
* javax.portlet.PortletResponse)
*/
@Override
public void administer(PortletRequest request, PortletResponse response) {
final SessionAction action = this.getAction(request);
final Object[] arguments = this.getArguments(request);
final int scope = this.getScope(request);
// Check the argument count
final int argumentCount = arguments != null ? arguments.length : 0;
if (argumentCount != action.getArgumentCount()) {
throw new IllegalArgumentException("SessionAction " + action + " requires " + action.getArgumentCount() + " arguments but " + argumentCount + " arguments were provided.");
}
// Get the session according to the action
final PortletSession portletSession = request.getPortletSession(action.isRequiresCreation());
switch(action) {
case CLEAR:
{
if (portletSession != null) {
for (final Enumeration<String> attributeNames = (Enumeration<String>) portletSession.getAttributeNames(scope); attributeNames.hasMoreElements(); ) {
final String attributeName = attributeNames.nextElement();
portletSession.removeAttribute(attributeName, scope);
}
}
}
break;
case SET_ATTRIBUTE:
{
final String attributeName = (String) arguments[0];
final Object value = arguments[1];
portletSession.setAttribute(attributeName, value, scope);
}
break;
}
}
use of javax.portlet.PortletSession in project uPortal by Jasig.
the class PortletSessionExpirationManager method onApplicationEvent.
/* (non-Javadoc)
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
*/
@Override
public void onApplicationEvent(HttpSessionDestroyedEvent event) {
final HttpSession session = ((HttpSessionDestroyedEvent) event).getSession();
@SuppressWarnings("unchecked") final Map<String, PortletSession> portletSessions = (Map<String, PortletSession>) session.getAttribute(PORTLET_SESSIONS_MAP);
if (portletSessions == null) {
return;
}
/*
* Since (at least) Tomcat 7.0.47, this method has the potential to
* generate a StackOverflowError because PortletSession.invalidate()
* will trigger another HttpSessionDestroyedEvent, which means this
* method will be called again. I don't know if this behavior is a bug
* in Tomcat or Spring, if this behavior is entirely proper, or if the
* reality somewhere in between.
*
* For the present, let's put a token in the HttpSession (which is
* available from the event object) as soon as we start invalidating it.
* We'll then ignore sessions that already have this token.
*/
if (session.getAttribute(ALREADY_INVALIDATING_SESSION_ATTRIBUTE) != null) {
// We're already invalidating; don't do it again
return;
}
session.setAttribute(ALREADY_INVALIDATING_SESSION_ATTRIBUTE, Boolean.TRUE);
for (final Map.Entry<String, PortletSession> portletSessionEntry : portletSessions.entrySet()) {
final String contextPath = portletSessionEntry.getKey();
final PortletSession portletSession = portletSessionEntry.getValue();
try {
portletSession.invalidate();
} catch (IllegalStateException e) {
this.logger.info("PortletSession with id '" + portletSession.getId() + "' for context '" + contextPath + "' has already been invalidated.");
} catch (Exception e) {
this.logger.warn("Failed to invalidate PortletSession with id '" + portletSession.getId() + "' for context '" + contextPath + "'", e);
}
}
}
use of javax.portlet.PortletSession in project uPortal by Jasig.
the class PortletSessionExpirationManager method onEnd.
/* (non-Javadoc)
* @see org.apache.pluto.spi.optional.PortletInvocationListener#onEnd(org.apache.pluto.spi.optional.PortletInvocationEvent)
*/
@SuppressWarnings("unchecked")
@Override
public void onEnd(PortletInvocationEvent event) {
final PortletRequest portletRequest = event.getPortletRequest();
final PortletSession portletSession = portletRequest.getPortletSession(false);
if (portletSession == null) {
return;
}
final HttpServletRequest portalRequest = this.portalRequestUtils.getPortletHttpRequest(portletRequest);
final HttpSession portalSession = portalRequest.getSession();
if (portalSession != null) {
NonSerializableMapHolder<String, PortletSession> portletSessions;
synchronized (WebUtils.getSessionMutex(portalSession)) {
portletSessions = (NonSerializableMapHolder<String, PortletSession>) portalSession.getAttribute(PORTLET_SESSIONS_MAP);
if (portletSessions == null || !portletSessions.isValid()) {
portletSessions = new NonSerializableMapHolder(new ConcurrentHashMap<String, PortletSession>());
portalSession.setAttribute(PORTLET_SESSIONS_MAP, portletSessions);
}
}
final String contextPath = portletRequest.getContextPath();
portletSessions.put(contextPath, portletSession);
}
}
Aggregations