You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Christian Schlichtherle <ch...@schlichtherle.de> on 2007/06/05 14:26:10 UTC

Setting request headers when accessing HTTP URLs from sitemap

Hi,
 
I need to set request headers when accessing an http: URL in a sitemap
pipeline.
 
I've digged into the source and came up with a solution to inherit from the
class org.apache.excalibur.source.impl.HTTPClientSourceFactory and override
the method getSource(String, Map). The second parameter is a Map which is
added to the GET request as header parameters by the super class. This map
seems to be null all the time. So I create a HashMap, populate it with my
parameters and pass it to the super class implementation. Finally I needed
to register my custom component in the configuration file cocoon.xconf.
Done.
 
Since all the building bricks already exist in Cocoon, I wonder if this has
been done already. There must be an even simpler solution, but I could not
figure it. Any hints would be welcome.
 
Kind regards,
Christian Schlichtherle
-- 
Schlichtherle IT Services
Wittelsbacherstr. 10a
10707 Berlin
 
Tel: +49 (0) 30 / 34 35 29 29
Mobil: +49 (0) 173 / 27 12 470
mailto:christian@schlichtherle.de
http://www.schlichtherle.de <http://www.schlichtherle.de/> 
 

Re: Setting request headers when accessing HTTP URLs from sitemap

Posted by Joerg Heinicke <jo...@gmx.de>.
On 05.06.2007 23:13, Christian Schlichtherle wrote:

> which allows to set arbitrary request headers via parameters in the
> query string with the prefix "request:". Any parameter with this
> prefix in its name is removed from the resulting URI and the
> remaining name and value are set as request headers in the GET
> request.

That was the part I was interested in. Thanks for the info.

Joerg

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: Setting request headers when accessing HTTP URLs from sitemap

Posted by Christian Schlichtherle <ch...@schlichtherle.de>.
Hi,

> > Since all the building bricks already exist in Cocoon, I wonder if 
> > this has been done already. There must be an even simpler solution, 
> > but I could not figure it. Any hints would be welcome.
> 
> I'm not aware of a usage of that source (factory) in Cocoon. 
> But there is a proxy block which might help you. From a short 
> look the generators use HttpClient directly, not that source. 
> I don't know if and how they handle request headers. How do 
> you plan to get the map filled with the actual request 
> headers? Are they configurable?

here's how it works: Any component should go through the SourceResolver
when, well, resolving source URIs. The source resolver is configurable via
the <source-factories> tag in cocoon.xconf. Given my custom resolver, here
is the relevant entry:

  <source-factories>
    <!-- rhttp: is an alias for http: which allows to set arbitrary request
         headers via parameters in the query string with the prefix
"request:".
         Any parameter with this prefix in its name is removed from the
         resulting URI and the remaining name and value are set as request
         headers in the GET request. E.g.
 
"rhttp://www.destination.com/?name=value&request:referer=http://www.origin.c
om/"
         resolves to "http://www.destination.com/?name=value" where the GET
         request has the "referer" parameter set to
"http://www.origin.com/".
      -->
    <component-instance
class="com.acme.source.RequestHTTPClientSourceFactory" name="rhttp"/>
    <!-- ... -->
  </source-factories>

The relevant source code in the class is really simple:

public class RequestHTTPClientSourceFactory
        extends HTTPClientSourceFactory {

    /* ... */

    public Source getSource(String uri, Map params)
    throws MalformedURLException, IOException {
        if (params == null)
            params = new HashMap();
        
        builder.setURI(uri); // URIBuilder
        builder.setScheme("http"); // replace custom scheme (e.g. "rhttp")
        for (String name : builder.getParameterNames()) {
            if (!name.startsWith(PARAMETER_PREFIX))
                continue;
            Collection<String> values = builder.getParameterValues(name);
            name = name.substring(PARAMETER_PREFIX.length());
            for (String value : values)
                params.put(name, value);
            values.clear(); // remove all parameter name=value pairs for the
current name
        }
        uri = builder.toString(); // retrieve updated URI

        return super.getSource(uri, params);
    }
}

