Created
April 24, 2019 19:18
-
-
Save psamsotha/ad127d5948e4f5234f037c3fa0a7623d 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
package com.example; | |
import java.io.IOException; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import java.lang.reflect.Method; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.NameBinding; | |
import javax.ws.rs.container.ContainerRequestContext; | |
import javax.ws.rs.container.ContainerResponseContext; | |
import javax.ws.rs.container.ContainerResponseFilter; | |
import javax.ws.rs.container.ResourceInfo; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.NewCookie; | |
public class StackOverflow_55711784_2587435 { | |
public interface SomeHelper { | |
NewCookie createCookie(ContainerRequestContext request); | |
} | |
public static class SomeHelperImpl implements SomeHelper { | |
@Override | |
public NewCookie createCookie(ContainerRequestContext request) { | |
return new NewCookie("Hello","World"); | |
} | |
} | |
@NameBinding | |
@Target({ElementType.TYPE, ElementType.METHOD}) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface AddCookie { | |
Class<? extends SomeHelper> value() default SomeHelper.class; | |
} | |
public interface ResourceClass { | |
@GET | |
@AddCookie(SomeHelperImpl.class) | |
String hello(); | |
} | |
@AddCookie() | |
public static class AddCookieFilter implements ContainerResponseFilter { | |
@Context | |
private ResourceInfo info; | |
@Override | |
public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException { | |
AddCookie addCookie = getInterfaceAnnotation(info.getResourceMethod()); | |
if (addCookie != null) { | |
Class<? extends SomeHelper> helperClass = addCookie.value(); | |
try { | |
SomeHelper helper = helperClass.newInstance(); | |
NewCookie cookie = helper.createCookie(request); | |
response.getHeaders().add("New-Cookie", cookie); | |
} catch (InstantiationException | IllegalAccessException e) { | |
e.printStackTrace(); | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
private static AddCookie getInterfaceAnnotation(Method resourceMethod) { | |
String methodName = resourceMethod.getName(); | |
Class<?>[] paramTypes = resourceMethod.getParameterTypes(); | |
Class<?> iface = resourceMethod.getDeclaringClass().getInterfaces()[0]; | |
Method ifaceMethod; | |
try { | |
ifaceMethod = iface.getDeclaredMethod(methodName, paramTypes); | |
} catch (NoSuchMethodException e) { | |
return null; | |
} | |
return ifaceMethod.getAnnotation(AddCookie.class); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment