You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rj...@apache.org on 2014/05/08 23:38:18 UTC

svn commit: r1593427 - in /lucene/dev/branches/branch_4x: ./ solr/ solr/core/ solr/core/src/java/org/apache/solr/response/ solr/core/src/java/org/apache/solr/servlet/ solr/core/src/test-files/solr/collection1/conf/ solr/core/src/test/org/apache/solr/se...

Author: rjernst
Date: Thu May  8 21:38:17 2014
New Revision: 1593427

URL: http://svn.apache.org/r1593427
Log:
SOLR-6043: Add ability to set http headers in solr response

Added:
    lucene/dev/branches/branch_4x/solr/core/src/test-files/solr/collection1/conf/solrconfig-headers.xml
      - copied unchanged from r1593423, lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/solrconfig-headers.xml
    lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/servlet/ResponseHeaderTest.java
      - copied unchanged from r1593423, lucene/dev/trunk/solr/core/src/test/org/apache/solr/servlet/ResponseHeaderTest.java
Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/solr/   (props changed)
    lucene/dev/branches/branch_4x/solr/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_4x/solr/core/   (props changed)
    lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java
    lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java

Modified: lucene/dev/branches/branch_4x/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/CHANGES.txt?rev=1593427&r1=1593426&r2=1593427&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/CHANGES.txt (original)
+++ lucene/dev/branches/branch_4x/solr/CHANGES.txt Thu May  8 21:38:17 2014
@@ -47,6 +47,9 @@ New Features
 * SOLR-5999: Add checkIntegrityAtMerge support to solrconfig.xml.
   (Varun Thacker via Ryan Ernst)
 
+* SOLR-6043: Add ability to set http headers in solr response
+  (Tomás Fernández Löbbe via Ryan Ernst)
+
 Bug Fixes
 ----------------------
 
@@ -79,7 +82,6 @@ Bug Fixes
 * SOLR-6039: fixed debug output when no results in response
   (Tomás Fernández Löbbe, hossman)
 
-
 Other Changes
 ---------------------
 

Modified: lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java?rev=1593427&r1=1593426&r2=1593427&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java (original)
+++ lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/response/SolrQueryResponse.java Thu May  8 21:38:17 2014
@@ -17,13 +17,19 @@
 
 package org.apache.solr.response;
 
+import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.servlet.http.HttpServletResponse;
+
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.util.SimpleOrderedMap;
 import org.apache.solr.search.ReturnFields;
 import org.apache.solr.search.SolrReturnFields;
 
-import java.util.*;
-
 /**
  * <code>SolrQueryResponse</code> is used by a query handler to return
  * the response to a query request.
@@ -77,6 +83,15 @@ public class SolrQueryResponse {
   protected NamedList<Object> toLog = new SimpleOrderedMap<>();
 
   protected ReturnFields returnFields;
+  
+  /**
+   * Container for storing HTTP headers. Internal Solr components can add headers to
+   * this SolrQueryResponse through the methods: {@link #addHttpHeader(String, String)}
+   * and {@link #setHttpHeader(String, String)}, or remove existing ones through 
+   * {@link #removeHttpHeader(String)} and {@link #removeHttpHeaders(String)}. 
+   * All these headers are going to be added to the HTTP response.
+   */
+  private final NamedList<String> headers = new SimpleOrderedMap<>();
 
   // error if this is set...
   protected Exception err;
