How to check a file upload control in a custom validator - java

I have created a custom validator in Java that I use on my XPage. All data are bound to a managed bean (including "child" data sets - rows - of the main data entity). In my validate method I check for the row component (and find some of its "siblings" to check that if one is filled the other also has to be filled or its value within a certain interval). This works fine for ordinary fields (selects, inputs, and checkboxes).
But how can I get a hold of the file upload control? A picture has to be attached if a checkbox in the same row is checked. This is the definition of my fileUpload:
<xp:fileUpload id="fileUploadSingle" value="#{row.photo}"
disableClientSideValidation="true" required="true">
<xp:this.validators>
<xp:validator validatorId="fishingTripValidator"></xp:validator>
<xp:validateRequired message="You must attach a photo"></xp:validateRequired>
</xp:this.validators>
</xp:fileUpload>
I think there are two issues at hand:
How to trigger "required" serverside. I have tested this by just setting the field to required. Works fine client-side - but does not trigger server-side... However, my validate method IS called if a value is filled in (i.e. selected a file) and I can read the value into a com.ibm.xsp.http.UploadedFile variable and get e.g. the client file name (getClientFileName()).
Get a hold of the file upload control when finding that the checkbox has been checked - and test if the file upload control has any file specified in it...
I can find the right control as a UIComponent and get a hold of the com.ibm.xsp.component.UIFileuploadEx.UploadedFile object. See this snippet:
com.ibm.xsp.component.xp.XspFileUpload fileUpload = (com.ibm.xsp.component.xp.XspFileUpload) component;
if (null != fileUpload) {
com.ibm.xsp.component.UIFileuploadEx.UploadedFile uploadedFile = (com.ibm.xsp.component.UIFileuploadEx.UploadedFile) fileUpload.getValue();
if (null != uploadedFile) {
return uploadedFile.getFilename();
}
}
return null;
The strange thing is that when I check the box AND attach a file - the above code will NOT find the file - and the validator returns an error. The filename shown on the file upload control has now disappeared. However, if I save again without changing anything then the file control in the code snippet correctly now finds the file - so no validation error is thrown and the file is correctly uploaded...
If only I could trigger validation on "required" I think I could avoid the other "hacks" where obviously something is happening "in between" the two save's.
Any ideas to how I can control this correctly??
/John

A workaround is to add a hidden input field to every row. This field's required property depends on the checkbox and the file upload control:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.data>
<xp:dominoDocument var="document1" formName="RTItem" action="editDocument" concurrencyMode="force" />
</xp:this.data>
<xp:repeat id="repeatFiles" rows="30" var="row" value="#{javascript:['A','B','C','D','E','F'];}">
<xp:checkBox text="File?" id="checkBoxFile" checkedValue="1" uncheckedValue="0" />
<xp:fileUpload id="fileUpload" value="#{document1[row]}" />
<xp:inputHidden id="inputHiddenFileHelper">
<xp:this.required>
<![CDATA[#{javascript:
var cmpCheck = getComponent( "checkBoxFile" );
var cmpFile = getComponent( "fileUpload" );
if( cmpCheck.value == "0" )
return false;
if( cmpFile.getValue() == null )
return true;
}]]>
</xp:this.required>
</xp:inputHidden>
<xp:br />
</xp:repeat>
<xp:messages id="messages1" />
<!-- a button to refresh the page -->
<xp:button value="Submit" id="buttonRefreshMe">
<xp:eventHandler event="onclick" submit="true" immediate="false" save="true"/>
</xp:button>
</xp:view>

Related

How to disable JSP error checking in WildFly 10

