-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
added support for custom actions by allowing katharsis JAX-RS methods to be added to katharsis repositories. Other implementations like Vert.x can follow the same pattern. Methods have to obey some rule to be recognized as either repository or resource actions. katharsis client extended by a getResourceRepository(...) method taking a QuerySpecResourceRepository subclass and returning a proxy that either invokes katharsis or jax-rs. Example available in ActionTest with the SchedulerRepository.
- Loading branch information
Showing
45 changed files
with
1,389 additions
and
325 deletions.
There are no files selected for viewing
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
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
12 changes: 12 additions & 0 deletions
12
katharsis-client/src/main/java/io/katharsis/client/action/ActionStubFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.katharsis.client.action; | ||
|
||
/** | ||
* Used to create stubs for repository interface having action methods. Stub is only used | ||
* to invoke the action, not the jsonapi methods. | ||
*/ | ||
public interface ActionStubFactory { | ||
|
||
void init(ActionStubFactoryContext context); | ||
|
||
<T> T createStub(Class<T> interfaceClass); | ||
} |
12 changes: 12 additions & 0 deletions
12
katharsis-client/src/main/java/io/katharsis/client/action/ActionStubFactoryContext.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.katharsis.client.action; | ||
|
||
import io.katharsis.client.http.HttpAdapter; | ||
import io.katharsis.resource.registry.ServiceUrlProvider; | ||
|
||
public interface ActionStubFactoryContext { | ||
|
||
ServiceUrlProvider getServiceUrlProvider(); | ||
|
||
HttpAdapter getHttpAdapter(); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
katharsis-client/src/main/java/io/katharsis/client/action/JerseyActionStubFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.katharsis.client.action; | ||
|
||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.WebTarget; | ||
|
||
import org.glassfish.jersey.client.proxy.WebResourceFactory; | ||
|
||
import io.katharsis.resource.registry.ServiceUrlProvider; | ||
|
||
public class JerseyActionStubFactory implements ActionStubFactory { | ||
|
||
private Client client; | ||
|
||
private ActionStubFactoryContext context; | ||
|
||
private JerseyActionStubFactory() { | ||
} | ||
|
||
public static JerseyActionStubFactory newInstance() { | ||
return newInstance(ClientBuilder.newClient()); | ||
} | ||
|
||
public static JerseyActionStubFactory newInstance(Client client) { | ||
JerseyActionStubFactory factory = new JerseyActionStubFactory(); | ||
factory.client = client; | ||
return factory; | ||
} | ||
|
||
@Override | ||
public void init(ActionStubFactoryContext context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public <T> T createStub(Class<T> interfaceClass) { | ||
ServiceUrlProvider serviceUrlProvider = context.getServiceUrlProvider(); | ||
String serviceUrl = serviceUrlProvider.getUrl(); | ||
|
||
WebTarget target = client.target(serviceUrl); | ||
return WebResourceFactory.newResource(interfaceClass, target); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
katharsis-client/src/main/java/io/katharsis/client/internal/ClientStubInvocationHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.katharsis.client.internal; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import io.katharsis.client.KatharsisClient; | ||
import io.katharsis.client.QuerySpecResourceRepositoryStub; | ||
import io.katharsis.client.action.ActionStubFactory; | ||
|
||
public class ClientStubInvocationHandler implements InvocationHandler { | ||
|
||
private static final Set<String> REPOSITORY_METHODS = getMethodNames(QuerySpecResourceRepositoryStub.class); | ||
|
||
private QuerySpecResourceRepositoryStub<?, Serializable> repositoryStub; | ||
|
||
private Object actionStub; | ||
|
||
public ClientStubInvocationHandler(QuerySpecResourceRepositoryStub<?, Serializable> repositoryStub, Object actionStub) { | ||
this.repositoryStub = repositoryStub; | ||
this.actionStub = actionStub; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
if (method.getDeclaringClass() == Object.class || REPOSITORY_METHODS.contains(method.getName())) { | ||
// execute repository method | ||
return method.invoke(repositoryStub, args); | ||
} | ||
else if (actionStub != null) { | ||
// execute action | ||
return method.invoke(actionStub, args); | ||
} | ||
else { | ||
throw new IllegalStateException("cannot execute actions, no " + ActionStubFactory.class.getSimpleName() + " set with " | ||
+ KatharsisClient.class.getName()); | ||
} | ||
} | ||
|
||
private static Set<String> getMethodNames(Class<?> clazz) { | ||
Set<String> repositoryMethods = new HashSet<>(); | ||
Method[] repositoryMethodObjects = clazz.getMethods(); | ||
for (Method repositoryMethodObject : repositoryMethodObjects) { | ||
repositoryMethods.add(repositoryMethodObject.getName()); | ||
} | ||
return repositoryMethods; | ||
} | ||
} |
Oops, something went wrong.