Search in sources :

Example 11 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project resteasy by resteasy.

the class GeneralValidatorImpl method checkForConstraintViolations.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void checkForConstraintViolations(HttpRequest request, Exception e) {
    Throwable t = e;
    while (t != null && !(t instanceof ConstraintViolationException)) {
        t = t.getCause();
    }
    if (t instanceof ResteasyViolationException) {
        throw ResteasyViolationException.class.cast(t);
    }
    if (t instanceof ConstraintViolationException) {
        SimpleViolationsContainer violationsContainer = getViolationsContainer(request, null);
        ConstraintViolationException cve = ConstraintViolationException.class.cast(t);
        Set cvs = cve.getConstraintViolations();
        violationsContainer.addViolations(cvs);
        if (violationsContainer.size() > 0) {
            throw new ResteasyViolationExceptionImpl(violationsContainer, request.getHttpHeaders().getAcceptableMediaTypes());
        }
    }
    return;
}
Also used : Set(java.util.Set) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ConstraintViolationException(jakarta.validation.ConstraintViolationException)

Example 12 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project org.openntf.xsp.jakartaee by OpenNTF.

the class GenericThrowableMapper method createResponseFromException.

protected Response createResponseFromException(final Throwable throwable, final int status, ResourceInfo resourceInfo, HttpServletRequest req) {
    MediaType type = getMediaType(resourceInfo);
    if (MediaType.TEXT_HTML_TYPE.isCompatible(type)) {
        // Handle as HTML
        return Response.status(status).type(MediaType.TEXT_HTML_TYPE).entity((StreamingOutput) out -> {
            try (PrintWriter w = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))) {
                XSPErrorPage.handleException(w, throwable, req.getRequestURL().toString(), false);
            } catch (ServletException e) {
                throw new IOException(e);
            }
        }).build();
    } else {
        // Handle as JSON
        return Response.status(status).type(MediaType.APPLICATION_JSON_TYPE).entity((StreamingOutput) out -> {
            Objects.requireNonNull(out);
            String message = "";
            Throwable t = throwable;
            while ((message == null || message.length() == 0) && t != null) {
                if (t instanceof NotesException) {
                    message = ((NotesException) t).text;
                } else if (t instanceof ConstraintViolationException) {
                    message = t.getMessage();
                    if (message == null || message.isEmpty()) {
                        List<String> cvMsgList = new ArrayList<>();
                        for (@SuppressWarnings("rawtypes") ConstraintViolation cv : ((ConstraintViolationException) t).getConstraintViolations()) {
                            String cvMsg = cv.getPropertyPath() + ": " + cv.getMessage();
                            cvMsgList.add(cvMsg);
                        }
                        message = String.join(",", cvMsgList);
                    }
                } else {
                    message = t.getMessage();
                }
                t = t.getCause();
            }
            JsonGeneratorFactory jsonFac = Json.createGeneratorFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));
            try (JsonGenerator json = jsonFac.createGenerator(out)) {
                json.writeStartObject();
                json.write("message", throwable.getClass().getName() + ": " + message);
                json.writeKey("stackTrace");
                json.writeStartArray();
                for (Throwable cause = throwable; cause != null; cause = cause.getCause()) {
                    json.writeStartArray();
                    json.write(cause.getClass().getName() + ": " + cause.getLocalizedMessage());
                    Arrays.stream(cause.getStackTrace()).map(String::valueOf).map(line -> "  at " + line).forEach(json::write);
                    json.writeEnd();
                }
                json.writeEnd();
                json.writeEnd();
            }
        }).build();
    }
}
Also used : Arrays(java.util.Arrays) XSPErrorPage(com.ibm.designer.runtime.domino.adapter.util.XSPErrorPage) Context(jakarta.ws.rs.core.Context) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) JsonGeneratorFactory(jakarta.json.stream.JsonGeneratorFactory) WebApplicationException(jakarta.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) Response(jakarta.ws.rs.core.Response) Controller(jakarta.mvc.Controller) OutputStreamWriter(java.io.OutputStreamWriter) Priority(jakarta.annotation.Priority) Produces(jakarta.ws.rs.Produces) Request(jakarta.ws.rs.core.Request) Method(java.lang.reflect.Method) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) NotesException(lotus.domino.NotesException) PrintWriter(java.io.PrintWriter) ConstraintViolation(jakarta.validation.ConstraintViolation) NotFoundException(jakarta.ws.rs.NotFoundException) IOException(java.io.IOException) ConstraintViolationException(jakarta.validation.ConstraintViolationException) UriInfo(jakarta.ws.rs.core.UriInfo) JsonGenerator(jakarta.json.stream.JsonGenerator) StandardCharsets(java.nio.charset.StandardCharsets) Json(jakarta.json.Json) Priorities(jakarta.ws.rs.Priorities) Objects(java.util.Objects) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper) ResourceInfo(jakarta.ws.rs.container.ResourceInfo) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) IOException(java.io.IOException) JsonGeneratorFactory(jakarta.json.stream.JsonGeneratorFactory) ServletException(javax.servlet.ServletException) NotesException(lotus.domino.NotesException) ConstraintViolation(jakarta.validation.ConstraintViolation) MediaType(jakarta.ws.rs.core.MediaType) ConstraintViolationException(jakarta.validation.ConstraintViolationException) JsonGenerator(jakarta.json.stream.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ArrayList(java.util.ArrayList) List(java.util.List) PrintWriter(java.io.PrintWriter)

Example 13 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project Clownfish by rawdog71.

the class JavaList method onCreate.

@Override
public void onCreate(ActionEvent actionEvent) {
    try {
        if (!javaName.isBlank()) {
            CfJava newjava = new CfJava();
            newjava.setName(javaName);
            switch(javaLanguage) {
                case 0:
                    newjava.setContent("package io.clownfish.java;\n\n" + "public class " + javaName + "\n{\n\n}");
                    break;
                case 1:
                    newjava.setContent("package io.clownfish.kotlin;\n\n" + "public class " + javaName + "\n{\n\n}");
                    break;
                case 2:
                    newjava.setContent("package io.clownfish.groovy;\n\n" + "public class " + javaName + "\n{\n\n}");
                    break;
                case 3:
                    newjava.setContent("package io.clownfish.scala\n\n" + "class " + javaName + "\n{\n\n}");
                    break;
            }
            newjava.setLanguage(javaLanguage);
            cfjavaService.create(newjava);
            javaListe = cfjavaService.findAll();
            javaName = "";
        } else {
            FacesMessage message = new FacesMessage("Please enter Java name");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    } catch (ConstraintViolationException ex) {
        LOGGER.error(ex.getMessage());
    }
}
Also used : CfJava(io.clownfish.clownfish.dbentities.CfJava) ConstraintViolationException(jakarta.validation.ConstraintViolationException) FacesMessage(javax.faces.application.FacesMessage)

Example 14 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project Clownfish by rawdog71.

the class JavascriptList method onCreate.

@Override
public void onCreate(ActionEvent actionEvent) {
    try {
        if (!javascriptName.isBlank()) {
            CfJavascript newjavascript = new CfJavascript();
            newjavascript.setName(javascriptName);
            newjavascript.setContent("//" + javascriptName);
            cfjavascriptService.create(newjavascript);
            javascriptListe = cfjavascriptService.findAll();
            javascriptName = "";
            selectedJavascript = newjavascript;
            onSelect(null);
            onCheckOut(null);
        } else {
            FacesMessage message = new FacesMessage("Please enter javascript name");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    } catch (ConstraintViolationException ex) {
        LOGGER.error(ex.getMessage());
    }
}
Also used : CfJavascript(io.clownfish.clownfish.dbentities.CfJavascript) ConstraintViolationException(jakarta.validation.ConstraintViolationException) FacesMessage(javax.faces.application.FacesMessage)

Example 15 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project Clownfish by rawdog71.

the class ClassList method onEdit.

public void onEdit(ActionEvent actionEvent) {
    try {
        selectedClass.setName(className);
        selectedClass.setSearchrelevant(classSearchrelevant);
        selectedClass.setMaintenance(classMaintenance);
        selectedClass.setTemplateref(selectedTemplateRef);
        cfclassService.edit(selectedClass);
        classListe = cfclassService.findAll();
        contentlist.init();
        datalist.init();
        FacesMessage message = new FacesMessage("Content changed");
        FacesContext.getCurrentInstance().addMessage(null, message);
    } catch (ConstraintViolationException ex) {
        LOGGER.error(ex.getMessage());
    }
}
Also used : ConstraintViolationException(jakarta.validation.ConstraintViolationException) FacesMessage(javax.faces.application.FacesMessage)

Aggregations

ConstraintViolationException (jakarta.validation.ConstraintViolationException)114 Test (org.testng.annotations.Test)55 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)35 ConstraintViolation (jakarta.validation.ConstraintViolation)32 Validator (jakarta.validation.Validator)29 TestForIssue (org.hibernate.validator.testutil.TestForIssue)26 HibernateValidator (org.hibernate.validator.HibernateValidator)19 Size (jakarta.validation.constraints.Size)16 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)15 Test (org.junit.Test)14 Session (org.hibernate.Session)10 Transaction (org.hibernate.Transaction)10 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)10 CustomerRepositoryImpl (org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl)10 Customer (org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer)9 FacesMessage (javax.faces.application.FacesMessage)8 NotNull (jakarta.validation.constraints.NotNull)7 BigDecimal (java.math.BigDecimal)7 Set (java.util.Set)7 EntityManager (jakarta.persistence.EntityManager)6