/// <exception cref="Edu.Stanford.Nlp.Pipeline.WebServiceAnnotator.ShouldRetryException"/> /// <exception cref="Edu.Stanford.Nlp.Pipeline.WebServiceAnnotator.PermanentlyFailedException"/> protected internal override void AnnotateImpl(Annotation ann) { Annotation ann_; // New annotaiton try { // Executes the connection from conn HttpURLConnection conn; conn = (HttpURLConnection) new URL(annotatorEndpoint + "/annotate/").OpenConnection(); conn.SetRequestMethod("POST"); conn.SetDoOutput(true); conn.SetRequestProperty("Content-Type", "application/octet-stream; charset=UTF-8"); using (OutputStream outputStream = conn.GetOutputStream()) { serializer.ToProto(ann).WriteDelimitedTo(outputStream); outputStream.Flush(); } conn.Connect(); try { using (InputStream inputStream = conn.GetInputStream()) { Pair <Annotation, InputStream> pair = serializer.Read(inputStream); ann_ = pair.first; } } catch (Exception e) { throw new WebServiceAnnotator.PermanentlyFailedException(e); } } catch (MalformedURLException e) { throw new WebServiceAnnotator.PermanentlyFailedException(e); } catch (IOException) { throw new WebServiceAnnotator.ShouldRetryException(); } // Copy over annotation. Copy(ann_, ann); }
public static Annotation ReadSerializedProtobufFile(File fileIn) { Annotation annotation; try { ProtobufAnnotationSerializer pas = new ProtobufAnnotationSerializer(); InputStream @is = new BufferedInputStream(new FileInputStream(fileIn)); Pair <Annotation, InputStream> pair = pas.Read(@is); pair.second.Close(); annotation = pair.first; IOUtils.CloseIgnoringExceptions(@is); return(annotation); } catch (Exception e) { throw new Exception(e); } }
// 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); } } }