Re: [Resteasy-users] Set http headers per request on client side proxy

2015-03-14 Thread Ron Sigal

Hi Veit,

One option is to install a ClientRequestFilter, which can access request 
headers before a request is sent. For example,,


   static class TestRequestFilter implements ClientRequestFilter
   {
  String value;

  public void setValue(String value)
  {
 this.value = value;
  }

  @Override
  public void filter(ClientRequestContext requestContext) throws 
IOException

  {
 MultivaluedMapString, Object mmap = requestContext.getHeaders();
 ListObject list = new ArrayListObject();
 list.add(value);
 mmap.put(TestHeader, list);
  }
   }

which could be used as follows:

  ResteasyClient client = new ResteasyClientBuilder().build();
  TestRequestFilter filter = new TestRequestFilter();
  client.register(filter);
  ResteasyWebTarget target = 
client.target(http://localhost:8081/post;);

  TestResource tr = target.proxy(TestResource.class);
  filter.setValue(abc);
  Assert.assertEquals(abc, tr.post(hello 1)); // Resource 
returns value of header

  filter.setValue(xyz);
  Assert.assertEquals(xyz, tr.post(hello 1));

I've attached an example program, TestProxy.java.

-Ron


Hi.

How can I set http headers when invoking methods on the client side proxy?
I'm aware that I can e.g. set credentials on the underlying HttpClient.
I do that once
before creating the proxy. But when using the proxy, I have to change
some http headers
on every request as well. So, what's the best practive for that?

Thanks for your help!
Veit


package org.jboss.resteasy.test.junk;

import junit.framework.Assert;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.plugins.server.servlet.ResteasyContextParameters;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.test.EmbeddedContainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.ws.rs.Consumes;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.MultivaluedMap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import static org.jboss.resteasy.test.TestPortProvider.generateURL;

/**
 * @author a href=mailto:ron.si...@jboss.comRon Sigal/a
 * @date Jan 6, 2012
 */
public class TestProxy
{
   protected static ResteasyDeployment deployment;
   protected static Dispatcher dispatcher;

   public interface TestResource
   {
  @POST
  @Consumes(text/plain)
  public String post(String s);
   }
   
   @Path(/)
   public static class TestResourceImpl
   {
  @HeaderParam(TestHeader) String param;
  
  @POST
  @Path(post)
  public String post(String s)
  {
 System.out.println(header:  + param);
 return param;
  }
   }
   
   static class TestRequestFilter implements ClientRequestFilter
   {
  String value;
  
  public void setValue(String value)
  {
 this.value = value;
  }
  
  @Override
  public void filter(ClientRequestContext requestContext) throws IOException
  {
 MultivaluedMapString, Object mmap = requestContext.getHeaders();
 ListObject list = new ArrayListObject();
 list.add(value);
 mmap.put(TestHeader, list);
  } 
   }

   @Before
   public void before() throws Exception
   {
  HashtableString,String initParams = new HashtableString,String();
  HashtableString,String contextParams = new HashtableString,String();
  deployment = EmbeddedContainer.start(initParams, contextParams);
  dispatcher = deployment.getDispatcher();
  deployment.getRegistry().addPerRequestResource(TestResourceImpl.class);
   }

   @After
   public void after() throws Exception
   {
  EmbeddedContainer.stop();
  dispatcher = null;
  deployment = null;
   }

   @Test
   public void testProxy() throws Exception
   {
  ResteasyClient client = new ResteasyClientBuilder().build();
  TestRequestFilter filter = new TestRequestFilter();
  client.register(filter);
  ResteasyWebTarget target = client.target(http://localhost:8081/post;);
  TestResource tr = target.proxy(TestResource.class);
  filter.setValue(abc);
  Assert.assertEquals(abc, tr.post(hello 1));
  filter.setValue(xyz);
  Assert.assertEquals(xyz, tr.post(hello 1));
   }
}
--
Dive into the World 

[Resteasy-users] Set http headers per request on client side proxy

2015-01-17 Thread Veit Guna
Hi.

How can I set http headers when invoking methods on the client side proxy?
I'm aware that I can e.g. set credentials on the underlying HttpClient.
I do that once
before creating the proxy. But when using the proxy, I have to change
some http headers
on every request as well. So, what's the best practive for that?

Thanks for your help!
Veit





--
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
___
Resteasy-users mailing list
Resteasy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/resteasy-users