Skip to content

Commit

Permalink
Merge pull request #348 from ArcBees/cv_dispatch_client_common_2
Browse files Browse the repository at this point in the history
Extracted common code between RPC and REST DispatchAsync
  • Loading branch information
Chris-V committed Nov 23, 2013
2 parents c23e356 + 0c1b236 commit 5a8258c
Show file tree
Hide file tree
Showing 189 changed files with 6,054 additions and 1,950 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.dispatch.rpc.shared.DispatchAsync;
import com.gwtplatform.dispatch.rest.shared.RestDispatch;

/**
* Basic configuration for PresenterWidgetTest, you test must extends this
Expand All @@ -34,5 +34,5 @@ public abstract class PresenterWidgetTestBase {
public RelayingRestDispatcher dispatcher;
@Inject
@Named("mock")
public DispatchAsync mockDispatcher;
public RestDispatch mockDispatcher;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.TypeLiteral;
import com.gwtplatform.dispatch.rpc.client.CompletedDispatchRequest;
import com.gwtplatform.dispatch.rpc.shared.DispatchRequest;
import com.gwtplatform.dispatch.client.CompletedDispatchRequest;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.rest.shared.RestDispatch;
import com.gwtplatform.dispatch.shared.DispatchRequest;

/**
* Class used to replace a real implementation of the @{link RestDispatch}. When executing
Expand Down
5 changes: 0 additions & 5 deletions gwtp-core/gwtp-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@
<artifactId>gwtp-crawler</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gwtp-crawler-service</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<module>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<inherits name="com.google.gwt.inject.Inject"/>

<!-- comma separated list of gin modules, mandatory when using Ginjector generation -->
<define-configuration-property name="gin.ginjector.modules" is-multi-valued="true"/>
Expand Down
19 changes: 19 additions & 0 deletions gwtp-core/gwtp-dispatch-common-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,23 @@
<artifactId>guava-gwt</artifactId>
</dependency>
</dependencies>

<build>
<resources>
<!-- Bundle sources with the jar, so they are visible to GWT's compiler -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</resource>
<!-- Bundle module descriptor with the jar, so it is visible to GWT's compiler -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.gwt.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,63 @@
* the License.
*/

package com.gwtplatform.dispatch.rest.client;
package com.gwtplatform.dispatch.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtplatform.dispatch.client.DelegatingDispatchRequest;
import com.gwtplatform.dispatch.client.actionhandler.ClientActionHandler;
import com.gwtplatform.dispatch.client.actionhandler.ClientActionHandlerMismatchException;
import com.gwtplatform.dispatch.client.actionhandler.ExecuteCommand;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.shared.DispatchRequest;
import com.gwtplatform.dispatch.shared.TypedAction;

