[Betwixt] Customizing Vector binding with dot betwixt file

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[Betwixt] Customizing Vector binding with dot betwixt file

Rodrigo Partearroyo
I?m trying to serialize a Vector of Strings. They are the security-roles
allowed to access some part of my webapp. The output i need is this:

<security-roles>
    <security-role rol="rol1" />
    <security-role rol="rol2" />
    <security-role rol="rol3" />
    <security-role rol="rol4" />
</security-roles>

but all i can obtain is this:

<security-roles>
    <security-role rol="[rol1, rol2, rol3, rol4]"/>
</security-roles>

The bean i'm using is :

public class SecurityRolesBean {
    private Vector rolesList;
    public SecurityRolesBean (){
        rolesList = new Vector();
    }
    public Vector getRolesList(){
        return rolesList;
    }
    public void setServiceRole(String role){
        setRolesList(role);
    }
    private void setRolesList(String rol){
        rolesList.addElement(rol);
    }
}

and the dot betwixt file:

<?xml version="1.0"?>
<info>
    <element name="security-roles">
    <element name="security-role">
        <attribute name='rol' property='rolesList'/>
    </element>
    </element>
</info>

The question is: How can i put each of the Vector's elements (which are
Strings) in separate xml elements?

The inverse step (populating the Bean with data read from a xml file) is
done with Digester following this rule:

[...]
<pattern value="security-role">
    <set-properties-rule>
        <alias attr-name="rol" prop-name="serviceRole" />
    </set-properties-rule>
</pattern>
[...]

Due to Digester cannot perform the inverse operation (writting beans to
xml files) i?m trying to move it to Betwixt, but the xml format cannot
change.

Thanks in advance.

--
Rodrigo Partearroyo Gonz?lez


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Betwixt] Customizing Vector binding with dot betwixt file

robert burrell donkin
On Tue, 2005-07-26 at 18:25 +0200, Rodrigo Partearroyo wrote:

> I´m trying to serialize a Vector of Strings. They are the security-roles
> allowed to access some part of my webapp. The output i need is this:
>
> <security-roles>
>     <security-role rol="rol1" />
>     <security-role rol="rol2" />
>     <security-role rol="rol3" />
>     <security-role rol="rol4" />
> </security-roles>
>
> but all i can obtain is this:
>
> <security-roles>
>     <security-role rol="[rol1, rol2, rol3, rol4]"/>
> </security-roles>
>
> The bean i'm using is :
>
> public class SecurityRolesBean {
>     private Vector rolesList;
>     public SecurityRolesBean (){
>         rolesList = new Vector();
>     }
>     public Vector getRolesList(){
>         return rolesList;
>     }
>     public void setServiceRole(String role){
>         setRolesList(role);
>     }
>     private void setRolesList(String rol){
>         rolesList.addElement(rol);
>     }
> }
>
> and the dot betwixt file:
>
> <?xml version="1.0"?>
> <info>
>     <element name="security-roles">
>     <element name="security-role">
>         <attribute name='rol' property='rolesList'/>
>     </element>
>     </element>
> </info>
>
> The question is: How can i put each of the Vector's elements (which are
> Strings) in separate xml elements?

that 'attribute' tag needs to be an 'element' tag

<?xml version="1.0"?>
<info>
    <element name="security-roles">
    <element name="security-role">
        <element name='rol' property='rolesList'/>
    </element>
    </element>
</info>

> The inverse step (populating the Bean with data read from a xml file) is
> done with Digester following this rule:
>
> [...]
> <pattern value="security-role">
>     <set-properties-rule>
>         <alias attr-name="rol" prop-name="serviceRole" />
>     </set-properties-rule>
> </pattern>
> [...]
>
> Due to Digester cannot perform the inverse operation (writting beans to
> xml files) i´m trying to move it to Betwixt, but the xml format cannot
> change.

betwixt should handle this pretty easily. you'll need to add the name of
the adder to be used to the dot betwixt file

<?xml version="1.0"?>
<info>
    <element name="security-roles">
    <element name="security-role">
        <element name='rol' property='rolesList' adder='setServiceRole'/>
    </element>
    </element>
</info>

