Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jdyer1 committed Feb 21, 2024
1 parent 2740318 commit c778623
Showing 1 changed file with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,24 @@ public NamedList<Object> request(SolrRequest<?> solrRequest, String collection)
switch (solrRequest.getMethod()) {
case GET:
{
validateGetRequest(solrRequest);
reqb.GET();
resp = doGet(url, reqb, solrRequest, queryParams);
break;
}
case POST:
{
PreparePostPutRequestReturnValue result =
preparePostPutRequest(reqb, solrRequest, queryParams);
queryParams = result.queryParams;
reqb.POST(result.bodyPublisher);
resp = doPutOrPost(url, false, reqb, solrRequest, queryParams);
break;
}
case PUT:
{
PreparePostPutRequestReturnValue result =
preparePostPutRequest(reqb, solrRequest, queryParams);
queryParams = result.queryParams;
reqb.PUT(result.bodyPublisher);
resp = doPutOrPost(url, true, reqb, solrRequest, queryParams);
break;
}
default:
{
throw new IllegalStateException("Unsupported method: " + solrRequest.getMethod());
}
}
decorateRequest(reqb, solrRequest);
reqb.uri(new URI(url + "?" + queryParams));
resp = client.send(reqb.build(), HttpResponse.BodyHandlers.ofInputStream());
return processErrorsAndResponse(solrRequest, parser, resp, url);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand All @@ -167,20 +157,19 @@ public NamedList<Object> request(SolrRequest<?> solrRequest, String collection)
}
}

private static class PreparePostPutRequestReturnValue {
ModifiableSolrParams queryParams;
HttpRequest.BodyPublisher bodyPublisher;

PreparePostPutRequestReturnValue(
ModifiableSolrParams queryParams, HttpRequest.BodyPublisher bodyPublisher) {
this.queryParams = queryParams;
this.bodyPublisher = bodyPublisher;
}
private HttpResponse<InputStream> doGet(String url, HttpRequest.Builder reqb, SolrRequest<?> solrRequest, ModifiableSolrParams queryParams)
throws IOException, InterruptedException, URISyntaxException {
validateGetRequest(solrRequest);
reqb.GET();
decorateRequest(reqb, solrRequest);
reqb.uri(new URI(url + "?" + queryParams));
return client.send(reqb.build(), HttpResponse.BodyHandlers.ofInputStream());
}

private PreparePostPutRequestReturnValue preparePostPutRequest(
HttpRequest.Builder reqb, SolrRequest<?> solrRequest, ModifiableSolrParams queryParams)
throws IOException {

private HttpResponse<InputStream> doPutOrPost(
String url, boolean isPut, HttpRequest.Builder reqb, SolrRequest<?> solrRequest, ModifiableSolrParams queryParams)
throws IOException, InterruptedException, URISyntaxException {
RequestWriter.ContentWriter contentWriter = requestWriter.getContentWriter(solrRequest);

Collection<ContentStream> streams = null;
Expand All @@ -198,27 +187,34 @@ private PreparePostPutRequestReturnValue preparePostPutRequest(
throw new UnsupportedOperationException("This client does not support multipart.");
}

HttpRequest.BodyPublisher bodyPublisher;

if (contentWriter != null) {
// TODO: There is likely a more memory-efficient way to do this!
ByteArrayOutputStream baos = new ByteArrayOutputStream();
contentWriter.write(baos);
byte[] bytes = baos.toByteArray();
return new PreparePostPutRequestReturnValue(
queryParams, HttpRequest.BodyPublishers.ofByteArray(bytes));
bodyPublisher = HttpRequest.BodyPublishers.ofByteArray(bytes);
} else if (streams != null && streams.size() == 1) {
ContentStream contentStream = streams.iterator().next();
InputStream is = contentStream.getStream();
return new PreparePostPutRequestReturnValue(
queryParams, HttpRequest.BodyPublishers.ofInputStream(() -> is));
bodyPublisher = HttpRequest.BodyPublishers.ofInputStream(() -> is);
} else if (queryParams != null && urlParamNames != null) {
ModifiableSolrParams requestParams = queryParams;
queryParams = calculateQueryParams(urlParamNames, requestParams);
queryParams.add(calculateQueryParams(solrRequest.getQueryParams(), requestParams));
return new PreparePostPutRequestReturnValue(
queryParams, HttpRequest.BodyPublishers.ofString(requestParams.toString()));
bodyPublisher = HttpRequest.BodyPublishers.ofString(requestParams.toString());
} else {
bodyPublisher = HttpRequest.BodyPublishers.noBody();
}
if(isPut) {
reqb.PUT(bodyPublisher);
} else {
return new PreparePostPutRequestReturnValue(queryParams, HttpRequest.BodyPublishers.noBody());
reqb.POST(bodyPublisher);
}
decorateRequest(reqb, solrRequest);
reqb.uri(new URI(url + "?" + queryParams));
return client.send(reqb.build(), HttpResponse.BodyHandlers.ofInputStream());
}

private void decorateRequest(HttpRequest.Builder reqb, SolrRequest<?> solrRequest) {
Expand Down

0 comments on commit c778623

Please sign in to comment.