GWT: Continue editing the same object after request to server - gwt

Is it possible to continue editing the same object after GWT server request?
Consider best-practices code from another question
void start() {
// Either get p
context1.get(..).to( new Receiver<P> { onSuccess(P resp){p = resp;} ... }).fire();
// OR create p
p = context2.create( P.class );
// Then save p
req = context2.persist(p).to( new Receiver<P>{ /* note do not use context1 */
onViolation(...) { /*JSR 303 handler*/ };
onFailure( error ) { /* handle */ error.getMessage() };
onSuccess(X x) { /* whatever persist() returns handler */ }; } );
// drive editor with p
driver.edit( p, req);
}
....
void onSave() {
// editor
ctxt = driver.flush() /* note ctxt == context2 */
if ( driver.hasErrors() ) { /*JSR 303 handler*/};
// RF
ctxt.fire();
}
The question is, how to handle un-successful server response in the last line? (add receiver to "ctxt.fire();")
void onSave() {
// editor
ctxt = driver.flush() /* note ctxt == context2 */
if ( driver.hasErrors() ) { /*JSR 303 handler*/};
// RF
ctxt.fire(new Receiver<S>{
onSuccess() { ... how to continue editing the "p" object? ... }
onFailure() { ... how to continue editing the "p" object? ... } });
});
}
For example, on save, sever does some additional validation (e.g. that value is unique). And does not accept to save it.
So, server request finishes with "onSuccess(response)" method, but object was not saved (response value may contain list of errors).
Is it possible to allow user to continue editing the unsaved, but updated on client side object and make another request to the server?
The "deadlock" that I see:
It is not possible to reuse request context (ctxt), because "A request is already in progress" exception will be thrown.
It is not possible to create a new context, because all modifications to the object are in the old context (so they will be lost).

Mutable proxies are always bound to a request context. Proxies you receive from the server, however, are frozen and not mutable. The .edit method will make a mutable clone of a frozen proxy for a given request context.
If a request couldn't be fired (connection issues, server error), the context will be re-usable and you can continue editing the proxy. Same applies if the constraints were violated.
If a request was successfully fired (no matter if the server method threw an exception or not), the request context cannot be used no more and the same applies to the proxy.
Have a look at onTransportSuccess in AbstractRequestContext - this will tell you: the only cases in which you can continue using the request context are violation and general failure. So either you enforce a violation or you return (the erroneous) object to the client and continue working on it with a fresh request context (this will lead to issues with entity proxies I'm afraid since it will loose the reference state)

Related

Vert.x: How to wait for a future to complete

Is there a way to wait for a future to complete without blocking the event loop?
An example of a use case with querying Mongo:
Future<Result> dbFut = Future.future();
mongo.findOne("myusers", myQuery, new JsonObject(), res -> {
if(res.succeeded()) {
...
dbFut.complete(res.result());
}
else {
...
dbFut.fail(res.cause());
}
}
});
// Here I need the result of the DB query
if(dbFut.succeeded()) {
doSomethingWith(dbFut.result());
}
else {
error();
}
I know the doSomethingWith(dbFut.result()); can be moved to the handler, yet if it's long, the code will get unreadable (Callback hell ?) It that the right solution ? Is that the omny solution without additional libraries ?
I'm aware that rxJava simplifies the code, but as I don't know it, learning Vert.x and rxJava is just too much.
I also wanted to give a try to vertx-sync. I put the dependency in the pom.xml; everything got downloaded fine but when I started my app, I got the following error
maurice#mickey> java \
-javaagent:~/.m2/repository/co/paralleluniverse/quasar-core/0.7.5/quasar-core-0.7.5-jdk8.jar \
-jar target/app-dev-0.1-fat.jar \
-conf conf/config.json
Error opening zip file or JAR manifest missing : ~/.m2/repository/co/paralleluniverse/quasar-core/0.7.5/quasar-core-0.7.5-jdk8.jar
Error occurred during initialization of VM
agent library failed to init: instrument
I know what the error means in general, but I don't know in that context... I tried to google for it but didn't find any clear explanation about which manifest to put where. And as previously, unless mandatory, I prefer to learn one thing at a time.
So, back to the question : is there a way with "basic" Vert.x to wait for a future without perturbation on the event loop ?
You can set a handler for the future to be executed upon completion or failure:
Future<Result> dbFut = Future.future();
mongo.findOne("myusers", myQuery, new JsonObject(), res -> {
if(res.succeeded()) {
...
dbFut.complete(res.result());
}
else {
...
dbFut.fail(res.cause());
}
}
});
dbFut.setHandler(asyncResult -> {
if(asyncResult.succeeded()) {
// your logic here
}
});
This is a pure Vert.x way that doesn't block the event loop
I agree that you should not block in the Vertx processing pipeline, but I make one exception to that rule: Start-up. By design, I want to block while my HTTP server is initialising.
This code might help you:
/**
* #return null when waiting on {#code Future<Void>}
*/
#Nullable
public static <T>
T awaitComplete(Future<T> f)
throws Throwable
{
final Object lock = new Object();
final AtomicReference<AsyncResult<T>> resultRef = new AtomicReference<>(null);
synchronized (lock)
{
// We *must* be locked before registering a callback.
// If result is ready, the callback is called immediately!
f.onComplete(
(AsyncResult<T> result) ->
{
resultRef.set(result);
synchronized (lock) {
lock.notify();
}
});
do {
// Nested sync on lock is fine. If we get a spurious wake-up before resultRef is set, we need to
// reacquire the lock, then wait again.
// Ref: https://stackoverflow.com/a/249907/257299
synchronized (lock)
{
// #Blocking
lock.wait();
}
}
while (null == resultRef.get());
}
final AsyncResult<T> result = resultRef.get();
#Nullable
final Throwable t = result.cause();
if (null != t) {
throw t;
}
#Nullable
final T x = result.result();
return x;
}

