001/* 002 * JKNIV, whinstone one contract to access your database. 003 * 004 * Copyright (C) 2017, the original author or authors. 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software Foundation, Inc., 018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 019 */ 020package net.sf.jkniv.whinstone.couchdb.commands; 021 022import java.io.IOException; 023import java.util.Map; 024 025import org.apache.http.client.methods.CloseableHttpResponse; 026import org.apache.http.client.methods.HttpDelete; 027import org.apache.http.impl.client.CloseableHttpClient; 028import org.apache.http.impl.client.HttpClients; 029import org.apache.http.util.EntityUtils; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032 033import net.sf.jkniv.sqlegance.RepositoryException; 034import net.sf.jkniv.whinstone.couchdb.HttpBuilder; 035 036/** 037 * D 038 * 039 * <pre> 040 * 041 * https://docs.couchdb.org/en/stable/api/database/find.html#delete--db-_index-designdoc-json-name 042 * 043 * 044 * DELETE /{db}/_index/{designdoc}/json/{name} 045 * Parameters: 046 * 047 * db – Database name. 048 * designdoc – Design document name. 049 * name – Index name. 050 * 051 * Response Headers: 052 * 053 * Content-Type – application/json 054 * 055 * Response JSON Object: 056 * 057 * ok (string) – “true” if successful. 058 * 059 * Status Codes: 060 * 061 * 200 OK – Success 062 * 400 Bad Request – Invalid request 063 * 401 Unauthorized – Writer permission required 064 * 404 Not Found – Index not found 065 * 500 Internal Server Error – Execution error 066 * </pre> 067 * 068 * @author Alisson Gomes 069 * @since 0.6.6 070 * 071 */ 072@SuppressWarnings("unchecked") 073public class DropIndexCommand extends AbstractCommand implements CouchCommand 074{ 075 private static final Logger LOG = LoggerFactory.getLogger(DropIndexCommand.class); 076 private static final Logger LOGSQL = net.sf.jkniv.whinstone.couchdb.LoggerFactory.getLogger(); 077 private HttpBuilder httpBuilder; 078 private String designDoc; 079 private String indexName; 080 081 public DropIndexCommand(HttpBuilder httpBuilder, String designDoc, String indexName) 082 { 083 super(); 084 this.httpBuilder = httpBuilder; 085 this.method = HttpMethod.DELETE; 086 this.designDoc = designDoc; 087 this.indexName = indexName; 088 } 089 090 @Override 091 public <T> T execute() 092 { 093 String json = null; 094 CloseableHttpResponse response = null; 095 T answer = null; 096 try 097 { 098 CloseableHttpClient httpclient = HttpClients.createDefault(); 099 // {db}/_index/{designdoc}/json/{name} 100 String url = String.format("%s_index/%s/json/%s", httpBuilder.getHostContext(), designDoc, indexName); 101 HttpDelete http = null; 102 http = (HttpDelete)asDelete().newHttp(url); 103 httpBuilder.setHeader(http); 104 printRequest(http); 105 response = httpclient.execute(http); 106 json = EntityUtils.toString(response.getEntity()); 107 printResponse(response, json); 108 109 int statusCode = response.getStatusLine().getStatusCode(); 110 if (isOk(statusCode)) 111 { 112 LOGSQL.info("Index {} was dropped successfully", url); 113 answer = (T)Integer.valueOf("1"); 114 } 115 else if(!isNotFound(statusCode)) 116 { 117 LOG.warn(errorFormat(http, response.getStatusLine(), json)); 118 } 119 } 120 catch (Exception e) // ClientProtocolException | JsonParseException | JsonMappingException | IOException 121 { 122 handlerException.handle(e); 123 } 124 finally 125 { 126 if (response != null) 127 { 128 try 129 { 130 response.close(); 131 } 132 catch (IOException e) 133 { 134 handlerException.handle(e); 135 } 136 } 137 } 138 return answer; 139 } 140 141 @Override 142 public String getBody() 143 { 144 return this.body; 145 } 146 147 @Override 148 public HttpMethod asDelete() 149 { 150 this.method = HttpMethod.DELETE; 151 return this.method; 152 } 153}