|
| 1 | +/* |
| 2 | + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.server.spring.test; |
| 18 | + |
| 19 | +import java.math.BigDecimal; |
| 20 | + |
| 21 | +import javax.ws.rs.Consumes; |
| 22 | +import javax.ws.rs.GET; |
| 23 | +import javax.ws.rs.PUT; |
| 24 | +import javax.ws.rs.Path; |
| 25 | +import javax.ws.rs.PathParam; |
| 26 | +import javax.ws.rs.core.MediaType; |
| 27 | + |
| 28 | +import javax.inject.Inject; |
| 29 | +import javax.inject.Named; |
| 30 | +import javax.servlet.http.HttpServletRequest; |
| 31 | + |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 34 | + |
| 35 | +/** |
| 36 | + * Jersey managed JAX-RS resource for testing jersey-spring. |
| 37 | + * |
| 38 | + * @author Marko Asplund (marko.asplund at yahoo.com) |
| 39 | + */ |
| 40 | +@Path("/jersey/account") |
| 41 | +public class AccountJerseyResource { |
| 42 | + |
| 43 | + @Inject |
| 44 | + @Named("AccountService-singleton") |
| 45 | + private AccountService accountServiceInject; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + @Qualifier("AccountService-singleton") |
| 49 | + private AccountService accountServiceAutowired; |
| 50 | + |
| 51 | + @Inject |
| 52 | + @Named("AccountService-request-1") |
| 53 | + private AccountService accountServiceRequest1; |
| 54 | + |
| 55 | + @Autowired |
| 56 | + @Qualifier("AccountService-request-1") |
| 57 | + private AccountService accountServiceRequest2; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + @Qualifier("AccountService-prototype-1") |
| 61 | + private AccountService accountServicePrototype1; |
| 62 | + |
| 63 | + @Autowired |
| 64 | + @Qualifier("AccountService-prototype-1") |
| 65 | + private AccountService accountServicePrototype2; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private HttpServletRequest httpServletRequest; |
| 69 | + |
| 70 | + @Inject |
| 71 | + private HK2ServiceSingleton hk2Singleton; |
| 72 | + |
| 73 | + @Inject |
| 74 | + private HK2ServiceRequestScoped hk2RequestScoped; |
| 75 | + |
| 76 | + @Inject |
| 77 | + private HK2ServicePerLookup hk2PerLookup; |
| 78 | + |
| 79 | + private String message = "n/a"; |
| 80 | + |
| 81 | + // resource methods for testing resource class scope |
| 82 | + @GET |
| 83 | + @Path("message") |
| 84 | + public String getMessage() { |
| 85 | + return message; |
| 86 | + } |
| 87 | + |
| 88 | + @PUT |
| 89 | + @Path("message") |
| 90 | + @Consumes(MediaType.TEXT_PLAIN) |
| 91 | + public String setMessage(final String message) { |
| 92 | + this.message = message; |
| 93 | + return message; |
| 94 | + } |
| 95 | + |
| 96 | + // JERSEY-2506 FIX VERIFICATION |
| 97 | + @GET |
| 98 | + @Path("server") |
| 99 | + public String verifyServletRequestInjection() { |
| 100 | + return "PASSED: " + httpServletRequest.getServerName(); |
| 101 | + } |
| 102 | + |
| 103 | + @GET |
| 104 | + @Path("singleton/server") |
| 105 | + public String verifyServletRequestInjectionIntoSingleton() { |
| 106 | + return accountServiceInject.verifyServletRequestInjection(); |
| 107 | + } |
| 108 | + |
| 109 | + @GET |
| 110 | + @Path("singleton/autowired/server") |
| 111 | + public String verifyServletRequestInjectionIntoAutowiredSingleton() { |
| 112 | + return accountServiceAutowired.verifyServletRequestInjection(); |
| 113 | + } |
| 114 | + |
| 115 | + @GET |
| 116 | + @Path("request/server") |
| 117 | + public String verifyServletRequestInjectionIntoRequestScopedBean() { |
| 118 | + return accountServiceRequest1.verifyServletRequestInjection(); |
| 119 | + } |
| 120 | + |
| 121 | + @GET |
| 122 | + @Path("prototype/server") |
| 123 | + public String verifyServletRequestInjectionIntoPrototypeScopedBean() { |
| 124 | + return accountServicePrototype1.verifyServletRequestInjection(); |
| 125 | + } |
| 126 | + |
| 127 | + // resource methods for testing singleton scoped beans |
| 128 | + @GET |
| 129 | + @Path("singleton/inject/{accountId}") |
| 130 | + public BigDecimal getAccountBalanceSingletonInject(@PathParam("accountId") final String accountId) { |
| 131 | + return accountServiceInject.getAccountBalance(accountId); |
| 132 | + } |
| 133 | + |
| 134 | + @GET |
| 135 | + @Path("singleton/autowired/{accountId}") |
| 136 | + public BigDecimal getAccountBalanceSingletonAutowired(@PathParam("accountId") final String accountId) { |
| 137 | + return accountServiceAutowired.getAccountBalance(accountId); |
| 138 | + } |
| 139 | + |
| 140 | + @PUT |
| 141 | + @Path("singleton/{accountId}") |
| 142 | + @Consumes(MediaType.TEXT_PLAIN) |
| 143 | + public void setAccountBalanceSingleton(@PathParam("accountId") final String accountId, final String balance) { |
| 144 | + accountServiceInject.setAccountBalance(accountId, new BigDecimal(balance)); |
| 145 | + } |
| 146 | + |
| 147 | + // resource methods for testing request scoped beans |
| 148 | + @PUT |
| 149 | + @Path("request/{accountId}") |
| 150 | + @Consumes(MediaType.TEXT_PLAIN) |
| 151 | + public BigDecimal setAccountBalanceRequest(@PathParam("accountId") final String accountId, final String balance) { |
| 152 | + accountServiceRequest1.setAccountBalance(accountId, new BigDecimal(balance)); |
| 153 | + return accountServiceRequest2.getAccountBalance(accountId); |
| 154 | + } |
| 155 | + |
| 156 | + // resource methods for testing prototype scoped beans |
| 157 | + @PUT |
| 158 | + @Path("prototype/{accountId}") |
| 159 | + @Consumes(MediaType.TEXT_PLAIN) |
| 160 | + public BigDecimal setAccountBalancePrototype(@PathParam("accountId") final String accountId, final String balance) { |
| 161 | + accountServicePrototype1.setAccountBalance(accountId, new BigDecimal(balance)); |
| 162 | + return accountServicePrototype2.getAccountBalance(accountId); |
| 163 | + } |
| 164 | +} |
0 commit comments