mirror of
https://github.com/svek95/WST_Labs.git
synced 2026-05-18 02:51:45 +03:00
Add Lab6
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package Lab6;
|
||||||
|
|
||||||
|
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
|
||||||
|
import com.sun.jersey.api.core.PackagesResourceConfig;
|
||||||
|
import com.sun.jersey.api.core.ResourceConfig;
|
||||||
|
import org.glassfish.grizzly.http.server.HttpServer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
private static final URI BASE_URI = URI.create("http://localhost:8081/rest/");
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
HttpServer server = null;
|
||||||
|
try {
|
||||||
|
ResourceConfig resourceConfig = new PackagesResourceConfig(CarExceptionsResource.class.getPackage().getName());
|
||||||
|
server = GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
|
||||||
|
server.start();
|
||||||
|
System.in.read();
|
||||||
|
stopServer(server);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
stopServer(server);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void stopServer(HttpServer server) {
|
||||||
|
if (server != null)
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package Lab6;
|
||||||
|
|
||||||
|
//import Lab3.Exception.CarServiceFault;
|
||||||
|
//import Lab3.Exception.WrongValueExcept;
|
||||||
|
import Lab6.Exception.WrongValueException;
|
||||||
|
import Lab4.common.Car;
|
||||||
|
import Lab5.CarsDAO;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
@Path("/cars")
|
||||||
|
@Produces({MediaType.APPLICATION_JSON})
|
||||||
|
public class CarExceptionsResource {
|
||||||
|
private final static CarsDAO carDao = new CarsDAO();
|
||||||
|
|
||||||
|
/// Find
|
||||||
|
@GET
|
||||||
|
public List<Car> findCars(@QueryParam("Name") String name,
|
||||||
|
@QueryParam("model") String model,
|
||||||
|
@QueryParam("country") String country,
|
||||||
|
@QueryParam("dateOfSales") Date dateOfSales,
|
||||||
|
@QueryParam("power") Integer power) {
|
||||||
|
return carDao.getDataByFields(name, model, country, dateOfSales, power);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// insert
|
||||||
|
@POST
|
||||||
|
@Path("/saves")
|
||||||
|
public Integer insertCars(@QueryParam("Name") String name,
|
||||||
|
@QueryParam("model") String model,
|
||||||
|
@QueryParam("country") String country,
|
||||||
|
@QueryParam("dateOfSales") Date dateOfSales,
|
||||||
|
@QueryParam("power") Integer power) throws WrongValueException {
|
||||||
|
if (power < 1 || power > 2000) {
|
||||||
|
// CarServiceFault fault = new CarServiceFault("insert", "ERROR insert, power not value");
|
||||||
|
throw new WrongValueException("insert", "Power must be between 1 PS and 2000 PS");
|
||||||
|
} else {
|
||||||
|
return carDao.insertDB(name, model, country, dateOfSales, power);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Update id
|
||||||
|
@PUT
|
||||||
|
@Path("/update/ID")
|
||||||
|
public String updateCars(@QueryParam("id") Integer id,
|
||||||
|
@QueryParam("Name") String name,
|
||||||
|
@QueryParam("model") String model,
|
||||||
|
@QueryParam("country") String country,
|
||||||
|
@QueryParam("dateOfSales") Date dateOfSales,
|
||||||
|
@QueryParam("power") Integer power) {
|
||||||
|
return String.valueOf(carDao.updateDB(id, name, model, country, dateOfSales, power));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete
|
||||||
|
|
||||||
|
@Path("/delete/ID")
|
||||||
|
@DELETE
|
||||||
|
public String deleteCars(@QueryParam("id") Integer id) {
|
||||||
|
|
||||||
|
return carDao.deleteDB(id).getStringValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package Lab6.Exception;
|
||||||
|
|
||||||
|
public class WrongValueException extends Exception {
|
||||||
|
private static final String MESSAGE_TEMPLATE = "Field value";
|
||||||
|
|
||||||
|
public WrongValueException(String FeilName, String possValues){
|
||||||
|
super(String.format(MESSAGE_TEMPLATE, FeilName, possValues));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user