Trying the attachment again as a txt

Paul R. Zepernick
Sr. Programmer Analyst
HealthSmart Benefit Solutions

-----Original Message-----
From: Paul Zepernick [mailto:paul.zepern...@healthsmart.com] 
Sent: Wednesday, February 14, 2018 8:44 AM
To: user@struts.apache.org
Subject: RE: Conversion Error Interceptor prevent errors for empty values

Yes, I have it happening on 2.5.14.1.  I have a select box pointing at a 
Integer like this: auditForm.incorrectAuditReason.id  I am using the rest 
plugin which is including it in the stack.  I have attached the source from 
Maven.  Where is the check being done in the code?  I see the check for 
shouldAddError() always returning true.

I have attempted to override the default interceptor by adding this in my 
package and overriding the shouldAddError(), but it continues to call the 
default Struts ConversionErrorInterceptor.

<interceptor name="conversionError" 
class="com.hs.iws.struts2.IwsConversionErrorInterceptor"/>



Paul R. Zepernick
Sr. Programmer Analyst
HealthSmart Benefit Solutions

-----Original Message-----
From: Yasser Zamani [mailto:yasser.zam...@live.com] On Behalf Of Yasser Zamani
Sent: Wednesday, February 14, 2018 4:25 AM
To: user@struts.apache.org
Subject: Re: Conversion Error Interceptor prevent errors for empty values



On 2/13/2018 8:06 PM, Paul Zepernick wrote:
> Can someone provide some clarification on if this interceptor should be 
> adding a field error when an empty string is passed to a Integer in the 
> action?

No, it should not be happening for `conversionError` interceptor. Does it 
happen?

>   I am trying to prevent the field error from happening in this case.  It 
> looks like it should not be happening according to the docs: 
> https://struts.apache.org/core-developers/conversion-error-interceptor.html , 
> or am I not understanding what it is saying here:
>
> "This interceptor extends ConversionErrorInterceptor but only adds conversion 
> errors from the ActionContext to the field errors of the action if the field 
> value is not null, "", or {""} (a size 1 String array with only an empty 
> String). See ConversionErrorInterceptor for more information, as well as the 
> Type Conversion documentation"

You understood correctly. I reviewed it's code and looks like it should behave 
as above. Doesn't it?

I also saw `conversionError` is already included in default stack.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org




Disclaimer: This communication and any files transmitted with it may contain 
information that is privileged, confidential and/or exempt from disclosure 
under applicable law. If you are not the intended recipient, you are hereby 
notified that any disclosure, copying, distribution, or use of the information 
contained herein (including any reliance thereon) is strictly prohibited. If 
you received this communication in error, please immediately contact the sender 
and destroy the material in its entirety, whether in electronic or hard copy 
format. Thank you.

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.commons.lang3.StringEscapeUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * <!-- START SNIPPET: description -->
 * ConversionErrorInterceptor adds conversion errors from the ActionContext to 
the Action's field errors.
 *
 * <p>
 * This interceptor adds any error found in the {@link ActionContext}'s 
conversionErrors map as a field error (provided
 * that the action implements {@link ValidationAware}). In addition, any field 
that contains a validation error has its
 * original value saved such that any subsequent requests for that value return 
the original value rather than the value
 * in the action. This is important because if the value "abc" is submitted and 
can't be converted to an int, we want to
 * display the original string ("abc") again rather than the int value (likely 
0, which would make very little sense to
 * the user).
 * </p>
 * 
 * <p>
 * <b>Note:</b> Since 2.5.2, this interceptor extends {@link 
MethodFilterInterceptor}, therefore being
 * able to deal with excludeMethods / includeMethods parameters. See [Workflow 
Interceptor]
 * (class {@link DefaultWorkflowInterceptor}) for documentation and examples on 
how to use this feature.
 * </p>
 * 
 * <!-- END SNIPPET: description -->
 *
 * <p><u>Interceptor parameters:</u></p>
 *
 * <!-- START SNIPPET: parameters -->
 *
 * <ul>
 *  <li>None</li>
 * </ul>
 *
 * <!-- END SNIPPET: parameters -->
 *
 * <p> <u>Extending the interceptor:</u></p>
 *
 * <!-- START SNIPPET: extending -->
 *
 * Because this interceptor is not web-specific, it abstracts the logic for 
whether an error should be added. This
 * allows for web-specific interceptors to use more complex logic in the {@link 
#shouldAddError} method for when a value
 * has a conversion error but is null or empty or otherwise indicates that the 
value was never actually entered by the
 * user.
 *
 * <!-- END SNIPPET: extending -->
 *
 * <p> <u>Example code:</u></p>
 *
 * <pre>
 * <!-- START SNIPPET: example -->
 * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
 *     &lt;interceptor-ref name="params"/&gt;
 *     &lt;interceptor-ref name="conversionError"/&gt;
 *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
 * &lt;/action&gt;
 * <!-- END SNIPPET: example -->
 * </pre>
 *
 * @author Jason Carreira
 */
public class ConversionErrorInterceptor extends MethodFilterInterceptor {

    public static final String ORIGINAL_PROPERTY_OVERRIDE = 
"original.property.override";

    protected Object getOverrideExpr(ActionInvocation invocation, Object value) 
{
        return escape(value);
    }

    protected String escape(Object value) {
        return "\"" + StringEscapeUtils.escapeJava(String.valueOf(value)) + 
"\"";
    }

    @Override
    public String doIntercept(ActionInvocation invocation) throws Exception {

        ActionContext invocationContext = invocation.getInvocationContext();
        Map<String, Object> conversionErrors = 
invocationContext.getConversionErrors();
        ValueStack stack = invocationContext.getValueStack();

        HashMap<Object, Object> fakie = null;

        for (Map.Entry<String, Object> entry : conversionErrors.entrySet()) {
            String propertyName = entry.getKey();
            Object value = entry.getValue();

            if (shouldAddError(propertyName, value)) {
                String message = 
XWorkConverter.getConversionErrorMessage(propertyName, stack);

                Object action = invocation.getAction();
                if (action instanceof ValidationAware) {
                    ValidationAware va = (ValidationAware) action;
                    va.addFieldError(propertyName, message);
                }

                if (fakie == null) {
                    fakie = new HashMap<>();
                }

                fakie.put(propertyName, getOverrideExpr(invocation, value));
            }
        }

        if (fakie != null) {
            // if there were some errors, put the original (fake) values in 
place right before the result
            stack.getContext().put(ORIGINAL_PROPERTY_OVERRIDE, fakie);
            invocation.addPreResultListener(new PreResultListener() {
                public void beforeResult(ActionInvocation invocation, String 
resultCode) {
                    Map<Object, Object> fakie = (Map<Object, Object>) 
invocation.getInvocationContext().get(ORIGINAL_PROPERTY_OVERRIDE);

                    if (fakie != null) {
                        invocation.getStack().setExprOverrides(fakie);
                    }
                }
            });
        }
        return invocation.invoke();
    }

    protected boolean shouldAddError(String propertyName, Object value) {
        return true;
    }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to