Search in sources :

Example 11 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class Palette method processSubmission.

@Override
protected void processSubmission(String controlName) {
    String parameterValue = request.getParameter(controlName);
    JSONArray values = new JSONArray(parameterValue);
    // Use a couple of local variables to cut down on access via bindings
    Collection<Object> selected = this.selected;
    selected.clear();
    ValueEncoder encoder = this.encoder;
    // TODO: Validation error if the model does not contain a value.
    int count = values.length();
    for (int i = 0; i < count; i++) {
        String value = values.getString(i);
        Object objectValue = encoder.toValue(value);
        selected.add(objectValue);
    }
    putPropertyNameIntoBeanValidationContext("selected");
    try {
        fieldValidationSupport.validate(selected, resources, validate);
        this.selected = selected;
    } catch (final ValidationException e) {
        validationTracker.recordError(this, e.getMessage());
    }
    removePropertyNameFromBeanValidationContext();
}
Also used : JSONArray(org.apache.tapestry5.json.JSONArray)

Example 12 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class FieldValidatorDefaultSourceImplTest method invokes_all_constraint_generators.

@SuppressWarnings("unchecked")
@Test
public void invokes_all_constraint_generators() throws Exception {
    getMocksControl().checkOrder(true);
    ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
    FieldValidator fv1 = mockFieldValidator();
    FieldValidator fv2 = mockFieldValidator();
    FieldValidatorSource source = mockFieldValidatorSource();
    Class propertyType = Integer.class;
    AnnotationProvider provider = mockAnnotationProvider();
    String overrideId = "overrideId";
    Messages overrideMessages = mockMessages();
    Field field = mockField();
    Locale locale = Locale.ENGLISH;
    String value = "*VALUE*";
    train_buildConstraints(gen, propertyType, provider, "cons1", "cons2");
    train_createValidator(source, field, "cons1", null, overrideId, overrideMessages, locale, fv1);
    train_createValidator(source, field, "cons2", null, overrideId, overrideMessages, locale, fv2);
    fv1.validate(value);
    fv2.validate(value);
    replay();
    FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source);
    FieldValidator composite = fieldValidatorSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, provider);
    composite.validate(value);
    verify();
}
Also used : ValidationConstraintGenerator(org.apache.tapestry5.services.ValidationConstraintGenerator) Locale(java.util.Locale) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) Field(org.apache.tapestry5.Field) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidator(org.apache.tapestry5.FieldValidator) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) Test(org.testng.annotations.Test)

Example 13 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class ClientDataEncoderImpl method decodeClientData.

public ObjectInputStream decodeClientData(String clientData) {
    // The clientData is Base64 that's been gzip'ed (i.e., this matches
    // what ClientDataSinkImpl does).
    int colonx = clientData.indexOf(':');
    if (colonx < 0) {
        throw new IllegalArgumentException("Client data must be prefixed with its HMAC code.");
    }
    // Extract the string presumably encoded by the server using the secret key.
    String storedHmacResult = clientData.substring(0, colonx);
    String clientStream = clientData.substring(colonx + 1);
    try {
        Base64InputStream b64in = new Base64InputStream(clientStream);
        validateHMAC(storedHmacResult, b64in);
        // After reading it once to validate, reset it for the actual read (which includes the GZip decompression).
        b64in.reset();
        BufferedInputStream buffered = new BufferedInputStream(new GZIPInputStream(b64in));
        return new ObjectInputStream(buffered);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) Base64InputStream(org.apache.tapestry5.internal.util.Base64InputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 14 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class ValidateBindingFactory method newBinding.

public Binding newBinding(String description, ComponentResources container, ComponentResources component, final String expression, Location location) {
    Object fieldAsObject = component.getComponent();
    if (!Field.class.isInstance(fieldAsObject))
        throw new TapestryException(String.format("Component '%s' is not a field (it does not implement the Field interface) and may not be used with the validate: binding prefix.", component.getCompleteId()), location, null);
    final Field field = (Field) fieldAsObject;
    return new InvariantBinding(location, FieldValidator.class, interner.intern(description + ": " + expression)) {

        public Object get() {
            return fieldValidatorSource.createValidators(field, expression);
        }
    };
}
Also used : Field(org.apache.tapestry5.Field) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 15 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class MessagesConstraintGenerator method buildConstraints.

public List<String> buildConstraints(Class propertyType, AnnotationProvider annotationProvider) {
    EnvironmentMessages environmentMessages = environment.peek(EnvironmentMessages.class);
    if (environmentMessages == null) {
        return null;
    }
    String key = environmentMessages.getOverrideId() + "-validate";
    Messages m = environmentMessages.getMessages();
    if (!m.contains(key)) {
        return null;
    }
    String result = m.get(key);
    if (InternalUtils.isBlank(result)) {
        return null;
    }
    return Arrays.asList(splitPattern.split(result));
}
Also used : Messages(org.apache.tapestry5.commons.Messages)

Aggregations

Test (org.testng.annotations.Test)16 Field (org.apache.tapestry5.Field)7 ValidationException (org.apache.tapestry5.ValidationException)5 Validate (org.apache.tapestry5.beaneditor.Validate)5 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)5 UploadedFile (org.apache.tapestry5.upload.services.UploadedFile)5 PropertyConduit (org.apache.tapestry5.beanmodel.PropertyConduit)4 Html5Support (org.apache.tapestry5.services.Html5Support)4 ValidationConstraintGenerator (org.apache.tapestry5.services.ValidationConstraintGenerator)4 Messages (org.apache.tapestry5.commons.Messages)3 MultipartDecoder (org.apache.tapestry5.upload.services.MultipartDecoder)3 FieldValidator (org.apache.tapestry5.FieldValidator)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 BeanValidationContextImpl (org.apache.tapestry5.internal.BeanValidationContextImpl)2 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)2 Heartbeat (org.apache.tapestry5.services.Heartbeat)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1