Commit d6f4f667 by Patryk Czarnik

SOAP klient i serwer - DO POPRAWIENIA

parent 13eb02f5
......@@ -39,5 +39,8 @@
<module>model</module>
<module>baza</module>
<module>rest_klient</module>
<module>soap_api</module>
<module>soap_serwer</module>
<module>soap_klient</module>
</modules>
</project>
\ No newline at end of file
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.alx.kjava</groupId>
<artifactId>PC39-Wielomodulowy</artifactId>
<version>1.0</version>
</parent>
<artifactId>soap_api</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>baza</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package sklep.soap;
import java.math.BigDecimal;
import java.util.List;
import jakarta.jws.WebParam;
import jakarta.jws.WebResult;
import jakarta.jws.WebService;
import sklep.db.DBException;
import sklep.db.RecordNotFound;
import sklep.model.Customer;
import sklep.model.Order;
import sklep.model.Product;
// Na taki interfejs mówi się "service endpoint interface" (SEI)
@WebService
public interface Sklep {
@WebResult(name="product")
List<Product> wszystkieProdukty() throws DBException;
@WebResult(name="product")
Product produktWgId(@WebParam(name="id") int productId) throws DBException, RecordNotFound;
@WebResult(name="product")
List<Product> produktyWgCen(
@WebParam(name="min") BigDecimal minPrice,
@WebParam(name="max") BigDecimal maxPrice) throws DBException;
void zapiszProdukt(@WebParam(name="product") Product product) throws DBException;
@WebResult(name="order")
Order zamowienieWgId(@WebParam(name="id") int orderId) throws DBException, RecordNotFound;
@WebResult(name="customer")
Customer klient(@WebParam(name="email") String email) throws DBException, RecordNotFound;
@WebResult(name="bytes")
byte[] foto(@WebParam(name="id") int productId) throws DBException, RecordNotFound;
}
\ No newline at end of file
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.alx.kjava</groupId>
<artifactId>PC39-Wielomodulowy</artifactId>
<version>1.0</version>
</parent>
<artifactId>soap_klient</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>soap_api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>4.0.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package sklep.klient;
import java.util.List;
import sklep.db.DBException;
import sklep.model.Product;
import sklep.soap.Sklep;
public class Klient1_WszystkieProdukty {
public static void main(String[] args) {
System.out.println("Startujemy");
SklepService service = new SklepService();
Sklep sklep = service.getSklepPort();
System.out.println("Mam obiekt proxy: " + sklep);
try {
List<Product> produkty = sklep.wszystkieProdukty();
System.out.println("Odczytano " + produkty.size() + " produktów:");
for (Product product : produkty) {
System.out.println(product.getProductName() + " za cenę " + product.getPrice());
}
} catch (DBException e) {
e.printStackTrace();
}
System.out.println("Koniec");
}
}
package sklep.klient;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import jakarta.xml.ws.Service;
import jakarta.xml.ws.WebEndpoint;
import jakarta.xml.ws.WebServiceClient;
import jakarta.xml.ws.WebServiceFeature;
import sklep.soap.Sklep;
/**
* This class was generated by Apache CXF 3.5.2-jbossorg-4
* 2023-05-24T16:08:33.585+02:00
* Generated source version: 3.5.2-jbossorg-4
*
*/
@WebServiceClient(name = "SklepService",
wsdlLocation = "http://localhost:8080/PC31-SoapSerwer/Sklep?wsdl",
targetNamespace = "http://soap.sklep/")
public class SklepService extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("http://soap.sklep/", "SklepService");
public final static QName SklepPort = new QName("http://soap.sklep/", "SklepPort");
static {
URL url = null;
try {
url = new URL("http://localhost:8080/PC31-SoapSerwer/Sklep?wsdl");
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(SklepService.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "http://localhost:8080/PC31-SoapSerwer/Sklep?wsdl");
}
WSDL_LOCATION = url;
}
public SklepService(URL wsdlLocation) {
super(wsdlLocation, SERVICE);
}
public SklepService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public SklepService() {
super(WSDL_LOCATION, SERVICE);
}
public SklepService(WebServiceFeature ... features) {
super(WSDL_LOCATION, SERVICE, features);
}
public SklepService(URL wsdlLocation, WebServiceFeature ... features) {
super(wsdlLocation, SERVICE, features);
}
public SklepService(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns Sklep
*/
@WebEndpoint(name = "SklepPort")
public Sklep getSklepPort() {
return super.getPort(SklepPort, Sklep.class);
}
/**
*
* @param features
* A list of {@link jakarta.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns Sklep
*/
@WebEndpoint(name = "SklepPort")
public Sklep getSklepPort(WebServiceFeature... features) {
return super.getPort(SklepPort, Sklep.class, features);
}
}
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.alx.kjava</groupId>
<artifactId>PC39-Wielomodulowy</artifactId>
<version>1.0</version>
</parent>
<artifactId>soap_serwer</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>soap_api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>baza</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
package sklep.soap;
import java.math.BigDecimal;
import java.util.List;
import jakarta.jws.WebService;
import jakarta.xml.ws.soap.MTOM;
import sklep.db.CustomerDAO;
import sklep.db.DBConnection;
import sklep.db.DBException;
import sklep.db.OrderDAO;
import sklep.db.ProductDAO;
import sklep.db.RecordNotFound;
import sklep.model.Customer;
import sklep.model.Order;
import sklep.model.Product;
import sklep.photo.PhotoUtil;
@WebService(endpointInterface="sklep.soap.Sklep")
@MTOM
public class SklepImpl implements Sklep {
public List<Product> wszystkieProdukty() throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
return productDAO.readAll();
}
}
public Product produktWgId(int productId) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
return productDAO.findById(productId);
}
}
public List<Product> produktyWgCen(
BigDecimal minPrice,
BigDecimal maxPrice) throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
return productDAO.findByPrice(minPrice, maxPrice);
}
}
public void zapiszProdukt(Product product) throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
productDAO.save(product);
db.commit();
}
}
public Order zamowienieWgId(int orderId) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
OrderDAO orderDAO = db.orderDAO();
return orderDAO.findById(orderId);
}
}
public Customer klient(String email) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
CustomerDAO customerDAO = db.customerDAO();
return customerDAO.findByEmail(email);
}
}
public byte[] foto(int productId) throws DBException, RecordNotFound {
return PhotoUtil.readBytes(productId);
}
}
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