Permalink
Browse files

auto-negotiate Content-Type of response; application/json if client A…

…ccepts it, text/plain otherwise.
  • Loading branch information...
1 parent 20af671 commit dea842aed559cbac5e8d69dfe443fcf05436edf2 Robert Newson committed Aug 2, 2009
Showing with 10 additions and 2 deletions.
  1. +10 −2 src/main/java/com/github/rnewson/couchdb/lucene/SearchRequest.java
@@ -72,6 +72,8 @@
private final String ifNoneMatch;
+ private final String contentType;
+
public SearchRequest(final JSONObject obj, final String viewsig) throws ParseException {
final JSONObject headers = obj.getJSONObject("headers");
final JSONObject query = obj.getJSONObject("query");
@@ -88,6 +90,13 @@ public SearchRequest(final JSONObject obj, final String viewsig) throws ParseExc
this.rewrite_query = query.optBoolean("rewrite", false);
this.callback = query.optString("callback", null);
+ // Negotiate Content-Type of response.
+ if (headers.optString("Accept").indexOf("application/json") != -1) {
+ this.contentType = "application/json";
+ } else {
+ this.contentType = "text/plain;charset=utf-8";
+ }
+
// Parse query.
this.q = Config.QP.parse(query.getString("q"));
@@ -247,8 +256,7 @@ public String execute(final IndexSearcher searcher) throws IOException {
result.put("code", 200);
final JSONObject headers = new JSONObject();
- // Allow viewing in browser.
- headers.put("Content-Type", "text/plain;charset=utf-8");
+ headers.put("Content-Type", contentType);
// Allow short-term caching.
headers.put("Cache-Control", "max-age=" + Config.COMMIT_MAX / 1000);
// Results can't change unless the IndexReader does.

0 comments on commit dea842a

Please sign in to comment.