SERVIDOR

Transcrição

SERVIDOR
SERVIDOR
package sd.aula1.srv;
import java.io.File;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class FileServerImplWS {
private File basePath;
public FileServerImplWS() {
this(".");
}
protected FileServerImplWS(String pathname) {
super();
basePath = new File(pathname);
}
@WebMethod
public FileInfo getFileInfo(String path) throws InfoNotFoundException {
File f = new File(basePath, path);
if (f.exists())
return new FileInfo(f.getName(), f.length(), new Date(f.lastModified()), f.isFile());
else
throw new InfoNotFoundException("File not found :" + path);
}
public static void main(String args[]) throws Exception {
String path = args.length > 0 ? args[0] : ".";
Endpoint.publish("http://0.0.0.0:8080/FileServer", new FileServerImplWS(path));
System.err.println("FileServer started");
}
}
WebServiceIDLhttp://localhost:8080/FileServer?wsdl
…
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp="http://www.w3.org/ns/ws-policy"
xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://srv.aula1.sd/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://srv.aula1.sd/"
name="FileServerImplWSService">
<types>
<xsd:schema>
<xsd:import namespace="http://srv.aula1.sd/" schemaLocation="http://localhost:8080/FileServer?xsd=1"/>
</xsd:schema>
</types>
<message name="getFileInfo">
<part name="parameters" element="tns:getFileInfo"/>
</message>
<message name="getFileInfoResponse">
<part name="parameters" element="tns:getFileInfoResponse"/>
</message>
<message name="InfoNotFoundException">
<part name="fault" element="tns:InfoNotFoundException"/>
</message>
<portType name="FileServerImplWS">
<operation name="getFileInfo">
<input wsam:Action="http://srv.aula1.sd/FileServerImplWS/getFileInfoRequest" message="tns:getFileInfo"/>
<output wsam:Action="http://srv.aula1.sd/FileServerImplWS/getFileInfoResponse" message="tns:getFileInfoResponse"/>
<fault message="tns:InfoNotFoundException" name="InfoNotFoundException"
…
CLIENTE (1) – geração dos stubs a partir de uma instância do
serviço
>> wsimport -keep -s src –d bin -p aula1.clt.ws http://localhost:8080/FileServer?wsdl
-keep indica que pretendemos manter o código fonte dos stubs
-s indica a directoria onde colocar o código fonte dos stubs
-d indica a directoria onde colocar as classes compiladas dos stubs
-p indica o pacote Java das classes dos stubs gerados
NOTA: Poderão ser geradas muitas classes, incluindo classes para representar os tipos dos parâmetros e do
retorno das operações fornecidas pelo webservice.
Haverá classes que poderão ter os mesmos nomes mas serão distintas entre o servidor e o cliente.
CLIENTE (2) – invocação
package sd.aula1.clt;
import java.net.URL;
import sd.aula1.clt.ws.FileInfo;
import sd.aula1.clt.ws.FileServerImplWS;
import sd.aula1.clt.ws.FileServerImplWSService;
public class GetFileInfo {
public static void main(String[] args) {
if (args.length != 2) {
System.out .println("Use: java GetFileInfo server_endpoint path");
System.exit(0);
}
String serverHost = args[0];
String path = args[1];
try {
URL wsURL = new URL(String.format("http://%s/FileServer", serverHost));
FileServerImplWSService service = new FileServerImplWSService(wsURL);
//
//
//
//
FileServerImplWSService service = new FileServerImplWSService();
A invocação sem parâmetros aponta para a instância usada na
criação dos stubs através
da ferrament wsimport
FileServerImplWS server = service.getFileServerImplWSPort();
FileInfo info = server.getFileInfo(path);
System.out .println("Name : " + info.getName() + "\nLength: " + info.getLength() + "\nDate modified: "
+ info.getModified() + "\nisFile : " + info.isIsFile());
} catch (Exception e) {
System.err .println("Erro: " + e.getMessage());
}
}
}