Created
March 2, 2021 12:50
-
-
Save psamsotha/23d74d2d6e67cca184603a23c6dd45a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.glassfish.hk2.api.Factory; | |
import org.glassfish.hk2.utilities.binding.AbstractBinder; | |
import org.glassfish.jersey.server.ContainerRequest; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.server.internal.inject.AbstractContainerRequestValueFactory; | |
import org.glassfish.jersey.server.model.Parameter; | |
import org.glassfish.jersey.server.spi.internal.ValueFactoryProvider; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
import javax.inject.Singleton; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.client.Entity; | |
import javax.ws.rs.core.Feature; | |
import javax.ws.rs.core.FeatureContext; | |
import javax.ws.rs.core.Response; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class ParamInjectTest extends JerseyTest { | |
@Target({ElementType.PARAMETER}) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Auth { | |
} | |
private static class User { | |
private final String username; | |
public User(String username) { | |
this.username = username; | |
} | |
public String getUsername() { | |
return this.username; | |
} | |
} | |
public static class AuthValueFactoryProvider implements ValueFactoryProvider { | |
@Override | |
public Factory<?> getValueFactory(Parameter parameter) { | |
if (parameter.getRawType().equals(User.class) | |
&& parameter.isAnnotationPresent(Auth.class)) { | |
return new UserParamProvider(); | |
} | |
return null; | |
} | |
@Override | |
public PriorityType getPriority() { | |
return Priority.HIGH; | |
} | |
private static class UserParamProvider extends AbstractContainerRequestValueFactory<User> { | |
@Override | |
public User provide() { | |
ContainerRequest request = getContainerRequest(); | |
return new User("Peeskillet"); | |
} | |
} | |
} | |
public static class AuthFeature implements Feature { | |
@Override | |
public boolean configure(FeatureContext context) { | |
context.register(new AbstractBinder() { | |
@Override | |
protected void configure() { | |
bind(AuthValueFactoryProvider.class) | |
.to(ValueFactoryProvider.class) | |
.in(Singleton.class); | |
} | |
}); | |
return true; | |
} | |
} | |
@Path("test") | |
@Consumes("text/plain") | |
public static class TestResource { | |
@POST | |
@Produces("text/plain") | |
public Response post(String text, @Auth User user) { | |
return Response.ok(user.getUsername() + ":" + text).build(); | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig() | |
.register(TestResource.class) | |
.register(AuthFeature.class); | |
} | |
@Test | |
public void testIt() { | |
final Response response = target("test") | |
.request() | |
.post(Entity.text("Test")); | |
assertThat(response.getStatus()).isEqualTo(200); | |
assertThat(response.readEntity(String.class)).isEqualTo("Peeskillet:Test"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment