<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=sbtourist" rel="nofollow">Sergio Bossa</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-4433?redirect=false" rel="nofollow">SPR-4433</a></strong> and commented</p>
<p dir="auto">The protected ServletRequestAttributes#getSession(boolean ) method returns a null session on child threads.<br>
This is wrong because it doesn't get into account what happens in the constructor:</p>
<p dir="auto">[code]<br>
public ServletRequestAttributes(HttpServletRequest request) {<br>
Assert.notNull(request, "Request must not be null");<br>
this.request = request;<br>
// Fetch existing session reference early, to have it available even<br>
// after request completion (for example, in a custom child thread).<br>
this.session = request.getSession(false);<br>
}<br>
[/code]</p>
<p dir="auto">I think that the following code into the getSession method:</p>
<p dir="auto">[code]<br>
this.session = this.request.getSession(allowCreate);<br>
return this.session;<br>
[/code]</p>
<p dir="auto">Should be as follows:</p>
<p dir="auto">[code]<br>
HttpSession newSession = this.request.getSession(allowCreate);<br>
if (newSession != null) {<br>
this.session = newSession;<br>
}<br>
return this.session;<br>
[/code]</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.0.8</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398085356" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9112" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9112/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9112">#9112</a> ServletRequestAttributes wrongly returns a null http session on child threads. (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=sbrannen" rel="nofollow">Sam Brannen</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9619?redirect=false" rel="nofollow">SPR-9619</a></strong> and commented</p>
<h4 dir="auto">Status Quo</h4>
<p dir="auto">Unlike the Spring .NET Expression Language, the Spring Expression Language currently does not provide a means to retrieve unique elements within a collection.</p>
<hr>
<h4 dir="auto">Use Case and Work-Around</h4>
<p dir="auto">With raw collection <em>projection</em>, the resulting collection may well contain duplicates due to the nature of the data. Some use cases, however, may require that the projection contains unique results.</p>
<p dir="auto">To solve this problem within a SpEL expression (in Spring 3.0.x and 3.1.x) we can implement a work-around by adding the projected elements to a <code class="notranslate">Set</code> that will automatically remove any duplicates (assuming the objects stored in the collection properly implement <code class="notranslate">equals()</code>).</p>
<p dir="auto">In the following example we instantiate a <code class="notranslate">HashSet</code>, passing it a list projection, thereby removing duplicates.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="// Book contains: List&lt;Author&gt; authors;
// Author contains: Address address;
// Address contains: String country;

// Book book = ...
EvaluationContext context = new StandardEvaluationContext(book);

ExpressionParser parser = new SpelExpressionParser();

Expression exp = parser.parseExpression(&quot;new java.util.HashSet(authors.![address.country])&quot;);

Set&lt;String&gt; uniqueCountries = (Set&lt;String&gt;) exp.getValue(context);"><pre class="notranslate"><code class="notranslate">// Book contains: List&lt;Author&gt; authors;
// Author contains: Address address;
// Address contains: String country;

// Book book = ...
EvaluationContext context = new StandardEvaluationContext(book);

ExpressionParser parser = new SpelExpressionParser();

Expression exp = parser.parseExpression("new java.util.HashSet(authors.![address.country])");

Set&lt;String&gt; uniqueCountries = (Set&lt;String&gt;) exp.getValue(context);
</code></pre></div>
<hr>
<h4 dir="auto">Deliverables</h4>
<ol dir="auto">
<li>Add a <code class="notranslate">unique()</code> collection processor to SpEL, analogous to the <code class="notranslate">distinct()</code> collection processor from Spring .NET (see <em>Further Resources</em>)</li>
<li>Consider supporting the remaining 'collection processors and aggregators' from .NET</li>
</ol>
<hr>
<h4 dir="auto">Further Resources</h4>
<ul dir="auto">
<li><a href="http://www.springframework.net/doc-latest/reference/html/expressions.html#d4e3398" rel="nofollow">Distinct Processor</a> functionality from the Spring .NET Expression Language</li>
</ul>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0 GA, 3.1.2</p>