You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Nils Preusker <n....@googlemail.com> on 2009/04/24 17:08:18 UTC

[C3] Accessing XSLT URL that requires Authentication

Hi all,

I just started trying out cocoon 3 alpha 1 and I'm wondering if there is a
solution for the following scenario: I'm retrieving all my XML and XSLT
content via URLs that require authentication. Without the authentication I
could simply create a pipeline like this:
...
Pipeline pipeline = new NonCachingPipeline();
pipeline.addComponent(new StringGenerator("<x></x>"));
pipeline.addComponent(new XSLTTransformer(new URL("
http://example.com/somexslt.xslt")));
pipeline.addComponent(new XMLSerializer());

ByteArrayOutputStream baos = new ByteArrayOutputStream();
pipeline.setup(baos);

pipeline.execute();
...
That's almost the same as the example on the cocoon website. The only
difference is that instead of a file I'm using a URL to provide the XSLT.
This works if I disable authentication for my XML/XSLT repository. But when
I enable authentication, I'll end up with an exception and a 401 from apache
somewhere in the stack trace...

So in short: I'm wondering if there is a way to create an XSLTTransformer
that can retrieve data from URLs that require authentication?

Any help would be greatly appreciated! Cheers, Nils

Re: [C3] Accessing XSLT URL that requires Authentication

Posted by Steven Dolg <st...@indoqa.com>.
Nils Preusker schrieb:
> Hi Steven,
>
> thanks for your reply, I created a new XSLTTransformer for now, based 
> on the code you suggested in your mail. Since we're talking feature 
> requests, I think it would also make sense to provide a 
> PipelineComponent that allows to access XML documents through 
> authenticated URLs. I'm using the following for now:
>
> ...
> public class AuthenticatedUrlXmlProducer extends AbstractXMLProducer 
> implements Starter {
>
>     private URL url;
>     private String username;
>     private String password;
>
>     public AuthenticatedUrlXmlProducer(URL url, String username, 
> String password) {
>         super();
>         this.url = url;
>         this.username = username;
>         this.password = password;
>     }
>
>     public void execute() {
>         try {
>             String encoding = new 
> sun.misc.BASE64Encoder().encode((username + ":" + password).getBytes());
>             URLConnection uc = url.openConnection();
>             uc.setRequestProperty("Authorization", "Basic " + encoding);
>             XMLUtils.toSax(uc.getInputStream(), this.getXMLConsumer());
>         } catch (IOException e) {
>             throw new ProcessingException(e);
>         }
>     }
> }

Yes, that looks nice.

Basically every pipeline component uses URLs to access resources. That's 
why I think there should be a rather easy way to support authentication 
in each and every component currently in existence.

I have created a new Jira issue for this 
(https://issues.apache.org/jira/browse/COCOON3-35) to keep track of the 
progress and collect all relevant information there.
We will have to do some refactoring to provide a flexible and easy way 
that is working for the components we have now and the ones that will 
come in the future, but I'm very confident that this easily done.

Please feel free to add your comments and thoughts to the issue...

>
> I think the alpha release of Cocoon 3 looks great and I'm looking 
> forwards to see where the journey is going with the first beta!

Very glad to hear this, of course. Thank you!

Steven

>
> Cheers, Nils
>
>     Hello Nils,
>
>     I'm afraid there is currently no support for accessing URLs that
>     require authentication.
>
>     While I think this is a feature all components should have, it
>     will take some time to add this and you'd have to wait for the
>     next release or change to the trunk in order to use it.
>     A faster way would be to create a new XSLTTransformer and add the
>     authentication to it.
>
>     The code for accessing the URL (currently in the
>     createTransformerHandler() method) should look like this (just top
>     off my head, no guarantees ;-) )
>
>           String userPassword = "Username" + ":" + "Password";
>           String encoded = new
>     sun.misc.BASE64Encoder().encode(userPassword.getBytes());
>           URLConnection connection = this.source.openConnection();
>           connection.setRequestProperty("Authorization", "Basic " +
>     encoded);
>           InputStream inputStream = connection.getInputStream();
>
>           InputSource inputSource = new InputSource();
>           inputSource.setByteStream(inputStream);
>
>           XMLReader xmlReader = XMLReaderFactory.createXMLReader();
>           xmlReader.setContentHandler(templatesHandler);
>           xmlReader.parse(inputSource);
>
>           inputStream.close();
>
>
>     But as I said above, this is a sound feature request and you can
>     expect this to be in one of the next releases...
>
>     Regards,
>     Steven
>
>
>     Nils Preusker schrieb:
>
>         Hi all,
>
>         I just started trying out cocoon 3 alpha 1 and I'm wondering
>         if there is a solution for the following scenario: I'm
>         retrieving all my XML and XSLT content via URLs that require
>         authentication. Without the authentication I could simply
>         create a pipeline like this:
>         ...
>         Pipeline pipeline = new NonCachingPipeline();
>         pipeline.addComponent(new StringGenerator("<x></x>"));
>         pipeline.addComponent(new XSLTTransformer(new
>         URL("http://example.com/somexslt.xslt")));
>         pipeline.addComponent(new XMLSerializer());
>
>         ByteArrayOutputStream baos = new ByteArrayOutputStream();
>         pipeline.setup(baos);
>
>         pipeline.execute();
>         ...
>         That's almost the same as the example on the cocoon website.
>         The only difference is that instead of a file I'm using a URL
>         to provide the XSLT. This works if I disable authentication
>         for my XML/XSLT repository. But when I enable authentication,
>         I'll end up with an exception and a 401 from apache somewhere
>         in the stack trace...
>
>         So in short: I'm wondering if there is a way to create an
>         XSLTTransformer that can retrieve data from URLs that require
>         authentication?
>
>         Any help would be greatly appreciated! Cheers, Nils
>
>
>
>     ---------------------------------------------------------------------
>     To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>     <ma...@cocoon.apache.org>
>     For additional commands, e-mail: users-help@cocoon.apache.org
>     <ma...@cocoon.apache.org>
>
>


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


Re: [C3] Accessing XSLT URL that requires Authentication

Posted by Nils Preusker <n....@googlemail.com>.
Hi Steven,

thanks for your reply, I created a new XSLTTransformer for now, based on the
code you suggested in your mail. Since we're talking feature requests, I
think it would also make sense to provide a PipelineComponent that allows to
access XML documents through authenticated URLs. I'm using the following for
now:

...
public class AuthenticatedUrlXmlProducer extends AbstractXMLProducer
implements Starter {

    private URL url;
    private String username;
    private String password;

    public AuthenticatedUrlXmlProducer(URL url, String username, String
password) {
        super();
        this.url = url;
        this.username = username;
        this.password = password;
    }

    public void execute() {
        try {
            String encoding = new sun.misc.BASE64Encoder().encode((username
+ ":" + password).getBytes());
            URLConnection uc = url.openConnection();
            uc.setRequestProperty("Authorization", "Basic " + encoding);
            XMLUtils.toSax(uc.getInputStream(), this.getXMLConsumer());
        } catch (IOException e) {
            throw new ProcessingException(e);
        }
    }
}

I think the alpha release of Cocoon 3 looks great and I'm looking forwards
to see where the journey is going with the first beta!

Cheers, Nils

Hello Nils,
>
> I'm afraid there is currently no support for accessing URLs that require
> authentication.
>
> While I think this is a feature all components should have, it will take
> some time to add this and you'd have to wait for the next release or change
> to the trunk in order to use it.
> A faster way would be to create a new XSLTTransformer and add the
> authentication to it.
>
> The code for accessing the URL (currently in the createTransformerHandler()
> method) should look like this (just top off my head, no guarantees ;-) )
>
>       String userPassword = "Username" + ":" + "Password";
>       String encoded = new
> sun.misc.BASE64Encoder().encode(userPassword.getBytes());
>       URLConnection connection = this.source.openConnection();
>       connection.setRequestProperty("Authorization", "Basic " + encoded);
>       InputStream inputStream = connection.getInputStream();
>
>       InputSource inputSource = new InputSource();
>       inputSource.setByteStream(inputStream);
>
>       XMLReader xmlReader = XMLReaderFactory.createXMLReader();
>       xmlReader.setContentHandler(templatesHandler);
>       xmlReader.parse(inputSource);
>
>       inputStream.close();
>
>
> But as I said above, this is a sound feature request and you can expect
> this to be in one of the next releases...
>
> Regards,
> Steven
>
>
> Nils Preusker schrieb:
>
>  Hi all,
>>
>> I just started trying out cocoon 3 alpha 1 and I'm wondering if there is a
>> solution for the following scenario: I'm retrieving all my XML and XSLT
>> content via URLs that require authentication. Without the authentication I
>> could simply create a pipeline like this:
>> ...
>> Pipeline pipeline = new NonCachingPipeline();
>> pipeline.addComponent(new StringGenerator("<x></x>"));
>> pipeline.addComponent(new XSLTTransformer(new URL("
>> http://example.com/somexslt.xslt")));
>> pipeline.addComponent(new XMLSerializer());
>>
>> ByteArrayOutputStream baos = new ByteArrayOutputStream();
>> pipeline.setup(baos);
>>
>> pipeline.execute();
>> ...
>> That's almost the same as the example on the cocoon website. The only
>> difference is that instead of a file I'm using a URL to provide the XSLT.
>> This works if I disable authentication for my XML/XSLT repository. But when
>> I enable authentication, I'll end up with an exception and a 401 from apache
>> somewhere in the stack trace...
>>
>> So in short: I'm wondering if there is a way to create an XSLTTransformer
>> that can retrieve data from URLs that require authentication?
>>
>> Any help would be greatly appreciated! Cheers, Nils
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>

Re: [C3] Accessing XSLT URL that requires Authentication

Posted by Steven Dolg <st...@indoqa.com>.
Hello Nils,

I'm afraid there is currently no support for accessing URLs that require 
authentication.

While I think this is a feature all components should have, it will take 
some time to add this and you'd have to wait for the next release or 
change to the trunk in order to use it.
A faster way would be to create a new XSLTTransformer and add the 
authentication to it.

The code for accessing the URL (currently in the 
createTransformerHandler() method) should look like this (just top off 
my head, no guarantees ;-) )

        String userPassword = "Username" + ":" + "Password";
        String encoded = new 
sun.misc.BASE64Encoder().encode(userPassword.getBytes());
        URLConnection connection = this.source.openConnection();
        connection.setRequestProperty("Authorization", "Basic " + encoded);
        InputStream inputStream = connection.getInputStream();

        InputSource inputSource = new InputSource();
        inputSource.setByteStream(inputStream);

        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setContentHandler(templatesHandler);
        xmlReader.parse(inputSource);

        inputStream.close();


But as I said above, this is a sound feature request and you can expect 
this to be in one of the next releases...

Regards,
Steven


Nils Preusker schrieb:
> Hi all,
>
> I just started trying out cocoon 3 alpha 1 and I'm wondering if there 
> is a solution for the following scenario: I'm retrieving all my XML 
> and XSLT content via URLs that require authentication. Without the 
> authentication I could simply create a pipeline like this:
> ...
> Pipeline pipeline = new NonCachingPipeline();
> pipeline.addComponent(new StringGenerator("<x></x>"));
> pipeline.addComponent(new XSLTTransformer(new 
> URL("http://example.com/somexslt.xslt")));
> pipeline.addComponent(new XMLSerializer());
>
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> pipeline.setup(baos);
>
> pipeline.execute();
> ...
> That's almost the same as the example on the cocoon website. The only 
> difference is that instead of a file I'm using a URL to provide the 
> XSLT. This works if I disable authentication for my XML/XSLT 
> repository. But when I enable authentication, I'll end up with an 
> exception and a 401 from apache somewhere in the stack trace...
>
> So in short: I'm wondering if there is a way to create an 
> XSLTTransformer that can retrieve data from URLs that require 
> authentication?
>
> Any help would be greatly appreciated! Cheers, Nils


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


Re: [C3] Accessing XSLT URL that requires Authentication

Posted by Nils Preusker <n....@googlemail.com>.
Hi Reinhard,

when I try providing the username and password like you suggested (
http:un:pw@example.com/some.xslt), I get a 401 from apache. However, using
URLConnection and setting the request property manually as mentioned in the
previous mails works fine:
...
     String encoding = new sun.misc.BASE64Encoder().
>
>     encode((username + ":" + password).getBytes());
>     URLConnection uc = url.openConnection();
>     uc.setRequestProperty("Authorization", "Basic " + encoding);
>      XMLUtils.toSax(uc.getInputStream(), this.getXMLConsumer());

...

Cheers, Nils

On Mon, Apr 27, 2009 at 10:26 PM, Reinhard Pötz <re...@apache.org> wrote:

> Nils Preusker wrote:
> > Hi all,
> >
> > I just started trying out cocoon 3 alpha 1 and I'm wondering if there is
> > a solution for the following scenario: I'm retrieving all my XML and
> > XSLT content via URLs that require authentication. Without the
> > authentication I could simply create a pipeline like this:
> > ...
> > Pipeline pipeline = new NonCachingPipeline();
> > pipeline.addComponent(new StringGenerator("<x></x>"));
> > pipeline.addComponent(new XSLTTransformer(new
> > URL("http://example.com/somexslt.xslt")));
> > pipeline.addComponent(new XMLSerializer());
> >
> > ByteArrayOutputStream baos = new ByteArrayOutputStream();
> > pipeline.setup(baos);
> >
> > pipeline.execute();
> > ...
> > That's almost the same as the example on the cocoon website. The only
> > difference is that instead of a file I'm using a URL to provide the
> > XSLT. This works if I disable authentication for my XML/XSLT repository.
> > But when I enable authentication, I'll end up with an exception and a
> > 401 from apache somewhere in the stack trace...
> >
> > So in short: I'm wondering if there is a way to create an
> > XSLTTransformer that can retrieve data from URLs that require
> > authentication?
> >
> > Any help would be greatly appreciated! Cheers, Nils
>
> Hi Nils,
>
> AFAIK the Java URL object supports RFC1738: See
> http://tools.ietf.org/html/rfc1738#section-3.1
>
> Could you please try
>
> http://username:password@example.com/somexslt.xslt
>
> Please report back if that works for you. Thanks!
>
> --
> Reinhard Pötz                           Managing Director, {Indoqa} GmbH
>                         http://www.indoqa.com/en/people/reinhard.poetz/
>
> Member of the Apache Software Foundation
> Apache Cocoon Committer, PMC member                  reinhard@apache.org
> ________________________________________________________________________
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>

Re: [C3] Accessing XSLT URL that requires Authentication

Posted by Reinhard Pötz <re...@apache.org>.
Nils Preusker wrote:
> Hi all,
> 
> I just started trying out cocoon 3 alpha 1 and I'm wondering if there is
> a solution for the following scenario: I'm retrieving all my XML and
> XSLT content via URLs that require authentication. Without the
> authentication I could simply create a pipeline like this:
> ...
> Pipeline pipeline = new NonCachingPipeline();
> pipeline.addComponent(new StringGenerator("<x></x>"));
> pipeline.addComponent(new XSLTTransformer(new
> URL("http://example.com/somexslt.xslt")));
> pipeline.addComponent(new XMLSerializer());
> 
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> pipeline.setup(baos);
> 
> pipeline.execute();
> ...
> That's almost the same as the example on the cocoon website. The only
> difference is that instead of a file I'm using a URL to provide the
> XSLT. This works if I disable authentication for my XML/XSLT repository.
> But when I enable authentication, I'll end up with an exception and a
> 401 from apache somewhere in the stack trace...
> 
> So in short: I'm wondering if there is a way to create an
> XSLTTransformer that can retrieve data from URLs that require
> authentication?
> 
> Any help would be greatly appreciated! Cheers, Nils

Hi Nils,

AFAIK the Java URL object supports RFC1738: See
http://tools.ietf.org/html/rfc1738#section-3.1

Could you please try

http://username:password@example.com/somexslt.xslt

Please report back if that works for you. Thanks!

-- 
Reinhard Pötz                           Managing Director, {Indoqa} GmbH
                         http://www.indoqa.com/en/people/reinhard.poetz/

Member of the Apache Software Foundation
Apache Cocoon Committer, PMC member                  reinhard@apache.org
________________________________________________________________________

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