Galera tô tentando fazer um Web Service simples, apenas para teste, para então fazer um sério, o problema é que mesmo com esse exemplo simples já to tendo um problema chato! E o pior, não sei resolver, pois todas as alternativas que eu vi pelo tio Google não resolvem. Estou usando o JBossAS, só pra lembrar, outro detalhe é que to usando WS-Security(depois vou por o Policies e o Reliable Messaging). O código consiste em uma calculadora localizada no servidor e um cliente para acessá-la, simples não?! Pois é, mas eu não tô conseguindo fazer a bagaça andar. Ta dando o erro: “Undefined port type: {http://teste/}Calculadora”. Peço desculpas pelo tamanho do tópico(na verdade pela quantidade de código) é que vejo muitas vezes o pessoal aqui pedindo pra postar o código inteiro pra tentar ajudar, então adiantei logo.
E ah, não tirem onda, eu sou bem iniciante em web serviceis(só soube que isso existia tem uma semana). :roll:
Interface Remota
@WebService
@Remote
public interface Calculadora {
@WebMethod
public Double somar(Double elm1,Double elm2);
@WebMethod
public Double subtrair(Double elm1,Double elm2);
@WebMethod
public Double multiplicar(Double elm1,Double elm2);
@WebMethod
public Double dividir(Double elm1,Double elm2);
}
Classe que implementa a calculadora.
@WebService(name="CalculadoraEndPoint",serviceName="CalculadoraService",portName="CalculadoraPort")
@WebServiceRef
@SOAPBinding(style=SOAPBinding.Style.RPC)
public @Stateless class CalculadoraBean implements Calculadora {
@WebMethod
public Double dividir(Double elm1, Double elm2) {
try{
System.out.println(elm1/elm2);
return elm1/elm2;
}
catch(ArithmeticException err){
err.printStackTrace();
return 0.0;
}
}
@WebMethod
public Double multiplicar(Double elm1, Double elm2) {
System.out.println(elm1*elm2);
return elm1*elm2;
}
@WebMethod
public Double somar(Double elm1, Double elm2) {
System.out.println(elm1+elm2);
return elm1+elm2;
}
@WebMethod
public Double subtrair(Double elm1, Double elm2) {
System.out.println(elm1-elm2);
return elm1-elm2;
}
}
Cliente
public static Calculadora getPort(String endpointURI) throws MalformedURLException {
QName serviceName = new QName("http://teste/", "CalculadoraService");
URL wsdlURL = new URL(endpointURI);
Service service = Service.create(wsdlURL, serviceName);
return service.getPort(Calculadora.class);
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String endpointURI ="http://127.0.0.1:8080/TesteWSSeguro3/calculadora?wsdl";
Calculadora calc = getPort(endpointURI);
Client cli = ClientProxy.getClient(calc);
org.apache.cxf.endpoint.Endpoint end = cli.getEndpoint();
Map<String,Object> outProps= new HashMap<String,Object>();
outProps.put("action", "Timestamp Signature Encrypt");
outProps.put("user", "client");
outProps.put("signaturePropFile", "META-INF/client.properties");
outProps.put("signatureKeyIdentifier", "DirectReference");
outProps.put("passwordCallbackClass", "teste.KeystorePasswordCallback");
outProps.put("signatureParts", "{Element}{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Timestamp;{Element}{http://schemas.xmlsoap.org/soap/envelope/}Body");
outProps.put("encryptionPropFile", "META-INF/client.properties");
outProps.put("encryptionUser", "client");
outProps.put("encryptionParts", "{Element}{http://www.w3.org/2000/09/xmldsig#}Signature;{Content}{http://schemas.xmlsoap.org/soap/envelope/}Body");
outProps.put("encryptionSymAlgorithm", "http://www.w3.org/2001/04/xmlenc#tripledes-cbc");
outProps.put("encryptionKeyTransportAlgorithm", "http://www.w3.org/2001/04/xmlenc#rsa-1_5");
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); //request
end.getOutInterceptors().add(wssOut);
end.getOutInterceptors().add(new SAAJOutInterceptor());
Map<String,Object> inProps= new HashMap<String,Object>();
inProps.put("action", "Timestamp Signature Encrypt");
inProps.put("signaturePropFile", "META-INF/server.properties");
inProps.put("passwordCallbackClass", "teste.KeystorePasswordCallback");
inProps.put("decryptionPropFile", "META-INF/server.properties");
WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps); //response
end.getInInterceptors().add(wssIn);
end.getInInterceptors().add(new SAAJInInterceptor());
}
}
Também tentei fazer um testcase, mas deu o mesmo erro
public class CalculadoraTestCase extends JBossWSTest{
private static final int port = 8878;
public void test() throws Exception
{
String publishURL1 = "http://" + getServerHost() + ":" + port + "/calculadora";
Endpoint endpoint1 = mandarEndpoint(new CalculadoraBean(), publishURL1);
rodarEndpoint(publishURL1);
endpoint1.stop();
}
private Endpoint mandarEndpoint(CalculadoraBean epImpl, String publishURL)
{
Endpoint endpoint = Endpoint.create(SOAPBinding.SOAP11HTTP_BINDING, epImpl);
endpoint.publish(publishURL);
return endpoint;
}
private void rodarEndpoint(String publishURL) throws Exception
{
URL wsdlURL = new URL(publishURL + "?wsdl");
QName qname = new QName("http://teste/", "CalculadoraService");
Service service = Service.create(wsdlURL, qname);
Calculadora port = (Calculadora)service.getPort(Calculadora.class);
Double elm1 = 10.0; Double elm2 = 10.0;
assertEquals(20.0, port.somar(elm1, elm2));
Object retObj = port.subtrair(elm1, elm2);
assertEquals(0.0, retObj);
assertEquals(100.0, port.multiplicar(elm1, elm2));
assertEquals(1.0, port.dividir(elm1, elm2));
}
}
Uma classe de callback, tirada dos exemplos do site da JBoss.
public class KeystorePassWordCallback {
private Map<String, String> passwords = new HashMap<String, String>();
public KeystorePassWordCallback()
{
passwords.put("client", "123456");
passwords.put("server", "123456");
}
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
{
for (int i = 0; i < callbacks.length; i++)
{
WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
String pass = passwords.get(pc.getIdentifer());
if (pass != null)
{
pc.setPassword(pass);
return;
}
}
}
public void setAliasPassword(String alias, String password)
{
passwords.put(alias, password);
}
}
E finalmente, o .xml
<beans>
xmlns='http://www.springframework.org/schema/beans'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:beans='http://www.springframework.org/schema/beans'
xmlns:jaxws='http://cxf.apache.org/jaxws'
xsi:schemaLocation='http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd'>
<bean id="Sign_Response" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="Timestamp Signature Encrypt"/>
<entry key="user" value="server"/>
<entry key="signaturePropFile" value="server.properties"/>
<entry key="encryptionPropFile" value="server.properties"/>
<entry key="encryptionUser" value="server"/>
<entry key="signatureKeyIdentifier" value="DirectReference"/>
<entry key="passwordCallbackClass" value="teste.KeystorePassWordCallback"/>
<entry key="signatureParts" value="{Element}{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Timestamp;{Element}{http://schemas.xmlsoap.org/soap/envelope/}Body"/>
<entry key="encryptionParts" value="{Element}{http://www.w3.org/2000/09/xmldsig#}Signature;{Content}{http://schemas.xmlsoap.org/soap/envelope/}Body"/>
<entry key="encryptionKeyTransportAlgorithm" value="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
<entry key="encryptionSymAlgorithm" value="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
</map>
</constructor-arg>
</bean>
<jaxws:endpoint
id='CalculadoraBean'
address='http://@jboss.bind.address@:8080/TesteWSSeguro3'
implementor='teste.CalculadoraBean'>
<jaxws:invoker>
<bean class='org.jboss.wsf.stack.cxf.InvokerEJB3'/>
</jaxws:invoker>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor"/>
<ref bean="Sign_Response"/>
</jaxws:outInterceptors>
<jaxws:inInterceptors>
<ref bean="Sign_Request"/>
<bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor"/>
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>