diff --git a/MrBean.java b/MrBean.java new file mode 100644 index 0000000..5061428 --- /dev/null +++ b/MrBean.java @@ -0,0 +1,110 @@ +package koddas.web.war; + +import java.io.Serializable; + +/** + * MrBean is a simple example of a Java bean; a small class that encapsulates a + * number of values. It exposes an empty constructor and has a number of + * members that are accessible through getters and setters. + * + * @author Johan Holmberg + */ +public class MrBean implements Serializable { + /** + * Being a serializable class, the bean needs a unique identification + * number in order for the Java system to recreate saved objects. + */ + private static final long serialVersionUID = -3168349974480377280L; + + private String name; + private int age; + private String nationality; + private String carBrand; + + /** + * Empty constructor. + * + * Gson uses reflection to construct beans. In order to be able to do so, a + * bean must expose a constructor that doesn't take any arguments. Its + * attributes will be set by Gson upon object creation. + */ + public MrBean() { + // Let's just give the class members some standard values + name = "Rowan Atkinson"; + age = 60; + nationality = "British"; + carBrand = "Leyland Mini"; + } + + /** + * Returns the name of the bean. + * + * @return The name. + */ + public String getName() { + return name; + } + + /** + * Gives the bean a new name. + * + * @param name The new name. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Returns the age of the bean. + * + * @return The age. + */ + public int getAge() { + return age; + } + + /** + * Gives the bean a new age. + * + * @param age The new age. + */ + public void setAge(int age) { + this.age = age; + } + + /** + * Returns the nationality of the bean. + * + * @return The nationality. + */ + public String getNationality() { + return nationality; + } + + /** + * Gives the bean a new nationality. + * + * @param nationality The new nationality. + */ + public void setNationality(String nationality) { + this.nationality = nationality; + } + + /** + * Returns the car brand of the bean. + * + * @return The car brand. + */ + public String getCarBrand() { + return carBrand; + } + + /** + * Gives the bean a new car brand. + * + * @param carBrand The new car brand. + */ + public void setCarBrand(String carBrand) { + this.carBrand = carBrand; + } +} diff --git a/WebService.java b/WebService.java new file mode 100644 index 0000000..0a5beff --- /dev/null +++ b/WebService.java @@ -0,0 +1,136 @@ +package koddas.web.war; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.google.gson.Gson; + +/** + * A very simple web service. + * + * @author Johan Holmberg + */ +@Path("service") // By altering the argument, you'll change the service's URL +public class WebService { + + /** + * Prints "It's working" when /wwp-1.0.0/webapi/service is accessed. + * + * @return A web response. + */ + @GET // This endpoint will be available using GET and GET only + @Produces(MediaType.TEXT_PLAIN) // The response will be in plain text. + public Response root() { + return Response.ok("It's working").build(); + } + + /** + * Prints "Hello, World!" when /wwp-1.0.0/webapi/service/hello is accessed. + * + * @return A web response. + */ + @GET + @Path("/hello") // This is appended to your URL, that is "service/hello" + @Produces(MediaType.TEXT_PLAIN) + public Response hello() { + return Response.ok("Hello, World!").build(); + } + + /** + * Prints current time when /wwp-1.0.0/webapi/service/time is accessed. + * + * @return A web response. + */ + @GET + @Path("/time") + @Produces(MediaType.TEXT_PLAIN) + public Response time() { + Response response = null; + DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss"); + Date dateobj = new Date(); + + response = Response.ok(df.format(dateobj)).build(); + + return response; + } + + /** + * Returns a JSON representation of a MrBean object when + * /wwp-1.0.0/webapi/service/send is accessed. + * + * @param name Mr Bean's new name + * @param age Mr Bean's new age + * @param nationality Mr Bean's new nationality + * @param carBrand The brand of Mr Bean's new car + * @return A web response. + */ + @POST + @Path("/send") + @Produces(MediaType.APPLICATION_JSON) + public Response send(@FormParam("name") String name, + @FormParam("age") int age, + @FormParam("nationality") String nationality, + @FormParam("car_brand") String carBrand) { + Gson gson = new Gson(); + MrBean bean = new MrBean(); + + bean.setName(name); + bean.setAge(age); + bean.setNationality(nationality); + bean.setCarBrand(carBrand); + + gson.toJson(bean); + + return Response.ok(gson.toJson(bean)).build(); + } + + /** + * Returns a JSON representation of a MrBean object and a recipient when + * /wwp-1.0.0/webapi/service/send/{to} is accessed. + * + * The {to} part of the URL can be any URL-encoded string. + * + * @param to The name of the recipient of the Mr Bean object + * @param name Mr Bean's new name + * @param age Mr Bean's new age + * @param nationality Mr Bean's new nationality + * @param carBrand The brand of Mr Bean's new car + * @return A web response. + */ + @POST + @Path("/send/{to}") // {to} will map to the to parameter. + @Produces(MediaType.APPLICATION_JSON) + public Response send(@PathParam("to") String to, + @FormParam("name") String name, + @FormParam("age") int age, + @FormParam("nationality") String nationality, + @FormParam("car_brand") String carBrand) { + Gson gson = new Gson(); + MrBean bean = new MrBean(); + Map map = new HashMap(); + + bean.setName(name); + bean.setAge(age); + bean.setNationality(nationality); + bean.setCarBrand(carBrand); + + map.put("to", to); + map.put("mr_bean", bean); + + gson.toJson(map); + + return Response.ok(gson.toJson(map)).build(); + } +} diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..c94967a --- /dev/null +++ b/backup.sh @@ -0,0 +1,10 @@ +#!/bin/bash +mkdir -p /home/ec2-user/tmp2 +cd /home/ec2-user/tmp2 +git clone https://github.com/vinaycit/backup.git +echo "WELCOME" +cd backup +sudo cp -r /var/lib/jenkins /home/ec2-user/tmp2/backup/ +sudo git add . +sudo git commit -m "committed" +sudo git push https://github.com/vinaycit/backup.git master diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1b4cc73 --- /dev/null +++ b/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + koddas.web.war + wwp + 1.0.0 + war + WAR Web Project + A very simple Maven-enabled WAR web project + + + com.google.code.gson + gson + 2.3.1 + + + javax.ws.rs + javax.ws.rs-api + 2.0.1 + provided + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + \ No newline at end of file