Commit c6141e9e authored by Bc. Edvard Rejthar's avatar Bc. Edvard Rejthar

second commit

parent b149f851
*.class
# Package Files #
*.jar
*.war
*.ear
/target
\ No newline at end of file
*.class
# Package Files #
*.jar
*.war
*.ear
/target
src/main/webapp/script.js
AOS / jersey_restful_webservice
=========================
Tento projekt je semestrální prací předmětu AOS
https://cw.felk.cvut.cz/wiki/courses/a4m36aos/cviceni/semestral_work#single_entry_point
\ No newline at end of file
jersey_restful_webservice
=========================
Jersey 2.5.1 RESTful Web Service with JPA 2.1 implemented using EclipseLink 2.5.2 and using Derby 10.10.1.1 In Memory Database
This is an example of an RESTful WebService using Jersey for the JAX-RS implementation, Genson for the JSON to Java / Java to JSON data binding, EclipseLink for the JPA implementation and Derby for the In Memory Database.
Jersey is Oracles implementation of the JAX-RS 2.0 API.
Genson simply binds the data from JSON to Java and vice versa, you do not have to configure POJO mapping in the web.xml or specify @XmlRootElement to POJO objects.
EclipseLink implements the Java JPA 2.0 API which is used for transaction management, persistence configuration and data binding between tables and POJOs via annotations.
Derby is used to store data also allowing you to run an embedded database, in memory database etc.
Jersey Test Framework 2.5.1 will execute unit tests and allow for the http response to be tested, this has been combined with Mockito to mock out the services however if you wish based out your configuration you can also perform end to end tests.
-------------------------
Test
-------------------------
Ensure the context root is set to jrws and post of web server is 8080 (for example purposes)
Once you have started up the application
http://localhost:8080/jrws
- Will navigate you to the index.jsp to ensure the application is running.
----------------------
Example Restful Calls
----------------------
Ensure you set the content-type in your request to application/json
SAVE:
url = http://localhost:8080/jrws/rest/customer/save
method = POST
data = {"id":"1", "firstName":"Robert", "lastName":"Leggett"}
RETRIEVE:
url = http://localhost:8080/jrws/rest/customer/retrieve/1
method = GET
DELETE:
url = http://localhost:8080/jrws/rest/customer/delete
method = POST
data = 1
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/src/main/webapp" relative="/" />
</webroots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-server:1.14" level="project" />
<orderEntry type="library" name="Maven: asm:asm:3.1" level="project" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-core:1.14" level="project" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-bundle:1.14" level="project" />
<orderEntry type="library" name="Maven: javax.ws.rs:jsr311-api:1.1.1" level="project" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-json:1.14" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jettison:jettison:1.1" level="project" />
<orderEntry type="library" name="Maven: stax:stax-api:1.0.1" level="project" />
<orderEntry type="library" name="Maven: com.sun.xml.bind:jaxb-impl:2.2.3-1" level="project" />
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.2.2" level="project" />
<orderEntry type="library" name="Maven: javax.xml.stream:stax-api:1.0-2" level="project" />
<orderEntry type="library" name="Maven: javax.activation:activation:1.1" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-jaxrs:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-xc:1.9.2" level="project" />
<orderEntry type="library" name="Maven: com.owlike:genson:0.98" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:eclipselink:2.5.2-M1" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:javax.persistence:2.1.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:commonj.sdo:2.1.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.derby:derby:10.10.1.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.jersey.test-framework:jersey-test-framework-core:2.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.jersey.core:jersey-server:2.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.jersey.core:jersey-common:2.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: javax.ws.rs:javax.ws.rs-api:2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: javax.annotation:javax.annotation-api:1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.google.guava:guava:14.0.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.hk2:hk2-api:2.2.0-b21" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.hk2:hk2-utils:2.2.0-b21" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.hk2.external:javax.inject:2.2.0-b21" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.hk2:hk2-locator:2.2.0-b21" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.hk2.external:asm-all-repackaged:2.2.0-b21" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.hk2.external:cglib:2.2.0-b21" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.hk2:osgi-resource-locator:1.0.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.jersey.core:jersey-client:2.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: javax.validation:validation-api:1.1.0.Final" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-jetty:2.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.glassfish.jersey.containers:jersey-container-jetty-http:2.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-server:9.0.6.v20130930" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty.orbit:javax.servlet:3.0.0.v201112011016" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-http:9.0.6.v20130930" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-util:9.0.6.v20130930" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-io:9.0.6.v20130930" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-continuation:9.0.6.v20130930" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.9.5" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:1.0" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.6" level="project" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>ide</org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>
<org-netbeans-modules-projectapi.jsf_2e_language>JSP</org-netbeans-modules-projectapi.jsf_2e_language>
</properties>
</project-shared-configuration>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.aos</groupId>
<artifactId>airline</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Airline</name>
<properties>
<jersey.version>2.5.1</jersey.version>
<genson.version>0.98</genson.version>
<eclipselink.version>2.5.2-M1</eclipselink.version>
<derby.version>10.10.1.1</derby.version>
<junit.version>4.8.2</junit.version>
<mockito.version>1.9.5</mockito.version>
<mysqlconnector.version>5.1.6</mysqlconnector.version>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>${genson.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>${derby.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysqlconnector.version}</version>
</dependency>
</dependencies>
<build>
<finalName>airline</finalName>
</build>
<developers>
<developer>
<name>Edvard Rejthar</name>
<roles>
<role>Team</role>
</roles>
</developer>
</developers>
</project>
\ No newline at end of file
package cz.aos.api;
import cz.aos.api.data.Destination;
import cz.aos.persistence.PersistenceException;
import cz.aos.service.DestinationService;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
/**
* @author rejthedv
*/
@Path(value = "destination")
public class DestinationResource {
protected DestinationService service;
public DestinationResource() {
this.service = new DestinationService();
}
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Response getAll() {
ArrayList<Destination> list = this.service.find();
Response.ResponseBuilder builder = Response.ok(list);
return builder.build();
}
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Destination getData(@PathParam("id") Long id) {
return this.service.find(id);
}
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveDestination(Destination data) {
try {
this.service.create(data);
return Response.status(Response.Status.OK).entity("Destination " + data.getId() + " created.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@PUT
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Response updateData(@PathParam("id") Long id, Destination data) {
try {
this.service.update(id, data);
return Response.status(Response.Status.OK).entity("Destination " + data.getId() + " updated.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@DELETE
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteData(@PathParam("id") Long id) {
try {
this.service.delete(id);
return Response.status(Response.Status.OK).entity("Destination " + id + " deleted.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
}
package cz.aos.api;
import cz.aos.api.data.Flight;
import cz.aos.persistence.PersistenceException;
import cz.aos.service.FlightService;
import cz.aos.service.ServiceException;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
/**
* @author rejthedv
*/
@Path(value = "flight")
public class FlightResource {
protected FlightService service;
public FlightResource() {
this.service = new FlightService();
}
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Response getAll(
@HeaderParam("X-Filter") String filter,
@HeaderParam("X-Base") String b,
@HeaderParam("X-Offset") String of,
@HeaderParam("X-Order") String order
) {
int limit = b == null ? 0 : Integer.parseInt(b);
int offset = of == null ? 0 : Integer.parseInt(of);
ArrayList<Flight> list;
try {
list = this.service.find(offset, limit, order, filter);
} catch (PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
Response.ResponseBuilder builder = Response.ok(list);
if (b == null) {
builder.header("X-Count-records", list.size());
}
return builder.build();
}
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Flight getData(@PathParam("id") Long id) {
return this.service.find(id);
}
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveFlight(Flight data) {
try {
this.service.create(data);
return Response.status(Response.Status.OK).entity("Flight " + data.getId() + " created.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
} catch(ServiceException e) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@PUT
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Response updateData(@PathParam("id") Long id, Flight data) {
try {
this.service.update(id, data);
return Response.status(Response.Status.OK).entity("Flight " + data.getId() + " updated.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
} catch(ServiceException e) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@DELETE
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteData(@PathParam("id") Long id) {
try {
this.service.delete(id);
return Response.status(Response.Status.OK).entity("Flight " + id + " deleted.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
}
package cz.aos.api;
import cz.aos.api.data.Reservation;
import cz.aos.persistence.PersistenceException;
import cz.aos.service.ReservationService;
import cz.aos.service.ServiceException;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
/**
* @author rejthedv
*/
@Path(value = "reservation")
public class ReservationResource {
protected ReservationService service;
public ReservationResource() {
this.service = new ReservationService();
}
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Response getAll() {
ArrayList<Reservation> list = this.service.find();
Response.ResponseBuilder builder = Response.ok(list);
return builder.build();
}
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Reservation getData(@PathParam("id") Long id) {
return this.service.find(id);
}
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveReservation(Reservation data) {
try {
this.service.create(data);
return Response.status(Response.Status.OK).entity("Reservation " + data.getId() + " created.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
} catch(ServiceException e) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@PUT
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Response updateData(@PathParam("id") Long id, Reservation data) {
try {
this.service.update(id, data);
return Response.status(Response.Status.OK).entity("Reservation " + data.getId() + " updated.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
} catch(ServiceException e) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@DELETE
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteData(@PathParam("id") Long id) {
try {
this.service.delete(id);
return Response.status(Response.Status.OK).entity("Reservation " + id + " deleted.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
}
package cz.aos.api;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
/**
* @author rejthedv
*/
@Path(value = "generic")
public class RootResource {
@GET
public String get() {
System.out.println("f");
return "fd";
}
}
package cz.aos.api.data;
import java.io.Serializable;
/**
* @author rejthedv
*/
public class Destination implements Serializable {
private Long id;
private String name;
private double latitude;
private double longitude;
private String url;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
package cz.aos.api.data;
import java.io.Serializable;
import java.util.Date;
/**
* @author rejthedv
*/
public class Flight implements Serializable {
private Long id;
private String name;
private int from;
private int to;
private String dateOfDeparture;
private double distance;
private double price;
private int seats;
private String url;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public int getTo() {
return to;
}
public void setTo(int to) {
this.to = to;
}
public String getDateOfDeparture() {
return dateOfDeparture;
}
public void setDateOfDeparture(String dateOfDeparture) {
this.dateOfDeparture = dateOfDeparture;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
package cz.aos.api.data;
import java.io.Serializable;
import java.util.Date;