THRIFT-689. java:  Notify client of recoverable protocol errors on java server

When a protocol error occurs, the server will now send back a message containing the exception. Clients should be able to detect and rethrow these exceptions as appropriate.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@916825 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index bb59ab8..949d062 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -2538,7 +2538,25 @@
 
   f_service_ <<
     indent() << argsname << " args = new " << argsname << "();" << endl <<
-    indent() << "args.read(iprot);" << endl <<
+    indent() << "try {" << endl;
+  indent_up();
+  f_service_ <<
+    indent() << "args.read(iprot);" << endl;
+  indent_down();
+  f_service_ << 
+    indent() << "} catch (TProtocolException e) {" << endl;
+  indent_up();
+  f_service_ <<
+    indent() << "iprot.readMessageEnd();" << endl <<
+    indent() << "TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());" << endl <<
+    indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl <<
+    indent() << "x.write(oprot);" << endl <<
+    indent() << "oprot.writeMessageEnd();" << endl <<
+    indent() << "oprot.getTransport().flush();" << endl <<
+    indent() << "return;" << endl;
+  indent_down();
+  f_service_ << indent() << "}" << endl;
+  f_service_ <<
     indent() << "iprot.readMessageEnd();" << endl;
 
   t_struct* xs = tfunction->get_xceptions();
diff --git a/lib/java/src/org/apache/thrift/TApplicationException.java b/lib/java/src/org/apache/thrift/TApplicationException.java
index f840392..c294fc3 100644
--- a/lib/java/src/org/apache/thrift/TApplicationException.java
+++ b/lib/java/src/org/apache/thrift/TApplicationException.java
@@ -44,6 +44,7 @@
   public static final int BAD_SEQUENCE_ID = 4;
   public static final int MISSING_RESULT = 5;
   public static final int INTERNAL_ERROR = 6;
+  public static final int PROTOCOL_ERROR = 7;
 
   protected int type_ = UNKNOWN;