Commit f35f4c1c by Patryk Czarnik

Stary projekt wielomodułowy

parent 4cd727ee
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>P40-OgloszeniaParent</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding/<project>=UTF-8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-api</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-api">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="1.8"/>
<installed facet="jst.utility" version="1.0"/>
</faceted-project>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-api</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
package ogloszenia.rest;
import java.math.BigDecimal;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
@Path("/ogloszenia")
public interface IOgloszenia {
@GET
@Produces("application/xml")
public ListaOgloszen wszystkieOgloszenia() throws BladBazyDanych;
@POST
@Consumes("application/xml")
@Produces("application/xml")
public OgloszenieSamochodowe zapiszOgloszenie(OgloszenieSamochodowe ogloszenie) throws BladBazyDanych;
@Path("/{id}")
@GET
@Produces("application/xml")
public OgloszenieSamochodowe jednoOgloszenie(
@PathParam("id") int idOgloszenia) throws BladBazyDanych, NieznanyRekord;
@Path("/{id}/cena")
@GET
@Produces("text/plain")
public BigDecimal getCena(@PathParam("id") int idOgloszenia) throws BladBazyDanych, NieznanyRekord;
@Path("/{id}/cena")
@PUT
@Consumes("text/plain")
public void setCena(@PathParam("id") int idOgloszenia,
BigDecimal nowaCena) throws BladBazyDanych, NieznanyRekord;
@Path("/{id}")
@DELETE
public void usun(@PathParam("id") int idOgloszenia) throws BladBazyDanych, NieznanyRekord;
@Path("/{id}/foto")
@GET
@Produces("image/jpeg")
public byte[] foto(@PathParam("id") int idOgloszenia) throws NieznanyRekord;
}
package ogloszenia.soap;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
@WebService(targetNamespace=Nazwy.NAMESPACE,
name=Nazwy.PORT_TYPE_NAME)
public interface ISerwisOgloszeniowy {
@WebResult(name="ogloszenie")
public List<OgloszenieSamochodowe> wszystkieOgloszenia() throws BladBazyDanych;
@WebResult(name="ogloszenie")
public OgloszenieSamochodowe jednoOgloszenie(@WebParam(name="id-ogloszenia") int idOgloszenia)
throws BladBazyDanych, NieznanyRekord;
public void zapiszOgloszenia(@WebParam(name="ogloszenie") OgloszenieSamochodowe ogloszenie)
throws BladBazyDanych;
@WebResult(name="foto")
public byte[] foto(@WebParam(name="id-ogloszenia") int idOgloszenia) throws NieznanyRekord;
}
package ogloszenia.soap;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
/** Klasa definiująca stałe: nazwy i adresy używane w usłudze SOAP. */
public final class Nazwy {
private Nazwy() {}
public static final String NAMESPACE = "http://vavatech.pl/jws/SerwisOgloszeniowy";
public static final String SERVICE_NAME = "SerwisOgloszeniowy";
public static final String PORT_NAME = "SerwisOgloszeniowyPort";
public static final String PORT_TYPE_NAME = "SerwisOgloszeniowy";
public static final QName SERVICE_QNAME = new QName(NAMESPACE, SERVICE_NAME);
public static final QName PORT_QNAME = new QName(NAMESPACE, PORT_NAME);
/** Spodziewany adres WSDL na serwerze. */
public static final String WSDL = "http://localhost:8080/ogloszenia-soap/SerwisOgloszeniowy?wsdl";
public static final URL WSDL_URL = generateUrl(WSDL);
private static URL generateUrl(String address) {
try {
return new URL(address);
} catch(MalformedURLException e) {
return null;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-baza</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-baza">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="1.8"/>
<installed facet="jst.utility" version="1.0"/>
</faceted-project>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-baza</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.20.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
package ogloszenia;
/** W tej klasie trzeba wpisać swoje ustawienia ścieżek. */
public class Ustawienia {
public static final String SQLITE_PATH = "C:/Tools/ogloszenia/ogloszenia.db";
public static final String PHOTOS_PATH = "C:/Tools/ogloszenia/foto";
}
package ogloszenia.baza.sqlite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import ogloszenia.Ustawienia;
import ogloszenia.baza.sqlite.OgloszenieDAO;
import ogloszenia.baza.sqlite.SprzedawcaDAO;
import ogloszenia.exn.BladBazyDanych;
public class DostepDoBazySqlite implements AutoCloseable {
private Connection c;
DostepDoBazySqlite(Connection con) {
c = con;
}
public static DostepDoBazySqlite newSQLite(String sqliteFile) throws BladBazyDanych {
try {
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:" + sqliteFile);
return new DostepDoBazySqlite(con);
} catch (Exception e) {
throw new BladBazyDanych("Błąd podczas otwierania bazy danych ("+e.getMessage()+")", e);
}
}
public static DostepDoBazySqlite newSQLite() throws BladBazyDanych {
return newSQLite(Ustawienia.SQLITE_PATH);
}
@Override
public void close() {
try {
if(c != null)
c.close();
} catch (SQLException e) {
System.err.println("Błąd podczas close: " + e.getMessage());
}
}
Connection c() {
return c;
}
public void beginTransaction() throws BladBazyDanych {
try {
c.setAutoCommit(false);
//c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
} catch (SQLException e) {
throw new BladBazyDanych("beginTransaction: " + e.getMessage(), e);
}
}
public void endTransaction(boolean commit) throws BladBazyDanych {
try {
if(commit) {
c.commit();
} else {
c.rollback();
}
c.setAutoCommit(true);
} catch (SQLException e) {
throw new BladBazyDanych("endTransaction: " + e.getMessage(), e);
}
}
public OgloszenieDAO newOgloszenieDAO() {
return new OgloszenieDAO(this);
}
public SprzedawcaDAO newSprzedawcaDAO() {
return new SprzedawcaDAO(this);
}
}
package ogloszenia.baza.sqlite;
import java.time.LocalDateTime;
import ogloszenia.model.Paliwo;
public class Konwersje {
public static LocalDateTime dateFromString(String s) {
return s == null ? null : LocalDateTime.parse(s.replace(' ', 'T'));
}
public static String dateToString(LocalDateTime localDateTime) {
return localDateTime == null ? null : localDateTime.toString().replace('T', ' ');
}
public static Paliwo paliwoFromString(String s) {
return s == null ? null : Paliwo.valueOf(s.trim().toUpperCase());
}
public static String paliwoToString(Paliwo paliwo) {
return paliwo == null ? null : paliwo.toString();
}
}
package ogloszenia.baza.sqlite;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.Adres;
import ogloszenia.model.Sprzedawca;
public class SprzedawcaDAO {
private DostepDoBazySqlite db;
SprzedawcaDAO(DostepDoBazySqlite db) {
this.db = db;
}
public DostepDoBazySqlite getDBHandler() {
return db;
}
public List<Integer> listaIdSprzedawcow() throws BladBazyDanych {
final String sql = "SELECT id_sprzedawcy FROM sprzedawcy";
List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(rs.getInt(1));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("listaIdSprzedawcow: "+e.getMessage(), e);
}
return lista;
}
public Sprzedawca byId(int idSprzedawcy) throws BladBazyDanych, NieznanyRekord {
final String sql = "SELECT * FROM sprzedawcy WHERE id_sprzedawcy = ?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) {
if(rs.next()) {
return sprzedawcaZResultSet(rs);
} else {
throw new NieznanyRekord("Nieznany sprzedawca " + idSprzedawcy);
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca byId: "+e.getMessage(), e);
}
}
public List<Sprzedawca> readAll() throws BladBazyDanych {
final String sql = "SELECT * FROM sprzedawcy";
List<Sprzedawca> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
lista.add(sprzedawcaZResultSet(rs));
}
}
} catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca readAll: "+e.getMessage(), e);
}
return lista;
}
private Sprzedawca sprzedawcaZResultSet(ResultSet rs) throws SQLException {
Adres adres = new Adres(rs.getString("ulica"),
rs.getString("kod_pocztowy"), rs.getString("miasto"));
return new Sprzedawca(
rs.getInt("id_sprzedawcy"), rs.getString("nazwa"),
adres, rs.getString("telefon"), rs.getString("email")
);
}
}
package ogloszenia.baza.sqlite;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
public class TestInsertUpdate {
public static void main(String[] args) {
try(DostepDoBazySqlite baza = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = baza.newOgloszenieDAO();
OgloszenieSamochodowe auto = dao.byId(2);
auto.setIdOgloszenia(103);
auto.setOpis("Opis złotego Matiza");
dao.save(auto);
System.out.println("Gotowe");
} catch (BladBazyDanych | NieznanyRekord e) {
e.printStackTrace();
}
}
}
package ogloszenia.baza.sqlite;
import java.time.LocalTime;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
public class TestInsertWstawNowy {
public static void main(String[] args) {
try(DostepDoBazySqlite baza = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = baza.newOgloszenieDAO();
OgloszenieSamochodowe auto = dao.byId(3);
auto.setIdOgloszenia(null);
auto.setOpis("Nowy z godziny " + LocalTime.now());
dao.save(auto);
} catch (BladBazyDanych e) {
e.printStackTrace();
} catch (NieznanyRekord e) {
e.printStackTrace();
}
}
}
package ogloszenia.baza.sqlite;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.OgloszenieSamochodowe;
public class TestOdczytOgloszen {
public static void main(String[] args) {
try (DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite()) {
OgloszenieDAO dao = db.newOgloszenieDAO();
List<OgloszenieSamochodowe> lista = dao.readAllFull();
for (OgloszenieSamochodowe ogloszenie : lista) {
System.out.println(ogloszenie);
}
} catch (BladBazyDanych e) {
e.printStackTrace();
}
}
}
package ogloszenia.baza.sqlite;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.Sprzedawca;
public class TestOdczytSprzedawcow {
public static void main(String[] args) {
try (DostepDoBazySqlite db = DostepDoBazySqlite.newSQLite("ogloszenia.db")) {
SprzedawcaDAO dao = db.newSprzedawcaDAO();
List<Sprzedawca> lista = dao.readAll();
for (Sprzedawca sprzedawca : lista) {
System.out.println(sprzedawca);
}
} catch (BladBazyDanych e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-ear</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding/<project>=UTF-8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-ear-1.0">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/ear-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/application" tag="defaultRootSource"/>
<dependent-module archiveName="ogloszenia-soap-1.0.war" deploy-path="/" handle="module:/resource/ogloszenia-soap/ogloszenia-soap">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="ogloszenia-rest-1.0.war" deploy-path="/" handle="module:/resource/ogloszenia-rest/ogloszenia-rest">
<dependency-type>uses</dependency-type>
</dependent-module>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="jst.ear" version="1.3"/>
</faceted-project>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-ear</artifactId>
<packaging>ear</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<generateApplicationXml>true</generateApplicationXml>
<skinnyWars>true</skinnyWars>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-soap</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-rest</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-klient-rest</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-klient-rest</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.5.0.Final</version>
</dependency>
</dependencies>
</project>
package ogloszenia.klient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class Klient1_Generyczny {
public static void main(String[] args) {
System.out.println("Przygotowania...");
Client client = ClientBuilder.newClient();
WebTarget targetRoot = client.target(Ustawienia.ADRES);
WebTarget targetOgloszenia = targetRoot.path("/ogloszenia");
Invocation invocation = targetOgloszenia.request().buildGet();
System.out.println("Wysyłam zapytanie...");
Response response = invocation.invoke();
System.out.println("Przyszła odpowiedź");
System.out.println("status: " + response.getStatus());
System.out.println("media type: " + response.getMediaType());
String dane = response.readEntity(String.class);
System.out.println("Treść odpowiedzi:");
System.out.println("------------");
System.out.println(dane);
System.out.println("------------");
System.out.println("KONIEC");
// Nie można po raz kolejny wywołać readEntity - dane są odczytywane jednorazowo
// String dane2 = response.readEntity(String.class);
// System.out.println(dane2);
}
}
package ogloszenia.klient;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class Klient2_Generyczny {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Podaj numer ogłoszenia");
int numer = sc.nextInt();
System.out.println("Pobieram zdjęcie nr " + numer + "...");
Client client = ClientBuilder.newClient();
WebTarget targetRoot = client.target(Ustawienia.ADRES);
WebTarget targetFoto = targetRoot.path("/ogloszenia/{id}/foto");
Invocation invocation = targetFoto.resolveTemplate("id", numer)
.request().buildGet();
Response response = invocation.invoke();
int status = response.getStatus();
System.out.println("Przyszła odpowiedź");
System.out.println("status: " + status);
System.out.println("media type: " + response.getMediaType());
if(status == 200) {
InputStream stream = response.readEntity(InputStream.class);
try {
Files.copy(stream, Paths.get("foto" + numer + ".jpg"),
StandardCopyOption.REPLACE_EXISTING);
System.out.println("Gotowe, plik zapisany");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package ogloszenia.klient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
public class Klient3_JAXB {
public static void main(String[] args) {
System.out.println("Przygotowania...");
Client client = ClientBuilder.newClient();
WebTarget targetRoot = client.target(Ustawienia.ADRES);
WebTarget targetOgloszenia = targetRoot.path("/ogloszenia");
Invocation invocation = targetOgloszenia.request().buildGet();
System.out.println("Wysyłam zapytanie...");
Response response = invocation.invoke();
System.out.println("Przyszła odpowiedź");
System.out.println("status: " + response.getStatus());
System.out.println("media type: " + response.getMediaType());
ListaOgloszen lista = response.readEntity(ListaOgloszen.class);
System.out.println("Mam listę ogłoszeń:");
for(OgloszenieSamochodowe ogloszenie : lista.ogloszenia) {
System.out.println(ogloszenie.getMarka() + " " + ogloszenie.getModel() + " " + ogloszenie.getCena());
}
}
}
package ogloszenia.klient;
import java.math.BigDecimal;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.model.Paliwo;
import ogloszenia.model.Silnik;
public class Klient4_JAXB_Post {
public static void main(String[] args) {
OgloszenieSamochodowe noweOgloszenie = new OgloszenieSamochodowe(null, 2, null, new BigDecimal("33000.33"), "Nowe BMW", "Sprzedam fajne BMW", "BMW", "X5", null, "czarny", 2010, 99900, new Silnik(200f, 3.0f, Paliwo.BENZYNA));
System.out.println("Ogloszenie do zapisania: " + noweOgloszenie);
Client client = ClientBuilder.newClient();
WebTarget targetRoot = client.target(Ustawienia.ADRES);
WebTarget targetOgloszenia = targetRoot.path("/ogloszenia");
Entity<OgloszenieSamochodowe> entity = Entity.entity(noweOgloszenie, "application/xml");
Invocation invocation = targetOgloszenia.request().buildPost(entity);
System.out.println("Wysyłam zapytanie...");
Response response = invocation.invoke();
System.out.println("Przyszła odpowiedź");
System.out.println("status: " + response.getStatus());
System.out.println("media type: " + response.getMediaType());
OgloszenieSamochodowe zapisane = response.readEntity(OgloszenieSamochodowe.class);
System.out.println("Zapisano ogloszenie pod numerem: " + zapisane.getIdOgloszenia());
System.out.println(zapisane);
}
}
package ogloszenia.klient;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.rest.IOgloszenia;
public class Klient5_Proxy {
public static void main(String[] args) {
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(Ustawienia.ADRES);
IOgloszenia proxy = target.proxy(IOgloszenia.class);
System.out.println("Wysyłam zapytanie");
try {
ListaOgloszen lista = proxy.wszystkieOgloszenia();
System.out.println("Odczytałem " + lista.ogloszenia.size() + " ogłoszeń:");
for(OgloszenieSamochodowe ogl : lista.ogloszenia) {
System.out.println(ogl);
}
} catch (BladBazyDanych e) {
// wyjątki nie działają w sposób przezroczysty
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.math.BigDecimal;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import ogloszenia.exn.BladDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.rest.IOgloszenia;
public class Klient3_Proxy_CXF {
private Client klient;
private WebTarget root;
public static void main(String[] args) {
new Klient3_Proxy_CXF().run();
}
public void run() {
SerwisOgloszeniowy proxy = JAXRSClientFactory.
create(Ustawienia.ADRES_USLUGI, IOgloszenia.class);
try {
OgloszenieSamochodowe ogl2 = proxy.ogloszeniePoId(2);
System.out.println(ogl2);
System.out.println(ogl2.getCena());
ogl2.setCena(new BigDecimal(66000));
proxy.zapiszOgloszenie(2, ogl2);
System.out.println("done");
} catch (BladDanych | NieznanyRekord e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.math.BigDecimal;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.model.Paliwo;
import ogloszenia.model.Silnik;
import ogloszenia.rest.IOgloszenia;
public class Klient6_Proxy_Post {
public static void main(String[] args) {
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(Ustawienia.ADRES);
IOgloszenia proxy = target.proxy(IOgloszenia.class);
OgloszenieSamochodowe noweOgloszenie = new OgloszenieSamochodowe(null, 2, null, new BigDecimal("33000.33"), "Nowe BMW", "Sprzedam fajne BMW", "BMW", "X5", null, "czarny", 2010, 99900, new Silnik(200f, 3.0f, Paliwo.BENZYNA));
System.out.println("Ogłoszenie do zapisania: " + noweOgloszenie);
try {
OgloszenieSamochodowe zapisane = proxy.zapiszOgloszenie(noweOgloszenie);
System.out.println("Zapisano ogłoszenie");
System.out.println(zapisane);
} catch (BladBazyDanych e) {
// wyjątki nie działają w sposób przezroczysty
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.math.BigDecimal;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.exn.NieznanyRekord;
import ogloszenia.rest.IOgloszenia;
public class Klient7_Proxy_ZmianaCeny {
public static void main(String[] args) {
//int id = 2;
int id = 24;
BigDecimal zmiana = new BigDecimal("250.00");
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(Ustawienia.ADRES);
IOgloszenia proxy = target.proxy(IOgloszenia.class);
System.out.println("Wysyłam zapytania...");
try {
BigDecimal staraCena = proxy.getCena(id);
System.out.println("Stara cena: " + staraCena);
BigDecimal nowaCena = staraCena.add(zmiana);
proxy.setCena(id, nowaCena);
BigDecimal jeszczeNowszaCena = proxy.getCena(id);
System.out.println("Cena po zapisie: " + jeszczeNowszaCena);
} catch (BladBazyDanych e) {
// wyjątki nie działają w sposób przezroczysty - po stronie klienta nie zobaczę wyjątków z własnej aplikacji serwerowej
e.printStackTrace();
} catch (NieznanyRekord e) {
System.out.println("wyjątek NIEZNANY REKORD");
e.printStackTrace();
} catch(NotFoundException e) {
// ale w zależności od kodu powrotu HTTP pojawią się wyspecjalizowane wyjątki JAX-RS
System.out.println("404 NOT FOUND");
Response response = e.getResponse();
String tresc = response.readEntity(String.class);
System.out.println(tresc);
e.printStackTrace();
} catch(InternalServerErrorException e) {
System.out.println("500 INTERNAL SERVER ERROR");
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
public class Ustawienia {
public static final String ADRES = "http://localhost:8080/ogloszenia-rest";
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-klient-soap</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-klient-soap</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ogloszenia-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
package ogloszenia.klient;
import java.io.File;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service.Mode;
import ogloszenia.soap.Nazwy;
/* W tym przykładzie zapytanie jest tworzone z pliku XML, a odpowiedź w formie XML wypisywana na konsolę.
* Tryb PAYLOAD oznacza, że podaje i odbiera się tylko zawartość soap:Body, a całe SOAP-owe opakowanie jest dodawane automatycznie.
*/
public class KlientDispatch1 {
public static void main(String[] args) {
SerwisOgloszeniowyService serwis = new SerwisOgloszeniowyService();
Dispatch<Source> dispatch = serwis.createDispatch(Nazwy.PORT_QNAME, Source.class, Mode.PAYLOAD);
StreamSource src = new StreamSource(new File("zapytanie1.xml"));
Source result = dispatch.invoke(src);
wypiszXmlZSource(result);
System.out.println("\n\nGotowe");
}
static void wypiszXmlZSource(Source xml) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
StreamResult res = new StreamResult(System.out);
t.transform(xml, res);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;
/* W tym przykładzie w ogóle nie używamy wygenerowanych klas, to można zrobić "zawsze", nawet bez procedury wsimport.
* Treść analogiczna do KlientDispatch1.
*/
public class KlientDispatch2 {
private static final QName PORT_NAME = new QName("http://soap.ogloszenia/", "SerwisOgloszeniowyPort");
private static final QName SERVICE_NAME = new QName("http://soap.ogloszenia/", "SerwisOgloszeniowyService");
private static final String WSDL = "http://localhost:8080/09-OgloszeniaSoap/SerwisOgloszeniowy?wsdl";
public static void main(String[] args) {
try {
final URL WSDL_URL = new URL(WSDL);
Service serwis = Service.create(WSDL_URL, SERVICE_NAME);
Dispatch<Source> dispatch = serwis.createDispatch(PORT_NAME, Source.class, Mode.PAYLOAD);
StreamSource src = new StreamSource(new File("zapytanie1.xml"));
Source result = dispatch.invoke(src);
wypiszXmlZSource(result);
System.out.println("\n\nGotowe");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
static void wypiszXmlZSource(Source xml) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
StreamResult res = new StreamResult(System.out);
t.transform(xml, res);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;
/* Zapytanie pobierane jest z pliku XML, ale odpowiedź jest przetwarzana przez arkusz XSLT, aby uzyskać HTML.
* To tylko przykład pokazujący, dlaczego obsługa danych XML-owych na poziomie Source może mieć sens.
* W tym programie też nie są używane wygenerowane klasy klienta.
*/
public class KlientDispatch3 {
private static final QName PORT_NAME = new QName("http://soap.ogloszenia/", "SerwisOgloszeniowyPort");
private static final QName SERVICE_NAME = new QName("http://soap.ogloszenia/", "SerwisOgloszeniowyService");
private static final String WSDL = "http://localhost:8080/09-OgloszeniaSoap/SerwisOgloszeniowy?wsdl";
public static void main(String[] args) {
try {
final URL WSDL_URL = new URL(WSDL);
Service serwis = Service.create(WSDL_URL, SERVICE_NAME);
Dispatch<Source> dispatch = serwis.createDispatch(PORT_NAME, Source.class, Mode.PAYLOAD);
StreamSource src = new StreamSource(new File("zapytanie1.xml"));
Source result = dispatch.invoke(src);
przeksztalcXml(result);
System.out.println("\n\nGotowe");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
static void przeksztalcXml(Source xml) {
try {
StreamSource xsl = new StreamSource(new File("arkusz.xsl"));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(xsl);
StreamResult res = new StreamResult(new File("wynik3.html"));
t.transform(xml, res);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.util.List;
import ogloszenia.exn.BladBazyDanych;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.soap.ISerwisOgloszeniowy;
public class KlientProxy1 {
public static void main(String[] args) {
SerwisOgloszeniowyService service = new SerwisOgloszeniowyService();
ISerwisOgloszeniowy port = service.getSerwisOgloszeniowyPort();
try {
List<OgloszenieSamochodowe> wszystkieOgloszenia = port.wszystkieOgloszenia();
System.out.println("Mam " + wszystkieOgloszenia.size() + " ogłoszeń");
for (OgloszenieSamochodowe ogl : wszystkieOgloszenia) {
System.out.println(ogl.getTytul() + " " + ogl.getCena());
}
} catch (BladBazyDanych e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import ogloszenia.exn.BladAplikacji;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.soap.ISerwisOgloszeniowy;
public class KlientProxy2 {
public static void main(String[] args) {
int id = Integer.parseInt(JOptionPane.showInputDialog("Podaj ID ogłoszenia"));
SerwisOgloszeniowyService service = new SerwisOgloszeniowyService();
ISerwisOgloszeniowy port = service.getSerwisOgloszeniowyPort();
try {
OgloszenieSamochodowe ogloszenie = port.jednoOgloszenie(id);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label = new JLabel(ogloszenie.getMarka() + " " + ogloszenie.getModel() + " rocznik " + ogloszenie.getRocznik());
label.setFont(new Font("Arial", Font.BOLD, 28));
label.setForeground(Color.BLUE);
panel.add(label);
panel.add(Box.createVerticalStrut(10));
label = new JLabel(ogloszenie.getTytul());
label.setFont(new Font("Arial", Font.BOLD, 28));
panel.add(label);
panel.add(Box.createVerticalStrut(10));
label = new JLabel(ogloszenie.getOpis());
label.setFont(new Font("Arial", Font.PLAIN, 22));
label.setForeground(Color.RED);
panel.add(label);
panel.add(Box.createVerticalStrut(10));
try {
byte[] foto = port.foto(id);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(foto));
ImageIcon icon = new ImageIcon(image);
panel.add(new JLabel(icon));
} catch (Exception e) {
System.err.println("Nie da rady wczytać zdjęcia, " + e);
}
JOptionPane.showMessageDialog(null, panel);
} catch (BladAplikacji e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
import ogloszenia.soap.ISerwisOgloszeniowy;
import ogloszenia.soap.Nazwy;
/* PC: Jak klasa wygenerowana w projekcie 11.
* Znając najważniejsze cechy: adnotację @WebServiceClient i dziedziczenie z klasy Service,
* łatwo możemy takie coś utworzyć od zera.
* Zostawiam tylko minimum potrzebne tu do działania.
*/
@WebServiceClient(targetNamespace = Nazwy.NAMESPACE,
name = Nazwy.SERVICE_NAME,
wsdlLocation = Nazwy.WSDL)
public class SerwisOgloszeniowyService extends Service {
// Zostawiam tylko konstruktor przyjmujący domyślne ustawienia
public SerwisOgloszeniowyService() {
super(Nazwy.WSDL_URL, Nazwy.SERVICE_QNAME);
}
@WebEndpoint(name = Nazwy.PORT_NAME)
public ISerwisOgloszeniowy getSerwisOgloszeniowyPort() {
return super.getPort(Nazwy.PORT_QNAME, ISerwisOgloszeniowy.class);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ogloszenia-model</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="ogloszenia-model">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="1.8"/>
<installed facet="jst.utility" version="1.0"/>
</faceted-project>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pl.vavatech.ogloszenia</groupId>
<artifactId>P10-OgloszeniaParent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ogloszenia-model</artifactId>
</project>
\ No newline at end of file
package ogloszenia.exn;
public class BladAplikacji extends Exception {
public BladAplikacji() {
super();
}
public BladAplikacji(String message, Throwable cause) {
super(message, cause);
}
public BladAplikacji(String message) {
super(message);
}
public BladAplikacji(Throwable cause) {
super(cause);
}
}
package ogloszenia.exn;
public class BladBazyDanych extends BladAplikacji {
public BladBazyDanych() {
super();
}
public BladBazyDanych(String message, Throwable cause) {
super(message, cause);
}
public BladBazyDanych(String message) {
super(message);
}
public BladBazyDanych(Throwable cause) {
super(cause);
}
}
package ogloszenia.exn;
public class NieznanyRekord extends BladAplikacji {
public NieznanyRekord() {
super();
}
public NieznanyRekord(String message) {
super(message);
}
}
package ogloszenia.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Adres {
private String ulica;
@XmlElement(name="kod-pocztowy")
private String kodPocztowy;
private String miasto;
public Adres() {
}
public Adres(String ulica, String kodPocztowy, String miasto) {
this.ulica = ulica;
this.kodPocztowy = kodPocztowy;
this.miasto = miasto;
}
public String getUlica() {
return ulica;
}
public String getKodPocztowy() {
return kodPocztowy;
}
public String getMiasto() {
return miasto;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((kodPocztowy == null) ? 0 : kodPocztowy.hashCode());
result = prime * result + ((miasto == null) ? 0 : miasto.hashCode());
result = prime * result + ((ulica == null) ? 0 : ulica.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Adres other = (Adres) obj;
if (kodPocztowy == null) {
if (other.kodPocztowy != null)
return false;
} else if (!kodPocztowy.equals(other.kodPocztowy))
return false;
if (miasto == null) {
if (other.miasto != null)
return false;
} else if (!miasto.equals(other.miasto))
return false;
if (ulica == null) {
if (other.ulica != null)
return false;
} else if (!ulica.equals(other.ulica))
return false;
return true;
}
@Override
public String toString() {
return String.format("%s, %s %s", ulica, kodPocztowy, miasto);
}
}
package ogloszenia.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="ogloszenia")
public class ListaOgloszen {
@XmlElement(name="ogloszenie")
public List<OgloszenieSamochodowe> ogloszenia;
public static ListaOgloszen nowa(List<OgloszenieSamochodowe> ogloszenia) {
ListaOgloszen lista = new ListaOgloszen();
lista.ogloszenia = ogloszenia;
return lista;
}
}
package ogloszenia.model;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import ogloszenia.xml.AdapterDaty;
public class Ogloszenie {
@XmlAttribute(name="id")
private Integer idOgloszenia;
@XmlAttribute(name="id-sprzedawcy")
// @XmlTransient - to by spowodowalo niewypisywanie tej informacji w ogole
private Integer idSprzedawcy;
@XmlElement(name="wystawione")
@XmlJavaTypeAdapter(AdapterDaty.class)
private LocalDateTime dataWystawienia;
private BigDecimal cena;
private String tytul;
private String opis;
private Sprzedawca sprzedawca;
public Ogloszenie() {
}
public Ogloszenie(Integer idOgloszenia, Integer idSprzedawcy, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia;
this.idSprzedawcy = idSprzedawcy;
this.dataWystawienia = dataWystawienia;
this.cena = cena;
this.tytul = tytul;
this.opis = opis;
}
/** Pozostawia pole sprzedawca równe null. */
public Ogloszenie(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia;
this.dataWystawienia = dataWystawienia;
this.cena = cena;
this.tytul = tytul;
this.opis = opis;
this.idSprzedawcy = null;
}
/** Wartość idSprzedawcy pobiera z obiektu sprzedawca. */
public Ogloszenie(Integer idOgloszenia, Sprzedawca sprzedawca, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia;
this.dataWystawienia = dataWystawienia;
this.cena = cena;
this.tytul = tytul;
this.opis = opis;
this.sprzedawca = sprzedawca;
if(sprzedawca != null) {
this.idSprzedawcy = sprzedawca.getIdSprzedawcy();
}
}
public Integer getIdOgloszenia() {
return idOgloszenia;
}
public void setIdOgloszenia(Integer noweId) {
this.idOgloszenia = noweId;
}
public String getTytul() {
return tytul;
}
public BigDecimal getCena() {
return cena;
}
public Integer getIdSprzedawcy() {
return idSprzedawcy;
}
public Sprzedawca getSprzedawca() {
return sprzedawca;
}
public void setSprzedawca(Sprzedawca sprzedawca) {
this.sprzedawca = sprzedawca;
}
public String getOpis() {
return opis;
}
public void setOpis(String opis) {
this.opis = opis;
}
public void setCena(BigDecimal cena) {
this.cena = cena;
}
public LocalDateTime getDataWystawienia() {
return dataWystawienia;
}
public void setDataWystawienia(LocalDateTime dataWystawienia) {
this.dataWystawienia = dataWystawienia;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cena == null) ? 0 : cena.hashCode());
result = prime * result + ((dataWystawienia == null) ? 0 : dataWystawienia.hashCode());
result = prime * result + ((idOgloszenia == null) ? 0 : idOgloszenia.hashCode());
result = prime * result + ((idSprzedawcy == null) ? 0 : idSprzedawcy.hashCode());
result = prime * result + ((opis == null) ? 0 : opis.hashCode());
result = prime * result + ((sprzedawca == null) ? 0 : sprzedawca.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ogloszenie other = (Ogloszenie) obj;
if (cena == null) {
if (other.cena != null)
return false;
} else if (!cena.equals(other.cena))
return false;
if (dataWystawienia == null) {
if (other.dataWystawienia != null)
return false;
} else if (!dataWystawienia.equals(other.dataWystawienia))
return false;
if (idOgloszenia == null) {
if (other.idOgloszenia != null)
return false;
} else if (!idOgloszenia.equals(other.idOgloszenia))
return false;
if (idSprzedawcy == null) {
if (other.idSprzedawcy != null)
return false;
} else if (!idSprzedawcy.equals(other.idSprzedawcy))
return false;
if (opis == null) {
if (other.opis != null)
return false;
} else if (!opis.equals(other.opis))
return false;
if (sprzedawca == null) {
if (other.sprzedawca != null)
return false;
} else if (!sprzedawca.equals(other.sprzedawca))
return false;
return true;
}
@Override
public String toString() {
return String.format("Ogłoszenie #%d: sprzedawca #%s, cena: %.2f\n %s",
idOgloszenia, idSprzedawcy, cena, tytul);
}
}
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