title: Error code 706 when signing PDF using Web Agent in Java
body: <p>When testing the Web Agent sample in Java, I am getting an error reply</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="error"&gt;
    &lt;Error&gt;
      &lt;returnCode&gt;706&lt;/returnCode&gt;
      &lt;errorMessage&gt;Value cannot be null.
  Parameter name: s&lt;/errorMessage&gt;
    &lt;/Error&gt;
  &lt;/response&gt;
  </code></pre>
  
  <p>I followed the Ruby example in the CoSign Web Agent samples and the <a href="http://docs.arx.com/CoSign_APIs/doc_v7.1/Default.htm#doc_7.1/Request%20Format.htm" rel="nofollow">documentation</a> </p>
  
  <p>I have used the <strong>demo.pdf</strong> file provided in the sample.</p>
  
  <p>This is the XML (from <a href="http://webagentdev.arx.com:88/testApp/" rel="nofollow">test app</a>) sent in the POST request (the <code>&lt;content&gt;&lt;/content&gt;</code> has the Base64 encoded PDF, but omitted because of length).</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;request&gt;
    &lt;Logic&gt;
      &lt;allowAdHoc&gt;true&lt;/allowAdHoc&gt;
      &lt;workingMode&gt;pull&lt;/workingMode&gt;
      &lt;enforceReason&gt;false&lt;/enforceReason&gt;
    &lt;/Logic&gt;
    &lt;Url&gt;
      &lt;finishURL&gt;http://localhost:64956/retrieveSignedFile.aspx&lt;/finishURL&gt;
    &lt;/Url&gt;
    &lt;Document&gt;
      &lt;fileID&gt;1234567890&lt;/fileID&gt;
      &lt;contentType&gt;pdf&lt;/contentType&gt;
      &lt;content&gt;{BASE64 encoded pdf content}&lt;/content&gt;
    &lt;/Document&gt;
  &lt;/request&gt;
  </code></pre>
  
  <p>The following is the java code I have used:</p>
  
  <pre><code>public class CoSignTest {
      private static final String INPUT = "D:\\tmp\\demo.pdf";
      private static final String PRECONTENT = "&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\n" +
              "&lt;request&gt;\n" +
              "  &lt;Logic&gt;\n" +
              "    &lt;allowAdHoc&gt;true&lt;/allowAdHoc&gt;\n" +
              "    &lt;workingMode&gt;pull&lt;/workingMode&gt;\n" +
              "    &lt;enforceReason&gt;false&lt;/enforceReason&gt;\n" +
              "  &lt;/Logic&gt;\n" +
              "  &lt;Url&gt;\n" +
              "    &lt;finishURL&gt;http://localhost:64956/retrieveSignedFile.aspx&lt;/finishURL&gt;\n" +
              "  &lt;/Url&gt;\n" +
              "  &lt;Document&gt;\n" +
              "    &lt;fileID&gt;1234567890&lt;/fileID&gt;\n" +
              "    &lt;contentType&gt;pdf&lt;/contentType&gt;\n" +
              "    &lt;content&gt;";
      private static final String POSTCONTENT = "&lt;/content&gt;\n" +
              "  &lt;/Document&gt;\n" +
              "&lt;/request&gt;";
      private static final String POST_URL = "https://webagentdev.arx.com/Sign/UploadFileToSign";
      private static final String PULL_URL = "https://webagentdev.arx.com/Sign/DownloadSignedFileG";
      public static final int TIMEOUT = 300000;
  
      public static void main(String[] args) throws Exception {
          InputStream is = new FileInputStream(INPUT);
          String content = PRECONTENT + new String(Base64.encodeBase64(loadResource(is)), "UTF-8") + POSTCONTENT;
          System.out.println(content);
          String reply = new String(sendDocForProcessing(URLEncoder.encode(content, "UTF-8")));
          System.out.println(reply);
          System.out.println("DONE");
      }
  
      private static String sendDocForProcessing(String content) throws Exception {
          HttpClient client = null;
          HttpMethodBase method = null;
          SimpleHttpConnectionManager mgr = new SimpleHttpConnectionManager();
          String reply = "";
          try {
              mgr.getParams().setConnectionTimeout(TIMEOUT);
              mgr.getParams().setSoTimeout(TIMEOUT);
              client = new HttpClient(mgr);
              method = new PostMethod(POST_URL);
              method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false));
              method.getParams().setParameter("http.socket.timeout", TIMEOUT);
              client.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT);
              client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
              method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
              method.getParams().setParameter("inputXML", content);
              client.executeMethod(method);
              reply = new String(method.getResponseBody());
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              if(method != null) {
                  method.releaseConnection();
              }
              client = null;
              mgr.shutdown();
          }
          if (isSigningSuccessful(reply)) {
              return reply;
          } else {
              throw new Exception("Failed in signing the document. Error: " + reply);
          }
      }
  
      private static boolean isSigningSuccessful(String reply) throws ParserConfigurationException, IOException, SAXException {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.parse(new ByteArrayInputStream(reply.getBytes()));
          Element elem = doc.getDocumentElement();
          String type = elem.getAttribute("type");
          return !"error".equals(type);
      }
  
  
      public static byte[] loadResource(InputStream in) {
          if (in == null) {
              return new byte[0];
          }
          try {
              int indice, tempIndice;
              byte[] tempArr;
              byte[] mainArr = new byte[0];
              byte[] byteArr = new byte[65535];
              for (indice = 0; (indice = in.read(byteArr)) &gt; 0;) {
                  tempIndice = mainArr.length + indice;
                  tempArr = new byte[tempIndice];
                  System.arraycopy(mainArr, 0, tempArr, 0, mainArr.length);
                  System.arraycopy(byteArr, 0, tempArr, mainArr.length, indice);
                  mainArr = tempArr;
              }
              in.close();
              return mainArr;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return new byte[0];
      }
  }
  </code></pre>