Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added delete to embedded API

  • Loading branch information...
commit 0a2025a622b3a2a5f2f76f8ad61517b638001562 1 parent 6ee9464
Ignacio Perez Gonzalez authored
Showing with 87 additions and 0 deletions.
  1. +87 −0 embedded-api/com/flaptor/indextank/api/resources/DeleteDocs.java
View
87 embedded-api/com/flaptor/indextank/api/resources/DeleteDocs.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2011 LinkedIn, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.flaptor.indextank.api.resources;
+
+import java.io.IOException;
+import java.util.logging.Logger;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.json.simple.parser.ParseException;
+
+import com.flaptor.indextank.api.IndexEngineApi;
+import com.ghosthack.turismo.action.Action;
+
+public class DeleteDocs extends Action {
+
+ /**
+ * @see java.lang.Runnable#run()
+ */
+ public void run() {
+ IndexEngineApi api = (IndexEngineApi) ctx().getAttribute("api");
+ try {
+ Object parse = JSONValue.parseWithException(req().getReader());
+ if(parse instanceof JSONObject) { // 200, 400, 404, 409, 503
+ JSONObject jo = (JSONObject) parse;
+ try {
+ deleteDocument(api, jo);
+ res().setStatus(200);
+ return;
+
+ } catch(Exception e) {
+ e.printStackTrace();
+ if(LOG_ENABLED) LOG.severe(e.getMessage());
+ res().setStatus(400);
+ print("Invalid or missing argument"); // TODO: descriptive error msg
+ return;
+ }
+ }
+ } catch (IOException e) {
+ if(LOG_ENABLED) LOG.severe("PUT doc, parse input " + e.getMessage());
+ } catch (ParseException e) {
+ if(LOG_ENABLED) LOG.severe("PUT doc, parse input " + e.getMessage());
+ } catch (Exception e) {
+ if(LOG_ENABLED) LOG.severe("PUT doc " + e.getMessage());
+ }
+ res().setStatus(503);
+ print("Service unavailable"); // TODO: descriptive error msg
+ }
+
+ private void deleteDocument(IndexEngineApi api, JSONObject jo) {
+ String docid = String.valueOf(jo.get("docid")); // TODO: empty & < 1024b
+ api.deleteDocument(docid);
+ }
+
+ private boolean validateDocument(JSONObject jo) {
+ return true; // TODO: validate the document
+ }
+
+ private boolean validateDocuments(JSONArray ja) {
+ for(Object o: ja) {
+ JSONObject jo = (JSONObject) o;
+ if(!validateDocument(jo)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static final Logger LOG = Logger.getLogger(DeleteDocs.class.getName());
+ private static final boolean LOG_ENABLED = true;
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.