Java tutorial
package i5.las2peer.services.todolist; import java.net.HttpURLConnection; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import com.fasterxml.jackson.core.JsonProcessingException; import i5.las2peer.api.Service; import i5.las2peer.restMapper.HttpResponse; import i5.las2peer.restMapper.MediaType; import i5.las2peer.restMapper.RESTMapper; import i5.las2peer.restMapper.annotations.ContentParam; import i5.las2peer.restMapper.annotations.Version; import i5.las2peer.services.todolist.database.DatabaseManager; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import io.swagger.annotations.Contact; import io.swagger.annotations.Info; import io.swagger.annotations.License; import io.swagger.annotations.SwaggerDefinition; import io.swagger.jaxrs.Reader; import io.swagger.models.Swagger; import io.swagger.util.Json; import net.minidev.json.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; /** * * ToDoList-Microservice * * This microservice was generated by the CAE (Community Application Editor). If you edit it, please * make sure to keep the general structure of the file and only add the body of the methods provided * in this main file. Private methods are also allowed, but any "deeper" functionality should be * outsourced to (imported) classes. * */ @Path("/todolist") @Version("0.1") // this annotation is used by the XML mapper @Api @SwaggerDefinition(info = @Info(title = "ToDoList-Microservice", version = "0.1", description = "A LAS2peer microservice generated by the CAE.", termsOfService = "none", contact = @Contact(name = "arifin", email = "CAEAddress@gmail.com"), license = @License(name = "BSD", url = "https://github.com/CAE-Dev/microservice-ToDoList-Microservice/blob/master/LICENSE.txt"))) public class Todolist extends Service { /* * Database configuration */ private String jdbcDriverClassName; private String jdbcLogin; private String jdbcPass; private String jdbcUrl; private String jdbcSchema; private DatabaseManager dbm; public Todolist() { // read and set properties values setFieldValues(); // instantiate a database manager to handle database connection pooling and credentials dbm = new DatabaseManager(jdbcDriverClassName, jdbcLogin, jdbcPass, jdbcUrl, jdbcSchema); } // ////////////////////////////////////////////////////////////////////////////////////// // Service methods. // ////////////////////////////////////////////////////////////////////////////////////// /** * * getData * * * @return HttpResponse * */ @GET @Path("/data") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_PLAIN) @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "responseGetData") }) @ApiOperation(value = "getData", notes = "") public HttpResponse getData() { JSONObject dataJson = new JSONObject(); Connection conn = null; try { conn = dbm.getConnection(); PreparedStatement stmt = conn .prepareStatement("SELECT * FROM gamificationCAE.todolist ORDER BY id ASC"); ResultSet rs = stmt.executeQuery(); while (rs.next()) { dataJson.put(rs.getInt("id"), rs.getString("name")); } if (!dataJson.isEmpty()) { return new HttpResponse(dataJson.toJSONString(), HttpURLConnection.HTTP_OK); } return new HttpResponse("No data Found", HttpURLConnection.HTTP_OK); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return new HttpResponse("Database connection error", HttpURLConnection.HTTP_INTERNAL_ERROR); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * * createData * * * @return HttpResponse * */ @POST @Path("/data/{name}") @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.TEXT_PLAIN) @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "responseCreateData") }) @ApiOperation(value = "createData", notes = "") public HttpResponse createData(@PathParam("name") String name) { Connection conn = null; try { conn = dbm.getConnection(); PreparedStatement stmt = conn .prepareStatement("INSERT INTO gamificationCAE.todolist (name) VALUES (?)"); stmt.setString(1, name); stmt.executeUpdate(); return new HttpResponse(name + " is added!", HttpURLConnection.HTTP_OK); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return new HttpResponse("Database connection error", HttpURLConnection.HTTP_INTERNAL_ERROR); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * * deleteData * * * @return HttpResponse * */ @DELETE @Path("/data/{id}") @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.TEXT_PLAIN) @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "responseDeleteData") }) @ApiOperation(value = "deleteData", notes = "") public HttpResponse deleteData(@PathParam("id") Integer id) { Connection conn = null; try { conn = dbm.getConnection(); PreparedStatement stmt = conn.prepareStatement("DELETE FROM gamificationCAE.todolist WHERE id = ?"); stmt.setInt(1, id); stmt.executeUpdate(); stmt = conn.prepareStatement("ALTER TABLE todolist AUTO_INCREMENT = 1;"); stmt.executeUpdate(); return new HttpResponse("data number " + id + " is deleted!", HttpURLConnection.HTTP_OK); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return new HttpResponse("Database connection error", HttpURLConnection.HTTP_INTERNAL_ERROR); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } // ////////////////////////////////////////////////////////////////////////////////////// // Methods required by the LAS2peer framework. // ////////////////////////////////////////////////////////////////////////////////////// /** * * This method is needed for every RESTful application in LAS2peer. Please don't change. * * @return the mapping * */ public String getRESTMapping() { String result = ""; try { result = RESTMapper.getMethodsAsXML(this.getClass()); } catch (Exception e) { e.printStackTrace(); } return result; } }