Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 8e6fc40

Browse files
author
petrbouda
committed
Attempt to fix JERSEY-2506 and BDB 23854380.
- Added Servlet-specific request attribute bridge for web app deployments. Change-Id: I090648bee0a7ec3434764283c2834bdca394d509
1 parent 3fac077 commit 8e6fc40

File tree

14 files changed

+297
-72
lines changed

14 files changed

+297
-72
lines changed

core-server/src/main/java/org/glassfish/jersey/server/spi/ComponentProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -70,7 +70,7 @@ public interface ComponentProvider {
7070
*
7171
* @param locator an HK2 service locator.
7272
*/
73-
public void initialize(final ServiceLocator locator);
73+
void initialize(final ServiceLocator locator);
7474

7575
/**
7676
* Jersey will invoke this method before binding of each component class internally
@@ -84,13 +84,13 @@ public interface ComponentProvider {
8484
* @param providerContracts provider contracts implemented by given component.
8585
* @return true if the component class has been bound by the provider, false otherwise
8686
*/
87-
public boolean bind(final Class<?> component, Set<Class<?>> providerContracts);
87+
boolean bind(final Class<?> component, Set<Class<?>> providerContracts);
8888

8989
/**
9090
* Jersey will invoke this method after all component classes have been bound.
9191
*
9292
* If the component provider wants to do some actions after it has seen all component classes
9393
* registered with the application, this is the right place for the corresponding code.
9494
*/
95-
public void done();
95+
void done();
9696
}

examples/helloworld-spring-webapp/pom.xml

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
55
6-
Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
6+
Copyright (c) 2012-2016 Oracle and/or its affiliates. All rights reserved.
77
88
The contents of this file are subject to the terms of either the GNU
99
General Public License Version 2 only ("GPL") or the Common Development
@@ -40,7 +40,8 @@
4040
holder.
4141
4242
-->
43-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
43+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4445
<modelVersion>4.0.0</modelVersion>
4546

4647
<!-- There is problem to run Spring example on GF - https://java.net/jira/browse/JERSEY-2032
@@ -65,33 +66,40 @@
6566

6667
<dependencies>
6768
<dependency>
68-
<groupId>org.glassfish.jersey.containers</groupId>
69-
<artifactId>jersey-container-servlet</artifactId>
69+
<groupId>org.glassfish.jersey.containers</groupId>
70+
<artifactId>jersey-container-servlet</artifactId>
7071
</dependency>
7172

7273
<dependency>
73-
<groupId>org.springframework</groupId>
74-
<artifactId>spring-web</artifactId>
75-
<version>${spring4.version}</version>
76-
<scope>compile</scope>
74+
<groupId>org.springframework</groupId>
75+
<artifactId>spring-web</artifactId>
76+
<version>${spring4.version}</version>
77+
<scope>compile</scope>
7778
</dependency>
7879

7980
<dependency>
80-
<groupId>commons-logging</groupId>
81-
<artifactId>commons-logging</artifactId>
82-
<version>1.1</version>
83-
<exclusions>
84-
<exclusion>
85-
<groupId>javax.servlet</groupId>
86-
<artifactId>servlet-api</artifactId>
87-
</exclusion>
88-
</exclusions>
81+
<groupId>commons-logging</groupId>
82+
<artifactId>commons-logging</artifactId>
83+
<version>1.1</version>
84+
<exclusions>
85+
<exclusion>
86+
<groupId>javax.servlet</groupId>
87+
<artifactId>servlet-api</artifactId>
88+
</exclusion>
89+
</exclusions>
8990
</dependency>
9091

9192
<dependency>
92-
<groupId>org.glassfish.jersey.ext</groupId>
93-
<artifactId>jersey-spring4</artifactId>
94-
<version>${project.version}</version>
93+
<groupId>org.glassfish.jersey.ext</groupId>
94+
<artifactId>jersey-spring4</artifactId>
95+
<version>${project.version}</version>
96+
</dependency>
97+
98+
<dependency>
99+
<groupId>org.mortbay.jetty</groupId>
100+
<artifactId>servlet-api-2.5</artifactId>
101+
<version>${jetty.servlet.api.25.version}</version>
102+
<scope>provided</scope>
95103
</dependency>
96104

97105
<dependency>
@@ -103,12 +111,10 @@
103111

104112
<build>
105113
<plugins>
114+
<!-- Run the application using "mvn jetty:run" to deploy to Jetty-->
106115
<plugin>
107-
<groupId>org.codehaus.mojo</groupId>
108-
<artifactId>exec-maven-plugin</artifactId>
109-
<configuration>
110-
<mainClass>org.glassfish.jersey.examples.helloworld.jaxrs.App</mainClass>
111-
</configuration>
116+
<groupId>org.mortbay.jetty</groupId>
117+
<artifactId>maven-jetty-plugin</artifactId>
112118
</plugin>
113119
</plugins>
114120
</build>

examples/helloworld-spring-webapp/src/main/java/org/glassfish/jersey/examples/helloworld/spring/GreetingServiceImpl.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -39,14 +39,25 @@
3939
*/
4040
package org.glassfish.jersey.examples.helloworld.spring;
4141