Apama Application route event as a priority

I am writing an Application that can receive 2 types of event coming into the system from separate sources. I want to have a Context to handle each of them. See the code below :
event MyEvent1{
//stuff for context1
}
event MyEvent2{
//stuff for context2
}
event Cascade{
//PRIORITY stuff for context1 & 2
}
monitor Application{
context parallel1 := context("E1processor");
context parallel2 := context("E2processor");
action onload{
spawn handleE1() to parallel1;
spawn handleE2() to parallel2;
on all MyEvent1() as e {
send e to parallel1;
}
on all MyEvent2() as e {
send e to parallel2;
}
}//onload
action handleE1( ){
on all MyEvent1() as e1 {
//do work, create and route CASCADE event
route Cascade();
//I want to do this!
route Cascade() to parallel2; // < ----- ERROR
}
on all Cascade(){
//URGENT stuff
}
}
action handleE2(){
on all MyEvent2() as e1 {
}
on all Cascade(){
//URGENT stuff
}
}
}//Application
My problem lies with the fact that I want to have the Cascade() event pushed to the front of the processing queue because it is a priority. But when I try to do the following:
//do work, create and route CASCADE event
route Cascade(); //<--- Works
//I want to do this!
route Cascade() to parallel2; // < ----- ERROR
It gives me an error - how can I route the event as a priority from one context to the other?
Unfortunately there's no way to priority send to another context. The answer might be more architectural in nature - can the Cascade handling simply be done in the main context for example?

How do I convert a Vert.x Future.compose() to an equivalent RxJava2 implementation

