1

I am having some trouble communicating between my javascript client and java server using Apache Thrift.

These answers does not get me much further. What I would like to know is how to change the tutorial java server in a way that makes it possible to communicate with a javascript client?

What I get from these answers is that I need to make the server communicate using the TJSONProtocol. I have done so with the following code

TServer server = new TSimpleServer(new Args(serverTransport).processor(processor).protocolFactory(new TJSONProtocol.Factory()));

Server code

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import 

org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;

// Generated code
import tutorial.*;
import shared.*;

import java.util.HashMap;

public class JavaServer {

  public static CalculatorHandler handler;
  public static int port = 9092;

  public static Calculator.Processor processor;

  public static void main(String [] args) {
    try {
      handler = new CalculatorHandler();
      processor = new Calculator.Processor(handler);

      Runnable simple = new Runnable() {
        public void run() {
          simple(processor);
        }
      };
      Runnable secure = new Runnable() {
        public void run() {
          secure(processor);
        }
      };

      new Thread(simple).start();
      new Thread(secure).start();
    } catch (Exception x) {
      x.printStackTrace();
    }
  }

  public static void simple(Calculator.Processor processor) {
    try {
      TServerTransport serverTransport = new TServerSocket(port);

      TServer server = new TSimpleServer(new Args(serverTransport).processor(processor).protocolFactory(new TSimpleJSONProtocol.Factory()));

      // Use this for a multithreaded server
      // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));

      System.out.println("Starting the simple server...");
      server.serve();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void secure(Calculator.Processor processor) {
    try {
      /*
       * Use TSSLTransportParameters to setup the required SSL parameters. In this example
       * we are setting the keystore and the keystore password. Other things like algorithms,
       * cipher suites, client auth etc can be set.
       */
      TSSLTransportParameters params = new TSSLTransportParameters();
      // The Keystore contains the private key
      params.setKeyStore("lib/java/test/.keystore", "thrift", null, null);

      /*
       * Use any of the TSSLTransportFactory to get a server transport with the appropriate
       * SSL configuration. You can use the default settings if properties are set in the command line.
       * Ex: -Djavax.net.ssl.keyStore=.keystore and -Djavax.net.ssl.keyStorePassword=thrift
       *
       * Note: You need not explicitly call open(). The underlying server socket is bound on return
       * from the factory class.
       */
      TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(9091, 0, null, params);
      TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));

      // Use this for a multi threaded server
      // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));

      System.out.println("Starting the secure server...");
      server.serve();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Client code

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Hello Thrift</title>
  </head>
  <body>
    Name: <input type="text" id="name_in">
    <input type="button" id="get_msg" value="Get Message" >
    <div id="output"></div>

    <script src="thrift.js"></script>
    <script src="gen-js/SharedService.js"></script>
    <script src="gen-js/shared_types.js"></script>
    <script src="gen-js/tutorial_types.js"></script>
    <script src="gen-js/Calculator.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <script>
      function calc() {
        var transport = new Thrift.Transport("http://localhost:9092");
        var protocol  = new Thrift.TJSONProtocol(transport);
        var client    = new CalculatorClient(protocol);
        var nameElement = document.getElementById("name_in");
        var outputElement = document.getElementById("output");
        document.getElementById("get_msg")
          .addEventListener("click", function(){
            var work = new Work()
            work.num1 = $("#num1").val();
            work.num2 = $("#num2").val();
            work.op = $("#op").val();

            result = client.calculate(1, work);
            outputElement.innerHTML = result;
            });
      };
      calc();
    </script>
  </body>
</html>

When I run the client against the simple server it hangs. When I run against the secure server I get the following error:

Uncaught SyntaxError: Unexpected token  in JSON at position 0
        at JSON.parse (<anonymous>)
        at Thrift.TJSONProtocol.Thrift.Protocol.readMessageBegin (http://localhost:8080/Thrift_1/tutorial1/thrift.js:1029:30)

I get the same error when running a java client from the tutorial against the secure server but works fine when communicating with the simple server.

JensG
  • 13,148
  • 4
  • 45
  • 55

0 Answers0