In my jsp code and there is an input field which I have used the same attribute twice but with different values.( I have used "disabled" attribute twice within <sj:submit/> in below code ). admin and operator are 2 boolean values which are passing from the backend.
<s:set id="admin" var="admin"><s:property value="admin" default="true"/></s:set>
<s:set id="operator" var="operator"><s:property value="operator" default="true"/></s:set>
<sj:submit button="true" value="Submit" name="submit" id="subview" onClick="todo()"
disabled="#admin" disabled="#operator" />
This code worked fine for weblogic server, but this code throws an org.apache.jasper.JasperException exception "jsp.error.attribute.duplicate" when using this code with wildfly 10. This exception occurred because of the 2 "disabled" attributes.Since I have to use both "disabled" attributes and I have used this in many pages, I can not change them one by one. Instead, is there a way to disable jsp error checking when using with wildfly 10 ?
I found the answer to this problem after some researchs. The answer is "there is no way to disable jsp error checking in wildfly 10,in order to avoid getting 'jsp.error.attribute.duplicate' error". If you check the attribute parsing method of undertow-io 'parseAttributes(boolean pageDirective)' ,you can see that, within the attribute parsing method it checks whether this attribute is a unique one or not, by using a 'UniqueAttributesImpl' object named as 'attrs' during the compilation.
Attributes parseAttributes(boolean pageDirective) throws JasperException {
UniqueAttributesImpl attrs = new UniqueAttributesImpl(pageDirective);
reader.skipSpaces();
int ws = 1;
try {
while (parseAttribute(attrs)) {
if (ws == 0 && STRICT_WHITESPACE) {
err.jspError(reader.mark(),
"jsp.error.attribute.nowhitespace");
}
ws = reader.skipSpaces();
}
} catch (IllegalArgumentException iae) {
// Duplicate attribute
err.jspError(reader.mark(), "jsp.error.attribute.duplicate");
}
return attrs;
}
If it found out that this attribute has used more than once, it throws an IllegalArgumentException exception, with the message 'jsp.error.attribute.duplicate'.
This article explains more about this issue.
It's possible to disable such checking by setting
org.apache.jasper.compiler.Parser.STRICT_WHITESPACE=false
in WildFly launch file.

Check fails even if it exists in scala

I have recorded a session in scala. One request is failing even though I can see that tag in View source and Inspect Element. I tried all the other hidden fields but seems this one is not found. Here is the script:
val scn = scenario("Scenario Name")
.feed(csv("user_credentials.csv"))
.exec(http("request_1")
.get("/userLogin")
.headers(headers_1)
.check(regex("""<input id="javax.faces.ViewState" """).saveAs("ViewState_id"))
).pause(1)
.exec(http("request_999")
.post("/userLogin")
.headers(headers_1)
.param("""loginForm""", """loginForm""")
.param("""errorMsg""", """""")
.param("""c_username""", "${username}")
.param("""javax.faces.ViewState""", "${ViewState_id}")
.param("""goButton""", """goButton""")
)
The error which I'm getting is,
c.e.e.g.h.a.GatlingAsyncHandlerActor - Request 'request_1'
failed : Check 'exists' failed, found None
I found the tag <input id="javax.faces.ViewState" ..../> in source but this script is not able to find it. I tried testing with other fields and some hidden fields also, all other components are found except this. How to solve this issue?
I suspect the problem is that the id of your ViewState object is not actually javax.faces.ViewState - the name will be javax.faces.ViewState, but in the JSF implementations that I've seen, the id is something like j_id1:javax.faces.ViewState:0.
The simplest solution would be to follow the instructions at https://github.com/excilys/gatling/wiki/Handling-JSF for handling JSF in Gatling. Or, you could search for the ViewState element by name, rather than id - something like:
.check(css("""input[name="javax.faces.ViewState"]""", "value").saveAs("ViewState_id"))
Should do the trick.

SWT Text field setting and RCP command binding to key

I have RCP application, and i have a command in it, that launches a wizard of some entity (edit wizard, that shows all fields of entity, and user can change it and finish wizard to save this entity). I'm using JFace data binding to bind entity's field to swt Texts and Combos.
That command have handler (that contains wizard call), and this handler is bind to some button and all works fine.
Then i need to bind this command to some Key combination (Ctrl+E for example). I'm using org.eclipse.ui.bindings extention for it:
<key
commandId="com.project.command"
contextId="com.project.view.context"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+E">
</key>
"com.project.view.context" is made by me to bind to same key combination in 2 different views and it's looks ok (and activates different commands in these 2 views).
But when i open my wizard through this key combination there is a problem:
SWT Text fields dont binds to Integer fields of entity. With String fields it's all ok, and they're binding fine. But Integer fields dont (There are just empty space in it).
What i've tried:
I debugged my wizard and wizard page, and all time entity state is
fine (their Integer fields is correct and not 0 or null)
Tried to write Integer to String Convertors for JFace binding. Didnt help.
I tried disable JFace binding for this fields, and seting Text field
value manual:
swtTextField.setText(entity.getIntegerField().toString());
But this also didnt work! Looks like it's not JFace binding problem,
but SWT text problem? Debuggin this situation :
entity.getIntegerField().toString() = "1234" before and after
"setText" swtTextField.getText = "" before and after "setText"
(And when i run this debug not from Key combination-command call,
all looks good and swtTextField.getText = "1234" after "setText")
Tried to change context of binding extention to default
("org.eclipse.ui.contexts.window") that didnt helped too.
So, summing up, all works fine, when i calling my command through button (or context menu). But there is a problem with Integer->Text fields (String fields works fine) when i calling my command through key combination binding extention.
Have any ideas what's wrong with it?
Added: I found out, that the problem is in key combination. When the key combination contains not-english key symbol (Ctrl+non-english-key my language key symbol, cause our application uses not-english key combinations) then the problem appears: SWT Text doesnt accept Integer values. When the key combinations is english (Ctrl+english-key) - all is ok.
All other commands (without SWT Text fields) works fine too, and they are binded to Ctrl+non-english-key combinations too...
That is very strange and I still dont understand, why that hanneps...
I have bumped into this problem several months ago. The problem was with JFace Data Bindings. What helped:
use org.eclipse.core.databinding.observable.value.IObservableValue for observing your Text. So you can write code like this: IObservableValue yourTextObserveTextObserveWidget = SWTObservables.observeText(yourText, SWT.Modify);
Use org.eclipse.core.databinding.beans.BeansObservables to observe the value of your entity so you can write code like this: BeansObservables.observeValue(yourModel, "yourInt");
Use org.eclipse.core.databinding.DataBindingContext and bind your IObservableValue like this: bindingContext.bindValue(yourTextObserveTextObserveWidget, yourModelTemplateObserveValue, null, null)
So the final code for binding your model to a Text will be like this:
DataBindingContext bindingContext = new DataBindingContext();
IObservableValue yourTextObserveTextObserveWidget = SWTObservables.observeText(yourText, SWT.Modify);
IObservableValue yourModelTemplateObserveValue = BeansObservables.observeValue(yourModel, "yourInt");
bindingContext.bindValue(yourTextObserveWidget, yourModelTemplateObserveValue, null, null);
Please check the documentation of the data binding if you have any further questions. This is working in my program with String, boolean and Integer types. I haven't tested anythig else though.

Java not whole object returned?

I am developing JSF but the problem is in the java I believe. Ok so I have a table with requests, when I press the id of the request that is sent to the reviewRequest page with:
<h:inputHidden id="id" value="#{requestClass.requestID}" />
Now that's working because I load the request details on next page (by taking ID and retrieving object from database). now when I modify the object from reviewRequest and accept, it says it is stored successfully. I then view the same page again from table I click the request id and there it goes bang nullpointerexception. When it is loading the object this time, it pass the id to retrieve method then it only returns the change but not the whole object details like name, contacts, etc. only that the user of type x submitted modification y. retrieve method from DB works for sure because all over the app it is working correctly. Any idea? some of the code below for illustration:
public void callIsValidUser(){
boolean holder = isValidUser();
if(holder == true){
rsvIns = loadDetails();
}else{
try{
FacesContext.getCurrentInstance().getExternalContext().dispatch("pending.xhtml");
}catch(IOException ioe){
System.err.print(ioe);
}
}
}
the method above works the first time but not after modification. in isValid():
public boolean isValidUser(){
boolean valid = false;
try{
rsvLocal = oracleRsv.retrieveReservation(id);
String reqDivHead = rsvLocal.rdhUser.getUserID();
//rsvLocal.rdhUser.getUserResponse();
String supervisor = rsvLocal.sUser.getUserID();
String divHead = rsvLocal.dhUser.getUserID();
String currentUser = System.getProperty("user.name");
.....
now when I inspect the rsvLocal in netbeans debug mode, i see that rdhUser.response holds the modification I entered but all rest is null. How can this happen? how can some data be retrieved from object?
Thanks,
Most likely you did not load the data at the very beginning. Then JSF created an empty bean and sets the values from the form. Now everything not mentioned in the form (and of course every empty field of the form) contains null. This half-baked bean is now stored in the DB overwriting the complete row. If you now load the row again you will see what you call "my modifications" but what is the complete content of the DB. Your old data is lost.
The key point is: JSF and the DB-Layers do not deal with "modifications" of individual fields - they handle complete entities.

Duplicate Validation Errors in Struts2

I am using validate() in my Action and here is how my validate() method looks.
public void validate() {
logger.info(".validate() : userName=["+userName+"] & password=["+password+"]");
clearActionErrors();
if(userName==null || userName.length()==0){
addFieldError( "userName", "User Name is required." );
}
if(password==null || password.length()==0){
addFieldError( "password", "Password is required." );
}
}
And in the jsp i added in the section.
The errors are showing up as expected. How ever when i refresh the page i see errors twice. Also if i keep refreshing it the no.of times the error message gets displayed is incremented by 1.
Not sure what's wrong with this.
I tried even calling clearActionErrors(); in my execute() method and that doesn't seem to resolve the Issue.
thanks
If you are using a table on ur jsp to display the form, then make sure that the table is a parent of the form tag. If the table is a child of form tag, the validation messages wont get cleared each time. Making the form tag as a child of table tag would solve your problem.

Categories

Resources