PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/uportal-war/src/main/java/org/jasig/portal/portlets/search/portletregistry/PortletRegistrySearchService.java

https://gitlab.com/apachipa/uportal
Java | 142 lines | 88 code | 20 blank | 34 comment | 10 complexity | 5e578edf67ae775084fddeebba39f60c MD5 | raw file
  1. /**
  2. * Licensed to Apereo under one or more contributor license
  3. * agreements. See the NOTICE file distributed with this work
  4. * for additional information regarding copyright ownership.
  5. * Apereo licenses this file to you under the Apache License,
  6. * Version 2.0 (the "License"); you may not use this file
  7. * except in compliance with the License. You may obtain a
  8. * copy of the License at the following location:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jasig.portal.portlets.search.portletregistry;
  20. import java.util.List;
  21. import javax.portlet.PortletRequest;
  22. import javax.servlet.http.HttpServletRequest;
  23. import org.jasig.portal.portlet.PortletUtils;
  24. import org.jasig.portal.portlet.om.IPortletDefinition;
  25. import org.jasig.portal.portlet.om.IPortletWindow;
  26. import org.jasig.portal.portlet.om.IPortletWindowId;
  27. import org.jasig.portal.portlet.registry.IPortletDefinitionRegistry;
  28. import org.jasig.portal.portlet.registry.IPortletWindowRegistry;
  29. import org.jasig.portal.portlets.groupselector.EntityEnum;
  30. import org.jasig.portal.portlets.search.IPortalSearchService;
  31. import org.jasig.portal.search.SearchRequest;
  32. import org.jasig.portal.search.SearchResult;
  33. import org.jasig.portal.search.SearchResults;
  34. import org.jasig.portal.security.IAuthorizationService;
  35. import org.jasig.portal.url.IPortalRequestUtils;
  36. import org.jasig.portal.url.IPortalUrlBuilder;
  37. import org.jasig.portal.url.IPortalUrlProvider;
  38. import org.jasig.portal.url.IPortletUrlBuilder;
  39. import org.jasig.portal.url.UrlType;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.beans.factory.annotation.Value;
  42. public class PortletRegistrySearchService implements IPortalSearchService {
  43. private IPortletDefinitionRegistry portletDefinitionRegistry;
  44. private IPortalUrlProvider portalUrlProvider;
  45. private IPortletWindowRegistry portletWindowRegistry;
  46. private IPortalRequestUtils portalRequestUtils;
  47. private IAuthorizationService authorizationService;
  48. @Value("${org.jasig.portal.portlets.portletRegistry.search.result.type:Portlet List}")
  49. private String searchResultType = "Portlet List";
  50. @Autowired
  51. public void setPortletDefinitionRegistry(IPortletDefinitionRegistry portletDefinitionRegistry) {
  52. this.portletDefinitionRegistry = portletDefinitionRegistry;
  53. }
  54. @Autowired
  55. public void setPortalUrlProvider(IPortalUrlProvider urlProvider) {
  56. this.portalUrlProvider = urlProvider;
  57. }
  58. @Autowired
  59. public void setPortletWindowRegistry(IPortletWindowRegistry portletWindowRegistry) {
  60. this.portletWindowRegistry = portletWindowRegistry;
  61. }
  62. @Autowired
  63. public void setPortalRequestUtils(IPortalRequestUtils portalRequestUtils) {
  64. this.portalRequestUtils = portalRequestUtils;
  65. }
  66. @Autowired
  67. public void setAuthorizationService(IAuthorizationService authorizationService) {
  68. this.authorizationService = authorizationService;
  69. }
  70. @Override
  71. public SearchResults getSearchResults(PortletRequest request,
  72. SearchRequest query) {
  73. final String queryString = query.getSearchTerms().toLowerCase();
  74. final List<IPortletDefinition> portlets = portletDefinitionRegistry.getAllPortletDefinitions();
  75. final HttpServletRequest httpServletRequest = this.portalRequestUtils.getPortletHttpRequest(request);
  76. final SearchResults results = new SearchResults();
  77. for (IPortletDefinition portlet : portlets) {
  78. if (matches(queryString, portlet)) {
  79. final SearchResult result = new SearchResult();
  80. result.setTitle(portlet.getTitle());
  81. result.setSummary(portlet.getDescription());
  82. result.getType().add(searchResultType);
  83. final IPortletWindow portletWindow = this.portletWindowRegistry.getOrCreateDefaultPortletWindowByFname(httpServletRequest, portlet.getFName());
  84. // portletWindow is null if user does not have access to portlet.
  85. // If user does not have browse permission, exclude the portlet.
  86. if (portletWindow != null && authorizationService.canPrincipalBrowse(
  87. authorizationService.newPrincipal(request.getRemoteUser(), EntityEnum.PERSON.getClazz()),
  88. portlet)) {
  89. final IPortletWindowId portletWindowId = portletWindow.getPortletWindowId();
  90. final IPortalUrlBuilder portalUrlBuilder = this.portalUrlProvider.getPortalUrlBuilderByPortletFName(httpServletRequest, portlet.getFName(), UrlType.RENDER);
  91. final IPortletUrlBuilder portletUrlBuilder = portalUrlBuilder.getPortletUrlBuilder(portletWindowId);
  92. portletUrlBuilder.setWindowState(PortletUtils.getWindowState("maximized"));
  93. result.setExternalUrl(portalUrlBuilder.getUrlString());
  94. results.getSearchResult().add(result);
  95. }
  96. }
  97. }
  98. return results;
  99. }
  100. /**
  101. * Performs a case-insensitive comparison of the user's query against
  102. * several important fields from the {@link IPortletDefinition}.
  103. *
  104. * @param query The user's search terms, which seem to be forced lower-case
  105. * @param portlet
  106. * @return
  107. */
  108. protected boolean matches(final String query, final IPortletDefinition portlet) {
  109. /*
  110. * The query parameter is coming in lower case always (even when upper
  111. * or mixed case is entered by the user). We really want a case-
  112. * insensitive comparison here anyway; for safety, we will make certain
  113. * it is insensitive.
  114. */
  115. final String lcQuery = query.toLowerCase();
  116. final boolean titleMatch = portlet.getTitle().toLowerCase().contains(lcQuery);
  117. final boolean nameMatch = portlet.getName().toLowerCase().contains(lcQuery);
  118. final boolean descMatch = portlet.getDescription() != null && portlet.getDescription().toLowerCase().contains(lcQuery);
  119. final boolean fnameMatch = portlet.getFName().toLowerCase().contains(lcQuery);
  120. return titleMatch || nameMatch || descMatch || fnameMatch;
  121. }
  122. }