The tricky part is the URIBuilder class, which provides synchronized views
for parameter name=value pairs and their components in the query string, but
that's a different story.

The benefit of this approach is that it works everwhere in the site map. I
use it with the nekohtml generator to parse data from another web site for a
mashup.

Kind regards,
Christian


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Setting request headers when accessing HTTP URLs from sitemap

Posted by Joerg Heinicke <jo...@gmx.de>.
On 05.06.2007 14:26, Christian Schlichtherle wrote:

> I need to set request headers when accessing an http: URL in a sitemap
> pipeline.
>  
> I've digged into the source and came up with a solution to inherit from the
> class org.apache.excalibur.source.impl.HTTPClientSourceFactory and override
> the method getSource(String, Map). The second parameter is a Map which is
> added to the GET request as header parameters by the super class. This map
> seems to be null all the time. So I create a HashMap, populate it with my
> parameters and pass it to the super class implementation. Finally I needed
> to register my custom component in the configuration file cocoon.xconf.
> Done.
>  
> Since all the building bricks already exist in Cocoon, I wonder if this has
> been done already. There must be an even simpler solution, but I could not
> figure it. Any hints would be welcome.

I'm not aware of a usage of that source (factory) in Cocoon. But there 
is a proxy block which might help you. From a short look the generators 
use HttpClient directly, not that source. I don't know if and how they 
handle request headers. How do you plan to get the map filled with the 
actual request headers? Are they configurable?

Joerg

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Setting request headers when accessing HTTP URLs from sitemap

Posted by Joerg Heinicke <jo...@gmx.de>.
On 05.06.2007 17:32, johnson wrote:

> http://cocoon.apache.org/2.1/userdocs/concepts/modules.html
> 
> using modules or using xsp internal to do this.

I don't see how they can help here.

Joerg

> Christian Schlichtherile

>> nI need to set rgequest headers when accessing an http: URL in a 
>> sitemap pipeline.
>>  
>> I've digged into the source and came up with a solution to inherit 
>> from the class 
>> org.apache.excalibur.source.impl.HTTPClientSourceFactory and override 
>> the method getSource(String, Map). The second parameter is a Map which 
>> is added to the GET request as header parameters by the super class. 
>> This map seems to be null all the time. So I create a HashMap, 
>> populate it with my parameters and pass it to the super class 
>> implementation. Finally I needed to register my custom component in 
>> the configuration file cocoon.xconf. Done.
>>  
>> Since all the building bricks already exist in Cocoon, I wonder if 
>> this has been done already. There must be an even simpler solution, 
>> but I could not figure it. Any hints would be welcome.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Setting request headers when accessing HTTP URLs from sitemap

Posted by johnson <jo...@erp.tw>.
http://cocoon.apache.org/2.1/userdocs/concepts/modules.html

using modules or using xsp internal to do this.

johnson

Christian Schlichtherile
> Hi,
>  
> nI need to set rgequest headers when accessing an http: URL in a 
> sitemap pipeline.
>  
> I've digged into the source and came up with a solution to inherit 
> from the class 
> org.apache.excalibur.source.impl.HTTPClientSourceFactory and override 
> the method getSource(String, Map). The second parameter is a Map which 
> is added to the GET request as header parameters by the super class. 
> This map seems to be null all the time. So I create a HashMap, 
> populate it with my parameters and pass it to the super class 
> implementation. Finally I needed to register my custom component in 
> the configuration file cocoon.xconf. Done.
>  
> Since all the building bricks already exist in Cocoon, I wonder if 
> this has been done already. There must be an even simpler solution, 
> but I could not figure it. Any hints would be welcome.
>  
> Kind regards,
> Christian Schlichtherle
> -- 
> Schlichtherle IT Services
> Wittelsbacherstr. 10a
> 10707 Berlin
>  
> Tel: +49 (0) 30 / 34 35 29 29
> Mobil: +49 (0) 173 / 27 12 470
> mailto:christian@schlichtherle.de
> http://www.schlichtherle.de <http://www.schlichtherle.de/>
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org