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 InventoryAddNewItemServlet 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); String[] newItem = body.split("&"); // Add new item int id, quant; String name; try { id = Integer.parseInt(newItem[0].split("=")[1]); name = newItem[1].split("=")[1].replace("+", " "); quant = Integer.parseInt(newItem[2].split("=")[1]); } catch (Exception e) { return HttpResponseFactory.create400BadRequest(Protocol.CLOSE); } InventoryObject item = new InventoryObject(id, name, quant); if (quant < 0) { return HttpResponseFactory.create400BadRequest(Protocol.CLOSE); } for (InventoryObject existingItem : inventoryList) { if (existingItem.getID() == id) { return HttpResponseFactory.create400BadRequest(Protocol.CLOSE); } } inventoryList.add(item); // 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; } }