<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rafael Liu Blog &#187; Web Services</title>
	<atom:link href="http://rafaelliu.net/category/web-services/feed/" rel="self" type="application/rss+xml" />
	<link>http://rafaelliu.net</link>
	<description>Café, pinguins e outras coisas</description>
	<lastBuildDate>Mon, 05 Dec 2011 19:08:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>WS-Security no CXF</title>
		<link>http://rafaelliu.net/2011/09/24/ws-security-no-cxf/</link>
		<comments>http://rafaelliu.net/2011/09/24/ws-security-no-cxf/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 03:00:43 +0000</pubDate>
		<dc:creator>rafaelliu</dc:creator>
				<category><![CDATA[SOA]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[cxf]]></category>
		<category><![CDATA[ws-seucity]]></category>

		<guid isPermaLink="false">http://rafaelliu.net/?p=468</guid>
		<description><![CDATA[Completando o post anterior (bom, agora no sentido &#8220;que veio algum momento antes&#8221;), vamos ver algo bem mais útil: como criar um web service utilizando username token para autenticação. Suponho que ficou claro como criar um Web Service e um client para ele usando CFX + Spring e não vou subestimar a inteligência de vocês, [...]]]></description>
			<content:encoded><![CDATA[<p>Completando o post anterior (bom, agora no sentido &#8220;que veio algum momento antes&#8221;), vamos ver algo bem mais útil: como criar um web service utilizando username token para autenticação. Suponho que ficou claro como criar um Web Service e um client para ele usando CFX + Spring e não vou subestimar a inteligência de vocês, vou mostrar só o que é preciso adicionar/alterar:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">   <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">   <span style="color: #000066;">xmlns:jaxws</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/jaxws&quot;</span></span>
<span style="color: #009900;">   <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span>
<span style="color: #009900;">http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</span>
<span style="color: #009900;">http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-extension-http-jetty.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:endpoint</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testService&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">implementor</span>=<span style="color: #ff0000;">&quot;net.rafaelliu.services.TestImpl&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://localhost:9000/test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
      <span style="color: #808080; font-style: italic;">&lt;!-- Enable WS-Addressing --&gt;</span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:inInterceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #808080; font-style: italic;">&lt;!-- Enable WS-Security --&gt;</span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;action&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;UsernameToken&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;passwordType&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;PasswordText&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;passwordCallbackClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;net.rafaelliu.callbacks.ServerPasswordCallback&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/constructor-arg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:inInterceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:endpoint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:client</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testClient&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://localhost:9000/test&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">serviceClass</span>=<span style="color: #ff0000;">&quot;net.rafaelliu.services.Test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:outInterceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;action&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;UsernameToken&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;passwordType&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;PasswordText&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;rafaelliu&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;passwordCallbackClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;net.rafaelliu.callbacks.ClientPasswordCallback&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/constructor-arg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:outInterceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:client<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>É possível ver que no client definimos um outInterceptor que irá adicionar os headers com o token Username (um dos possíveis tokens como havia dito no post anterior, poderia ser Kerberos, SAML ou algum certificado) e no serviço definimos um inInterceptor que irá vazer a validação do envelope SOAP que chegar, verificando no header as credenciais.</p>
<p>Ambos os interceptors utilizam Callbacks para recuperar a senha a partir do nome do usuário. No client o callback será utilizado para setar a senha e no serviço será utilizado para recuperar a senha a partir do nome do usuário presente no header, que será comparada com a senha que veio junto do header.</p>
<p>A implementação dos Callbacks é Java puro, provavelmente utilizaria um DB ou LDAP, e é bastante simples:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClientPasswordCallback <span style="color: #000000; font-weight: bold;">implements</span> CallbackHandler  <span style="color: #009900;">&#123;</span>
&nbsp;
   @Override
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> handle<span style="color: #009900;">&#40;</span>Callback<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> callbacks<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, UnsupportedCallbackException <span style="color: #009900;">&#123;</span>
        WSPasswordCallback pc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>WSPasswordCallback<span style="color: #009900;">&#41;</span> callbacks<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pc.<span style="color: #006633;">getIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rafaelliu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          pc.<span style="color: #006633;">setPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;abc123&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>E para o serviço:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ServerPasswordCallback <span style="color: #000000; font-weight: bold;">implements</span> CallbackHandler  <span style="color: #009900;">&#123;</span>
&nbsp;
   @Override
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> handle<span style="color: #009900;">&#40;</span>Callback<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> callbacks<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, UnsupportedCallbackException <span style="color: #009900;">&#123;</span>
        WSPasswordCallback pc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>WSPasswordCallback<span style="color: #009900;">&#41;</span> callbacks<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> password <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pc.<span style="color: #006633;">getIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rafaelliu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;abc123&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>password <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>password.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>pc.<span style="color: #006633;">getPassword</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;wrong password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Uma nota importante é que o callback do serviço é responsável por lançar uma exceção caso o a senha do usuário não seja validade. Isso se deve à implementação do WSS4J, framework também da ASF que o CXF usa para WS-Security. Não testei, mas parece que na versão 2.4 do CXF, ou mais precisamente no <a title="Open Source Security" href="http://coheigea.blogspot.com/2011/02/usernametoken-processing-changes-in.html" target="_blank">WSS4J 1.6</a>, esse comportamento mudou e o callback realmente ficou só um callback.</p>
<p>Bonito hein?</p>
]]></content:encoded>
			<wfw:commentRss>http://rafaelliu.net/2011/09/24/ws-security-no-cxf/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Criando um Web Service com WS-Addressing (Apache CXF)</title>
		<link>http://rafaelliu.net/2011/08/16/criando-um-web-service-com-ws-addressing-apache-cxf/</link>
		<comments>http://rafaelliu.net/2011/08/16/criando-um-web-service-com-ws-addressing-apache-cxf/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 18:16:02 +0000</pubDate>
		<dc:creator>rafaelliu</dc:creator>
				<category><![CDATA[SOA]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[cxf]]></category>
		<category><![CDATA[jax-ws]]></category>
		<category><![CDATA[ws-addressing]]></category>

		<guid isPermaLink="false">http://rafaelliu.net/?p=440</guid>
		<description><![CDATA[Apache CXF é o projeto da ASF para criação de Web Services utilizando JAX-WS e JAX-RS. Entre outros padrões WS-*, ele dá suporte a WS-Addressing e WS-Security. O WS-Addressing busca independência da camada de transporte enviando informações de transporte no envelope SOAP. Embora na maioria dos casos seja utilizado HTTP que já supre as necessidades [...]]]></description>
			<content:encoded><![CDATA[<p>Apache <a href="http://cxf.apache.org/" target="_blank">CXF</a> é o projeto da ASF para criação de Web Services utilizando JAX-WS e JAX-RS. Entre outros padrões WS-*, ele dá suporte a WS-Addressing e WS-Security.</p>
<p>O <strong>WS-Addressing</strong> busca <strong>independência</strong> da camada de transporte enviando <strong>informações de transporte</strong> no envelope SOAP. Embora na maioria dos casos seja utilizado HTTP que já supre as necessidades mais básicas como endereços de origem e destino, Web Services podem ser utilizado sobre outros protocolos. Imagine ter acesso a um envelope SOAP solto. Da onde ele veio? Pra onde vai? O servidor já recebeu esse envelope? Sem o WS-Addressing essas informações podem ser descobertas apenas analisando a camada de transporte. E se o protocolo de transporte utilizado não armazenar a origem do consumidor? Como o servidor irá eventualmente responder uma requisição? Com o WS-Addressing é possível utilizar protocolos muito mais simples que, a princípio são &#8220;incompletos&#8221;.</p>
<p>O <strong>WS-Security</strong> é o padrão para <strong>autenticação</strong>, <strong>assinatura</strong> e <strong>encriptação</strong> de envelopes SOAP. É um padrão bastante interessante, amplo e flexível. Trabalha com o conceito de <strong>tokens</strong>. Token são utilizados para a autenticação de usuários e assinatura de dados. O interessante desse padrão é que a utilização de <em>tokens</em> torna o padrão extensível, sendo possível implementar <em>tokens </em>proprietários. O problema é que tanto o cliente quanto o servidor precisam entender a forma do token, portanto a especificação definiu alguns tokens padrão como Username (usuário/senha), X.509, SAML e Kerberos. A utilização de tokens permite a encriptação de mensagens (ou trechos de mensagens) e sua utilização junto com um algoritmo de hash (que também é configurável) permite a assinatura de mensagens (ou trechos de mensagens).</p>
<h3>Criando o Web Service</h3>
<p>O primeiro passo é criar um Web Service, o que é <span style="text-decoration: line-through;">ridículo</span> muito simples utilizando JAX-WS:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@WebService
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MeuServico <span style="color: #009900;">&#123;</span>
&nbsp;
	@WebMethod
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> falar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Blah&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>E é isso. O web service está pronto, agora precisamos publicar ele. De novo, a especificação torna isso muito fácil:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Endpoint.<span style="color: #006633;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://localhost:9000/MeuServico&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> MeuServico<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Basta acessar <code>http://localhost:9000/MeuServico?wsdl</code> e ver o WSDL gerado. Utilizando o <a title="SoapUI" href="http://www.soapui.org/" target="_blank">SoapUI</a> é possível testar nosso web service.</p>
<h3>Utilizando Spring com o CXF</h3>
<p>Embora tenha sido muito simples publicar nosso web service utilizando a classe Endpoint, é possível também utilizar o Spring para isso. A utilização do Spring trás algumas vantagens como poder mapear nosso web service no web.xml de uma webapp, poder centralizar as configurações em um XML e poder definir beans que podem ser reutilizados e injetados de forma declarativa. O CXF implementa os padrões WS-* através de interceptors. Vamos utilizar dois interceptors, um para WS-Addressing e outro para WS-Security, que vamos definir no XML do Spring:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">   <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">   <span style="color: #000066;">xmlns:jaxws</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/jaxws&quot;</span></span>
<span style="color: #009900;">   <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span>
<span style="color: #009900;">http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</span>
<span style="color: #009900;">http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-extension-http-jetty.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:endpoint</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testService&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">implementor</span>=<span style="color: #ff0000;">&quot;net.rafaelliu.services.TestImpl&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://localhost:9000/test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
      <span style="color: #808080; font-style: italic;">&lt;!-- Enable WS-Addressing --&gt;</span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:features<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wsa:addressing</span> <span style="color: #000066;">xmlns:wsa</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/ws/addressing&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:features<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
      <span style="color: #808080; font-style: italic;">&lt;!-- Optional, enables logging for inbound --&gt;</span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:inInterceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.interceptor.LoggingInInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:inInterceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
      <span style="color: #808080; font-style: italic;">&lt;!-- Optional, enables logging for outbound --&gt;</span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:outInterceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.interceptor.LoggingOutInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:outInterceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:endpoint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:client</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testClient&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://localhost:9000/test&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">serviceClass</span>=<span style="color: #ff0000;">&quot;net.rafaelliu.services.Test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:features<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wsa:addressing</span> <span style="color: #000066;">xmlns:wsa</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/ws/addressing&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:features<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:client<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Agora o Endpoint pegaremos do ApplicationContext do Spring:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ClassPathXmlApplicationContext appContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassPathXmlApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;./cxf-config.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Endpoint ep <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Endpoint<span style="color: #009900;">&#41;</span> appContext.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testService&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ep.<span style="color: #006633;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nesse ponto temos um serviço utilizando WS-Addressing no ar. Vamos usar um <em>proxy client</em> do CXF para invocar o web service:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ClassPathXmlApplicationContext appContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassPathXmlApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;./cxf-config.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Test client <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Test<span style="color: #009900;">&#41;</span> appContext.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testClient&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
client.<span style="color: #006633;">doTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Como ficou tudo transparente, é mais do que certo o leitor duvidar que realmente algo está acontecendo. Para tirar a dúvida é possível utilizar o SoapUI mencionado, mas fica como dever de casa descobrir como ele funciona com WS-Addressing :p (quer uma <a href="http://www.soapui.org/SOAP-and-WSDL/using-ws-addressing.html" target="_blank">dica</a>?)</p>
<p><strong>Conclusão</strong></p>
<p>O CXF está entre os frameworks que mais acho legais de mexer. Muito tem arquitetado, fácil de usar, extensível e <em>just works</em>. Ele não é o framework de web services mais completo em termos de suporte aos padrões WS-*, mas definitivamente vale uma olhada. Foi possível ver como utilizar o CXF para publicar e consumir web services e como fazer a configuração via Spring. O padrão WS-Addressing não é assim tão comum (ou mesmo &#8220;útil&#8221;), mas futuramente quero mostrar como usar WS-Security no CXF também, o que é um pouco mais complicado e tornaria o post massante.</p>
]]></content:encoded>
			<wfw:commentRss>http://rafaelliu.net/2011/08/16/criando-um-web-service-com-ws-addressing-apache-cxf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>setProperty must be overridden by all subclasses of SOAPMessage</title>
		<link>http://rafaelliu.net/2008/05/08/setproperty-must-be-overridden-by-all-subclasses-of-soapmessage/</link>
		<comments>http://rafaelliu.net/2008/05/08/setproperty-must-be-overridden-by-all-subclasses-of-soapmessage/#comments</comments>
		<pubDate>Fri, 09 May 2008 01:19:14 +0000</pubDate>
		<dc:creator>rafaelliu</dc:creator>
				<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[endorsed]]></category>
		<category><![CDATA[jboss 4]]></category>
		<category><![CDATA[jboss 5]]></category>
		<category><![CDATA[SAAJ]]></category>
		<category><![CDATA[UnsupportedOperationException]]></category>

		<guid isPermaLink="false">http://rafaelliu.wordpress.com/?p=11</guid>
		<description><![CDATA[Tive recentemente que fazer uma prova de conceito, usando web services. Criei uma aplicação usando JAX-WS no JBoss 4.2.2 rodando na JDK 6 da Sun e me deparei um a seguinte exceção: java.lang.UnsupportedOperationException: setProperty must be overridden by all subclasses of SOAPMessage at javax.xml.soap.SOAPMessage.setProperty&#40;SOAPMessage.java:424&#41; at org.jboss.ws.core.soap.SOAPMessageImpl.&#60;init&#62;&#40;SOAPMessageImpl.java:83&#41; at org.jboss.ws.core.soap.MessageFactoryImpl.createMessage&#40;MessageFactoryImpl.java:161&#41; Pesquisando um pouco achei vários relatos similares [...]]]></description>
			<content:encoded><![CDATA[<p>Tive recentemente que fazer uma prova de conceito, usando web services. Criei uma aplicação usando JAX-WS no JBoss 4.2.2 rodando na JDK 6 da Sun e me deparei um a seguinte exceção:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">UnsupportedOperationException</span><span style="color: #339933;">:</span> setProperty must be overridden by all subclasses of SOAPMessage
at javax.<span style="color: #006633;">xml</span>.<span style="color: #006633;">soap</span>.<span style="color: #006633;">SOAPMessage</span>.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span>SOAPMessage.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">424</span><span style="color: #009900;">&#41;</span>
at org.<span style="color: #006633;">jboss</span>.<span style="color: #006633;">ws</span>.<span style="color: #006633;">core</span>.<span style="color: #006633;">soap</span>.<span style="color: #006633;">SOAPMessageImpl</span>.<span style="color: #339933;">&lt;</span>init<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>SOAPMessageImpl.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">83</span><span style="color: #009900;">&#41;</span>
at org.<span style="color: #006633;">jboss</span>.<span style="color: #006633;">ws</span>.<span style="color: #006633;">core</span>.<span style="color: #006633;">soap</span>.<span style="color: #006633;">MessageFactoryImpl</span>.<span style="color: #006633;">createMessage</span><span style="color: #009900;">&#40;</span>MessageFactoryImpl.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">161</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Pesquisando um pouco achei vários relatos similares e até <a href="http://jira.jboss.org/jira/browse/JBWS-1439" target="_blank">uma entrada no JIRA</a>. A classe SOAPMessage é da biblioteca SAAJ, e o problema é entre o JBoss e a JDK 6. Até a versão 6, essa biblioteca era distruida separadamente, mas na nova versão ela já vem integrada. O problema é que o JBoss já possui uma implementação própria dessa biblioteca e ao se carregar o SOAPMessage a biblioteca da Sun é utilizada, a qual está implementada de forma lançar um UnsupportedOperationException.</p>
<p>Uma solução é usar o esquema de <em>endorsed</em> da JVM. Bibliotecas <em>endorsed</em> são carregadas antes, tendo prioridade então sobre quaisquer outras bibliotecas. Assim, basta adicionarmos o jboss-saaj.jar (em &lt;CONFIG_HOME&gt;/lib) ao &lt;JBOSS_HOME&gt;/lib/endorsed. O script run.sh define a variável JBOSS_ENDORSED_DIRS=&#8221;$JBOSS_HOME/lib/endorsed&#8221; e cuida de setar esse diretório na propriedade java.endorsed.dirs da JVM.</p>
<h4>Mas eu estou rodando o JBoss para JDK 6! (EDIT)</h4>
<p>Existe uma &#8220;peculiaridade&#8221; se você estiver rodando o JBoss 5 para JDK 6 no WTP do Eclipse. O JBoss 5 usa o $JBOSS_HOME/lib/endorsed como o java.endorsed.dirs, setando esse parâmetro quando executamos o $JBOSS_HOME/bin/run.sh. O problema quando executamos via WTP é que o eclipse não usar o run.sh, portanto não seta o diretório endorsed e caímos no mesmo problema do JBoss 4.</p>
<p>Para resolver o problema basta clicar no &#8220;launch configuration&#8221; do servidor e adicionar o seguinte parâmetro no &#8220;VM arguments&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">-Djava.endorsed.dirs=..<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>endorsed</pre></div></div>

<p><strong>IMPORTANTE:</strong> O JBoss não é oficialmente suportado na JDK 6 e outros imprevistos podem vir a ocorrer. Recomenda-se utilizar a JDK 5, ao invés.</p>
]]></content:encoded>
			<wfw:commentRss>http://rafaelliu.net/2008/05/08/setproperty-must-be-overridden-by-all-subclasses-of-soapmessage/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

