public String unTag(NewTagModel untag) { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet oldTagGet = new HttpGet(cloudantURI + "/tag/" + untag.get_id()); oldTagGet.addHeader(authHeaderKey, authHeaderValue); oldTagGet.addHeader(acceptHeaderKey, acceptHeaderValue); oldTagGet.addHeader(contentHeaderKey, contentHeaderValue); HttpResponse oldTagResp = httpclient.execute(oldTagGet); Gson gson = new Gson(); TagModel updatedtag = gson.fromJson(EntityUtils.toString(oldTagResp.getEntity()), TagModel.class); httpclient.close(); updatedtag.getTagged().remove(untag.getUsername()); LOGGER.info(untag.get_id() + " is untagging " + untag.getUsername()); HttpPut updatedTagPut = new HttpPut(cloudantURI + "/tag/" + untag.get_id()); updatedTagPut.addHeader(authHeaderKey, authHeaderValue); updatedTagPut.addHeader(acceptHeaderKey, acceptHeaderValue); updatedTagPut.addHeader(contentHeaderKey, contentHeaderValue); updatedTagPut.setEntity(new StringEntity(gson.toJson(updatedtag))); httpclient = HttpClients.createDefault(); HttpResponse updatedTagResp = httpclient.execute(updatedTagPut); if (!(updatedTagResp.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED)) { String updatedTagEntity = EntityUtils.toString(updatedTagResp.getEntity()); httpclient.close(); return updatedTagEntity; } httpclient.close(); return successJson; } catch (Exception e) { e.printStackTrace(); return failJson; } }
public String updateTagged(NewTagModel newtag) { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // get old tagged from cloudant // in NewTagModel - get_id() returns person initiating tag - getUsername() returns person to // tag HttpGet oldTagGet = new HttpGet(cloudantURI + "/tag/" + newtag.get_id()); oldTagGet.addHeader(authHeaderKey, authHeaderValue); oldTagGet.addHeader(acceptHeaderKey, acceptHeaderValue); oldTagGet.addHeader(contentHeaderKey, contentHeaderValue); HttpResponse oldTagResp = httpclient.execute(oldTagGet); Gson gson = new Gson(); TagModel updatedtag = gson.fromJson(EntityUtils.toString(oldTagResp.getEntity()), TagModel.class); httpclient.close(); // check for and don't allow retagging - currently front-end design shouldn't allow for this // but needs to be checked on server side as well if (updatedtag.getTagged().contains(newtag.getUsername())) { LOGGER.info( newtag.getUsername() + " already exists in tagged list for " + updatedtag.get_id()); return alreadyTaggedJson; } // update array of tagged in updatedtag and update entry in cloudant updatedtag.getTagged().add(newtag.getUsername()); HttpPut updatedTagPut = new HttpPut(cloudantURI + "/tag/" + newtag.get_id()); updatedTagPut.addHeader(authHeaderKey, authHeaderValue); updatedTagPut.addHeader(acceptHeaderKey, acceptHeaderValue); updatedTagPut.addHeader(contentHeaderKey, contentHeaderValue); updatedTagPut.setEntity(new StringEntity(gson.toJson(updatedtag))); httpclient = HttpClients.createDefault(); HttpResponse updatedTagResp = httpclient.execute(updatedTagPut); if (!(updatedTagResp.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED)) { String updatedTagEntity = EntityUtils.toString(updatedTagResp.getEntity()); httpclient.close(); return updatedTagEntity; } httpclient.close(); LOGGER.info(newtag.get_id() + " tagged " + newtag.getUsername()); return successJson; } catch (Exception e) { try { httpclient.close(); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); return failJson; } }