please take a look at the documentation
(http://jakarta.apache.org/commons/betwixt) for more details.

- robert


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[HttpClient] : How I send a compressed stream using HttpClient

Nicolas Maujean
Hi,

How I send a compressed stream using HttpClient ?

thank you for your answer,

Nicolas Maujean



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [HttpClient] : How I send a compressed stream using HttpClient

Nicolas Maujean
ok you don't have to look for this I found the answer

>From: "Nicolas Maujean" <[hidden email]>
>Reply-To: "Jakarta Commons Users List" <[hidden email]>
>To: [hidden email]
>Subject: [HttpClient] : How I send a compressed stream using HttpClient
>Date: Mon, 01 Aug 2005 11:15:57 +0000
>
>Hi,
>
>How I send a compressed stream using HttpClient ?
>
>thank you for your answer,
>
>Nicolas Maujean
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [HttpClient] : How I send a compressed stream using HttpClient

Rick Herrick
Can you post the answer?

Rick Herrick
[hidden email]

> -----Original Message-----
> From: Nicolas Maujean [mailto:[hidden email]]
> Sent: Tuesday, August 02, 2005 2:24 AM
> To: [hidden email]
> Subject: RE: [HttpClient] : How I send a compressed stream using
> HttpClient
>
> ok you don't have to look for this I found the answer
>
> >From: "Nicolas Maujean" <[hidden email]>
> >Reply-To: "Jakarta Commons Users List" <[hidden email]>
> >To: [hidden email]
> >Subject: [HttpClient] : How I send a compressed stream using HttpClient
> >Date: Mon, 01 Aug 2005 11:15:57 +0000
> >
> >Hi,
> >
> >How I send a compressed stream using HttpClient ?
> >
> >thank you for your answer,
> >
> >Nicolas Maujean
> >
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [hidden email]
> >For additional commands, e-mail: [hidden email]
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [HttpClient] : How I send a compressed stream using HttpClient

Nicolas Maujean
I shall put it directly in HttpClient, I contact the responsible of the
project for that

>From: "Rick Herrick" <[hidden email]>
>Reply-To: "Jakarta Commons Users List" <[hidden email]>
>To: "'Jakarta Commons Users List'" <[hidden email]>
>Subject: RE: [HttpClient] : How I send a compressed stream using HttpClient
>Date: Tue, 2 Aug 2005 07:07:17 -0700
>
>Can you post the answer?
>
>Rick Herrick
>[hidden email]
>
> > -----Original Message-----
> > From: Nicolas Maujean [mailto:[hidden email]]
> > Sent: Tuesday, August 02, 2005 2:24 AM
> > To: [hidden email]
> > Subject: RE: [HttpClient] : How I send a compressed stream using
> > HttpClient
> >
> > ok you don't have to look for this I found the answer
> >
> > >From: "Nicolas Maujean" <[hidden email]>
> > >Reply-To: "Jakarta Commons Users List"
><[hidden email]>
> > >To: [hidden email]
> > >Subject: [HttpClient] : How I send a compressed stream using HttpClient
> > >Date: Mon, 01 Aug 2005 11:15:57 +0000
> > >
> > >Hi,
> > >
> > >How I send a compressed stream using HttpClient ?
> > >
> > >thank you for your answer,
> > >
> > >Nicolas Maujean
> > >
> > >
> > >
> > >---------------------------------------------------------------------
> > >To unsubscribe, e-mail: [hidden email]
> > >For additional commands, e-mail: [hidden email]
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [HttpClient] : How I send a compressed stream using HttpClient

Nicolas Maujean
In reply to this post by Rick Herrick
ok here are the two classes which allow to compress the datas in a stream, I
shall
post it also to the developper mailing list

import java.io.IOException;

import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpRecoverableException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.methods.PostMethod;

/**
* GZIPPostMethod
*
* <p>
* Titre : GZIPPostMethod
* </p>
*/
public class GZIPPostMethod
    extends PostMethod {

    /** Creates a new instance of GZIPAwarePostMethod */
    public GZIPPostMethod() {
        super();
    }

    /**
     * Constructor specifying a URI.
     *
     * @param uri
     *            either an absolute or relative URI
     *
     * @since 1.0
     */
    public GZIPPostMethod(String uri) {
        super(uri);
    }

    /**
     * Overrides method in {@link HttpMethodBase}.
     *
     * Notifies the server that we can process a GZIP-compressed response
before sending the request.
     *
     * @param state
     * @param conn
     *
     * @return
     * @throws HttpException
     * @throws HttpRecoverableException
     * @throws IOException
     *
     */
    public int execute(HttpState state,
                       HttpConnection conn) throws HttpException,
HttpRecoverableException, IOException {
        // Tell the server that we can handle GZIP-compressed data in the
response body
        addRequestHeader("Content-Encoding", "gzip");

        return super.execute(state,
                             conn);
    }
}


/**
* GZIPRequestEntity
*
* <p>
* Titre : GZIPRequestEntity
* </p>
*/
public class GZIPRequestEntity
    implements RequestEntity {
    /**
     * Total de bytes transfere
     */
    long mTotal = 0;


    /**
     * The content length will be calculated automatically. This implies
buffering of the content.
     */
    public static final int CONTENT_LENGTH_AUTO = -2;

    private long contentLength;

    private InputStream content;

    /** The buffered request body, if any. */
    private byte[] buffer = null;

    /** The content type */
    private String contentType;

    /**
     * Creates a new InputStreamRequestEntity with the given content and a
content type of {@link #CONTENT_LENGTH_AUTO}.
     *
     * @param content
     *            The content to set.
     */
    public GZIPRequestEntity(InputStream content) {
        this(content,
             null);
    }

    /**
     * Creates a new InputStreamRequestEntity with the given content,
content type, and a content length of
     * {@link #CONTENT_LENGTH_AUTO}.
     *
     * @param content
     *            The content to set.
     * @param contentType
     *            The type of the content, or <code>null</code>.
     */
    public GZIPRequestEntity(InputStream content,
                             String contentType) {
        this(content,
             CONTENT_LENGTH_AUTO,
             contentType);
    }

    /**
     * Creates a new InputStreamRequestEntity with the given content and
content length.
     *
     * @param content
     *            The content to set.
     * @param contentLength
     *            The content size in bytes
     */
    public GZIPRequestEntity(InputStream content,
                             long contentLength) {
        this(content,
             contentLength,
             null);
    }

    /**
     * Creates a new InputStreamRequestEntity with the given content,
content length, and content type.
     *
     * @param content
     *            The content to set.
     * @param contentLength
     *            The content size in bytes
     *
     * @param contentType
     *            The type of the content, or <code>null</code>.
     */
    public GZIPRequestEntity(InputStream content,
                             long contentLength,
                             String contentType) {
        if (content == null) {
            throw new IllegalArgumentException("The content cannot be
null");
        }
        this.content = content;
        this.contentLength = contentLength;
        this.contentType = contentType;
    }

    /*
     * (non-Javadoc)
     *
     * @see
org.apache.commons.httpclient.methods.RequestEntity#getContentType()
     */
    public String getContentType() {
        return contentType;
    }

    /**
     * Buffers request body input stream.
     */
    private void bufferContent() {

        if (this.buffer != null) {
            // Already been buffered
            return;
        }
        if (this.content != null) {
            try {
                ByteArrayOutputStream tmp = new ByteArrayOutputStream();
                byte[] data = new byte[4096];
                int l = 0;
                while ((l = this.content.read(data)) >= 0) {
                    System.out.println("buffer content l :" + l);
                    tmp.write(data,
                              0,
                              l);
                }
                this.buffer = tmp.toByteArray();
                this.content = null;
                this.contentLength = buffer.length;
            } catch (IOException e) {
                sLogger.debug(e.getMessage(),
                              e);
                this.buffer = null;
                this.content = null;
                this.contentLength = 0;
            }
        }
    }

    /**
     * Tests if this method is repeatable. Only <code>true</code> if the
content has been buffered.
     *
     * @see #getContentLength()
     */
    public boolean isRepeatable() {
        return buffer != null;
    }

    /*
     * (non-Javadoc)
     *
     * @see
org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
     */
    public void writeRequest(OutputStream out) throws IOException {

        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(out);

        try {
            if (content != null) {
                byte[] tmp = new byte[4096];
                int nbreBytes = 0;

                nbreBytes = content.read(tmp);

                while (nbreBytes >= 0) {

                    gzipOutputStream.write(tmp,
                                           0,
                                           nbreBytes);
                    mTotal += nbreBytes;
                    nbreBytes = content.read(tmp);
                }

                gzipOutputStream.flush();
            } else if (buffer != null) {
                System.out.println("write buffer");
                gzipOutputStream.write(buffer);
            } else {
                throw new IllegalStateException("Content must be set before
entity is written");
            }
        } finally {

            if (gzipOutputStream != null) {

                gzipOutputStream.close();
            }

            gzipOutputStream = null;
        }
    }

    /**
     * Gets the content length. If the content length has not been set, the
content will be buffered to determine the
     * actual content length.
     */
    public long getContentLength() {
        if (contentLength == CONTENT_LENGTH_AUTO && buffer == null) {
            bufferContent();
        }
        return contentLength;
    }

    /**
     * @return Returns the content.
     */
    public InputStream getContent() {
        return content;
    }

    /**
     * Retourne le nombre de bytes transfere
     *
     * @return Nombre de bytes transfere
     */
    public long getNbreBytesTransfere() {

        return mTotal;
    }
}


Piece of code to use these classes :

        mFluxFichierSource = new BufferedInputStream(new
FileInputStream(mFichierSource));
        // ignorer les parties avant l'offset et lire celles qui sont apres
        // l'offset
        mFluxFichierSource.skip(mOffsetDebut);

        GZIPPostMethod httppost = new GZIPPostMethod(mURL.toString());

        mGZIPRequestEntity = new GZIPRequestEntity(mFluxFichierSource);

        httppost.setRequestEntity(mGZIPRequestEntity);

        //httppost.setRequestBody(mFluxFichierSource);
        httppost.setContentChunked(true);

        HttpClient client = new HttpClient();

        client.executeMethod(httppost);

        if (httppost.getStatusCode() == HttpStatus.SC_OK) {

            sLogger.debug(httppost.getResponseBodyAsString());
        } else {

            sLogger.debug("Unexpected failure: " + httppost.getStatusLine()
                .toString());
        }

        httppost.releaseConnection();



>From: "Rick Herrick" <[hidden email]>
>Reply-To: "Jakarta Commons Users List" <[hidden email]>
>To: "'Jakarta Commons Users List'" <[hidden email]>
>Subject: RE: [HttpClient] : How I send a compressed stream using HttpClient
>Date: Tue, 2 Aug 2005 07:07:17 -0700
>
>Can you post the answer?
>
>Rick Herrick
>[hidden email]
>
> > -----Original Message-----
> > From: Nicolas Maujean [mailto:[hidden email]]
> > Sent: Tuesday, August 02, 2005 2:24 AM
> > To: [hidden email]
> > Subject: RE: [HttpClient] : How I send a compressed stream using
> > HttpClient
> >
> > ok you don't have to look for this I found the answer
> >
> > >From: "Nicolas Maujean" <[hidden email]>
> > >Reply-To: "Jakarta Commons Users List"
><[hidden email]>
> > >To: [hidden email]
> > >Subject: [HttpClient] : How I send a compressed stream using HttpClient
> > >Date: Mon, 01 Aug 2005 11:15:57 +0000
> > >
> > >Hi,
> > >
> > >How I send a compressed stream using HttpClient ?
> > >
> > >thank you for your answer,
> > >
> > >Nicolas Maujean
> > >
> > >
> > >
> > >---------------------------------------------------------------------
> > >To unsubscribe, e-mail: [hidden email]
> > >For additional commands, e-mail: [hidden email]
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Betwixt] Customizing Vector binding with dot betwixt file

Rodrigo Partearroyo
In reply to this post by robert burrell donkin
Thank for your response Robert, i?ve been on vacations till today. I'll
try your solution.

robert burrell donkin wrote:

> On Tue, 2005-07-26 at 18:25 +0200, Rodrigo Partearroyo wrote:
>
>>I?m trying to serialize a Vector of Strings. They are the security-roles
>>allowed to access some part of my webapp. The output i need is this:
>>
>><security-roles>
>>    <security-role rol="rol1" />
>>    <security-role rol="rol2" />
>>    <security-role rol="rol3" />
>>    <security-role rol="rol4" />
>></security-roles>
>>
>>but all i can obtain is this:
>>
>><security-roles>
>>    <security-role rol="[rol1, rol2, rol3, rol4]"/>
>></security-roles>
>>
>>The bean i'm using is :
>>
>>public class SecurityRolesBean {
>>    private Vector rolesList;
>>    public SecurityRolesBean (){
>>        rolesList = new Vector();
>>    }
>>    public Vector getRolesList(){
>>        return rolesList;
>>    }
>>    public void setServiceRole(String role){
>>        setRolesList(role);
>>    }
>>    private void setRolesList(String rol){
>>        rolesList.addElement(rol);
>>    }
>>}
>>
>>and the dot betwixt file:
>>
>><?xml version="1.0"?>
>><info>
>>    <element name="security-roles">
>>    <element name="security-role">
>>        <attribute name='rol' property='rolesList'/>
>>    </element>
>>    </element>
>></info>
>>
>>The question is: How can i put each of the Vector's elements (which are
>>Strings) in separate xml elements?
>
>
> that 'attribute' tag needs to be an 'element' tag
>
> <?xml version="1.0"?>
> <info>
>     <element name="security-roles">
>     <element name="security-role">
>         <element name='rol' property='rolesList'/>
>     </element>
>     </element>
> </info>
>
>>The inverse step (populating the Bean with data read from a xml file) is
>>done with Digester following this rule:
>>
>>[...]
>><pattern value="security-role">
>>    <set-properties-rule>
>>        <alias attr-name="rol" prop-name="serviceRole" />
>>    </set-properties-rule>
>></pattern>
>>[...]
>>
>>Due to Digester cannot perform the inverse operation (writting beans to
>>xml files) i?m trying to move it to Betwixt, but the xml format cannot
>>change.
>
>
> betwixt should handle this pretty easily. you'll need to add the name of
> the adder to be used to the dot betwixt file
>
> <?xml version="1.0"?>
> <info>
>     <element name="security-roles">
>     <element name="security-role">
>         <element name='rol' property='rolesList' adder='setServiceRole'/>
>     </element>
>     </element>
> </info>
>
> please take a look at the documentation
> (http://jakarta.apache.org/commons/betwixt) for more details.
>
> - robert
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

--
Rodrigo Partearroyo Gonz?lez

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]