Java tutorial
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import protocol.HttpRequest; import protocol.HttpResponse; import protocol.HttpResponseFactory; import protocol.IServlet; import protocol.Protocol; /* * InventoryAddNewItemServlet.java * Nov 11, 2015 * * Simple Web Server (SWS) for EE407/507 and CS455/555 * * Copyright (C) 2011 Chandan Raj Rupakheti, Clarkson University * * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, either * version 3 of the License, or any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/lgpl.html>. * * Contact Us: * Chandan Raj Rupakheti (rupakhcr@clarkson.edu) * Department of Electrical and Computer Engineering * Clarkson University * Potsdam * NY 13699-5722 * http://clarkson.edu/~rupakhcr */ /** * * @author Chandan R. Rupakheti (rupakhcr@clarkson.edu) */ public class InventoryDeleteItemServlet implements IServlet { /* (non-Javadoc) * @see protocol.IServlet#doWork(protocol.HttpRequest, java.lang.String) */ @Override public HttpResponse doWork(HttpRequest request, String rootDirectory) { HttpResponse response; try { String inventoryUrl = "./web/inventory.json"; File inventory = new File(inventoryUrl); StringBuilder sb = new StringBuilder(); Scanner sc = new Scanner(inventory); while (sc.hasNext()) { sb.append(sc.nextLine()); } sc.close(); // Add this new book to the list of existing books Gson gson = new Gson(); InventoryObject[] inventoryArray = gson.fromJson(sb.toString(), InventoryObject[].class); ArrayList<InventoryObject> inventoryList = new ArrayList<InventoryObject>( Arrays.asList(inventoryArray)); String body = new String(request.getBody()); System.out.println(body); // Find existing item and remove it int id = Integer.parseInt(body.split("=")[1]); for (InventoryObject item : inventoryList) { if (item.getID() == id) { inventoryList.remove(item); break; } } // Serialize new list Gson gsonBuilder = new GsonBuilder().setPrettyPrinting().create(); String arrayListToJson = gsonBuilder.toJson(inventoryList); BufferedWriter bw = new BufferedWriter(new FileWriter(inventory)); bw.write(arrayListToJson); bw.close(); response = HttpResponseFactory.create200OK(inventory, Protocol.CLOSE); } catch (Exception e) { response = HttpResponseFactory.create404NotFound(Protocol.CLOSE); } return response; } }