Commit 73fa7e57 authored by Bc. Edvard Rejthar's avatar Bc. Edvard Rejthar

checkpoint 2

parent ed44256f
......@@ -97,6 +97,12 @@
<version>1.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.glassfish.metro</groupId>
<artifactId>webservices-rt</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
......
......@@ -40,6 +40,7 @@ public class DestinationResource {
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Response getAll() {
System.out.println("get all destinations...");
ArrayList<Destination> list = this.service.find();
Response.ResponseBuilder builder = Response.ok(list);
return builder.build();
......@@ -96,6 +97,7 @@ public class DestinationResource {
data.setLatitude(Double.parseDouble(lat));
data.setLongitude(Double.parseDouble(lng));
this.service.create(data);
System.out.println(data.getLatitude());
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();
......
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 java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import org.apache.derby.iapi.types.XML;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
* @author rejthedv
*/
@Path(value = "reservation/{reservation_id}/payment")
public class PaymentResource {
@GET
public String get() {
System.out.println("get payment");
return "get payment returned";
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public String post(@PathParam("reservation_id") Long id, JSONObject json) {
ReservationService service = new ReservationService();
Reservation r = service.find(id);
r.setState(Reservation.PAID);
try {
service.update(id, r);
System.out.println(json.getString("account-number") + " - json");
} catch (JSONException ex) {
Logger.getLogger(PaymentResource.class.getName()).log(Level.SEVERE, null, ex);
} catch (PersistenceException ex) {
Logger.getLogger(PaymentResource.class.getName()).log(Level.SEVERE, null, ex);
} catch (ServiceException ex) {
Logger.getLogger(PaymentResource.class.getName()).log(Level.SEVERE, null, ex);
}
return "prijmuto json JOO" + r + r.getState();
}
@POST
@Consumes(MediaType.APPLICATION_XML)
public String postXml(@PathParam("reservation_id") Long id, Object xml) {
ReservationService service = new ReservationService();
Reservation r = service.find(id);
r.setState(Reservation.PAID);
System.out.println("xml");
try {
service.update(id, r);
} catch (PersistenceException ex) {
Logger.getLogger(PaymentResource.class.getName()).log(Level.SEVERE, null, ex);
} catch (ServiceException ex) {
Logger.getLogger(PaymentResource.class.getName()).log(Level.SEVERE, null, ex);
}
return "prijmuto xml";
}
}
package cz.aos.api;
import cz.aos.api.data.Reservation;
import cz.aos.persistence.PersistenceException;
import cz.aos.service.ReservationService;
......@@ -8,6 +9,7 @@ import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import javax.annotation.security.RolesAllowed;
/**
* @author rejthedv
......@@ -15,65 +17,84 @@ import java.util.ArrayList;
@Path(value = "reservation")
public class ReservationResource {
protected ReservationService service;
protected ReservationService service;
public ReservationResource() {
this.service = new ReservationService();
}
public ReservationResource() {
this.service = new ReservationService();
}
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Response getAll() {
System.out.println("get all reservations...");
ArrayList<Reservation> list = this.service.find();
Response.ResponseBuilder builder = Response.ok(list);
return builder.build();
}
@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, @HeaderParam("x-Password") String password) {
System.out.println("get single reservation...");
Reservation res = this.service.find(id);
if (checkPassword(res, password)) {
System.out.println("check ok");
return res;
} else {
System.out.println("check failed");
return null;
}
}
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Reservation getData(@PathParam("id") Long id) {
return this.service.find(id);
}
protected boolean checkPassword(Reservation res, String password) {
return (res.getPassword().equals(password));
}
@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();
}
}
@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();
}
}
@PUT
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Response updateData(@PathParam("id") Long id, Reservation data, @HeaderParam("x-Password") String password) {
if (checkPassword(this.service.find(id), password)) {
System.out.println("check ok");
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();
}
} else {
System.out.println("check failed");
return null;
}
}
@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();
}
}
@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";
}
}
......@@ -7,6 +7,9 @@ import java.util.Date;
* @author rejthedv
*/
public class Reservation implements Serializable {
public final static String PAID = "PAID";
public final static String NEW = "NEW";
private Long id;
private int flight;
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tickets;
import cz.aos.api.data.Reservation;
import cz.aos.service.ReservationService;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
/**
*
* @author rejthedv
*/
@WebService(serviceName = "Tickets")
public class Tickets {
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
try {
System.out.println("WEB service jede!met");
String str = "This is a String ~ GoGoGo";
InputStream is = new ByteArrayInputStream(str.getBytes());
} catch (Exception ex) {
Logger.getLogger(Tickets.class.getName()).log(Level.SEVERE, null, ex);
}
return "Hello " + txt + " !";
}
/**
* Web service operation
*/
@WebMethod(operationName = "getFile")
public String getFile(@WebParam(name = "reservation") String id) {
System.out.println("GET FILE BEGIN");
ReservationService service = new ReservationService();
Reservation res = service.find(Long.parseLong(id));
System.out.println("RES");
if ("PAID".equals(res.getState())) {
System.out.println("PAID");
return "letenka file" + id;
}
System.out.println("not in paid state");
return "not in paid state";
}
/**
* Web service operation
*/
@WebMethod(operationName = "getFileFromReservationJson")
public String getFileFromReservationJson(@WebParam(name = "reservation") Reservation reservation) {
if ("PAID".equals(reservation.getState())) {
System.out.println("PAID");
return "letenka file" + reservation.getId();
}
System.out.println("not in paid state");
return "not in paid state";
}
}
......@@ -10,7 +10,7 @@
<security-role-mapping>
<role-name>admin</role-name>
<principal-name>b</principal-name>
<principal-name>manager</principal-name>
</security-role-mapping>
<security-role-mapping>
<role-name>admin</role-name>
......@@ -20,13 +20,5 @@
<role-name>admin</role-name>
<principal-name>admin</principal-name>
</security-role-mapping>
<security-role-mapping>
<role-name>admin</role-name>
<principal-name>manager</principal-name>
</security-role-mapping>
<security-role-mapping>
<role-name>admin</role-name>
<principal-name>a</principal-name>
</security-role-mapping>
</glassfish-web-app>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
<security-role-mapping>
<role-name>admin</role-name>
<group-name>admin</group-name>
</security-role-mapping>
</sun-web-app>
\ No newline at end of file
......@@ -39,7 +39,7 @@ see implementation details http://jersey.java.net/nonav/documentation/latest/jax
</auth-constraint>
</security-constraint>
<security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>AdminFlight</web-resource-name>
<url-pattern>/source/flight/*</url-pattern>
......@@ -51,6 +51,18 @@ see implementation details http://jersey.java.net/nonav/documentation/latest/jax
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>AdminReservation</web-resource-name>
<url-pattern>/source/reservation/</url-pattern>
<http-method>GET</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<servlet-mapping>
......
......@@ -28,6 +28,10 @@
<a id="createReservation" href="#createReservation">create</a>
<a id="listReservations" href="#listReservations">list</a>
</li>
<li>
Payment:
<a id="makePayment" href="#makePayment">create</a>
</li>
</ul>
</nav>
<div id="content"></div>
......
......@@ -4,7 +4,7 @@ headers = {};
//
//pickerformat = "'T'";//-MM-DDThh:mm:ssTZD
var Client, DestinationForm, DestinationList, FlightForm, FlightList, ReservationForm, ReservationList, client,
var Client, DestinationForm, PaymentForm, DestinationList, FlightForm, FlightList, ReservationForm, ReservationList, client,
__bind = function (fn, me) {
return function () {
return fn.apply(me, arguments);
......@@ -19,6 +19,7 @@ Client = (function () {
this.showReservationForm = __bind(this.showReservationForm, this);
this.showDestinationsList = __bind(this.showDestinationsList, this);
this.showDestinationForm = __bind(this.showDestinationForm, this);
this.showPaymentForm = __bind(this.showPaymentForm, this);
this.showFlightsList = __bind(this.showFlightsList, this);
this.showFlightForm = __bind(this.showFlightForm, this);
this.setUpListeners = __bind(this.setUpListeners, this);
......@@ -32,9 +33,10 @@ Client = (function () {
Client.prototype.setUpListeners = function () {
document.getElementById("createFlight").addEventListener('click', this.showFlightForm, false);
document.getElementById("listFlights").addEventListener('click', this.showFlightsList, false);
document.getElementById("makePayment").addEventListener('click', this.showPaymentForm, false);
document.getElementById("createDestination").addEventListener('click', this.showDestinationForm, false);
document.getElementById("listDestinations").addEventListener('click', this.showDestinationsList, false);
document.getElementById("createReservation").addEventListener('click', this.showReservationForm, false);
document.getElementById("createReservation").addEventListener('click', this.showReservationForm, false);
return document.getElementById("listReservations").addEventListener('click', this.showReservationsList, false);
};
......@@ -52,6 +54,11 @@ Client = (function () {
var form;
return form = new DestinationForm(this.content);
};
Client.prototype.showPaymentForm = function (e) {
var form;
return form = new PaymentForm(this.content);
};
Client.prototype.showDestinationsList = function (e) {
var list;
......@@ -128,6 +135,55 @@ FlightForm = (function () {
})();
PaymentForm = (function () {
function PaymentForm(content) {
this.content = content;
this.sendForm = __bind(this.sendForm, this);
this.createForm = __bind(this.createForm, this);
$(this.content).empty();
this.createForm();
$('form').submit(this.sendForm);
}
PaymentForm.prototype.createForm = function () {
var form, table, tbody;
form = document.createElement("form");
form.method = "post";
form.action = "#";
table = document.createElement("table");
tbody = document.createElement("tbody");
tbody.innerHTML = this.getForm();
table.appendChild(tbody);
form.appendChild(table);
this.content.appendChild(form);
return form;
};
PaymentForm.prototype.sendForm = function (e) {
var ar;
e.preventDefault();
ar = $(e.target).serializeArray();
this.data = {};
this.data[ar[0].name] = ar[0].value;
this.data["url"] = "";
$.ajax({
url: 'http://localhost:8080/airline/source/reservation/'+Number(ar[1].value)+'/payment',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(this.data)
});
return $(this.content).empty();
};
PaymentForm.prototype.getForm = function () {
return "<tr> <th>Account-number: </th> <td><input type=\"text\" name=\"account-number\" id=\"account-number\"></td> </tr> <tr> <th>Reservation id: </th> <td><input type=\"text\" name=\"reservation-id\" id=\"reservation-id\"></td> </tr> <tr> <th></th> <td><input type=\"submit\" value=\"Pay\"></td> </tr>";
};
return PaymentForm;
})();
DestinationForm = (function () {
function DestinationForm(content) {
this.content = content;
......@@ -512,7 +568,7 @@ ReservationList = (function () {
$.ajax({
url: 'http://localhost:8080/airline/source/reservation',
type: 'GET',
dataType: 'json'
dataType: 'json'
}).done(this.printList);
}
......@@ -571,7 +627,8 @@ ReservationList = (function () {
return $.ajax({
url: 'http://localhost:8080/airline/source/reservation/' + this.id,
type: 'GET',
dataType: 'json'
dataType: 'json',
headers: { 'x-password': prompt("Password") }
}).done(this.createForm);
};
......@@ -606,7 +663,8 @@ ReservationList = (function () {
type: 'PUT',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(this.data)
data: JSON.stringify(this.data),
headers: { 'x-password': prompt("Password") }
});
return $(this.content).empty();
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment