primefacesjsf-2.2bootsfaces

ManagedBean method is changing dataTable object


I have a <p:dataTable /> that lists "System Parameters" of a web page I'm working. One of that parameters is a mail server password, and it is encoded in Base64 in the DB. In the last column of the datatable, I have a button to edit that parameters, and when I click on it, a bootstrap modal is showed with the fields "name" and "value".

I have a ManagedBean with @ViewScoped scope, and a function that also recognizes if the parameter to edit is the mail server password, to have the value decoded. The method gets the object as an argument the datatable iteration object. My problem is that the first time I open the modal, the value of the object is correct, but in second time the object value changes and it generates an exception because the value is already decoded, and the value has a dot inside and it generates the error.

I don't understand why the datatable object value is changed when I never change it literally.

I'm using PrimeFaces, BootsFaces, JSF 2.2. This is my <p:dataTable />:

<p:dataTable value="#{parameterBean.listParameter}" var="p" 
                         paginator="true" rows="10" 
                         reflow="true" id="tblParameter" 
                         emptyMessage="No data available.">
    <!-- Some columns -->

    <p:column headerText="Options">
        <b:commandButton value="Edit" icon="edit" look="info" 
                         ajax="true" update="parametroForm" 
                         onclick="ajax:parameterBean.editParameter(p)" 
                         oncomplete="$('#parameterModal').modal('show');" />
</p:dataTable>

And this is my bean method:

public void editParameter(TblParameter p) {
   System.out.println("ENCODE: " + p.getVlrParameter());
    this.param = new TblParameter(); //- This is an object in the bean to access it from modal form.

    try {
        setParam(p);

        if (getParam().getIdParameter().equals(new Long(4))) {//- Validate if is the server mail password.
            byte[] clv64 = Base64.getDecoder().decode(p.getVlrParameter().getBytes());
            getParam().setVlrParameter(new String(clv64));
        }

    } catch(Exception e) {
        System.out.println("Error: " + e.getMessage());
        e.printStackTrace();
    }
}

Finally, as I said when I open for first time the modal, the output printed is this:

ENCODE: MTIzLjQ1Ng==

But in second time, it shows:

ENCODE: 123.456
java.lang.IllegalArgumentException: Illegal base64 character 2e
at java.util.Base64$Decoder.decode0(Base64.java:714)
at java.util.Base64$Decoder.decode(Base64.java:526)
at com.abcpagos.otis.beans.admin.ParametroBean.editarParametro(ParametroBean.java:146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)...

If someone could please give me an explanation of this behavior, I would appreciate it very much.


Solution

  • Actually, you do change the parameter accidentially. Have a look at these lines:

    byte[] clv64 = Base64.getDecoder().decode(p.getVlrParameter().getBytes());
    //                                          ^^^^^^^^^^^^^^^
    getParam().setVlrParameter(new String(clv64));
    //         ^^^^^^^^^^^^^^^                
    

    So the second time you try to decode the parameter that's already stored in plain text.