I have a segment of code which looks like:
this.initConfigRetriever() // Produces Future<JsonObject>
.compose(this::asyncLoadDbSchema) // Consumes JsonObject, produces Future<Void>
.compose(v -> this.provisionRouter()) // Produces Future<RouterFactory>
.compose(this::createHttpServer) // Consumes RouterFactory
.compose(s -> startFuture.complete(), startFuture);
And I would like to know how I can convert that into something equivalent in RxJava2? Ideally, I would like something like what Completable does, but with values passed from one to the next:
For example:
this.initConfigRetriever() // Returns a JsonObject
.andThen(this::asyncLoadDbSchema) // Consumes JsonObject and produces a Void
.andThen(this::provisionRouter) // Consume Void and produces RouterFactory
.andThen(this::createHttpServer) // Consumes RouterFactory
.onError(startFuture::fail) // Consumes any errors along the chain
below might be along the lines of what you're looking for.
the flatMap operator allows you to pass values from one stream into the creation of another
errors can be handled by the subscriber
i've used Single off the assumption that this appears to be related to bootstrapping logic that runs once
// Produces Future<JsonObject>
Single.just("...")
.flatMapCompletable {
// Consumes JsonObject, emits "completion" (or ends the stream)
Completable.fromCallable { /* ... */ }
}
.toSingle {
// On complete produces RouterFactory
Single.just("...")
}
.flatMapCompletable {
// Consumes RouterFactory, emits "completion" (or ends the stream)
Completable.fromCallable { /* ... */ }
}
.subscribeBy(
onComplete = {
// Handle completion...
},
onError = { error ->
// Handle errors...
}
)
i hope that helps!
Here's what my structure finally ended up looking like
this.initConfigRetriever() // If it fails, jump to error handler
.flatMap(this::asyncLoadDbSchema) // If it fails, jump to error handler
.flatMap(this::provisionRouter) // If it fails, jump to error handler
.flatMap(this::createHttpServer) // If it fails, jump to error handler
.doOnError(startFuture::fail)
.subscribe(m -> startFuture.complete()); // If all steps succeeded
The way that I accomplished this is that each of the methods being flatMapped return Maybe<T>. This solved my problem pretty cleanly.
An interesting negative side to this is that when I was debugging to figure this out it was quite difficult to figure out where things were going wrong (I had never started consuming the stream). In order to make this easier to debug in IntelliJ, I converted the method references to lambdas so that I could set breakpoints inside of the lambdas.
For details, you can see the code HERE

Socket connection gets closed for no apparent reason

