/** * Validate presence of required parameters. * Note the model type is java.lang.Object as opposed to a generic * <T extends BaseResource> in order to accommodate parameters types * such as Action. * * @param model the incoming representation * @param required the required field names * @throws WebApplicationException wrapping an appropriate response * iff a required parameter is missing */ public static void validateParameters(Object model, String... required) { validateParameters(INCOMPLETE_PARAMS_REASON, INCOMPLETE_PARAMS_DETAIL, model, 2, required); }
if (topLevel(r)) { if (!assertFields(model, subField(r))) { missing.add(r); } else if (isList(model, superField(r))) { for (Object item : asList(model, superField(r))) { if (!assertFields(item, subField(r))) { missing.add(r); } else if(!isLeaf(r)){ Object superType = get(model, superField(r)); if(superType != null) { missing.addAll(joinSuperType(doAssertRequired(reason, detail, superType, frameOffset, subField(r)), superType)); } else { missing.add(r); for (String superField : superField(r).split(ALTERNATIVE)) { found = found || (isSet(model, capitalize(superField)) && assertFields(model, superField, subField(r)));
/** * Validate presence of required parameters. * Note the model type is java.lang.Object as opposed to a generic * <T extends BaseResource> in order to accommodate parameters types * such as Action. * * @param model the incoming representation * @param required the required field names * @param frameOffset the stack frame offset of the public resource method * @throws WebApplicationException wrapping an appropriate response * iff a required parameter is missing */ public static void validateParameters(Object model, int frameOffset, String... required) { Response error = assertRequired(INCOMPLETE_PARAMS_REASON, INCOMPLETE_PARAMS_DETAIL, model, frameOffset, required); if (error != null) { throw new WebApplicationException(error); } }
private static boolean assertFields(Object model, String fields) { return assertFields(model, null, fields); }
/** * Validate presence of required parameters. * * @param reason the fault reason * @param detail the fault detail * @param model the incoming representation * @param frameOffset the stack frame offset of the public resource method * @param missingMembers the stack frame offset of the public resource method * @param required the required field names * @return error Response if appropriate */ private static Response assertRequired(String reason, String detail, Object model, int frameOffset, String... required) { List<String> missing = doAssertRequired(reason, detail, model, frameOffset, required); Response response = null; if (!missing.isEmpty()) { StackTraceElement[] trace = new Throwable().getStackTrace(); Fault fault = new Fault(); fault.setReason(reason); fault.setDetail(MessageFormat.format(detail, model.getClass().getSimpleName(), missing, trace[frameOffset + 1].getMethodName())); response = Response.status(INCOMPLETE_PARAMS_STATUS) .entity(fault) .build(); } return response; } /**
/** * Validate presence of required parameters. * Note the model type is java.lang.Object as opposed to a generic * <T extends BaseResource> in order to accommodate parameters types * such as Action. * * @param reason the fault reason * @param detail the fault detail * @param model the incoming representation * @param required the required field names * @throws WebApplicationException wrapping an appropriate response * iff a required parameter is missing */ public static void validateParameters(String reason, String detail, Object model, String... required) { validateParameters(reason, detail, model, 2, required); }
/** * Validate presence of required parameters. * Note the model type is java.lang.Object as opposed to a generic * <T extends BaseResource> in order to accommodate parameters types * such as Action. * * @param reason the fault reason * @param detail the fault detail * @param model the incoming representation * @param required the required field names * @param frameOffset the stack frame offset of the public resource method * @throws WebApplicationException wrapping an appropriate response * iff a required parameter is missing */ public static void validateParameters(String reason, String detail, Object model, int frameOffset, String... required) { Response error = assertRequired(reason, detail, model, frameOffset, required); if (error != null) { throw new WebApplicationException(error); } }
private void offsetValidateParameters(Vm vm) { CompletenessAssertor.validateParameters(vm, 2, "status"); }
public void validateParameters(Object model, int frameOffset, String... required) { String reason = localize(Messages.INCOMPLETE_PARAMS_REASON); String detail = localize(Messages.INCOMPLETE_PARAMS_DETAIL_TEMPLATE); CompletenessAssertor.validateParameters(reason, detail, model, frameOffset + 1, required); }
@Test public void testMissingListSubFields() { HostNic bond = new HostNic(); try { CompletenessAssertor.validateParameters(bond, "name", "network.id|name", "bonding.slaves.id|name"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "HostNic", "name, network.id|name, bonding.slaves.id|name"); } }
@Test public void testCompleteParametersAlternativesSecond() { Vm vm = new Vm(); vm.setName("foo"); vm.setStatus(VmStatus.UP); CompletenessAssertor.validateParameters(vm, "name", "host|status"); }
@Test public void testMissingParameter() { Vm vm = new Vm(); vm.setDescription("incomplete"); try { CompletenessAssertor.validateParameters(vm, "status"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "Vm", "status"); } }
@Test public void testMissingParameterSpecificReason() { Vm vm = new Vm(); vm.setDescription("incomplete"); try { CompletenessAssertor.validateParameters("Missing stuff", "{0} {1} required for {2}", vm, "status"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException("Missing stuff", wae, "Vm", "status"); } }
@Test public void testMissingParameters() { Vm vm = new Vm(); vm.setDescription("incomplete"); try { CompletenessAssertor.validateParameters(vm, "name", "host", "status"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "Vm", "name", "host", "status"); } }
@Test public void testMissingParameterAlteratives() { Vm vm = new Vm(); vm.setDescription("incomplete"); try { CompletenessAssertor.validateParameters(vm, "status|host|name"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "Vm", "status|host|name"); } }
@Test public void testCompleteParametersAlternativesFirst() { Vm vm = new Vm(); vm.setName("foo"); vm.setHost(new Host()); CompletenessAssertor.validateParameters(vm, "name", "host|status"); }
@Test public void testMissingSuperField() { Vm vm = new Vm(); vm.setDescription("incomplete"); try { CompletenessAssertor.validateParameters(vm, "host.id"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "Vm", "host.id"); } }
@Test public void testMissingSuperFieldAlternatives() { Permission permission = new Permission(); permission.setVm(new Vm()); try { CompletenessAssertor.validateParameters(permission, "user|vm.name"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "Permission", "user|vm.name"); } }
@Test public void testCompleteSuperFieldAlternatives() { Permission permission = new Permission(); permission.setUser(new User()); permission.getUser().setName("joe"); CompletenessAssertor.validateParameters(permission, "vm|user.name"); }
@Test public void testCompleteBothAlternatives() { Permission permission = new Permission(); permission.setUser(new User()); permission.getUser().setName("joe"); CompletenessAssertor.validateParameters(permission, "vm|user.name|id"); }