private static string ExtractRequestParam(IReplicationRequest request, string paramName) { string param = request.QueryParam(paramName); if (param is null) { throw ServletException.Create("Missing mandatory parameter: " + paramName); } return(param); }
// LUCENENET specific - copy method not used /// <summary> /// Executes the replication task. /// </summary> /// <exception cref="InvalidOperationException">required parameters are missing</exception> public virtual void Perform(IReplicationRequest request, IReplicationResponse response) { string[] pathElements = GetPathElements(request); if (pathElements.Length != 2) { throw ServletException.Create("invalid path, must contain shard ID and action, e.g. */s1/update"); } if (!Enum.TryParse(pathElements[ACTION_IDX], true, out ReplicationAction action)) { throw ServletException.Create("Unsupported action provided: " + pathElements[ACTION_IDX]); } if (!replicators.TryGetValue(pathElements[SHARD_IDX], out IReplicator replicator)) { throw ServletException.Create("unrecognized shard ID " + pathElements[SHARD_IDX]); } // SOLR-8933 Don't close this stream. try { switch (action) { case ReplicationAction.OBTAIN: string sessionId = ExtractRequestParam(request, REPLICATE_SESSION_ID_PARAM); string fileName = ExtractRequestParam(request, REPLICATE_FILENAME_PARAM); string source = ExtractRequestParam(request, REPLICATE_SOURCE_PARAM); using (Stream stream = replicator.ObtainFile(sessionId, source, fileName)) stream.CopyTo(response.Body); break; case ReplicationAction.RELEASE: replicator.Release(ExtractRequestParam(request, REPLICATE_SESSION_ID_PARAM)); break; case ReplicationAction.UPDATE: string currentVersion = request.QueryParam(REPLICATE_VERSION_PARAM); SessionToken token = replicator.CheckForUpdate(currentVersion); if (token is null) { response.Body.Write(new byte[] { 0 }, 0, 1); // marker for null token } else { response.Body.Write(new byte[] { 1 }, 0, 1); token.Serialize(new DataOutputStream(response.Body)); } break; // LUCENENET specific: default: if (Debugging.AssertsEnabled) { Debugging.Assert(false, "Invalid ReplicationAction specified"); } break; } } catch (Exception e) { response.StatusCode = (int)HttpStatusCode.InternalServerError; // propagate the failure try { TextWriter writer = new StreamWriter(response.Body); JsonSerializer serializer = JsonSerializer.Create(JSON_SERIALIZER_SETTINGS); serializer.Serialize(writer, e, e.GetType()); } catch (Exception e2) when(e2.IsException()) { throw new IOException("Could not serialize", e2); } } finally { response.Flush(); } }