Skip to content

Commit

Permalink
Added binding annotation to abstract dispatch module. Both dispatcher…
Browse files Browse the repository at this point in the history
…s work together.
  • Loading branch information
Chris-V committed Apr 7, 2014
1 parent 50614ec commit ef8999f
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package com.gwtplatform.dispatch.client.gin;

import java.lang.annotation.Annotation;

import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.inject.client.binder.GinAnnotatedBindingBuilder;
import com.google.gwt.inject.client.binder.GinLinkedBindingBuilder;
import com.gwtplatform.dispatch.client.DefaultExceptionHandler;
import com.gwtplatform.dispatch.client.DefaultSecurityCookieAccessor;
import com.gwtplatform.dispatch.client.ExceptionHandler;
Expand All @@ -25,7 +29,8 @@
import com.gwtplatform.dispatch.shared.SecurityCookieAccessor;

/**
* This gin module provides provides access to the dispatcher singleton, which is used to make calls to the server. This
* This gin module provides provides access to the dispatcher singleton, which is used to make calls to the server.
* This
* module requires an {@link ExceptionHandler}, a {@link DefaultClientActionHandlerRegistry} and a
* {@link SecurityCookieAccessor}. By default, these will be bound to {@link DefaultExceptionHandler},
* {@link DefaultClientActionHandlerRegistry} and {@link DefaultSecurityCookieAccessor} respectively.
Expand Down Expand Up @@ -69,6 +74,7 @@ public Builder() {
* Specify an alternative exception handler.
*
* @param exceptionHandlerType The {@link ExceptionHandler} class.
*
* @return a {@link Builder} object.
*/
public Builder exceptionHandler(Class<? extends ExceptionHandler> exceptionHandlerType) {
Expand All @@ -80,6 +86,7 @@ public Builder exceptionHandler(Class<? extends ExceptionHandler> exceptionHandl
* Specify an alternate client action handler registry.
*
* @param clientActionHandlerRegistryType A {@link ClientActionHandlerRegistry} class.
*
* @return a {@link Builder} object.
*/
public Builder clientActionHandlerRegistry(
Expand All @@ -92,6 +99,7 @@ public Builder clientActionHandlerRegistry(
* Specify an alternate session accessor.
*
* @param sessionAccessorType The {@link SecurityCookieAccessor} class.
*
* @return a {@link Builder} object.
*/
public Builder sessionAccessor(Class<? extends SecurityCookieAccessor> sessionAccessorType) {
Expand All @@ -107,32 +115,23 @@ public Builder sessionAccessor(Class<? extends SecurityCookieAccessor> sessionAc
public abstract AbstractDispatchAsyncModule build();
}

private static Boolean alreadyBound = false;
private static Class<? extends AbstractDispatchAsyncModule> boundType;

private final Builder builder;
private final Class<? extends Annotation> annotationClass;

protected AbstractDispatchAsyncModule(Builder builder) {
protected AbstractDispatchAsyncModule(
Builder builder,
Class<? extends Annotation> annotationClass) {
this.builder = builder;
this.annotationClass = annotationClass;
}

@Override
protected final void configure() {
if (alreadyBound) {
if (!boundType.equals(getClass())) {
throw new RuntimeException("You are trying to use more than one DispatchAsync implementation. " +
boundType.getName() + " was already installed.");
}
} else {
alreadyBound = true;
boundType = getClass();

bind(ClientActionHandlerRegistry.class).to(builder.clientActionHandlerRegistryType).asEagerSingleton();
bind(ExceptionHandler.class).to(builder.exceptionHandlerType);
bind(SecurityCookieAccessor.class).to(builder.sessionAccessorType);

configureDispatch();
}
bindAnnotated(ClientActionHandlerRegistry.class).to(builder.clientActionHandlerRegistryType).asEagerSingleton();
bindAnnotated(ExceptionHandler.class).to(builder.exceptionHandlerType);
bindAnnotated(SecurityCookieAccessor.class).to(builder.sessionAccessorType);

configureDispatch();
}

/**
Expand All @@ -141,4 +140,14 @@ protected final void configure() {
*/
protected void configureDispatch() {
}

private <T> GinLinkedBindingBuilder<T> bindAnnotated(Class<T> clazz) {
GinAnnotatedBindingBuilder<T> binding = bind(clazz);

if (annotationClass != null) {
return binding.annotatedWith(annotationClass);
}

return binding;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public class DefaultRestDispatchCallFactory implements RestDispatchCallFactory {
private final RestResponseDeserializer restResponseDeserializer;

@Inject
DefaultRestDispatchCallFactory(ExceptionHandler exceptionHandler,
ClientActionHandlerRegistry clientActionHandlerRegistry,
SecurityCookieAccessor securityCookieAccessor,
RestRequestBuilderFactory requestBuilderFactory,
RestResponseDeserializer restResponseDeserializer) {
DefaultRestDispatchCallFactory(
@RestBinding ExceptionHandler exceptionHandler,
@RestBinding ClientActionHandlerRegistry clientActionHandlerRegistry,
@RestBinding SecurityCookieAccessor securityCookieAccessor,
RestRequestBuilderFactory requestBuilderFactory,
RestResponseDeserializer restResponseDeserializer) {
this.exceptionHandler = exceptionHandler;
this.clientActionHandlerRegistry = clientActionHandlerRegistry;
this.securityCookieAccessor = securityCookieAccessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ public class DefaultRestRequestBuilderFactory implements RestRequestBuilderFacto
private final Integer requestTimeoutMs;

@Inject
DefaultRestRequestBuilderFactory(ActionMetadataProvider metadataProvider,
Serialization serialization,
HttpRequestBuilderFactory httpRequestBuilderFactory,
UrlUtils urlUtils,
@GlobalHeaderParams Multimap<HttpMethod, RestParameter> globalHeaderParams,
@GlobalQueryParams Multimap<HttpMethod, RestParameter> globalQueryParams,
@RestApplicationPath String baseUrl,
@XsrfHeaderName String securityHeaderName,
@RequestTimeout Integer requestTimeoutMs) {
DefaultRestRequestBuilderFactory(
ActionMetadataProvider metadataProvider,
Serialization serialization,
HttpRequestBuilderFactory httpRequestBuilderFactory,
UrlUtils urlUtils,
@GlobalHeaderParams Multimap<HttpMethod, RestParameter> globalHeaderParams,
@GlobalQueryParams Multimap<HttpMethod, RestParameter> globalQueryParams,
@RestApplicationPath String baseUrl,
@XsrfHeaderName String securityHeaderName,
@RequestTimeout Integer requestTimeoutMs) {
this.metadataProvider = metadataProvider;
this.serialization = serialization;
this.httpRequestBuilderFactory = httpRequestBuilderFactory;
Expand Down Expand Up @@ -107,7 +108,9 @@ public <A extends RestAction<?>> RequestBuilder build(A action, String securityT
* {@link UrlUtils#encodePathSegment(String)}.
*
* @param value the value to encode.
*
* @return the encoded path parameter.
*
* @throws ActionException if an exception occurred while encoding the path parameter.
* @see #encode(com.gwtplatform.dispatch.rest.shared.RestParameter)
*/
Expand All @@ -120,7 +123,9 @@ protected String encodePathParam(String value) throws ActionException {
* {@link UrlUtils#encodeQueryString(String)}.
*
* @param value the value to encode.
*
* @return the encoded query parameter.
*
* @throws ActionException if an exception occurred while encoding the query parameter.
* @see #encode(com.gwtplatform.dispatch.rest.shared.RestParameter)
*/
Expand All @@ -132,6 +137,7 @@ protected String encodeQueryParam(String value) throws ActionException {
* Verify if the provided <code>bodyType</code> can be serialized.
*
* @param bodyType the parameterized type to verify if it can be serialized.
*
* @return <code>true</code> if <code>bodyType</code> can be serialized, otherwise <code>false</code>.
*/
protected boolean canSerialize(String bodyType) {
Expand All @@ -142,8 +148,9 @@ protected boolean canSerialize(String bodyType) {
* Serialize the given object. We assume {@link #canSerialize(String)} returns <code>true</code> or a runtime
* exception may be thrown.
*
* @param object the object to serialize.
* @param object the object to serialize.
* @param bodyType The parameterized type of the object to serialize.
*
* @return The serialized string.
*/
protected String serialize(Object object, String bodyType) {
Expand Down Expand Up @@ -242,9 +249,9 @@ private String buildQueryString(List<RestParameter> params) throws ActionExcepti

for (RestParameter param : params) {
queryString.append("&")
.append(param.getName())
.append("=")
.append(encodeQueryParam(param.getStringValue()));
.append(param.getName())
.append("=")
.append(encodeQueryParam(param.getStringValue()));
}

if (queryString.length() != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public class DefaultRestResponseDeserializer implements RestResponseDeserializer
private final Serialization serialization;

@Inject
DefaultRestResponseDeserializer(ActionMetadataProvider metadataProvider,
Serialization serialization) {
DefaultRestResponseDeserializer(
ActionMetadataProvider metadataProvider,
Serialization serialization) {
this.metadataProvider = metadataProvider;
this.serialization = serialization;
}
Expand All @@ -53,6 +54,7 @@ public <A extends RestAction<R>, R> R deserialize(A action, Response response) t
* Verify if the provided <code>resultType</code> can be deserialized.
*
* @param resultType the parameterized type to verify if it can be deserialized.
*
* @return <code>true</code> if <code>resultType</code> can be deserialized, <code>false</code> otherwise.
*/
protected boolean canDeserialize(String resultType) {
Expand All @@ -63,8 +65,9 @@ protected boolean canDeserialize(String resultType) {
* Deserializes the json as an object of the <code>resultType</code> type.
*
* @param resultType the parameterized type of the object once deserialized.
* @param json the json to deserialize.
* @param <R> the type of the object once deserialized
* @param json the json to deserialize.
* @param <R> the type of the object once deserialized
*
* @return The deserialized object.
*/
protected <R> R deserializeValue(String resultType, String json) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2014 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.rest.client;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.google.inject.BindingAnnotation;

/**
* Annotation used to inject implementations specific to dispatch-rest of shared components.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface RestBinding {
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public class RestDispatchCall<A extends RestAction<R>, R> extends DispatchCall<A
private final RestRequestBuilderFactory requestBuilderFactory;
private final RestResponseDeserializer restResponseDeserializer;

public RestDispatchCall(ExceptionHandler exceptionHandler,
ClientActionHandlerRegistry clientActionHandlerRegistry,
SecurityCookieAccessor securityCookieAccessor,
RestRequestBuilderFactory requestBuilderFactory,
RestResponseDeserializer restResponseDeserializer,
A action,
AsyncCallback<R> callback) {
public RestDispatchCall(
ExceptionHandler exceptionHandler,
ClientActionHandlerRegistry clientActionHandlerRegistry,
SecurityCookieAccessor securityCookieAccessor,
RestRequestBuilderFactory requestBuilderFactory,
RestResponseDeserializer restResponseDeserializer,
A action,
AsyncCallback<R> callback) {
super(exceptionHandler, clientActionHandlerRegistry, securityCookieAccessor, action, callback);

this.requestBuilderFactory = requestBuilderFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.gwtplatform.dispatch.rest.client.GlobalHeaderParams;
import com.gwtplatform.dispatch.rest.client.GlobalQueryParams;
import com.gwtplatform.dispatch.rest.client.RequestTimeout;
import com.gwtplatform.dispatch.rest.client.RestBinding;
import com.gwtplatform.dispatch.rest.client.RestDispatchAsync;
import com.gwtplatform.dispatch.rest.client.RestDispatchCallFactory;
import com.gwtplatform.dispatch.rest.client.RestRequestBuilderFactory;
Expand Down Expand Up @@ -73,7 +74,7 @@ public RestDispatchAsyncModule() {
}

RestDispatchAsyncModule(RestDispatchAsyncModuleBuilder builder) {
super(builder);
super(builder, RestBinding.class);

this.builder = builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DispatchAsyncModule() {
}

private DispatchAsyncModule(Builder builder) {
super(builder);
super(builder, null);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public class DefaultRpcDispatchCallFactory implements RpcDispatchCallFactory {
private final SecurityCookieAccessor securityCookieAccessor;

@Inject
DefaultRpcDispatchCallFactory(DispatchServiceAsync dispatchService,
ExceptionHandler exceptionHandler,
ClientActionHandlerRegistry clientActionHandlerRegistry,
SecurityCookieAccessor securityCookieAccessor) {
DefaultRpcDispatchCallFactory(
DispatchServiceAsync dispatchService,
@RpcBinding ExceptionHandler exceptionHandler,
@RpcBinding ClientActionHandlerRegistry clientActionHandlerRegistry,
@RpcBinding SecurityCookieAccessor securityCookieAccessor) {
this.dispatchService = dispatchService;
this.exceptionHandler = exceptionHandler;
this.clientActionHandlerRegistry = clientActionHandlerRegistry;
Expand All @@ -48,15 +49,14 @@ public class DefaultRpcDispatchCallFactory implements RpcDispatchCallFactory {

@Override
public <A extends Action<R>, R extends Result> RpcDispatchExecuteCall<A, R> create(A action,
AsyncCallback<R> callback) {
AsyncCallback<R> callback) {
return new RpcDispatchExecuteCall<A, R>(dispatchService, exceptionHandler, clientActionHandlerRegistry,
securityCookieAccessor, action, callback);
}

@Override
public <A extends Action<R>, R extends Result> RpcDispatchUndoCall<A, R> create(A action,
R result,
AsyncCallback<Void> callback) {
R result, AsyncCallback<Void> callback) {
return new RpcDispatchUndoCall<A, R>(dispatchService, exceptionHandler, clientActionHandlerRegistry,
securityCookieAccessor, action, result, callback);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2014 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.rpc.client;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.google.inject.BindingAnnotation;

/**
* Annotation used to inject implementations specific to dispatch-rpc of shared components.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface RpcBinding {
}
Loading

0 comments on commit ef8999f

Please sign in to comment.