Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tidy ServletUtils, make names of the same form.

  • Loading branch information...
commit 2a15f14f49b63b189bee0c9466adace35b92dd57 1 parent f53e98b
@rnewson authored
View
12 src/main/java/com/github/rnewson/couchdb/lucene/DatabaseIndexer.java
@@ -261,7 +261,7 @@ public void admin(final HttpServletRequest req,
logger.info("Expunging deletes from " + state);
state.writer.expungeDeletes(false);
resp.setStatus(202);
- ServletUtils.writeJsonSuccess(req, resp);
+ ServletUtils.sendJsonSuccess(req, resp);
return;
}
@@ -269,11 +269,11 @@ public void admin(final HttpServletRequest req,
logger.info("Optimizing " + state);
state.writer.optimize(false);
resp.setStatus(202);
- ServletUtils.writeJsonSuccess(req, resp);
+ ServletUtils.sendJsonSuccess(req, resp);
return;
}
- ServletUtils.sendJSONError(req, resp, 400, "bad_request");
+ ServletUtils.sendJsonError(req, resp, 400, "bad_request");
}
public void awaitInitialization() {
@@ -596,7 +596,7 @@ public void search(final HttpServletRequest req,
result.put(queryRow);
}
} catch (final ParseException e) {
- ServletUtils.sendJSONError(req, resp, 400, "Bad query syntax: "
+ ServletUtils.sendJsonError(req, resp, 400, "Bad query syntax: "
+ e.getMessage());
return;
} finally {
@@ -699,13 +699,13 @@ private IndexState getState(final HttpServletRequest req,
final HttpServletResponse resp) throws IOException, JSONException {
final View view = paths.get(toPath(req));
if (view == null) {
- ServletUtils.sendJSONError(req, resp, 400, "no_such_view");
+ ServletUtils.sendJsonError(req, resp, 400, "no_such_view");
return null;
}
final IndexState result = states.get(view);
if (result == null) {
- ServletUtils.sendJSONError(req, resp, 400, "no_such_state");
+ ServletUtils.sendJsonError(req, resp, 400, "no_such_state");
}
return result;
}
View
4 src/main/java/com/github/rnewson/couchdb/lucene/JSONErrorHandler.java
@@ -42,10 +42,10 @@ public void handle(String target, HttpServletRequest request, HttpServletRespons
final String reason = connection.getResponse().getReason();
try {
if (reason.startsWith("{")) {
- ServletUtils.sendJSONError(request, response, connection.getResponse().getStatus(),
+ ServletUtils.sendJsonError(request, response, connection.getResponse().getStatus(),
new JSONObject(reason));
} else {
- ServletUtils.sendJSONError(request, response, connection.getResponse().getStatus(),
+ ServletUtils.sendJsonError(request, response, connection.getResponse().getStatus(),
reason);
}
} catch (final JSONException e) {
View
12 src/main/java/com/github/rnewson/couchdb/lucene/LuceneServlet.java
@@ -95,7 +95,7 @@ private void cleanup(final HttpServletRequest req,
if (dirs == null) {
LOG.warn(DatabaseIndexer.uuidDir(root, db.getUuid())
+ " is not a directory or could not be read.");
- ServletUtils.sendJSONError(req, resp, 500, "index_dir_perms");
+ ServletUtils.sendJsonError(req, resp, 500, "index_dir_perms");
return;
} else {
for (final File dir : dirs) {
@@ -116,7 +116,7 @@ private void cleanup(final HttpServletRequest req,
}
resp.setStatus(202);
- ServletUtils.writeJsonSuccess(req, resp);
+ ServletUtils.sendJsonSuccess(req, resp);
}
private Couch getCouch(final HttpServletRequest req) throws IOException {
@@ -162,7 +162,7 @@ private void handleWelcomeReq(final HttpServletRequest req,
final JSONObject welcome = new JSONObject();
welcome.put("couchdb-lucene", "Welcome");
welcome.put("version", p.getImplementationVersion());
- ServletUtils.writeJson(req, resp, welcome);
+ ServletUtils.sendJson(req, resp, welcome);
}
@Override
@@ -185,7 +185,7 @@ private void doGetInternal(final HttpServletRequest req, final HttpServletRespon
case 5:
final DatabaseIndexer indexer = getIndexer(req);
if (indexer == null) {
- ServletUtils.sendJSONError(req, resp, 500, "error_creating_index");
+ ServletUtils.sendJsonError(req, resp, 500, "error_creating_index");
return;
}
@@ -197,7 +197,7 @@ private void doGetInternal(final HttpServletRequest req, final HttpServletRespon
return;
}
- ServletUtils.sendJSONError(req, resp, 400, "bad_request");
+ ServletUtils.sendJsonError(req, resp, 400, "bad_request");
}
@Override
@@ -225,7 +225,7 @@ private void doPostInternal(final HttpServletRequest req, final HttpServletRespo
indexer.admin(req, resp);
return;
}
- ServletUtils.sendJSONError(req, resp, 400, "bad_request");
+ ServletUtils.sendJsonError(req, resp, 400, "bad_request");
}
}
View
43 src/main/java/com/github/rnewson/couchdb/lucene/util/ServletUtils.java
@@ -47,44 +47,43 @@ public static String getParameter(final HttpServletRequest req, final String par
return result != null ? result : defaultValue;
}
- public static void sendJSONError(final HttpServletRequest request, final HttpServletResponse response, final int code,
+ public static void setResponseContentTypeAndEncoding(final HttpServletRequest req, final HttpServletResponse resp) {
+ final String accept = req.getHeader("Accept");
+ if (getBooleanParameter(req, "force_json") || (accept != null && accept.contains("application/json"))) {
+ resp.setContentType("application/json");
+ } else {
+ resp.setContentType("text/plain");
+ }
+ if (!resp.containsHeader("Vary")) {
+ resp.addHeader("Vary", "Accept");
+ }
+ resp.setCharacterEncoding("utf-8");
+ }
+
+ public static void sendJsonError(final HttpServletRequest request, final HttpServletResponse response, final int code,
final String reason) throws IOException, JSONException {
final JSONObject obj = new JSONObject();
obj.put("reason", reason);
- sendJSONError(request, response, code, obj);
+ sendJsonError(request, response, code, obj);
}
- public static void sendJSONError(final HttpServletRequest request, final HttpServletResponse response, final int code,
+ public static void sendJsonError(final HttpServletRequest request, final HttpServletResponse response, final int code,
final JSONObject error) throws IOException, JSONException {
- error.put("code", code);
-
setResponseContentTypeAndEncoding(request, response);
response.setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store");
response.setStatus(code);
+ error.put("code", code);
final Writer writer = response.getWriter();
- try {
+ try {
writer.write(error.toString());
- writer.write("\n");
+ writer.write("\r\n");
} finally {
writer.close();
}
}
- public static void setResponseContentTypeAndEncoding(final HttpServletRequest req, final HttpServletResponse resp) {
- final String accept = req.getHeader("Accept");
- if (getBooleanParameter(req, "force_json") || (accept != null && accept.contains("application/json"))) {
- resp.setContentType("application/json");
- } else {
- resp.setContentType("text/plain");
- }
- if (!resp.containsHeader("Vary")) {
- resp.addHeader("Vary", "Accept");
- }
- resp.setCharacterEncoding("utf-8");
- }
-
- public static void writeJson(final HttpServletRequest req, final HttpServletResponse resp, final JSONObject json) throws IOException {
+ public static void sendJson(final HttpServletRequest req, final HttpServletResponse resp, final JSONObject json) throws IOException {
setResponseContentTypeAndEncoding(req, resp);
final Writer writer = resp.getWriter();
try {
@@ -94,7 +93,7 @@ public static void writeJson(final HttpServletRequest req, final HttpServletResp
}
}
- public static void writeJsonSuccess(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
+ public static void sendJsonSuccess(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
setResponseContentTypeAndEncoding(req, resp);
final Writer writer = resp.getWriter();
try {
Please sign in to comment.
Something went wrong with that request. Please try again.