Last active
December 18, 2016 13:18
-
-
Save psamsotha/c29c438aa27b75fd7a52f65db5cf6eeb to your computer and use it in GitHub Desktop.
This file contains 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.api.ServiceLocator; | |
import org.glassfish.hk2.utilities.binding.AbstractBinder; | |
import org.glassfish.jersey.logging.LoggingFeature; | |
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.Inject; | |
import javax.inject.Singleton; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.ExceptionMapper; | |
import javax.ws.rs.ext.Provider; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import static org.junit.Assert.assertEquals; | |
/** | |
* Run it like any other JUnit test. Only one required dependency | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* <version>2.25</version> | |
* </dependency> | |
* | |
* @author Paul Samsotha | |
*/ | |
public class CustomParamTest extends JerseyTest { | |
@Provider | |
public static class DebugExceptionMapper implements ExceptionMapper<Throwable> { | |
@Override | |
public Response toResponse(Throwable exception) { | |
exception.printStackTrace(); | |
return Response.serverError().build(); | |
} | |
} | |
@Target({ElementType.PARAMETER}) | |
@Retention(RetentionPolicy.RUNTIME) | |
public static @interface MyAnnotation {} | |
static class CustomValue { | |
private String value; | |
CustomValue(String value) { | |
this.value = value; | |
} | |
String get() { | |
return this.value; | |
} | |
} | |
public static class MyAnnotationParamValueProvider implements ValueFactoryProvider { | |
@Inject | |
private ServiceLocator locator; | |
@Override | |
public Factory<?> getValueFactory(Parameter parameter) { | |
if (parameter.getAnnotation(MyAnnotation.class) != null | |
&& parameter.getRawType() == CustomValue.class) { | |
final Factory<CustomValue> factory = new AbstractContainerRequestValueFactory<CustomValue>() { | |
@Override | |
public CustomValue provide() { | |
final ContainerRequest request = getContainerRequest(); | |
final String value = request.getHeaderString("X-Value"); | |
return new CustomValue(value); | |
} | |
}; | |
locator.inject(factory); | |
return factory; | |
} | |
return null; | |
} | |
@Override | |
public PriorityType getPriority() { | |
return Priority.NORMAL; | |
} | |
} | |
@Path("test") | |
public static class TestResource { | |
@GET | |
public String get(@MyAnnotation CustomValue value) { | |
return value.get(); | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig() | |
.register(TestResource.class) | |
.register(LoggingFeature.class) | |
.register(DebugExceptionMapper.class) | |
.register(new AbstractBinder() { | |
@Override | |
protected void configure() { | |
bind(MyAnnotationParamValueProvider.class) | |
.to(ValueFactoryProvider.class) | |
.in(Singleton.class); | |
} | |
}); | |
} | |
@Test | |
public void doit() { | |
final Response response = target("test") | |
.request() | |
.header("X-Value", "Hello World") | |
.get(); | |
assertEquals("Hello World", response.readEntity(String.class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment