<?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; SOA</title>
	<atom:link href="http://rafaelliu.net/category/soa/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>Drools 5</title>
		<link>http://rafaelliu.net/2011/02/10/drools-5/</link>
		<comments>http://rafaelliu.net/2011/02/10/drools-5/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 13:56:45 +0000</pubDate>
		<dc:creator>rafaelliu</dc:creator>
				<category><![CDATA[Regras]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[artigo]]></category>

		<guid isPermaLink="false">http://rafaelliu.net/?p=413</guid>
		<description><![CDATA[Acaba de sair na Java Magazine edição 88 um artigo que escrevi sobre o Drools 5. Quem tiver interesse no assunto pode ter uma prévia no site da DevMedia. O código de exemplo é publicamente acessível, lembrando que é preciso ter o Maven 3 instalado. http://www.devmedia.com.br/articles/viewcomp.asp?comp=19367]]></description>
			<content:encoded><![CDATA[<p>Acaba de sair na Java Magazine edição 88 um artigo que escrevi sobre o Drools 5. Quem tiver interesse no assunto pode ter uma prévia no site da <a title="DevMedia" href="http://www.devmedia.com.br/articles/viewcomp.asp?comp=19270" target="_blank">DevMedia</a>. O <a title="Código de exemplo" href="http://www.devmedia.com.br/articles/viewcomp.asp?comp=19367 " target="_blank">código de exemplo</a> é publicamente acessível, lembrando que é preciso ter o Maven 3 instalado.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"><span style="font-family: VErdana; font-size: x-small;"><span style="font-size: xx-small;"><span style="font-family: VErdana; font-size: x-small;"><span style="color: blue;"><span style="color: black; font-size: xx-small;"><span style="font-family: Verdana; font-size: small;"><span style="font-family: VErdana; font-size: x-small;"><span style="font-size: xx-small;"><span style="font-family: VErdana; font-size: x-small;"><span style="color: blue;"><span style="color: black; font-size: xx-small;"><span style="color: black;"><span style="font-family: verdana; font-size: 12px; line-height: 19px; color: #384042;">http://www.devmedia.com.br/articles/viewcomp.asp?comp=19367 </span></span></span></span></span></span></span></span></span></span></span></span></span></div>
]]></content:encoded>
			<wfw:commentRss>http://rafaelliu.net/2011/02/10/drools-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REST com RESTEasy &#8211; JAXB e RESTEasy Client</title>
		<link>http://rafaelliu.net/2009/02/05/rest-com-resteasy-jaxb-e-resteasy-client/</link>
		<comments>http://rafaelliu.net/2009/02/05/rest-com-resteasy-jaxb-e-resteasy-client/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 19:27:09 +0000</pubDate>
		<dc:creator>rafaelliu</dc:creator>
				<category><![CDATA[JBoss]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[jaxb]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTEasy]]></category>

		<guid isPermaLink="false">http://rafaelliu.net/?p=68</guid>
		<description><![CDATA[Uma ótima apresentação sobre REST me fez querer escrever um pouco sobre. Mais especificamente sobre o RESTEasy, implemetação certificada JAX-RS da JBoss. No projeto que fiz para certificação JBoss Certified SOA Developer o usei bastante e quero falar de algumas interessantes funcionalidades dele. Primeiro algumas considerações sobre REST: RESTEasy é um framework para criação de [...]]]></description>
			<content:encoded><![CDATA[<p>Uma <a title="Apresentação REST" href="http://www.infoq.com/news/2009/01/REST-Stefan-Tilkov" target="_blank">ótima apresentação sobre REST</a> me fez querer escrever um pouco sobre. Mais especificamente sobre o <a title="Projeto RESTEasy" href="http://jboss.org/resteasy/" target="_blank">RESTEasy</a>, implemetação certificada JAX-RS da JBoss. No projeto que fiz para certificação <em>JBoss Certified SOA Developer</em> o usei bastante e quero falar de algumas interessantes funcionalidades dele. Primeiro algumas considerações sobre REST:</p>
<ol>
<li>RESTEasy é um framework para criação de <span style="text-decoration: underline;"><em>RESTful Web Services</em></span>, o que em algum senso já é uma desvirtuação de REST (tal como Roy Fielding definiu), que trata de recursos. O que tem haver um serviço RESTful que loga o usuário? (O que aliás abre uma <span style="text-decoration: line-through;">sedutora</span> brecha para quebrar outro conceito REST: o de statelessness) De agora em diante irei usar REST e RESTful Web Services indistintamente.</li>
<li>REST não é uma alternativa a SOA! Na verdade podemos usar REST para implementar SOA com WOA. Confuso? REST é uma alternativa ao WS-*, podendo ser usado no lugar dele para implementar SOA. A implementação RESTful de SOA é chamada WOA, ou ROA (veja a <a title="TLAs" href="/?page_id=64" target="_blank">página de TLAs</a>), que é um subconjunto de SOA.</li>
</ol>
<h4>JAXB binding</h4>
<p>RESTEasy possui suporte a marshalling/unmarshalling de classes usando JAXB. Sou péssimo para dar exemplos mas vamos lá&#8230;:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Servico <span style="color: #009900;">&#123;</span>
&nbsp;
	@GET @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/tempo&quot;</span><span style="color: #009900;">&#41;</span>
	@Produces<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Tempo getTempo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Parcialmente nublado&quot;</span>, <span style="color: #cc66cc;">20.3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@POST @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/literal&quot;</span><span style="color: #009900;">&#41;</span>
	@Consumes<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTempo<span style="color: #009900;">&#40;</span>Tempo tempo<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;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Descrição: %s%n&quot;</span>, tempo.<span style="color: #006633;">getDescricao</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Temperatura: %d%n&quot;</span>, tempo.<span style="color: #006633;">getTemperatura</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">// classe anotada com JAXB</span>
@XmlRootElement
<span style="color: #000000; font-weight: bold;">class</span> Tempo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> descricao<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Double</span> temperatura<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nome, <span style="color: #003399;">Double</span> preco<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descricao</span> <span style="color: #339933;">=</span> nome<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">temperatura</span> <span style="color: #339933;">=</span> preco<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescricao<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> descricao<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescricao<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nome<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descricao</span> <span style="color: #339933;">=</span> nome<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> getTemperatura<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> temperatura<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTemperatura<span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span> preco<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">temperatura</span> <span style="color: #339933;">=</span> preco<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Na invocação de getTempo() RESTEasy vai ver que o tipo de retorno é o objeto Tempo e que ele está anotado com @XmlRootElement e vai fazer um marshalling dele. A anotação @Produces(&#8220;text/xml&#8221;) diz que a representação gerada deve ser XML. Quer que seja em JSON? Basta mudar para @Produces(&#8220;application/json&#8221;)!</p>
<p>Do mesmo jeito funciona o setTempo(). O parâmetro passados sem nenhuma anotação é interpretado como o corpo da requisição HTTP, que no caso é um objeto.</p>
<h4>RESTEasy Client</h4>
<p>Algo muito interessante que não é da especificação JAX-RS é o suporte a clients. Esses clients são proxies que transformam invocações Java em requisições HTTP. Para isso devemos criar uma interface com as mesmas assinaturas do nosso serviço e anotá-las:</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> Main <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// precisa ser chamo uma única vez para registrar providers RESTEasy, scanear classes, etc</span>
		 RegisterBuiltin.<span style="color: #006633;">register</span><span style="color: #009900;">&#40;</span>ResteasyProviderFactory.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// precisa ser chamado uma única vez para</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 		 IServico client <span style="color: #339933;">=</span> ProxyFactory.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>IServico.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;http://localhost:8080/&amp;lt;Context&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		 Tempo tempo <span style="color: #339933;">=</span> client.<span style="color: #006633;">getTempo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 tempo.<span style="color: #006633;">setTemperatura</span><span style="color: #009900;">&#40;</span>tempo.<span style="color: #006633;">getTemperatura</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">5.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 client.<span style="color: #006633;">setTempo</span><span style="color: #009900;">&#40;</span>tempo<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: #000000; font-weight: bold;">interface</span> IServico <span style="color: #009900;">&#123;</span>
&nbsp;
	@GET @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/tempo&quot;</span><span style="color: #009900;">&#41;</span>
	@Produces<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> Tempo getTempo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@POST @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/literal&quot;</span><span style="color: #009900;">&#41;</span>
	@Consumes<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> setTempo<span style="color: #009900;">&#40;</span>Tempo tempo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// classe anotada com JAXB</span>
@XmlRootElement
<span style="color: #000000; font-weight: bold;">class</span> Tempo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> descricao<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Double</span> temperatura<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nome, <span style="color: #003399;">Double</span> preco<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descricao</span> <span style="color: #339933;">=</span> nome<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">temperatura</span> <span style="color: #339933;">=</span> preco<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescricao<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> descricao<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescricao<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nome<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descricao</span> <span style="color: #339933;">=</span> nome<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> getTemperatura<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> temperatura<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTemperatura<span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span> preco<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">temperatura</span> <span style="color: #339933;">=</span> preco<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Agora você deve ver no console:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Descrição: Parcialmente nublado
Temperatura: 34.000000</pre></div></div>

<p>Precisamos a classe Tempo também apenas porque estamos usando JAXB. Tudo transparente, muito fácil agora fazer teste unitários de serviços RESTful!</p>
]]></content:encoded>
			<wfw:commentRss>http://rafaelliu.net/2009/02/05/rest-com-resteasy-jaxb-e-resteasy-client/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Two components with the same name and precedence</title>
		<link>http://rafaelliu.net/2009/01/08/two-components-with-the-same-name-and-precedence/</link>
		<comments>http://rafaelliu.net/2009/01/08/two-components-with-the-same-name-and-precedence/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 12:54:03 +0000</pubDate>
		<dc:creator>rafaelliu</dc:creator>
				<category><![CDATA[JBoss]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[seam]]></category>

		<guid isPermaLink="false">http://rafaelliu.net/?p=52</guid>
		<description><![CDATA[Há um tempo atrás me pediram ajuda com a seguinte exceção: java.lang.IllegalStateException: Two components with the same name and precedence - component name: authenticator, component classes: com.app.security.Authenticator, com.app.security.Authenticator at org.jboss.seam.init.Initialization.addComponentDescriptor(Initialization.java:596) at org.jboss.seam.init.Initialization.installScannedComponentAndRoles(Initialization.java:949) at org.jboss.seam.init.Initialization.scanForComponents(Initialization.java:889) at org.jboss.seam.init.Initialization.init(Initialization.java:701) at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:35) Essa exceção pode ocorrer, claro, quando existirem duas classes com o mesmo @Name, ou pode ocorrer num [...]]]></description>
			<content:encoded><![CDATA[<p>Há um tempo atrás me pediram ajuda com a seguinte exceção:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.IllegalStateException: Two components with the same name and precedence - component name: authenticator, component classes: com.app.security.Authenticator, com.app.security.Authenticator
at org.jboss.seam.init.Initialization.addComponentDescriptor(Initialization.java:596)
at org.jboss.seam.init.Initialization.installScannedComponentAndRoles(Initialization.java:949)
at org.jboss.seam.init.Initialization.scanForComponents(Initialization.java:889)
at org.jboss.seam.init.Initialization.init(Initialization.java:701)
at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:35)</pre></div></div>

<p>Essa exceção pode ocorrer, claro, quando existirem duas classes com o mesmo @Name, ou pode ocorrer num caso mais sutil. O Seam possui um modo de debug em que ele faz <em>hot deploy</em> de páginas Facelets e pages.xml&#8217;s. Projetos criados pelo seam-gen já vêm com esse modo habilitado:</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;core:init</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Nesse modo o Seam usa um classloader próprio ao invés do classloader do container para carregar as classes. Ele faz isso criando um pasta WEB-INF/dev de onde carrega as classes. Isso não vale para EJB3 (então sem <em>hot deploy</em> de <em>entity beans</em>..) e as classes não serão visíveis para o container ou para outros projetos Seam (já que o Seam usa uma instância própria de classloader).</p>
<p>O problema é que de algum jeito o Seam (na verdade deve ser o JBoss Tools) se confunde em algumas situações e tenta fazer um novo <em>deploy</em> do componente, ao invés de fazer um <em>redeploy</em>. Com isso há um conflito de @Name. Não consegui identificar em que casos exatamente isso ocorre. Fazer um Clean, Build, etc não adianta porque a pasta dev não é mexida.</p>
<p>Bom, para resumir, basta apagar a pasta WEB-INF/dev <img src='http://rafaelliu.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://rafaelliu.net/2009/01/08/two-components-with-the-same-name-and-precedence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IBM Test 664, SOA Fundamentals</title>
		<link>http://rafaelliu.net/2008/03/25/ibm-test-664-soa-fundamentals-2/</link>
		<comments>http://rafaelliu.net/2008/03/25/ibm-test-664-soa-fundamentals-2/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 21:01:36 +0000</pubDate>
		<dc:creator>rafaelliu</dc:creator>
				<category><![CDATA[Certificação]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[ibm]]></category>

		<guid isPermaLink="false">http://rafaelliu.wordpress.com/?p=7</guid>
		<description><![CDATA[Bom, como deve ser aparente, estive ausente por um tempo. Estive bastante ocupado. Tive que fazer a prova SOA Fundamentals, para tirar a IBM Certified SOA Associate para a empresa. Nesse um mês que tive para me preparar precisei ir a Curitiba prestar serviços SOA e BPM lá, e continuar com outros projetos da empresa [...]]]></description>
			<content:encoded><![CDATA[<p>Bom, como deve ser aparente, estive ausente por um tempo. Estive <span style="text-decoration:underline;">bastante</span> ocupado. Tive que fazer a prova <em>SOA Fundamentals</em>, para tirar a <em>IBM Certified SOA Associate </em>para a empresa. Nesse um mês que tive para me preparar precisei ir a Curitiba prestar serviços SOA e BPM lá, e continuar com outros projetos da empresa enquanto estudava.</p>
<p>Hoje foi a prova, foram 54 questões, passei com 72%. O score não foi tão folgado, mas fiquei satisfeito.</p>
<p>Os materiais que usei para estudar para a prova foram:</p>
<ul>
<li><a href="http://www.amazon.com/Principles-Service-Prentice-Service-Oriented-Computing/dp/0132344823/ref=pd_lpo_k2_dp_k2a_1_img/104-1902904-4947968?pf_rd_m=ATVPDKIKX0DER&amp;pf_rd_s=lpo-top-stripe-2&amp;pf_rd_r=0V61XGXYT639H2JP8X8C&amp;pf_rd_t=201&amp;pf_rd_p=304485601&amp;pf_rd_i=0136135161" target="_blank">SOA Principles of Service Design</a>;</li>
<li><a href="http://www.amazon.com/Service-Oriented-Architecture-Dummies-Computer/dp/0470054352/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1206477435&amp;sr=1-1" target="_blank">Service Oriented Architecture For Dummies</a>;</li>
<li><a href="http://www.amazon.com/Service-Oriented-Architecture-SOA-Compass-developerWorks/dp/0131870025/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1206477397&amp;sr=1-1" target="_blank">Service-Oriented Architecture (SOA) Compass: Business Value, Planning, and Enterprise Roadmap</a>.</li>
</ul>
<p>Como não tive tempo hábil, li apenas as algumas partes de cada e não consegui ler o material dos cursos SW717, SW718 e SW719 (recomendado pelo pessoal que faz a prova). Preferi me guiar pela página de <a href="http://www-03.ibm.com/certify/tests/edu664.shtml" target="_blank">preparação para o teste</a>, de onde tirei a maioria dos títulos acima. Usei também vários artigos da IBM sobre SOA, sempre olhando a página de <a href="http://www-03.ibm.com/certify/tests/obj664.shtml" target="_blank">objetivos</a> para não perder o foco.</p>
<p>Descrobri também o grupo <a href="http://tech.groups.yahoo.com/group/ibmsoacert/" target="_blank">IBM SOA CERTIFICATION</a>, que embora esteja praticamente reduzido a <em>spam</em>s, possui uma ótima sessão de arquivos. Cuidado para não confundir o teste 664 com o 665! O grupo possui bastante material para o 665 pois é voltado para SOA em geral.</p>
]]></content:encoded>
			<wfw:commentRss>http://rafaelliu.net/2008/03/25/ibm-test-664-soa-fundamentals-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

