This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
SeamDispatchAsync.java
165 lines (143 loc) · 6.75 KB
/
SeamDispatchAsync.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package org.zanata.webtrans.client.rpc;
import net.customware.gwt.dispatch.shared.Action;
import net.customware.gwt.dispatch.shared.Result;
import net.customware.gwt.presenter.client.EventBus;
import org.zanata.webtrans.client.Application;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.resources.RpcMessages;
import org.zanata.webtrans.client.util.JavascriptUtil;
import org.zanata.webtrans.shared.DispatchService;
import org.zanata.webtrans.shared.DispatchServiceAsync;
import org.zanata.webtrans.shared.auth.AuthenticationError;
import org.zanata.webtrans.shared.auth.AuthorizationError;
import org.zanata.webtrans.shared.auth.Identity;
import org.zanata.webtrans.shared.auth.InvalidTokenError;
import org.zanata.webtrans.shared.model.UserWorkspaceContext;
import org.zanata.webtrans.shared.rpc.AbstractWorkspaceAction;
import org.zanata.webtrans.shared.rpc.ExitWorkspaceAction;
import org.zanata.webtrans.shared.rpc.WrappedAction;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.StatusCodeException;
import com.google.inject.Inject;
public class SeamDispatchAsync implements CachingDispatchAsync {
private static final DispatchServiceAsync realService;
static {
realService = GWT.create(DispatchService.class);
}
protected UserWorkspaceContext userWorkspaceContext;
protected Identity identity;
private final RpcMessages messages;
private EventBus eventBus;
@Inject
public SeamDispatchAsync() {
this.messages = GWT.create(RpcMessages.class);
final String endpointURL =
Application.getModuleParentBaseUrl() + "seam/resource/gwt";
((ServiceDefTarget) realService).setServiceEntryPoint(endpointURL);
}
@Override
public <A extends Action<R>, R extends Result> void execute(final A action,
final AsyncCallback<R> callback) {
if (action instanceof AbstractWorkspaceAction<?>) {
AbstractWorkspaceAction<?> wsAction =
(AbstractWorkspaceAction<?>) action;
if (userWorkspaceContext == null || identity == null) {
callback.onFailure(new AuthorizationError(messages
.dispatcherSetupFailed()));
return;
}
wsAction.setEditorClientId(identity.getEditorClientId());
wsAction.setWorkspaceId(userWorkspaceContext.getWorkspaceContext()
.getWorkspaceId());
}
final String sessionId = getSessionId();
realService.execute(new WrappedAction<R>(action, sessionId),
new AbstractAsyncCallback<Result>() {
public void onFailure(final Throwable caught) {
if (caught instanceof com.google.gwt.user.client.rpc.StatusCodeException
&& ((StatusCodeException) caught)
.getStatusCode() == 0) {
if (!(action instanceof ExitWorkspaceAction)) {
eventBus.fireEvent(new NotificationEvent(
NotificationEvent.Severity.Error,
messages.noResponseFromServer()));
}
}
if (caught instanceof AuthenticationError) {
Log.error("Authentication error.", caught);
Application.redirectToLogin();
} else if (caught instanceof InvalidTokenError) {
Log.error("Invalid Token error ("+ sessionId + ")", caught);
Application.redirectToLogin();
} else if (caught instanceof AuthorizationError) {
Log.info("RCP Authorization Error calling "
+ action.getClass() + ": "
+ caught.getMessage());
callback.onFailure(caught);
} else {
callback.onFailure(caught);
}
}
@SuppressWarnings("unchecked")
public void onSuccess(final Result result) {
callback.onSuccess((R) result);
}
});
}
private String getSessionId() {
return JavascriptUtil.getJavascriptValue("zanataSessionId");
}
@Override
public void setUserWorkspaceContext(
UserWorkspaceContext userWorkspaceContext) {
this.userWorkspaceContext = userWorkspaceContext;
}
@Override
public void setIdentity(Identity identity) {
this.identity = identity;
}
@Override
public void setEventBus(EventBus eventBus) {
this.eventBus = eventBus;
}
@Override
public <A extends Action<R>, R extends Result> void rollback(
final A action, final R result, final AsyncCallback<Void> callback) {
if (action instanceof AbstractWorkspaceAction<?>) {
AbstractWorkspaceAction<?> wsAction =
(AbstractWorkspaceAction<?>) action;
if (userWorkspaceContext == null || identity == null) {
callback.onFailure(new AuthorizationError(messages
.dispatcherSetupFailed()));
return;
}
wsAction.setEditorClientId(identity.getEditorClientId());
wsAction.setWorkspaceId(userWorkspaceContext.getWorkspaceContext()
.getWorkspaceId());
}
String sessionId = getSessionId();
realService.rollback(new WrappedAction<R>(action, sessionId), result,
new AsyncCallback<Void>() {
public void onFailure(final Throwable caught) {
if (caught instanceof AuthenticationError) {
Log.error("Authentication error.");
Application.redirectToLogin();
} else if (caught instanceof AuthorizationError) {
Log.info("RCP Authorization Error calling "
+ action.getClass() + ": "
+ caught.getMessage());
callback.onFailure(caught);
} else {
callback.onFailure(caught);
}
}
@Override
public void onSuccess(Void result) {
}
});
}
}