Skip to content

Commit

Permalink
Merge pull request #1480 from drewwills/fix-portlet-html-api
Browse files Browse the repository at this point in the history
fix:  update the PortletsRESTController.getRenderedPortlet method (AP…
  • Loading branch information
drewwills committed Jan 3, 2019
2 parents 938a8d4 + ea8bf25 commit 2e808e1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apereo.portal.security.IPerson;
import org.apereo.portal.security.IPersonManager;
import org.apereo.portal.services.AuthorizationServiceFacade;
import org.apereo.portal.url.PortalHttpServletFactoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -60,6 +61,8 @@ public class PortletsRESTController {

@Autowired private IPersonManager personManager;

@Autowired private PortalHttpServletFactoryService servletFactoryService;

@Autowired private IPortletWindowRegistry portletWindowRegistry;

@Autowired private IPortletExecutionManager portletExecutionManager;
Expand Down Expand Up @@ -114,8 +117,7 @@ public ModelAndView getPortlet(
*/
@RequestMapping(value = "/v4-3/portlet/{fname}.html", method = RequestMethod.GET)
public @ResponseBody String getRenderedPortlet(
HttpServletRequest req, HttpServletResponse res, @PathVariable String fname)
throws Exception {
HttpServletRequest req, HttpServletResponse res, @PathVariable String fname) {

// Does the portlet exist in the registry?
final IPortletDefinition portletDef =
Expand All @@ -134,12 +136,18 @@ public ModelAndView getPortlet(

// Proceed...
try {

final PortalHttpServletFactoryService.RequestAndResponseWrapper wrapper =
servletFactoryService.createRequestAndResponseWrapper(req, res);

final IPortletWindow portletWindow =
portletWindowRegistry.getOrCreateDefaultPortletWindow(
req, portletDef.getPortletDefinitionId());
wrapper.getRequest(), portletDef.getPortletDefinitionId());
final String rslt =
portletExecutionManager.getPortletOutput(
portletWindow.getPortletWindowId(), req, res);
portletWindow.getPortletWindowId(),
wrapper.getRequest(),
wrapper.getResponse());
return rslt;
} catch (Exception e) {
logger.error("Failed to render the requested portlet '{}'", fname, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Licensed to Apereo under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright ownership. Apereo
* licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at the
* following location:
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apereo.portal.url;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apereo.portal.user.IUserInstanceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* This <code>Service</code> bean is responsible for creating and properly initializing instances of
* {@link PortalHttpServletRequestWrapper} and {@link PortalHttpServletResponseWrapper}.
*
* @since 5.4
*/
@Service
public class PortalHttpServletFactoryService {

@Autowired private IUrlSyntaxProvider urlSyntaxProvider;

@Autowired private IUserInstanceManager userInstanceManager;

public RequestAndResponseWrapper createRequestAndResponseWrapper(
HttpServletRequest request, HttpServletResponse response) {

final IPortalRequestInfo portalRequestInfo =
urlSyntaxProvider.getPortalRequestInfo(request);

final UrlType urlType = portalRequestInfo.getUrlType();
final UrlState urlState = portalRequestInfo.getUrlState();

final PortalHttpServletResponseWrapper responseWrapper =
new PortalHttpServletResponseWrapper(response);
final PortalHttpServletRequestWrapper requestWrapper =
new PortalHttpServletRequestWrapper(request, responseWrapper, userInstanceManager);

requestWrapper.setHeader(IPortalRequestInfo.URL_TYPE_HEADER, urlType.toString());
requestWrapper.setHeader(IPortalRequestInfo.URL_STATE_HEADER, urlState.toString());

// Hack to make PortalController work in light of
// https://jira.springsource.org/secure/attachment/18283/SPR7346.patch
requestWrapper.setHeader(
IPortalRequestInfo.URL_TYPE_HEADER + "." + urlType, Boolean.TRUE.toString());
requestWrapper.setHeader(
IPortalRequestInfo.URL_STATE_HEADER + "." + urlState, Boolean.TRUE.toString());

return new RequestAndResponseWrapper(requestWrapper, responseWrapper);
}

/*
* Nested Types
*/

public static final class RequestAndResponseWrapper {

final PortalHttpServletRequestWrapper requestWrapper;
final PortalHttpServletResponseWrapper responseWrapper;

/* package-private */ RequestAndResponseWrapper(
PortalHttpServletRequestWrapper requestWrapper,
PortalHttpServletResponseWrapper responseWrapper) {
this.requestWrapper = requestWrapper;
this.responseWrapper = responseWrapper;
}

public PortalHttpServletRequestWrapper getRequest() {
return requestWrapper;
}

public PortalHttpServletResponseWrapper getResponse() {
return responseWrapper;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
@Service("portalRequestUtils")
public class PortalRequestUtilsImpl implements IPortalRequestUtils {

/* (non-Javadoc)
* @see org.apereo.portal.url.IPortalRequestUtils#getOriginalPortalRequest(javax.portlet.PortletRequest)
*/
@Override
public HttpServletRequest getPortletHttpRequest(PortletRequest portletRequest) {
final HttpServletRequest portalRequest =
Expand All @@ -53,9 +50,6 @@ public HttpServletRequest getPortletHttpRequest(PortletRequest portletRequest) {
+ "'");
}

/* (non-Javadoc)
* @see org.apereo.portal.url.IPortalRequestUtils#getOriginalPortalRequest(javax.servlet.http.HttpServletRequest)
*/
@Override
public HttpServletRequest getOriginalPortalRequest(HttpServletRequest portletRequest) {
final HttpServletRequest portalRequest =
Expand Down Expand Up @@ -103,7 +97,7 @@ public HttpServletRequest getOriginalPortalRequest(WebRequest request) {
}

throw new IllegalArgumentException(
"The orginal portal HttpServletRequest is not available from the WebRequest using attribute '"
"The original portal HttpServletRequest is not available from the WebRequest using attribute '"
+ PortalHttpServletRequestWrapper.ATTRIBUTE__HTTP_SERVLET_REQUEST
+ "'");
}
Expand Down Expand Up @@ -140,9 +134,6 @@ public HttpServletResponse getOriginalPortalResponse(HttpServletRequest portletR
+ "'");
}

/* (non-Javadoc)
* @see org.apereo.portal.url.IPortalRequestUtils#getCurrentPortalRequest()
*/
@Override
public HttpServletRequest getCurrentPortalRequest() {
final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.commons.lang.math.NumberUtils;
import org.apereo.portal.security.IPerson;
import org.apereo.portal.security.IPersonManager;
import org.apereo.portal.user.IUserInstanceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -37,7 +36,7 @@ public class UrlCanonicalizingFilter extends OncePerRequestFilter {

private IUrlSyntaxProvider urlSyntaxProvider;
private IPersonManager personManager;
private IUserInstanceManager userInstanceManager;
private PortalHttpServletFactoryService servletFactoryService;
private int maximumRedirects = 5;
private LoginRefUrlEncoder loginRefUrlEncoder;

Expand All @@ -52,8 +51,8 @@ public void setPersonManager(IPersonManager personManager) {
}

@Autowired
public void setUserInstanceManager(IUserInstanceManager userInstanceManager) {
this.userInstanceManager = userInstanceManager;
public void setServletFactoryService(PortalHttpServletFactoryService servletFactoryService) {
this.servletFactoryService = servletFactoryService;
}

@Autowired(required = false)
Expand Down Expand Up @@ -153,32 +152,13 @@ protected void doFilterInternal(
}
}

final IPortalRequestInfo portalRequestInfo =
this.urlSyntaxProvider.getPortalRequestInfo(request);
final UrlType urlType = portalRequestInfo.getUrlType();
final UrlState urlState = portalRequestInfo.getUrlState();

final PortalHttpServletResponseWrapper httpServletResponseWrapper =
new PortalHttpServletResponseWrapper(response);
final PortalHttpServletRequestWrapper httpServletRequestWrapper =
new PortalHttpServletRequestWrapper(
request, httpServletResponseWrapper, this.userInstanceManager);

httpServletRequestWrapper.setHeader(IPortalRequestInfo.URL_TYPE_HEADER, urlType.toString());
httpServletRequestWrapper.setHeader(
IPortalRequestInfo.URL_STATE_HEADER, urlState.toString());

// Hack to make PortalController work in light of
// https://jira.springsource.org/secure/attachment/18283/SPR7346.patch
httpServletRequestWrapper.setHeader(
IPortalRequestInfo.URL_TYPE_HEADER + "." + urlType, Boolean.TRUE.toString());
httpServletRequestWrapper.setHeader(
IPortalRequestInfo.URL_STATE_HEADER + "." + urlState, Boolean.TRUE.toString());

filterChain.doFilter(httpServletRequestWrapper, httpServletResponseWrapper);
final PortalHttpServletFactoryService.RequestAndResponseWrapper wrapper =
servletFactoryService.createRequestAndResponseWrapper(request, response);

filterChain.doFilter(wrapper.getRequest(), wrapper.getResponse());
}

protected void clearRedirectCount(HttpServletRequest request, HttpServletResponse response) {
private void clearRedirectCount(HttpServletRequest request, HttpServletResponse response) {
final Cookie cookie = new Cookie(COOKIE_NAME, "");
cookie.setPath(request.getContextPath());
cookie.setMaxAge(0);
Expand All @@ -187,7 +167,7 @@ protected void clearRedirectCount(HttpServletRequest request, HttpServletRespons
response.addCookie(cookie);
}

protected void setRedirectCount(
private void setRedirectCount(
HttpServletRequest request, HttpServletResponse response, int count) {
final Cookie cookie = new Cookie(COOKIE_NAME, Integer.toString(count));
cookie.setPath(request.getContextPath());
Expand All @@ -197,7 +177,7 @@ protected void setRedirectCount(
response.addCookie(cookie);
}

protected int getRedirectCount(HttpServletRequest request) {
private int getRedirectCount(HttpServletRequest request) {
final Cookie[] cookies = request.getCookies();
if (cookies == null) {
return 0;
Expand Down

0 comments on commit 2e808e1

Please sign in to comment.