// 1. Create the input // 1.1 Create a protocol buffer // 1.2 Create the query params // 2. Create a connection // 3. Do the annotation // This method has two contracts: // 1. It should call the two relevant callbacks // 2. It must not throw an exception /// <summary>Actually try to perform the annotation on the server side.</summary> /// <remarks> /// Actually try to perform the annotation on the server side. /// This is factored out so that we can retry up to 3 times. /// </remarks> /// <param name="annotation">The annotation we need to fill.</param> /// <param name="backend">The backend we are querying against.</param> /// <param name="serverURL">The URL of the server we are hitting.</param> /// <param name="message">The message we are sending the server (don't need to recompute each retry).</param> /// <param name="tries">The number of times we've tried already.</param> private void DoAnnotation(Annotation annotation, StanfordCoreNLPClient.Backend backend, URL serverURL, byte[] message, int tries) { try { // 1. Set up the connection URLConnection connection = serverURL.OpenConnection(); // 1.1 Set authentication if (apiKey != null && apiSecret != null) { string userpass = apiKey + ":" + apiSecret; string basicAuth = "Basic " + Sharpen.Runtime.GetStringForBytes(Base64.GetEncoder().Encode(Sharpen.Runtime.GetBytesForString(userpass))); connection.SetRequestProperty("Authorization", basicAuth); } // 1.2 Set some protocol-independent properties connection.SetDoOutput(true); connection.SetRequestProperty("Content-Type", "application/x-protobuf"); connection.SetRequestProperty("Content-Length", int.ToString(message.Length)); connection.SetRequestProperty("Accept-Charset", "utf-8"); connection.SetRequestProperty("User-Agent", typeof(StanfordCoreNLPClient).FullName); switch (backend.protocol) { case "https": case "http": { // 1.3 Set some protocol-dependent properties ((HttpURLConnection)connection).SetRequestMethod("POST"); break; } default: { throw new InvalidOperationException("Haven't implemented protocol: " + backend.protocol); } } // 2. Annotate // 2.1. Fire off the request connection.Connect(); connection.GetOutputStream().Write(message); connection.GetOutputStream().Flush(); // 2.2 Await a response // -- It might be possible to send more than one message, but we are not going to do that. Annotation response = serializer.Read(connection.GetInputStream()).first; // 2.3. Copy response over to original annotation foreach (Type key in response.KeySet()) { annotation.Set(key, response.Get(key)); } } catch (Exception t) { // 3. We encountered an error -- retry if (tries < 3) { log.Warn(t); DoAnnotation(annotation, backend, serverURL, message, tries + 1); } else { throw new Exception(t); } } }