I am trying to implement Facebook X_FACEBOOK_PLATFORM SASL mechanism so I could integrate Facebook Chat to my application over XMPP.
Here is the code:
var ak = "my app id";
var sk = "access token";
var aps = "my app secret";
using (var client = new TcpClient())
{
client.Connect("chat.facebook.com", 5222);
using (var writer = new StreamWriter(client.GetStream())) using (var reader = new StreamReader(client.GetStream()))
{
// Write for the first time
writer.Write("<stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" to=\"chat.facebook.com\"><auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" mechanism=\"X-FACEBOOK-PLATFORM\" /></stream:stream>");
writer.Flush();
Thread.Sleep(500);
// I am pretty sure following works or at least it's not what causes the error
var challenge = Encoding.UTF8.GetString(Convert.FromBase64String(XElement.Parse(reader.ReadToEnd()).Elements().Last().Value)).Split('&').Select(s => s.Split('=')).ToDictionary(s => s[0], s => s[1]);
var response = new SortedDictionary<string, string>() { { "api_key", ak }, { "call_id", DateTime.Now.Ticks.ToString() }, { "method", challenge["method"] }, { "nonce", challenge["nonce"] }, { "session_key", sk }, { "v", "1.0" } };
var responseString1 = string.Format("{0}{1}", string.Join(string.Empty, response.Select(p => string.Format("{0}={1}", p.Key, p.Value)).ToArray()), aps);
byte[] hashedResponse1 = null;
using (var prov = new MD5CryptoServiceProvider()) hashedResponse1 = prov.ComputeHash(Encoding.UTF8.GetBytes(responseString1));
var builder = new StringBuilder();
foreach (var item in hashedResponse1) builder.Append(item.ToString("x2"));
var responseString2 = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}&sig={1}", string.Join("&", response.Select(p => string.Format("{0}={1}", p.Key, p.Value)).ToArray()), builder.ToString().ToLower()))); ;
// Write for the second time
writer.Write(string.Format("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">{0}</response>", responseString2));
writer.Flush();
Thread.Sleep(500);
MessageBox.Show(reader.ReadToEnd());
}
}
I shortened and shrunk the code as much as possible, because I think my SASL implementation (whether it works or not, I haven't had a chance to test it yet) is not what causes the error.
I get the following exception thrown at my face: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
10053
System.Net.Sockets.SocketError.ConnectionAborted
It happens every time I try to read from client's stream for the second time. As you can see i pause a thread here so Facebook server has enough time to answer me, but I used asynchronous approach before and I encountered the exact same thing, so I decided to try it synchronously first. Anyway actual SASL mechanism implementation really shouldn't cause this because if I don't try to authenticate right away, but I send the request to see what mechanisms server uses and select that mechanism in another round of reading and writing, it fails, but when I send mechanism selection XML right away, it works and fails on whatever second I send.
So the conclusion is following: I open the socket connection, write to it, read from it (first read works both sync and async), write to it for the second time and try to read from it for the second time and here it always fails. Clearly then, problem is with socket connection itself. I tried to use new StreamReader for second read but to no avail. This is rather unpleasant since I would really like to implement facade over NetworkStream with "Received" event or something like Send(string data, Action<string> responseProcessor) to get some comfort working with that stream, and I already had the implementation, but it also failed on second read.
Thanks for your suggestions.
Edit: Here is the code of facade over NetworkStream. Same thing happens when using this asynchronous approach, but couple of hours ago it worked, but for second response returned same string as for first. I can't figute out what I changed in a meantime and how.
public void Send(XElement fragment)
{
if (Sent != null) Sent(this, new XmppEventArgs(fragment));
byte[] buffer = new byte[1024];
AsyncCallback callback = null;
callback = (a) =>
{
var available = NetworkStream.EndRead(a);
if (available > 0)
{
StringBuilder.Append(Encoding.UTF8.GetString(buffer, 0, available));
NetworkStream.BeginRead(buffer, 0, buffer.Length, callback, buffer);
}
else
{
var args = new XmppEventArgs(XElement.Parse(StringBuilder.ToString()));
if (Received != null) Received(this, args);
StringBuilder = new StringBuilder();
// NetworkStream.BeginRead(buffer, 0, buffer.Length, callback, buffer);
}
};
NetworkStream.BeginRead(buffer, 0, buffer.Length, callback, buffer);
NetworkStreamWriter.Write(fragment);
NetworkStreamWriter.Flush();
}
The reader.ReadToEnd() call consumes everything until end-of-stream, i.e. until TCP connection is closed.

How to display a generic error page in Asp.Net MVC 2

I have the following in my base controller:
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
// If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
}
Exception exception = filterContext.Exception;
// If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
// ignore it.
if (new HttpException(null, exception).GetHttpCode() != 500)
{
return;
}
// TODO: What is the namespace for ExceptionType?
//if (!ExceptionType.IsInstanceOfType(exception))
//{
// return;
//}
// Send Email
MailException(exception);
// TODO: What does this line do?
base.OnException(filterContext);
filterContext.Result = new ViewResult
{
ViewName = "Error"
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
}
In my Shared folder, I have an Error.aspx View.
Web.config
<customErrors mode="On" />
I am still seeing the yellow screen when an exception occurs. What am I doing incorrectly?
I would imagine that invoking base.OnException handler is what is causing your problem. Without actually looking at the code, I would imagine that it is what is responsible for handling the error and generating a response with the exception and stack trace. Remove that line from your code -- it's not needed as since you're replacing the ViewResult anyway.
I would recommend that you use ELMAH and implement a HandleError attribute that works with it: see this question. ELMAH is very flexible and configuration driven, rather than code driven.
Server.ClearError()
What happens if you call that?