@@ -245,4 +260,110 @@ public class SolrQueryResponse {
   public boolean isHttpCaching() {
     return this.httpCaching;
   }
+  
+  /**
+   *
+   * Sets a response header with the given name and value. This header
+   * will be included in the HTTP response
+   * If the header had already been set, the new value overwrites the
+   * previous ones (all of them if there are multiple for the same name).
+   * 
+   * @param name the name of the header
+   * @param value the header value  If it contains octet string,
+   *    it should be encoded according to RFC 2047
+   *    (http://www.ietf.org/rfc/rfc2047.txt)
+   *
+   * @see #addHttpHeader
+   * @see HttpServletResponse#setHeader
+   */
+  public void setHttpHeader(String name, String value) {
+    headers.removeAll(name);
+    headers.add(name, value);
+  }
+ 
+  /**
+   * Adds a response header with the given name and value. This header
+   * will be included in the HTTP response
+   * This method allows response headers to have multiple values.
+   * 
+   * @param name  the name of the header
+   * @param value the additional header value   If it contains
+   *    octet string, it should be encoded
+   *    according to RFC 2047
+   *    (http://www.ietf.org/rfc/rfc2047.txt)
+   *
+   * @see #setHttpHeader
+   * @see HttpServletResponse#addHeader
+   */
+  public void addHttpHeader(String name, String value) {
+     headers.add(name, value);
+  }
+ 
+  /**
+   * Gets the value of the response header with the given name.
+   * 
+   * <p>If a response header with the given name exists and contains
+   * multiple values, the value that was added first will be returned.</p>
+   *
+   * <p>NOTE: this runs in linear time (it scans starting at the
+   * beginning of the list until it finds the first pair with
+   * the specified name).</p>
+   *
+   * @param name the name of the response header whose value to return
+   * @return the value of the response header with the given name,
+   * or <tt>null</tt> if no header with the given name has been set
+   * on this response
+   */
+  public String getHttpHeader(String name) {
+    return headers.get(name);
+  }
+
+  /**
+   * Gets the values of the response header with the given name.
+   *
+   * @param name the name of the response header whose values to return
+   *
+   * @return a (possibly empty) <code>Collection</code> of the values
+   * of the response header with the given name
+   *
+   */     
+  public Collection<String> getHttpHeaders(String name) {
+    return headers.getAll(name);
+  }
+
+  /**
+   * Removes a previously added header with the given name (only
+   * the first one if multiple are present for the same name) 
+   *
+   * <p>NOTE: this runs in linear time (it scans starting at the
+   * beginning of the list until it finds the first pair with
+   * the specified name).</p>
+   *
+   * @param name the name of the response header to remove
+   * @return the value of the removed entry or <tt>null</tt> if no 
+   * value is found for the given header name 
+   */
+  public String removeHttpHeader(String name) {
+    return headers.remove(name);
+  }
+
+  /**
+   * Removes all previously added headers with the given name. 
+   *
+   * @param name the name of the response headers to remove
+   * @return a <code>Collection</code> with all the values
+   * of the removed entries. It returns <code>null</code> if no
+   * entries are found for the given name
+   */
+  public Collection<String> removeHttpHeaders(String name) {
+    return headers.removeAll(name);
+  }
+
+  /**
+   * Returns a new iterator of response headers
+   * @return a new Iterator instance for the response headers
+   */
+  public Iterator<Entry<String, String>> httpHeaders() {
+    return headers.iterator();
+  }
 }

Modified: lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java?rev=1593427&r1=1593426&r2=1593427&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java (original)
+++ lucene/dev/branches/branch_4x/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java Thu May  8 21:38:17 2014
@@ -417,16 +417,11 @@ public class SolrDispatchFilter extends 
                 SolrRequestInfo.setRequestInfo(new SolrRequestInfo(solrReq, solrRsp));
                 this.execute( req, handler, solrReq, solrRsp );
                 HttpCacheHeaderUtil.checkHttpCachingVeto(solrRsp, resp, reqMethod);
-              // add info to http headers
-              //TODO: See SOLR-232 and SOLR-267.  
-                /*try {
-                  NamedList solrRspHeader = solrRsp.getResponseHeader();
-                 for (int i=0; i<solrRspHeader.size(); i++) {
-                   ((javax.servlet.http.HttpServletResponse) response).addHeader(("Solr-" + solrRspHeader.getName(i)), String.valueOf(solrRspHeader.getVal(i)));
-                 }
-                } catch (ClassCastException cce) {
-                  log.log(Level.WARNING, "exception adding response header log information", cce);
-                }*/
+                Iterator<Entry<String, String>> headers = solrRsp.httpHeaders();
+                while (headers.hasNext()) {
+                  Entry<String, String> entry = headers.next();
+                  resp.addHeader(entry.getKey(), entry.getValue());
+                }
                QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq);
                writeResponse(solrRsp, response, responseWriter, solrReq, reqMethod);
             }