Hi,
i wrote a function to put Attachments into couchbase. The function seemed to work fine, but now it started to get error-code 400 from couchbase on the line where i get the InputStream with httpcon.getInputStream();
here is the function:
/**
*
* @param id - id of the document
* @param rev - last rev of the document
* @param filename
* @param content
* @param content_type - as "application/pdf"
* @param syncBucket - name of the bucket
* @param syncGatewayIP - ip without port
* @param username
* @param password
* @return new revision
*/
public static String putAttachmentByREST(String id, String rev, String filename, byte[] content, String content_type,
String syncBucket, String syncGatewayIP, String username, String password) {
String response = "failed";
if (filename.contains(" ")) {
filename = filename.replaceAll(" ", "");
}
try {
URL url = new URL("http://" + syncGatewayIP + ":4984/" + syncBucket + "/" + id + "/"
+ URLEncoder.encode(filename, "UTF-8") + "?rev=" + rev);
log.info("putAttachment url: " + url.toString());
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
if (username != null && password != null && !username.isEmpty() && !password.isEmpty()) {
setAuthInfo(httpCon, username, password);
}
httpCon.setRequestMethod("PUT");
httpCon.setRequestProperty("Content-Type", content_type);
OutputStream out = httpCon.getOutputStream();
out.write(content);
out.close();
InputStream inStream = httpCon.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
String line;
StringBuilder result = new StringBuilder();
while ((line = br.readLine()) != null) {
result.append(line + "\n");
}
response = result.toString();
JsonObject responseObject = JsonObject.fromJson(response);
if (responseObject.containsKey("_rev")) {
return responseObject.getString("_rev");
} else if (responseObject.containsKey("rev")) {
return responseObject.getString("rev");
} else {
return "";
}
} catch (MalformedURLException e) {
log.error(e);
} catch (ProtocolException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
return response;
}
The Exception:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://xx.xxx.xxx.x:4984/service_sync/thisisadocumentname/thisisanattachmentnamewhichisthesameasthedocumentname?rev=8-5393d79d9c552236de249795ca2966d8
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
I tried to get the error-log on TRACE-level but my application creates way too much output on that level.
Here is the calling code:
byte[] decoded = Base64.getDecoder().decode(attachment.getData());
if(jsObject.containsKey("data")){
jsObject.removeKey("data");
}
DatabaseManager.initialize();
String rev = CouchbaseUtil.createOrUpdateCBDocumentByREST(id, jsObject, DatabaseManager.SYNC_USER);
if(rev != null && !rev.isEmpty()){
CouchbaseUtil.putAttachmentByREST(id, rev, id, decoded, attachment.getContent_type(), DatabaseManager.SYNC_USER);
}
The function getRev
gets the last revision of a document.
Its able to upload the first document of a whole series of pdfs but then starts throwing the IOException
.
Also I am able to upload Attachments from plain text without a problem. I want to mention that the filename equals the ID of the document in the above example.
So my question is, why does it return 400? The function seems to be fine. Could this have to do with the format of the document?
Thanks in advance
Malte