Apache Solr open-source search software
SOLR-15116: Support a HEAD request for managed resources (#495)
Eric Pughd60458aeae33
diff --git a/solr/core/src/java/org/apache/solr/rest/RestManager.java b/solr/core/src/java/org/apache/solr/rest/RestManager.javaindex ea1d3c08072..7977cc4dbe0 100644--- a/solr/core/src/java/org/apache/solr/rest/RestManager.java+++ b/solr/core/src/java/org/apache/solr/rest/RestManager.java@@ -42,6 +42,7 @@ import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.common.util.ContentStream;import org.apache.solr.common.util.NamedList;+import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.core.SolrResourceLoader;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.SolrQueryResponse;@@ -327,6 +328,10 @@ public class RestManager {final String method = req.getHttpMethod();try {switch (method) {+ case "HEAD":+ managedResource.doGet(this, childId);+ doHead(this);+ break;case "GET":managedResource.doGet(this, childId);break;@@ -346,6 +351,16 @@ public class RestManager {handlePostExecution(log);}+ private void doHead(ManagedEndpoint managedEndpoint) {+ // Setting the response to blank clears the content out,+ // however it also means the Content-Length HTTP Header is set to 0+ // which isn't compliant with the specification of how HEAD should work,+ // it should instead return the length of the content if you did a GET.+ NamedList<Object> blank = new SimpleOrderedMap<>();+ managedEndpoint.getSolrResponse().setAllValues(blank);++ }+protected void doDelete() {// only delegate delete child resources to the ManagedResource// as deleting the actual resource is best handled by thediff --git a/solr/core/src/test/org/apache/solr/response/TestSolrQueryResponse.java b/solr/core/src/test/org/apache/solr/response/TestSolrQueryResponse.javaindex 66aca82c98a..f658158e393 100644--- a/solr/core/src/test/org/apache/solr/response/TestSolrQueryResponse.java+++ b/solr/core/src/test/org/apache/solr/response/TestSolrQueryResponse.java@@ -289,4 +289,32 @@ public class TestSolrQueryResponse extends SolrTestCase {assertEquals("httpCaching new value", newValue, response.isHttpCaching());}+ @Test+ public void testConvertToHEADStyleResponse() throws Exception {+ final SolrQueryResponse response = new SolrQueryResponse();+ final NamedList<Object> newValue = new SimpleOrderedMap<>();+ newValue.add("responseHeaderKey1", "value1");+ response.add("responseHeaderKey2", "value2");+ response.addResponseHeader(newValue);++ response.addResponse("foo");+ response.add("bob", "dole");++ response.addHttpHeader("key1", "value1");++ NamedList<Object> blank = new SimpleOrderedMap<>();+ response.setAllValues(blank);++ Iterator<Entry<String, String>> it = response.httpHeaders();+ assertTrue(it.hasNext());+ Entry<String, String> entry = it.next();+ assertEquals("key1", entry.getKey());+ assertEquals("value1", entry.getValue());+ assertFalse(it.hasNext());+++++ }+}