Search in sources :

Example 11 with PortletSession

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);
}
Also used : SearchRequest(org.apereo.portal.search.SearchRequest) PortletSession(javax.portlet.PortletSession) ActionMapping(org.springframework.web.portlet.bind.annotation.ActionMapping)

Example 12 with PortletSession

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);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.portlet.ModelAndView) SearchResult(org.apereo.portal.search.SearchResult) PortletSession(javax.portlet.PortletSession) PortletPreferences(javax.portlet.PortletPreferences) List(java.util.List) ArrayList(java.util.ArrayList) ResourceMapping(org.springframework.web.portlet.bind.annotation.ResourceMapping)

Example 13 with PortletSession

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;
    }
}
Also used : Enumeration(java.util.Enumeration) PortletSession(javax.portlet.PortletSession)

Example 14 with PortletSession

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);
        }
    }
}
Also used : PortletSession(javax.portlet.PortletSession) HttpSession(javax.servlet.http.HttpSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HttpSessionDestroyedEvent(org.springframework.security.web.session.HttpSessionDestroyedEvent) IOException(java.io.IOException)

Example 15 with PortletSession

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PortletRequest(javax.portlet.PortletRequest) PortletSession(javax.portlet.PortletSession) HttpSession(javax.servlet.http.HttpSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

PortletSession (javax.portlet.PortletSession)16 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 PortletRequest (javax.portlet.PortletRequest)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpSession (javax.servlet.http.HttpSession)2 IPortletWindowId (org.apereo.portal.portlet.om.IPortletWindowId)2 SearchRequest (org.apereo.portal.search.SearchRequest)2 SearchResult (org.apereo.portal.search.SearchResult)2 Cache (com.google.common.cache.Cache)1 Enumeration (java.util.Enumeration)1 Event (javax.portlet.Event)1 PortletMode (javax.portlet.PortletMode)1 PortletPreferences (javax.portlet.PortletPreferences)1