Servlet/PortletResponse supported as a resolvable dependency now (in particular for web controllers)

This feature required support for response exposure on Servlet/PortletRequestAttributes, instead of just in the Servlet/PortletWebRequest subclasses.

Issue: SPR-11795
This commit is contained in:
Juergen Hoeller
2014-05-16 18:06:29 +02:00
parent 5faacd5a3d
commit ea88bc2c81
11 changed files with 138 additions and 67 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -507,7 +507,7 @@ public abstract class FrameworkPortlet extends GenericPortletBean
RequestAttributes previousRequestAttributes = RequestContextHolder.getRequestAttributes();
PortletRequestAttributes requestAttributes = null;
if (previousRequestAttributes == null || previousRequestAttributes.getClass().equals(PortletRequestAttributes.class)) {
requestAttributes = new PortletRequestAttributes(request);
requestAttributes = new PortletRequestAttributes(request, response);
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import java.util.Map;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.PortletSession;
import javax.servlet.ServletContext;
@@ -125,6 +126,7 @@ public abstract class PortletApplicationContextUtils {
}
beanFactory.registerResolvableDependency(PortletRequest.class, new RequestObjectFactory());
beanFactory.registerResolvableDependency(PortletResponse.class, new ResponseObjectFactory());
beanFactory.registerResolvableDependency(PortletSession.class, new SessionObjectFactory());
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
}
@@ -255,6 +257,28 @@ public abstract class PortletApplicationContextUtils {
}
/**
* Factory that exposes the current response object on demand.
*/
@SuppressWarnings("serial")
private static class ResponseObjectFactory implements ObjectFactory<PortletResponse>, Serializable {
@Override
public PortletResponse getObject() {
PortletResponse response = currentRequestAttributes().getResponse();
if (response == null) {
throw new IllegalStateException("Current portlet response not available");
}
return response;
}
@Override
public String toString() {
return "Current PortletResponse";
}
}
/**
* Factory that exposes the current session object on demand.
*/
@@ -281,7 +305,8 @@ public abstract class PortletApplicationContextUtils {
@Override
public WebRequest getObject() {
return new PortletWebRequest(currentRequestAttributes().getRequest());
PortletRequestAttributes requestAttr = currentRequestAttributes();
return new PortletWebRequest(requestAttr.getRequest(), requestAttr.getResponse());
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.web.portlet.context;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.PortletSession;
import org.springframework.util.Assert;
@@ -57,6 +58,8 @@ public class PortletRequestAttributes extends AbstractRequestAttributes {
private final PortletRequest request;
private PortletResponse response;
private volatile PortletSession session;
private final Map<String, Object> sessionAttributesToUpdate = new ConcurrentHashMap<String, Object>(1);
@@ -73,6 +76,16 @@ public class PortletRequestAttributes extends AbstractRequestAttributes {
this.request = request;
}
/**
* Create a new PortletRequestAttributes instance for the given request.
* @param request current portlet request
* @param response current portlet response (for optional exposure)
*/
public PortletRequestAttributes(PortletRequest request, PortletResponse response) {
this(request);
this.response = response;
}
/**
* Exposes the native {@link PortletRequest} that we're wrapping.
@@ -81,6 +94,13 @@ public class PortletRequestAttributes extends AbstractRequestAttributes {
return this.request;
}
/**
* Exposes the native {@link PortletResponse} that we're wrapping (if any).
*/
public final PortletResponse getResponse() {
return this.response;
}
/**
* Exposes the {@link PortletSession} that we're wrapping.
* @param allowCreate whether to allow creation of a new session if none exists yet

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,9 +39,6 @@ import org.springframework.web.portlet.util.PortletUtils;
*/
public class PortletWebRequest extends PortletRequestAttributes implements NativeWebRequest {
private PortletResponse response;
/**
* Create a new PortletWebRequest instance for the given request.
* @param request current portlet request
@@ -56,18 +53,10 @@ public class PortletWebRequest extends PortletRequestAttributes implements Nativ
* @param response current portlet response
*/
public PortletWebRequest(PortletRequest request, PortletResponse response) {
this(request);
this.response = response;
super(request, response);
}
/**
* Exposes the native {@link PortletResponse} that we're wrapping (if any).
*/
public final PortletResponse getResponse() {
return this.response;
}
@Override
public Object getNativeRequest() {
return getRequest();