use of org.apereo.portal.search.SearchRequest in project uPortal by Jasig.
the class DirectoryPortletController method search2.
@EventMapping(SearchConstants.SEARCH_REQUEST_QNAME_STRING)
public void search2(EventRequest request, EventResponse response) {
// get the search query object from the event
Event event = request.getEvent();
SearchRequest query = (SearchRequest) event.getValue();
// search the portal's directory service for people matching the request
final List<IPersonAttributes> people = searchDirectory(query.getSearchTerms(), request);
if (people.size() > 0) {
// transform the list of directory results into our generic search
// response object
final SearchResults results = new SearchResults();
results.setQueryId(query.getQueryId());
results.setWindowId(request.getWindowID());
for (IPersonAttributes person : people) {
final SearchResult result = new SearchResult();
result.setTitle((String) person.getAttributeValue("displayName"));
result.getType().add(directorySearchResultType);
PortletUrl url = new PortletUrl();
url.setType(PortletUrlType.RENDER);
url.setPortletMode("VIEW");
url.setWindowState("maximized");
PortletUrlParameter actionParam = new PortletUrlParameter();
actionParam.setName("action");
actionParam.getValue().add("findByUsername");
url.getParam().add(actionParam);
PortletUrlParameter usernameParam = new PortletUrlParameter();
usernameParam.setName("username");
usernameParam.getValue().add(person.getName());
url.getParam().add(usernameParam);
result.setPortletUrl(url);
results.getSearchResult().add(result);
}
// fire a search response event
response.setEvent(SearchConstants.SEARCH_RESULTS_QNAME, results);
}
}
use of org.apereo.portal.search.SearchRequest in project uPortal by Jasig.
the class SearchPortletController method handleSearchRequest.
/**
* Performs a search of the explicitly configured {@link IPortalSearchService}s. This is done as
* an event handler so that it can run concurrently with the other portlets handling the search
* request
*/
@SuppressWarnings("unchecked")
@EventMapping(SearchConstants.SEARCH_REQUEST_QNAME_STRING)
public void handleSearchRequest(EventRequest request, EventResponse response) {
// UP-3887 Design flaw. Both the searchLauncher portlet instance and the search portlet
// instance receive
// searchRequest and searchResult events because they are in the same portlet code base (to
// share
// autosuggest_handler.jsp and because we have to calculate the search portlet url for the
// ajax call)
// and share the portlet.xml which defines the event handling behavior.
// If this instance is the searchLauncher, ignore the searchResult. The search was submitted
// to the search
// portlet instance.
final String searchLaunchFname = request.getPreferences().getValue(SEARCH_LAUNCH_FNAME, null);
if (searchLaunchFname != null) {
// discarding message");
return;
}
final Event event = request.getEvent();
final SearchRequest searchQuery = (SearchRequest) event.getValue();
// Map used to track searches that have been handled, used so that one search doesn't get
// duplicate results
ConcurrentMap<String, Boolean> searchHandledCache;
final PortletSession session = request.getPortletSession();
synchronized (org.springframework.web.portlet.util.PortletUtils.getSessionMutex(session)) {
searchHandledCache = (ConcurrentMap<String, Boolean>) session.getAttribute(SEARCH_HANDLED_CACHE_NAME, PortletSession.APPLICATION_SCOPE);
if (searchHandledCache == null) {
searchHandledCache = CacheBuilder.newBuilder().maximumSize(20).expireAfterAccess(5, TimeUnit.MINUTES).<String, Boolean>build().asMap();
session.setAttribute(SEARCH_HANDLED_CACHE_NAME, searchHandledCache, PortletSession.APPLICATION_SCOPE);
}
}
final String queryId = searchQuery.getQueryId();
if (searchHandledCache.putIfAbsent(queryId, Boolean.TRUE) != null) {
// Already handled this search request
return;
}
// Create the results
final SearchResults results = new SearchResults();
results.setQueryId(queryId);
results.setWindowId(request.getWindowID());
final List<SearchResult> searchResultList = results.getSearchResult();
// Run the search for each service appending the results
for (IPortalSearchService searchService : searchServices) {
try {
logger.debug("For queryId {}, query '{}', searching search service {}", queryId, searchQuery.getSearchTerms(), searchService.getClass().toString());
final SearchResults serviceResults = searchService.getSearchResults(request, searchQuery);
logger.debug("For queryId {}, obtained {} results from search service {}", queryId, serviceResults.getSearchResult().size(), searchService.getClass().toString());
searchResultList.addAll(serviceResults.getSearchResult());
} catch (Exception e) {
logger.warn(searchService.getClass() + " threw an exception when searching, it will be ignored. " + searchQuery, e);
}
}
// Respond with a results event if results were found
if (!searchResultList.isEmpty()) {
response.setEvent(SearchConstants.SEARCH_RESULTS_QNAME, results);
}
}
use of org.apereo.portal.search.SearchRequest 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
logger.debug("Sending search event: {}", queryObj);
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 org.apereo.portal.search.SearchRequest in project uPortal by Jasig.
the class GoogleCustomSearchServiceTest method testGoogleSearchController.
@Test
public void testGoogleSearchController() throws Exception {
final String json = IOUtils.toString(this.getClass().getResourceAsStream("/org/apereo/portal/portlets/search/google/result.json"));
when(clientHttpRequestFactory.createRequest(new URI("https://www.googleapis.com/customsearch/v1?q=news&key=12345&userIp=128.104.17.46&start=1&cx="), HttpMethod.GET)).thenReturn(clientHttpRequest);
when(clientHttpResponse.getBody()).thenReturn(new ByteArrayInputStream(json.getBytes()));
when(responseHttpHeaders.getContentLength()).thenReturn((long) json.length());
when(portletRequest.getProperty("REMOTE_ADDR")).thenReturn("128.104.17.46");
final SearchRequest query = new SearchRequest();
query.setSearchTerms("news");
final SearchResults results = googleSearchController.getSearchResults(portletRequest, query);
assertNotNull(results);
assertEquals(2, results.getSearchResult().size());
}
Aggregations