class DelegatingAsyncCallback<A extends RestAction<R>, R> implements AsyncCallback<ClientActionHandler<?, ?>>,
public class DelegatingAsyncCallback<A extends TypedAction<R>, R> implements AsyncCallback<ClientActionHandler<?, ?>>,
ExecuteCommand<A, R> {
private final RestDispatchAsync restDispatchAsync;
private final DelegatingDispatchRequest dispatchRequest;
private final DispatchCall dispatchCall;
private final A action;
private final AsyncCallback<R> callback;
private final String securityCookie;
private final DelegatingDispatchRequest dispatchRequest;

DelegatingAsyncCallback(RestDispatchAsync restDispatchAsync,
A action,
AsyncCallback<R> callback,
DelegatingDispatchRequest dispatchRequest,
String securityCookie) {
this.restDispatchAsync = restDispatchAsync;
this.dispatchRequest = dispatchRequest;
public DelegatingAsyncCallback(DispatchCall dispatchCall,
A action,
AsyncCallback<R> callback,
DelegatingDispatchRequest dispatchRequest) {
this.dispatchCall = dispatchCall;
this.action = action;
this.callback = callback;
this.securityCookie = securityCookie;
this.dispatchRequest = dispatchRequest;
}

@SuppressWarnings("unchecked")
@Override
public void onSuccess(ClientActionHandler<?, ?> clientActionHandler) {
if (clientActionHandler.getActionType() != action.getClass()) {
delegateFailure(clientActionHandler);
return;
}

if (dispatchRequest.isPending()) {
} else if (dispatchRequest.isPending()) {
delegateExecute((ClientActionHandler<A, R>) clientActionHandler);
}
}

@Override
public void onFailure(Throwable caught) {
dispatchRequest.cancel();
callback.onFailure(caught);

dispatchCall.onExecuteFailure(caught);
}

@Override
public DispatchRequest execute(A action, AsyncCallback<R> resultCallback) {
if (dispatchRequest.isPending()) {
return restDispatchAsync.doExecute(securityCookie, action, resultCallback);
return dispatchCall.doExecute();
} else {
return null;
}
}

@SuppressWarnings("unchecked")
private void delegateFailure(ClientActionHandler<?, ?> clientActionHandler) {
dispatchRequest.cancel();
ClientActionHandlerMismatchException exception =
new ClientActionHandlerMismatchException(action.getClass(), clientActionHandler.getActionType());

Class<? extends RestAction<?>> requestedActionClass = (Class<? extends RestAction<?>>) action.getClass();
Class<?> supportedActionType = clientActionHandler.getActionType();
callback.onFailure(new ClientActionHandlerMismatchException(requestedActionClass, supportedActionType));
onFailure(exception);
}

private void delegateExecute(ClientActionHandler<A, R> clientActionHandler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Copyright 2013 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.client;

import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtplatform.common.client.IndirectProvider;
import com.gwtplatform.dispatch.client.ExceptionHandler.Status;
import com.gwtplatform.dispatch.client.actionhandler.ClientActionHandler;
import com.gwtplatform.dispatch.client.actionhandler.ClientActionHandlerRegistry;
import com.gwtplatform.dispatch.shared.DispatchRequest;
import com.gwtplatform.dispatch.shared.SecurityCookieAccessor;
import com.gwtplatform.dispatch.shared.TypedAction;

/**
*/
public abstract class DispatchCall<A extends TypedAction<R>, R> {
private final A action;
private final AsyncCallback<R> callback;
private final ClientActionHandlerRegistry clientActionHandlerRegistry;
private final ExceptionHandler exceptionHandler;
private final SecurityCookieAccessor securityCookieAccessor;

private String securityCookie;

public DispatchCall(ExceptionHandler exceptionHandler,
ClientActionHandlerRegistry clientActionHandlerRegistry,
SecurityCookieAccessor securityCookieAccessor,
A action,
AsyncCallback<R> callback) {
this.action = action;
this.callback = callback;
this.exceptionHandler = exceptionHandler;
this.clientActionHandlerRegistry = clientActionHandlerRegistry;
this.securityCookieAccessor = securityCookieAccessor;
}

public DispatchRequest execute() {
securityCookie = securityCookieAccessor.getCookieContent();

IndirectProvider<ClientActionHandler<?, ?>> clientActionHandlerProvider =
clientActionHandlerRegistry.find(action.getClass());

if (clientActionHandlerProvider != null) {
DelegatingDispatchRequest dispatchRequest = new DelegatingDispatchRequest();
DelegatingAsyncCallback<A, R> delegatingCallback =
new DelegatingAsyncCallback<A, R>(this, action, callback, dispatchRequest);

clientActionHandlerProvider.get(delegatingCallback);

return dispatchRequest;
} else {
return doExecute();
}
}

protected abstract DispatchRequest doExecute();

protected A getAction() {
return action;
}

protected AsyncCallback<R> getCallback() {
return callback;
}

protected ClientActionHandlerRegistry getClientActionHandlerRegistry() {
return clientActionHandlerRegistry;
}

protected ExceptionHandler getExceptionHandler() {
return exceptionHandler;
}

protected SecurityCookieAccessor getSecurityCookieAccessor() {
return securityCookieAccessor;
}

protected String getSecurityCookie() {
return securityCookie;
}

protected void onExecuteSuccess(R result) {
callback.onSuccess(result);
}

protected void onExecuteSuccess(R result, Response response) {
onExecuteSuccess(result);
}

protected void onExecuteFailure(Throwable caught) {
if (exceptionHandler != null && exceptionHandler.onFailure(caught) == Status.STOP) {
return;
}

callback.onFailure(caught);
}

protected void onExecuteFailure(Throwable caught, Response response) {
onExecuteFailure(caught);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

/**
* Interface for client side caching.
*
* @author Sunny Gupta
*/
public interface Cache {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<inherits name='com.google.gwt.user.User'/>
<inherits name="com.google.gwt.inject.Inject"/>

<inherits name="com.gwtplatform.dispatch.DispatchCommonShared"/>
<inherits name="com.gwtplatform.common.ClientsCommon"/>
<inherits name="com.google.common.collect.Collect"/>

Expand Down
19 changes: 19 additions & 0 deletions gwtp-core/gwtp-dispatch-common-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,23 @@
<optional>true</optional>
</dependency>
</dependencies>

<build>
<resources>
<!-- Bundle sources with the jar, so they are visible to GWT's compiler -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</resource>
<!-- Bundle module descriptor with the jar, so it is visible to GWT's compiler -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.gwt.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
* These are thrown by
* {@link com.gwtplatform.dispatch.server.Dispatch#execute(Action)} if there is
* a problem executing a particular {@link Action}.
*
* @author David Peterson
*/
public class ActionException extends Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
/**
* This interface provides a common base to give the ability to the client to
* cancel a request or see if a request is still pending.
*
* @author Christian Goudreau
*/
public interface DispatchRequest {
void cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
* <p/>
* See {@link com.gwtplatform.dispatch.client.DefaultSecurityCookieAccessor} for
* details.
*
* @author Philippe Beaudoin
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
* <p/>
* Notice that the default implementation is {@link EmptySecurityCookieAccessor}
* which does not prevent XSRF attacks.
*
* @author David Peterson
* @author Philippe Beaudoin
*/
public interface SecurityCookieAccessor {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2013 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.shared;

public interface TypedAction<R> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"http://google-web-toolkit.googlecode.com/svn/tags/2.4.0/distro-source/core/src/gwt-module.dtd">

<module>
<source path='shared'/>
<source path="shared"/>
</module>
Loading

0 comments on commit 5a8258c

Please sign in to comment.