List of usage examples for org.apache.commons.lang.builder ToStringBuilder reflectionToString
public static String reflectionToString(Object object, ToStringStyle style)
Forwards to ReflectionToStringBuilder
.
From source file:org.raml.jaxrs.codegen.core.AbstractGenerator.java
private void addMinMaxConstraint(final AbstractParam parameter, final String name, final Class<? extends Annotation> clazz, final BigDecimal value, final JVar argumentVariable) { try {// w w w . ja v a 2 s . c om final long boundary = value.longValueExact(); argumentVariable.annotate(clazz).param(DEFAULT_ANNOTATION_PARAMETER, boundary); } catch (final ArithmeticException ae) { LOGGER.info("Non integer " + name + " constraint ignored for parameter: " + ToStringBuilder.reflectionToString(parameter, SHORT_PREFIX_STYLE)); } }
From source file:org.raml.jaxrs.codegen.core.Generator.java
private void addJsr303Annotations(final AbstractParam parameter, final JVar argumentVariable) { if (isNotBlank(parameter.getPattern())) { LOGGER.info("Pattern constraint ignored for parameter: " + ToStringBuilder.reflectionToString(parameter, SHORT_PREFIX_STYLE)); }// ww w.j a v a 2 s . c o m final Integer minLength = parameter.getMinLength(); final Integer maxLength = parameter.getMaxLength(); if ((minLength != null) || (maxLength != null)) { final JAnnotationUse sizeAnnotation = argumentVariable.annotate(Size.class); if (minLength != null) { sizeAnnotation.param("min", minLength); } if (maxLength != null) { sizeAnnotation.param("max", maxLength); } } final BigDecimal minimum = parameter.getMinimum(); if (minimum != null) { addMinMaxConstraint(parameter, "minimum", Min.class, minimum, argumentVariable); } final BigDecimal maximum = parameter.getMinimum(); if (maximum != null) { addMinMaxConstraint(parameter, "maximum", Max.class, maximum, argumentVariable); } if (parameter.isRequired()) { argumentVariable.annotate(NotNull.class); } }
From source file:org.raml.jaxrs.codegen.core.GeneratorTestCase.java
private void run(final JaxrsVersion jaxrsVersion, final boolean useJsr303Annotations) throws Exception { final Set<String> generatedSources = new HashSet<String>(); final Configuration configuration = new Configuration(); configuration.setJaxrsVersion(jaxrsVersion); configuration.setUseJsr303Annotations(useJsr303Annotations); configuration.setOutputDirectory(codegenOutputFolder.getRoot()); configuration.setBasePackageName(TEST_BASE_PACKAGE); generatedSources.addAll(new Generator().run( new InputStreamReader(getClass().getResourceAsStream("/org/raml/full-config-with-patch.yaml")), configuration));//from w w w.j a v a2 s. co m configuration.setBasePackageName(TEST_BASE_PACKAGE + ".params"); generatedSources.addAll(new Generator().run( new InputStreamReader( getClass().getResourceAsStream("/org/raml/params/param-types-with-repeat.yaml")), configuration)); configuration.setBasePackageName(TEST_BASE_PACKAGE + ".integration"); generatedSources.addAll(new Generator().run( new InputStreamReader(getClass() .getResourceAsStream("/org/raml/integration/sales-enablement-api-with-collections.yaml")), configuration)); configuration.setBasePackageName(TEST_BASE_PACKAGE + ".rules"); generatedSources.addAll(new Generator().run( new InputStreamReader(getClass().getResourceAsStream("/org/raml/rules/resource-full-ok.yaml")), configuration)); generatedSources.addAll(new Generator().run( new InputStreamReader( getClass().getResourceAsStream("/org/raml/rules/resource-with-description-ok.yaml")), configuration)); generatedSources.addAll(new Generator().run( new InputStreamReader(getClass().getResourceAsStream("/org/raml/rules/resource-with-uri.yaml")), configuration)); configuration.setBasePackageName(TEST_BASE_PACKAGE + ".schema"); generatedSources.addAll(new Generator().run( new InputStreamReader(getClass().getResourceAsStream("/org/raml/schema/valid-xml-global.yaml")), configuration)); generatedSources.addAll(new Generator().run( new InputStreamReader(getClass().getResourceAsStream("/org/raml/schema/valid-xml.yaml")), configuration)); // test compile the classes final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse"); final JavaCompilerSettings settings = compiler.createDefaultSettings(); settings.setSourceVersion("1.5"); settings.setTargetVersion("1.5"); settings.setDebug(true); final String[] sources = generatedSources.toArray(EMPTY_STRING_ARRAY); System.out.println("Test compiling: " + Arrays.toString(sources)); final FileResourceReader sourceReader = new FileResourceReader(codegenOutputFolder.getRoot()); final FileResourceStore classWriter = new FileResourceStore(compilationOutputFolder.getRoot()); final CompilationResult result = compiler.compile(sources, sourceReader, classWriter, Thread.currentThread().getContextClassLoader(), settings); assertThat(ToStringBuilder.reflectionToString(result.getErrors(), ToStringStyle.SHORT_PREFIX_STYLE), result.getErrors(), is(emptyArray())); assertThat(ToStringBuilder.reflectionToString(result.getWarnings(), ToStringStyle.SHORT_PREFIX_STYLE), result.getWarnings(), is(emptyArray())); // test load the classes with Jersey final URLClassLoader resourceClassLoader = new URLClassLoader( new URL[] { compilationOutputFolder.getRoot().toURI().toURL() }); final ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(resourceClassLoader); final ResourceConfig config = new PackagesResourceConfig(TEST_BASE_PACKAGE); assertThat("Found: " + config.getRootResourceClasses(), config.getRootResourceClasses(), hasSize(13)); // TODO testing: actually send HTTP requests at the resources } finally { Thread.currentThread().setContextClassLoader(initialClassLoader); } }
From source file:org.raml.jaxrs.codegen.core.MediaTypeGeneratorTestCase.java
private void run(final JaxrsVersion jaxrsVersion, final boolean useJsr303Annotations) throws Exception { final Set<String> generatedSources = new HashSet<String>(); final Configuration configuration = new Configuration(); configuration.setJaxrsVersion(jaxrsVersion); configuration.setUseJsr303Annotations(useJsr303Annotations); configuration.setOutputDirectory(codegenOutputFolder.getRoot()); configuration.setBasePackageName(TEST_BASE_PACKAGE); String dirPath = getClass().getResource("/org/raml").getPath(); configuration.setSourceDirectory(new File(dirPath)); generatedSources.addAll(new Generator().run( new InputStreamReader( getClass().getResourceAsStream("/org/raml/mediatype/application_octet-stream.yaml")), configuration));//from www . j a va 2 s . c o m // test compile the classes final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse"); final JavaCompilerSettings settings = compiler.createDefaultSettings(); settings.setSourceVersion("1.5"); settings.setTargetVersion("1.5"); settings.setDebug(true); final String[] sources = generatedSources.toArray(EMPTY_STRING_ARRAY); final FileResourceReader sourceReader = new FileResourceReader(codegenOutputFolder.getRoot()); final FileResourceStore classWriter = new FileResourceStore(compilationOutputFolder.getRoot()); final CompilationResult result = compiler.compile(sources, sourceReader, classWriter, Thread.currentThread().getContextClassLoader(), settings); assertThat(ToStringBuilder.reflectionToString(result.getErrors(), ToStringStyle.SHORT_PREFIX_STYLE), result.getErrors(), is(emptyArray())); assertThat(ToStringBuilder.reflectionToString(result.getWarnings(), ToStringStyle.SHORT_PREFIX_STYLE), result.getWarnings(), is(emptyArray())); // test load the classes with Jersey final URLClassLoader resourceClassLoader = new URLClassLoader( new URL[] { compilationOutputFolder.getRoot().toURI().toURL() }); final ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(resourceClassLoader); final ResourceConfig config = new PackagesResourceConfig(TEST_BASE_PACKAGE); Set<Class<?>> classes = config.getClasses(); Iterator<Class<?>> it = classes.iterator(); Class<?> something = it.next(); Method[] methods = something.getDeclaredMethods(); Method methodWithInputStreamParam = methods[0]; assertEquals(InputStream.class.getName(), methodWithInputStreamParam.getParameterTypes()[0].getName()); } finally { Thread.currentThread().setContextClassLoader(initialClassLoader); } }
From source file:org.raml.jaxrs.codegen.core.ResponseWrapperGeneratorTestCase.java
private void run(final JaxrsVersion jaxrsVersion, final boolean useJsr303Annotations) throws Exception { final Set<String> generatedSources = new HashSet<String>(); final Configuration configuration = new Configuration(); configuration.setJaxrsVersion(jaxrsVersion); configuration.setUseJsr303Annotations(useJsr303Annotations); configuration.setOutputDirectory(codegenOutputFolder.getRoot()); configuration.setBasePackageName(TEST_BASE_PACKAGE); String dirPath = getClass().getResource("/org/raml").getPath(); configuration.setSourceDirectory(new File(dirPath)); generatedSources.addAll(new Generator().run( new InputStreamReader(getClass().getResourceAsStream("/org/raml/responses/wrapper.yaml")), configuration));//w w w . jav a2 s . c o m // test compile the classes final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse"); final JavaCompilerSettings settings = compiler.createDefaultSettings(); settings.setSourceVersion("1.5"); settings.setTargetVersion("1.5"); settings.setDebug(true); final String[] sources = generatedSources.toArray(EMPTY_STRING_ARRAY); final FileResourceReader sourceReader = new FileResourceReader(codegenOutputFolder.getRoot()); final FileResourceStore classWriter = new FileResourceStore(compilationOutputFolder.getRoot()); final CompilationResult result = compiler.compile(sources, sourceReader, classWriter, Thread.currentThread().getContextClassLoader(), settings); CompilationProblem[] errors = result.getErrors(); assertThat(ToStringBuilder.reflectionToString(errors, ToStringStyle.SHORT_PREFIX_STYLE), errors, is(emptyArray())); assertThat(ToStringBuilder.reflectionToString(result.getWarnings(), ToStringStyle.SHORT_PREFIX_STYLE), result.getWarnings(), is(emptyArray())); }
From source file:org.slc.sli.api.security.context.ContextValidator.java
/** * Validates entities, based upon entity definition and entity ids to validate. * * @param def - Definition of entities to validate * @param ids - Collection of ids of entities to validate * @param isTransitive - Determines whether validation is through another entity type * * @throws APIAccessDeniedException - When entities cannot be accessed *//*from w w w . ja v a 2 s . c o m*/ public void validateContextToEntities(EntityDefinition def, Collection<String> ids, boolean isTransitive) throws APIAccessDeniedException { LOG.debug(">>>ContextValidator.validateContextToEntities()"); LOG.debug(" def: " + ToStringBuilder.reflectionToString(def, ToStringStyle.DEFAULT_STYLE)); LOG.debug(" ids: {}", (ids == null) ? "null" : ids.toArray().toString()); LOG.debug(" isTransitive" + isTransitive); IContextValidator validator = findValidator(def.getType(), isTransitive); LOG.debug(" loaded validator: " + ToStringBuilder.reflectionToString(validator, ToStringStyle.DEFAULT_STYLE)); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery( new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); LOG.debug(" getIdsQuery: " + getIdsQuery.toString()); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); LOG.debug(" entities: " + ToStringBuilder.reflectionToString(entities, ToStringStyle.DEFAULT_STYLE)); Set<String> idsToValidate = getEntityIdsToValidate(def, entities, isTransitive, ids); LOG.debug(" idsToValidate: " + idsToValidate.toString()); Set<String> validatedIds = getValidatedIds(def, idsToValidate, validator); LOG.debug(" validatedIds: " + validatedIds.toString()); if (!validatedIds.containsAll(idsToValidate)) { LOG.debug(" ...APIAccessDeniedException"); throw new APIAccessDeniedException("Cannot access entities", def.getType(), idsToValidate); } } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } }
From source file:org.slc.sli.api.security.context.resolver.EdOrgHelper.java
/** * Get directly associated education organizations for the specified principal, filtered by * data ownership./* ww w. j a va 2 s .co m*/ */ public Set<String> getDirectEdorgs(Entity principal) { LOG.trace(">>>getDirectEdorgs.getDirectEdorgs(using security principal)"); LOG.trace(" principal: " + ToStringBuilder.reflectionToString(principal, ToStringStyle.MULTI_LINE_STYLE)); return getEdOrgs(principal, true); }
From source file:org.slc.sli.api.security.context.resolver.EdOrgHelper.java
private Set<String> getEdOrgs(Entity principal, boolean filterByOwnership) { LOG.trace(">>>EdOrgHelper.getDirectEdorgs()"); LOG.trace(" principal: " + ToStringBuilder.reflectionToString(principal, ToStringStyle.MULTI_LINE_STYLE)); LOG.trace(" filterByOwnership: " + filterByOwnership); Set<String> result = new HashSet<String>(); if (isStaff(principal) || isTeacher(principal)) { LOG.trace(" ...staff or teacher"); result = getStaffDirectlyAssociatedEdorgs(principal, filterByOwnership); } else if (isStudent(principal)) { LOG.trace(" ...student"); result = getStudentsCurrentAssociatedEdOrgs(Collections.singleton(principal.getEntityId()), filterByOwnership);/*from w w w .j a v a 2s. c o m*/ } else if (isParent(principal)) { LOG.trace(" ...parent"); SLIPrincipal prince = new SLIPrincipal(); prince.setEntity(principal); prince.populateChildren(repo); result = getStudentsCurrentAssociatedEdOrgs(prince.getOwnedStudentIds(), false); } if (result == null) { LOG.debug(" ...method did not do anything so return empty set..."); } return result; }
From source file:org.slc.sli.api.security.context.resolver.EdOrgHelper.java
/** * Get current education organizations for the specified staff member. */// w ww. j a v a 2s . c om private Set<String> getStaffDirectlyAssociatedEdorgs(Entity staff, boolean filterByOwnership) { LOG.trace(">>>EdOrgHelper.getStaffDirectlyAssociatedEdorgs()"); LOG.trace(" staff: " + ToStringBuilder.reflectionToString(staff, ToStringStyle.MULTI_LINE_STYLE)); LOG.trace(" filterByOwnership: " + filterByOwnership); Set<String> edorgs = new HashSet<String>(); Iterable<Entity> associations = locateValidSEOAs(staff.getEntityId(), filterByOwnership); for (Entity association : associations) { LOG.trace(" association: " + ToStringBuilder.reflectionToString(association, ToStringStyle.MULTI_LINE_STYLE)); edorgs.add((String) association.getBody().get(ParameterConstants.EDUCATION_ORGANIZATION_REFERENCE)); } return edorgs; }
From source file:org.slc.sli.api.security.context.validator.StaffToStudentValidator.java
private Set<String> validateStaffToStudentContextThroughSharedEducationOrganization(Collection<String> ids) { LOG.trace(">>>StaffToStudentValidator.validateStaffToStudentContextThroughSharedEducationOrganization()"); LOG.debug(" ids: {}", (ids == null) ? "null" : ToStringBuilder.reflectionToString(ids, ToStringStyle.DEFAULT_STYLE)); // lookup current staff edOrg associations and get the Ed Org Ids Set<String> staffsEdOrgIds = getStaffCurrentAssociatedEdOrgs(); // lookup students Iterable<Entity> students = getStudentEntitiesFromIds(ids); Set<String> validIds = new HashSet<String>(); if (students != null && students.iterator().hasNext()) { LOG.debug(" Iterating on each student entity."); for (Entity entity : students) { LOG.debug(" student: " + ToStringBuilder.reflectionToString(entity, ToStringStyle.DEFAULT_STYLE)); Set<String> studentsEdOrgs = getStudentsEdOrgs(entity); Set<String> validPrograms = programValidator.validate(EntityNames.PROGRAM, getValidPrograms(entity)); Set<String> validCohorts = cohortValidator.validate(EntityNames.COHORT, getValidCohorts(entity)); LOG.debug(" # studentsEdOrgs: {}", (studentsEdOrgs == null) ? "null" : studentsEdOrgs.size()); LOG.debug(" # validPrograms: {}", (validPrograms == null) ? "null" : validPrograms.size()); LOG.debug(" # validCohorts: {}", (validCohorts == null) ? "null" : validCohorts.size()); boolean hasStaffStudentIntersection = isIntersection(staffsEdOrgIds, studentsEdOrgs); LOG.debug(" hasStaffStudentIntersection = " + hasStaffStudentIntersection); if ((hasStaffStudentIntersection || !validPrograms.isEmpty() || !validCohorts.isEmpty())) { validIds.add(entity.getEntityId()); }/*from w w w.j av a 2s .com*/ } } return validIds; }