package ch.softappeal.yass.tutorial.server;

import ch.softappeal.yass.core.Interceptor;
import ch.softappeal.yass.core.remote.Request;
import ch.softappeal.yass.core.remote.Server;
import ch.softappeal.yass.serialize.Reader;
import ch.softappeal.yass.serialize.Serializer;
import ch.softappeal.yass.serialize.Writer;
import ch.softappeal.yass.tutorial.contract.Config;
import ch.softappeal.yass.util.Exceptions;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class XhrServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private static void invoke(final Server server, final Serializer serializer, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
        serializer.write(
            server.invocation((Request)serializer.read(Reader.create(request.getInputStream()))).invoke(
                Interceptor.DIRECT // note: we could add a http session interceptor here if needed
            ),
            Writer.create(response.getOutputStream())
        );
    }

    @Override protected void doPost(final HttpServletRequest request, final HttpServletResponse response) {
        try {
            invoke(ServerSetup.SERVER, Config.MESSAGE_SERIALIZER, request, response);
        } catch (final Exception e) {
            throw Exceptions.wrap(e);
        }
    }

}

@@ -171,24 +171,87 @@
 ote.Server;%0A
+import ch.softappeal.yass.core.remote.Server.ServerInvocation;%0A
 import ch.so
@@ -802,33 +802,37 @@
 pServletRequest 
-r
+httpR
 equest, final Ht
@@ -841,33 +841,37 @@
 ServletResponse 
-r
+httpR
 esponse) throws 
@@ -894,37 +894,146 @@
     
-serializer.write(%0A           
+final Request request = (Request)serializer.read(Reader.create(httpRequest.getInputStream()));%0A        final ServerInvocation invocation =
  ser
@@ -1051,55 +1051,192 @@
 ion(
-(R
+r
 equest)
-serializer.read(Reader.create(
+;%0A        if (invocation.oneWay) %7B%0A            throw new IllegalArgumentException(%22xhr not allowed for oneway method (serviceId %22 + request.serviceId + %22, methodId %22 + 
 request.
 getI
@@ -1235,35 +1235,24 @@
 est.
-getInputStream()))).invoke(
+methodId + ')');
 %0A   
@@ -1256,24 +1256,26 @@
         
+%7D%0A
         
 Intercep
@@ -1266,16 +1266,64 @@
         
+serializer.write(%0A            invocation.invoke(
 Intercep
@@ -1332,16 +1332,18 @@
 r.DIRECT
+),
  // note
@@ -1403,23 +1403,8 @@
 ded%0A
-            ),%0A
     
@@ -1425,17 +1425,21 @@
 .create(
-r
+httpR
 esponse.

check for illegal oneway xhr method call added