Skip to content

Commit 55e4bfd

Browse files
author
hideki
committed
Removed duplicated codes.
1 parent cd9c2c2 commit 55e4bfd

1 file changed

Lines changed: 15 additions & 41 deletions

File tree

src/main/java/com/couchbase/lite/router/Router.java

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,9 @@ private boolean cacheWithEtag(String etag) {
198198

199199
private Map<String, Object> getBodyAsDictionary() throws CouchbaseLiteException {
200200
// check if content-type is `application/json`
201-
String contentType = getContentType(connection);
202-
if (contentType != null) {
203-
if (!contentType.equals("application/json"))
204-
throw new CouchbaseLiteException(Status.NOT_ACCEPTABLE);
205-
}
201+
String contentType = getRequestHeaderContentType();
202+
if (contentType != null && !contentType.equals("application/json"))
203+
throw new CouchbaseLiteException(Status.NOT_ACCEPTABLE);
206204

207205
// parse body text
208206
InputStream contentStream = connection.getRequestInputStream();
@@ -289,7 +287,7 @@ private boolean getQueryOptions(QueryOptions options) {
289287
}
290288

291289
private String getMultipartRequestType() {
292-
String accept = connection.getRequestProperty("Accept");
290+
String accept = getRequestHeaderValue("Accept");
293291
if (accept.startsWith("multipart/")) {
294292
return accept;
295293
}
@@ -338,49 +336,25 @@ private void sendResponse() {
338336
}
339337
}
340338

341-
private void sendErrorResponse(Status status) {
342-
Map<String, Object> result = new HashMap<String, Object>();
343-
result.put("error", status.getHTTPMessage());
344-
result.put("status", status.getHTTPCode());
345-
connection.setResponseBody(new Body(result));
346-
connection.setResponseCode(status.getCode());
347-
sendResponseHeaders(status);
348-
setResponse();
349-
sendResponse();
350-
}
351-
352339
// get Content-Type from URLConnection
353-
private static String getContentType(URLConnection connection) {
354-
String contentType = connection.getRequestProperty("Content-Type");
355-
if (contentType == null)
356-
// From Android: http://developer.android.com/reference/java/net/URLConnection.html
357-
contentType = connection.getRequestProperty("content-type");
358-
340+
private String getRequestHeaderContentType() {
341+
String contentType = getRequestHeaderValue("Content-Type");
359342
if (contentType != null) {
360343
// remove parameter (Content-Type := type "/" subtype *[";" parameter] )
361344
int index = contentType.indexOf(';');
362345
if (index > 0)
363346
contentType = contentType.substring(0, index);
364347
contentType = contentType.trim();
365348
}
366-
367349
return contentType;
368350
}
369351

370-
private static String getAccept(URLConnection connection) {
371-
String accept = connection.getRequestProperty("Accept");
372-
if (accept == null)
352+
private String getRequestHeaderValue(String paramName) {
353+
String value = connection.getRequestProperty(paramName);
354+
if (value == null)
373355
// From Android: http://developer.android.com/reference/java/net/URLConnection.html
374-
accept = connection.getRequestProperty("accept");
375-
return accept;
376-
}
377-
378-
private static String getIfMatch(URLConnection connection) {
379-
String ifMatch = connection.getRequestProperty("If-Match");
380-
if (ifMatch == null)
381-
// From Android: http://developer.android.com/reference/java/net/URLConnection.html
382-
ifMatch = connection.getRequestProperty("if-match");
383-
return ifMatch;
356+
value = connection.getRequestProperty(paramName.toLowerCase());
357+
return value;
384358
}
385359

386360
public void start() {
@@ -684,7 +658,7 @@ private Status sendResponseHeaders(Status status) {
684658
connection.getResHeader().add("Server", String.format("Couchbase Lite %s", getVersionString()));
685659

686660
// Check for a mismatch between the Accept request header and the response type:
687-
String accept = getAccept(connection);
661+
String accept = getRequestHeaderValue("Accept");
688662
if (accept != null && !"*/*".equals(accept)) {
689663
String responseType = connection.getBaseContentType();
690664
if (responseType != null && responseType.indexOf(accept) < 0) {
@@ -1756,7 +1730,7 @@ private void stopHeartbeat() {
17561730
*/
17571731

17581732
private String getRevIDFromIfMatchHeader() {
1759-
String ifMatch = connection.getRequestProperty("If-Match");
1733+
String ifMatch = getRequestHeaderValue("If-Match");
17601734
if (ifMatch == null) {
17611735
return null;
17621736
}
@@ -2096,7 +2070,7 @@ private Status update(Database _db, String docID, Body body, boolean deleting) {
20962070
if (docID != null && docID.isEmpty() == false) {
20972071
// On PUT/DELETE, get revision ID from either ?rev= query or doc body:
20982072
String revParam = getQuery("rev");
2099-
String ifMatch = getIfMatch(connection);
2073+
String ifMatch = getRequestHeaderValue("If-Match");
21002074
if (ifMatch != null) {
21012075
if(revParam == null)
21022076
revParam = ifMatch;
@@ -2193,7 +2167,7 @@ private Status updateAttachment(String attachment,
21932167
RevisionInternal rev = db.updateAttachment(
21942168
attachment,
21952169
body,
2196-
getContentType(connection),
2170+
getRequestHeaderContentType(),
21972171
AttachmentInternal.AttachmentEncoding.AttachmentEncodingNone,
21982172
docID,
21992173
revID,

0 commit comments

Comments
 (0)