42+
import javax.servlet.http.HttpServletRequest;
43+
44+
import org.springframework.beans.factory.annotation.Autowired;
45+
4246
/**
4347
* Simple {@link GreetingService} implementation to just say hello.
4448
*
4549
* @author Marko Asplund (marko.asplund at yahoo.com)
50+
* @author Marek Potociar (marek.potociar at oracle.com)
4651
*/
4752
public class GreetingServiceImpl implements GreetingService {
53+
54+
@Autowired
55+
private HttpServletRequest servletRequest;
56+
4857
@Override
4958
public String greet(String who) {
50-
return String.format("hello, %s!", who);
59+
final String serverName = servletRequest.getServerName();
60+
return String.format("hello, %s! Greetings from server %s!", who, serverName);
5161
}
52-
}
62+
63+
}

examples/helloworld-spring-webapp/src/main/java/org/glassfish/jersey/examples/helloworld/spring/SpringSingletonResource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -41,6 +41,7 @@
4141

4242
import java.util.concurrent.atomic.AtomicInteger;
4343

44+
import javax.inject.Singleton;
4445
import javax.ws.rs.GET;
4546
import javax.ws.rs.Path;
4647
import javax.ws.rs.Produces;
@@ -60,9 +61,10 @@
6061
*/
6162
@Path("spring-singleton-hello")
6263
@Component
64+
@Singleton
6365
public class SpringSingletonResource {
6466

65-
AtomicInteger counter = new AtomicInteger();
67+
private final AtomicInteger counter = new AtomicInteger();
6668

6769
@Autowired
6870
private GreetingService greetingService;

examples/helloworld-spring-webapp/src/main/resources/applicationContext.xml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
55
6-
Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.
6+
Copyright (c) 2010-2016 Oracle and/or its affiliates. All rights reserved.
77
88
The contents of this file are subject to the terms of either the GNU
99
General Public License Version 2 only ("GPL") or the Common Development
@@ -42,15 +42,13 @@
4242
-->
4343
<beans xmlns="http://www.springframework.org/schema/beans"
4444
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
45-
xmlns:context="http://www.springframework.org/schema/context"
4645
xsi:schemaLocation="http://www.springframework.org/schema/beans
47-
http://www.springframework.org/schema/beans/spring-beans.xsd
48-
http://www.springframework.org/schema/context
49-
http://www.springframework.org/schema/context/spring-context.xsd"
50-
>
46+
http://www.springframework.org/schema/beans/spring-beans.xsd">
5147

5248
<bean id="greetingService" class="org.glassfish.jersey.examples.helloworld.spring.GreetingServiceImpl"/>
5349

50+
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
51+
5452
<bean class="org.glassfish.jersey.examples.helloworld.spring.DateTimeService" scope="request"/>
5553

5654
<bean class="org.glassfish.jersey.examples.helloworld.spring.SpringSingletonResource"/>

ext/spring4/src/main/java/org/glassfish/jersey/server/spring/scope/JaxrsRequestAttributes.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -42,21 +42,22 @@
4242
import javax.ws.rs.container.ContainerRequestContext;
4343

4444
import org.glassfish.jersey.server.spring.LocalizationMessages;
45-
4645
import org.springframework.util.StringUtils;
4746
import org.springframework.web.context.request.AbstractRequestAttributes;
4847

4948
/**
5049
* JAX-RS based Spring RequestAttributes implementation.
5150
*
5251
* @author Marko Asplund (marko.asplund at yahoo.com)
52+
* @author Marek Potociar (marek.potociar at oracle.com)
5353
*/
5454
class JaxrsRequestAttributes extends AbstractRequestAttributes {
5555

5656
private final ContainerRequestContext requestContext;
5757

5858
/**
5959
* Create a new instance.
60+
*
6061
* @param requestContext JAX-RS container request context
6162
*/
6263
public JaxrsRequestAttributes(ContainerRequestContext requestContext) {
@@ -70,7 +71,7 @@ protected void updateAccessedSessionAttributes() {
7071

7172
@Override
7273
public Object getAttribute(String name, int scope) {
73-
return requestContext.getProperty(name);
74+
return requestContext.getProperty(name);
7475
}
7576

7677
@Override
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.server.spring.scope;
41+
42+
import javax.ws.rs.container.ContainerRequestContext;
43+
44+
import javax.servlet.http.HttpServletRequest;
45+
46+
import org.springframework.web.context.request.ServletRequestAttributes;
47+
48+
/**
49+
* JAX-RS based Spring RequestAttributes implementation for Servlet-based applications.
50+
*
51+
* @author Marek Potociar (marek.potociar at oracle.com)
52+
*/
53+
class JaxrsServletRequestAttributes extends ServletRequestAttributes {
54+
55+
private final ContainerRequestContext requestContext;
56+
57+
/**
58+
* Create a new JAX-RS ServletRequestAttributes instance for the given request.
59+
*
60+
* @param request current HTTP request
61+
* @param requestContext JAX-RS request context
62+
*/
63+
public JaxrsServletRequestAttributes(final HttpServletRequest request, final ContainerRequestContext requestContext) {
64+
super(request);
65+
this.requestContext = requestContext;
66+
}
67+
68+
@Override
69+
public Object resolveReference(String key) {
70+
if (REFERENCE_REQUEST.equals(key)) {
71+
return this.requestContext;
72+
} else if (REFERENCE_SESSION.equals(key)) {
73+
return super.getSession(true);
74+
} else {
75+
return null;
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)