This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-jacksonexporter.git
commit 66a0c832c57e40b85a33b8a0463fc05383057d87 Author: Stefan Seifert <stefanseif...@users.noreply.github.com> AuthorDate: Mon Jun 24 14:49:00 2024 +0200 SLING-12360 apply spotless code formatting --- pom.xml | 40 +++++------ .../models/jacksonexporter/ModuleProvider.java | 30 ++++---- .../ConfigurableSerializationModuleProvider.java | 71 +++++++++--------- .../impl/EnumerationSerializer.java | 33 ++++----- .../impl/HttpServletRequestMixin.java | 39 +++++----- .../impl/IgnoringResourceResolverMixin.java | 8 +-- .../jacksonexporter/impl/JacksonExporter.java | 44 ++++++------ .../impl/ModelSkippingSerializers.java | 29 ++++---- .../jacksonexporter/impl/PropertiesUtil.java | 3 +- .../impl/RequestModuleProvider.java | 36 +++++----- .../impl/ResourceModuleProvider.java | 45 ++++++------ .../jacksonexporter/impl/ResourceSerializer.java | 84 ++++++++++++---------- .../jacksonexporter/impl/ServletRequestMixin.java | 37 +++++----- .../impl/SlingHttpServletRequestMixin.java | 37 +++++----- .../impl/WarningResourceResolverMixin.java | 31 +++----- .../JacksonExporterLimitSerializationTest.java | 80 ++++++++++----------- .../impl/JacksonExporterMappingOptionsTest.java | 30 ++++---- .../impl/ModelSkippingSerializersTest.java | 35 ++++----- .../jacksonexporter/impl/PropertiesUtilTest.java | 17 ++--- .../jacksonexporter/impl/example/ExamplePojo.java | 1 - .../impl/example/PojoWithResourceResolver.java | 10 +-- .../jacksonexporter/impl/util/LogCapture.java | 16 ++--- 22 files changed, 375 insertions(+), 381 deletions(-) diff --git a/pom.xml b/pom.xml index 576c1cd..b907238 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="ISO-8859-1"?> +<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file @@ -34,14 +34,26 @@ <scm> <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-jacksonexporter.git</connection> <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-jacksonexporter.git</developerConnection> + <tag>HEAD</tag> <url>https://github.com/apache/sling-org-apache-sling-models-jacksonexporter.git</url> - <tag>HEAD</tag> - </scm> + </scm> <properties> <project.build.outputTimestamp>2023-07-13T09:44:49Z</project.build.outputTimestamp> </properties> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.junit</groupId> + <artifactId>junit-bom</artifactId> + <version>5.8.2</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> <dependency> <groupId>org.osgi</groupId> @@ -156,24 +168,12 @@ <scope>test</scope> </dependency> <dependency> - <groupId>ch.qos.logback</groupId> - <artifactId>logback-classic</artifactId> - <version>1.2.13</version> - <scope>test</scope> - </dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>1.2.13</version> + <scope>test</scope> + </dependency> </dependencies> - <dependencyManagement> - <dependencies> - <dependency> - <groupId>org.junit</groupId> - <artifactId>junit-bom</artifactId> - <version>5.8.2</version> - <type>pom</type> - <scope>import</scope> - </dependency> - </dependencies> - </dependencyManagement> - </project> diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/ModuleProvider.java b/src/main/java/org/apache/sling/models/jacksonexporter/ModuleProvider.java index a1e62e2..ada4d1e 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/ModuleProvider.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/ModuleProvider.java @@ -1,24 +1,25 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter; -import org.osgi.annotation.versioning.ConsumerType; - import com.fasterxml.jackson.databind.Module; +import org.osgi.annotation.versioning.ConsumerType; /** * Extension interface which allows for plugging in Jackson Modules @@ -28,5 +29,4 @@ import com.fasterxml.jackson.databind.Module; public interface ModuleProvider { Module getModule(); - } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ConfigurableSerializationModuleProvider.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ConfigurableSerializationModuleProvider.java index afd4b40..f62d332 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ConfigurableSerializationModuleProvider.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ConfigurableSerializationModuleProvider.java @@ -1,24 +1,28 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; import java.util.Arrays; import java.util.List; +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.module.SimpleModule; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.models.jacksonexporter.ModuleProvider; import org.osgi.service.component.annotations.Activate; @@ -29,44 +33,45 @@ import org.osgi.service.metatype.annotations.ObjectClassDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.Module; -import com.fasterxml.jackson.databind.module.SimpleModule; - @Component(service = ModuleProvider.class) @Designate(ocd = ConfigurableSerializationModuleProvider.Config.class) public class ConfigurableSerializationModuleProvider implements ModuleProvider { - - @ObjectClassDefinition(name = "Apache Sling Models Jackson Exporter - Serialization Blocker", + + @ObjectClassDefinition( + name = "Apache Sling Models Jackson Exporter - Serialization Blocker", description = "Provider of a Jackson Module which can disable the serialization of classes") static @interface Config { - @AttributeDefinition(name ="disable serialization", - description = "provide a list of the full classnames which should not get serialized; currently only \"" + RESOURCERESOLVER + "\" is supported") + @AttributeDefinition( + name = "disable serialization", + description = "provide a list of the full classnames which should not get serialized; currently only \"" + + RESOURCERESOLVER + "\" is supported") String[] disable_serialization() default {}; - - @AttributeDefinition(name ="warn on serialization", - description = "provide a list of the full classnames for which a warning should be written when serialized; currently only \"" + RESOURCERESOLVER + "\" is supported") - String[] enable_warn_logging() default {ConfigurableSerializationModuleProvider.RESOURCERESOLVER}; - + @AttributeDefinition( + name = "warn on serialization", + description = + "provide a list of the full classnames for which a warning should be written when serialized; currently only \"" + + RESOURCERESOLVER + "\" is supported") + String[] enable_warn_logging() default {ConfigurableSerializationModuleProvider.RESOURCERESOLVER}; } - + private static final Logger LOG = LoggerFactory.getLogger(ConfigurableSerializationModuleProvider.class); protected static final String RESOURCERESOLVER = "org.apache.sling.api.resource.ResourceResolver"; - + boolean ignoringRR = false; - + SimpleModule moduleInstance; - + @Activate private void activate(Config config) { this.moduleInstance = new SimpleModule(); List<String> disabled = Arrays.asList(config.disable_serialization()); List<String> logging = Arrays.asList(config.enable_warn_logging()); - + // Currently only the Sling ResourceResolver is supported - for (String type: disabled) { + for (String type : disabled) { if (RESOURCERESOLVER.equals(type)) { moduleInstance.setMixInAnnotation(ResourceResolver.class, IgnoringResourceResolverMixin.class); ignoringRR = true; @@ -74,8 +79,8 @@ public class ConfigurableSerializationModuleProvider implements ModuleProvider { LOG.warn("Support to disable the serialization of type {} is not implemented", type); } } - - for (String type: logging) { + + for (String type : logging) { if (RESOURCERESOLVER.equals(type)) { if (!ignoringRR) { moduleInstance.setMixInAnnotation(ResourceResolver.class, WarningResourceResolverMixin.class); @@ -85,11 +90,9 @@ public class ConfigurableSerializationModuleProvider implements ModuleProvider { } } } - @Override public Module getModule() { return moduleInstance; } - } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/EnumerationSerializer.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/EnumerationSerializer.java index 5812939..054ffb9 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/EnumerationSerializer.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/EnumerationSerializer.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; @@ -20,14 +22,13 @@ import java.io.IOException; import java.util.Enumeration; import java.util.Iterator; -import org.apache.commons.collections4.iterators.EnumerationIterator; - import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.ResolvableSerializer; +import org.apache.commons.collections4.iterators.EnumerationIterator; /** * Trivial serializer for Enumeration types (needed for Servlet APIs) which leverages @@ -44,8 +45,8 @@ public class EnumerationSerializer extends JsonSerializer<Enumeration> implement @Override @SuppressWarnings("unchecked") - public void serialize(Enumeration value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { + public void serialize(Enumeration value, JsonGenerator jgen, SerializerProvider provider) + throws IOException, JsonProcessingException { iteratorSerializer.serialize(new EnumerationIterator(value), jgen, provider); } - } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/HttpServletRequestMixin.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/HttpServletRequestMixin.java index d161453..471162b 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/HttpServletRequestMixin.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/HttpServletRequestMixin.java @@ -1,27 +1,29 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; -import java.security.Principal; -import java.util.Enumeration; - import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; +import java.security.Principal; +import java.util.Enumeration; + import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonGetter; @@ -29,7 +31,10 @@ import com.fasterxml.jackson.annotation.JsonGetter; * This "mixin" interface instructs the Jackson ObjectMapper what properties should be included in JSON view of a HttpServletRequest object. * Without it, the auto-detection may lead to unexpected results, e.g. StackOverflow errors. */ -@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE) +@JsonAutoDetect( + getterVisibility = JsonAutoDetect.Visibility.NONE, + fieldVisibility = JsonAutoDetect.Visibility.NONE, + isGetterVisibility = JsonAutoDetect.Visibility.NONE) public interface HttpServletRequestMixin extends ServletRequestMixin, HttpServletRequest { @JsonGetter @@ -62,7 +67,7 @@ public interface HttpServletRequestMixin extends ServletRequestMixin, HttpServle @JsonGetter @Override - public String getRemoteUser(); + public String getRemoteUser(); @JsonGetter @Override diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/IgnoringResourceResolverMixin.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/IgnoringResourceResolverMixin.java index 5f4a3c5..376b930 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/IgnoringResourceResolverMixin.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/IgnoringResourceResolverMixin.java @@ -18,21 +18,17 @@ */ package org.apache.sling.models.jacksonexporter.impl; -import org.apache.sling.api.resource.ResourceResolver; - import com.fasterxml.jackson.annotation.JsonIgnoreType; +import org.apache.sling.api.resource.ResourceResolver; /** * This mixin disables the serialization of the ResourceResolver. * */ - @JsonIgnoreType public abstract interface IgnoringResourceResolverMixin extends ResourceResolver { - /** * TODO: find a way how we can both ignore this type and write a warning when trying to serialize a ResourceResolver. - */ - + */ } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporter.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporter.java index c5406c1..2b36db4 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporter.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporter.java @@ -24,6 +24,13 @@ import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.SerializableString; +import com.fasterxml.jackson.core.io.CharacterEscapes; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import org.apache.sling.models.export.spi.ModelExporter; import org.apache.sling.models.factory.ExportException; import org.apache.sling.models.jacksonexporter.ModuleProvider; @@ -39,14 +46,6 @@ import org.osgi.service.metatype.annotations.ObjectClassDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.SerializableString; -import com.fasterxml.jackson.core.io.CharacterEscapes; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; - @Component(service = ModelExporter.class) @Designate(ocd = JacksonExporter.Config.class) public class JacksonExporter implements ModelExporter { @@ -61,20 +60,24 @@ public class JacksonExporter implements ModelExporter { private static final int MAPPER_FEATURE_PREFIX_LENGTH = MAPPER_FEATURE_PREFIX.length(); - @ObjectClassDefinition(name = "Apache Sling Models Jackson Exporter", + @ObjectClassDefinition( + name = "Apache Sling Models Jackson Exporter", description = "Configures the Jackson JSON Exporter for Sling Models.") static @interface Config { - @AttributeDefinition(name ="Mapping options", - description = "Mapping options to override default Jackson settings. " - + "Mapping options that are passed via request parameters have higher precedence. " - + "Example: SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS=true, MapperFeature.SORT_PROPERTIES_ALPHABETICALLY=true") + @AttributeDefinition( + name = "Mapping options", + description = + "Mapping options to override default Jackson settings. " + + "Mapping options that are passed via request parameters have higher precedence. " + + "Example: SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS=true, MapperFeature.SORT_PROPERTIES_ALPHABETICALLY=true") String[] mapping_options(); - } - @Reference(service = ModuleProvider.class, - cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) + @Reference( + service = ModuleProvider.class, + cardinality = ReferenceCardinality.MULTIPLE, + policy = ReferencePolicy.DYNAMIC) private volatile Collection<ModuleProvider> moduleProviders; private Map<String, String> mappingOptions; @@ -85,11 +88,11 @@ public class JacksonExporter implements ModelExporter { } @Override - @SuppressWarnings({ "null", "unchecked" }) + @SuppressWarnings({"null", "unchecked"}) public <T> T export(@NotNull Object model, @NotNull Class<T> clazz, @NotNull Map<String, String> options) throws ExportException { ObjectMapper mapper = new ObjectMapper(); - Map<String,String> mergedOptions = mergeWithConfiguredOptions(options); + Map<String, String> mergedOptions = mergeWithConfiguredOptions(options); for (Map.Entry<String, String> optionEntry : mergedOptions.entrySet()) { String key = optionEntry.getKey(); if (key.startsWith(SERIALIZATION_FEATURE_PREFIX)) { @@ -143,8 +146,8 @@ public class JacksonExporter implements ModelExporter { } } - private Map<String,String> mergeWithConfiguredOptions(@NotNull Map<String, String> options) { - Map<String,String> mergedMap = new LinkedHashMap<>(); + private Map<String, String> mergeWithConfiguredOptions(@NotNull Map<String, String> options) { + Map<String, String> mergedMap = new LinkedHashMap<>(); mergedMap.putAll(mappingOptions); mergedMap.putAll(options); return mergedMap; @@ -182,5 +185,4 @@ public class JacksonExporter implements ModelExporter { return null; } } - } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java index bec1b8e..7b713a9 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java @@ -1,28 +1,29 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; -import org.apache.sling.models.annotations.Model; - import com.fasterxml.jackson.databind.BeanDescription; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializationConfig; import com.fasterxml.jackson.databind.module.SimpleSerializers; +import org.apache.sling.models.annotations.Model; public class ModelSkippingSerializers extends SimpleSerializers { private static final long serialVersionUID = -638837465287955097L; diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/PropertiesUtil.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/PropertiesUtil.java index 42d17c6..f206ec2 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/PropertiesUtil.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/PropertiesUtil.java @@ -46,7 +46,7 @@ final class PropertiesUtil { * @return Map value */ static Map<String, String> toMap(String @NotNull [] stringArray) { - //in property values + // in property values Map<String, String> result = new LinkedHashMap<>(); for (String kv : stringArray) { if (kv == null) { @@ -63,5 +63,4 @@ final class PropertiesUtil { } return result; } - } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/RequestModuleProvider.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/RequestModuleProvider.java index 30c0856..53ee732 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/RequestModuleProvider.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/RequestModuleProvider.java @@ -1,32 +1,33 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; -import java.util.Enumeration; - import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; -import org.apache.sling.api.SlingHttpServletRequest; -import org.apache.sling.models.jacksonexporter.ModuleProvider; -import org.osgi.service.component.annotations.Component; +import java.util.Enumeration; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.module.SimpleModule; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.jacksonexporter.ModuleProvider; +import org.osgi.service.component.annotations.Component; @Component(service = ModuleProvider.class) public class RequestModuleProvider implements ModuleProvider { @@ -45,5 +46,4 @@ public class RequestModuleProvider implements ModuleProvider { public Module getModule() { return moduleInstance; } - } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java index 7418c2a..6f1dd9f 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java @@ -1,21 +1,25 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.module.SimpleModule; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.jacksonexporter.ModuleProvider; import org.osgi.service.component.annotations.Activate; @@ -24,23 +28,23 @@ import org.osgi.service.metatype.annotations.AttributeDefinition; import org.osgi.service.metatype.annotations.Designate; import org.osgi.service.metatype.annotations.ObjectClassDefinition; -import com.fasterxml.jackson.databind.Module; -import com.fasterxml.jackson.databind.module.SimpleModule; - @Component(service = ModuleProvider.class) @Designate(ocd = ResourceModuleProvider.Config.class) public class ResourceModuleProvider implements ModuleProvider { private static final int DEFAULT_MAX_RECURSION_LEVELS = -1; - @ObjectClassDefinition(name = "Apache Sling Models Jackson Exporter - Resource object support", - description = "Provider of a Jackson Module which enables support for proper serialization of Resource objects") + @ObjectClassDefinition( + name = "Apache Sling Models Jackson Exporter - Resource object support", + description = + "Provider of a Jackson Module which enables support for proper serialization of Resource objects") static @interface Config { - @AttributeDefinition(name ="Maximum Recursion Levels", - description = "Maximum number of levels of child resources which will be exported for each resource. Specify -1 for infinite.") + @AttributeDefinition( + name = "Maximum Recursion Levels", + description = + "Maximum number of levels of child resources which will be exported for each resource. Specify -1 for infinite.") int max_recursion_levels() default DEFAULT_MAX_RECURSION_LEVELS; - } private SimpleModule moduleInstance; @@ -58,5 +62,4 @@ public class ResourceModuleProvider implements ModuleProvider { public Module getModule() { return moduleInstance; } - } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceSerializer.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceSerializer.java index 07559f6..53c5cfc 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceSerializer.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceSerializer.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; @@ -23,14 +25,13 @@ import java.util.Calendar; import java.util.Iterator; import java.util.Map; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ValueMap; - import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.ResolvableSerializer; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ValueMap; public class ResourceSerializer extends JsonSerializer<Resource> implements ResolvableSerializer { @@ -42,14 +43,19 @@ public class ResourceSerializer extends JsonSerializer<Resource> implements Reso } @Override - public void serialize(final Resource value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException { + public void serialize(final Resource value, final JsonGenerator jgen, final SerializerProvider provider) + throws IOException { create(value, jgen, 0, provider); } /** Dump given resource in JSON, optionally recursing into its objects */ - @SuppressWarnings({ "unused", "null" }) - private void create(final Resource resource, final JsonGenerator jgen, final int currentRecursionLevel, - final SerializerProvider provider) throws IOException { + @SuppressWarnings({"unused", "null"}) + private void create( + final Resource resource, + final JsonGenerator jgen, + final int currentRecursionLevel, + final SerializerProvider provider) + throws IOException { jgen.writeStartObject(); final ValueMap valueMap = resource.adaptTo(ValueMap.class); @@ -76,7 +82,6 @@ public class ResourceSerializer extends JsonSerializer<Resource> implements Reso } jgen.writeEndArray(); } - } } else { @@ -109,37 +114,40 @@ public class ResourceSerializer extends JsonSerializer<Resource> implements Reso * Write a single property */ @SuppressWarnings("null") - private void createProperty(final JsonGenerator jgen, final ValueMap valueMap, final String key, final Object value, - final SerializerProvider provider) + private void createProperty( + final JsonGenerator jgen, + final ValueMap valueMap, + final String key, + final Object value, + final SerializerProvider provider) throws IOException { Object[] values = null; if (value.getClass().isArray()) { final int length = Array.getLength(value); // write out empty array - if ( length == 0 ) { + if (length == 0) { jgen.writeArrayFieldStart(key); jgen.writeEndArray(); return; } values = new Object[Array.getLength(value)]; - for(int i=0; i<length; i++) { + for (int i = 0; i < length; i++) { values[i] = Array.get(value, i); } } // special handling for binaries: we dump the length and not the data! - if (value instanceof InputStream - || (values != null && values[0] instanceof InputStream)) { + if (value instanceof InputStream || (values != null && values[0] instanceof InputStream)) { // TODO for now we mark binary properties with an initial colon in // their name // (colon is not allowed as a JCR property name) // in the name, and the value should be the size of the binary data if (values == null) { - jgen.writeNumberField(":" + key, getLength(valueMap, -1, key, (InputStream)value)); + jgen.writeNumberField(":" + key, getLength(valueMap, -1, key, (InputStream) value)); } else { jgen.writeArrayFieldStart(":" + key); for (int i = 0; i < values.length; i++) { - jgen.writeNumber(getLength(valueMap, i, key, (InputStream)values[i])); + jgen.writeNumber(getLength(valueMap, i, key, (InputStream) values[i])); } jgen.writeEndArray(); } @@ -166,15 +174,16 @@ public class ResourceSerializer extends JsonSerializer<Resource> implements Reso private long getLength(final ValueMap valueMap, final int index, final String key, final InputStream stream) { try { stream.close(); - } catch (IOException ignore) {} + } catch (IOException ignore) { + } long length = -1; - if ( valueMap != null ) { - if ( index == -1 ) { + if (valueMap != null) { + if (index == -1) { length = valueMap.get(key, length); } else { Long[] lengths = valueMap.get(key, Long[].class); - if ( lengths != null && lengths.length > index ) { + if (lengths != null && lengths.length > index) { length = lengths[index]; } } @@ -183,20 +192,21 @@ public class ResourceSerializer extends JsonSerializer<Resource> implements Reso } /** Dump only a value in the correct format */ - private void writeValue(final JsonGenerator jgen, final Object value, final SerializerProvider provider) throws IOException { + private void writeValue(final JsonGenerator jgen, final Object value, final SerializerProvider provider) + throws IOException { if (value instanceof InputStream) { // input stream is already handled jgen.writeNumber(0); } else if (value instanceof Calendar) { calendarSerializer.serialize(value, jgen, provider); } else if (value instanceof Boolean) { - jgen.writeBoolean(((Boolean)value).booleanValue()); + jgen.writeBoolean(((Boolean) value).booleanValue()); } else if (value instanceof Long) { - jgen.writeNumber(((Long)value).longValue()); + jgen.writeNumber(((Long) value).longValue()); } else if (value instanceof Integer) { - jgen.writeNumber(((Integer)value).intValue()); + jgen.writeNumber(((Integer) value).intValue()); } else if (value instanceof Double) { - jgen.writeNumber(((Double)value).doubleValue()); + jgen.writeNumber(((Double) value).doubleValue()); } else if (value != null) { jgen.writeString(value.toString()); } else { diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ServletRequestMixin.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ServletRequestMixin.java index ac839ec..c4ba2b6 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ServletRequestMixin.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ServletRequestMixin.java @@ -1,27 +1,29 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; +import javax.servlet.ServletRequest; + import java.util.Enumeration; import java.util.Locale; import java.util.Map; -import javax.servlet.ServletRequest; - import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonGetter; @@ -29,12 +31,15 @@ import com.fasterxml.jackson.annotation.JsonGetter; * This "mixin" interface instructs the Jackson ObjectMapper what properties should be included in JSON view of a ServletRequest object. * Without it, the auto-detection may lead to unexpected results, e.g. StackOverflow errors. */ -@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE) +@JsonAutoDetect( + getterVisibility = JsonAutoDetect.Visibility.NONE, + fieldVisibility = JsonAutoDetect.Visibility.NONE, + isGetterVisibility = JsonAutoDetect.Visibility.NONE) public interface ServletRequestMixin extends ServletRequest { @JsonGetter("parameters") @Override - public Map<String,String[]> getParameterMap(); + public Map<String, String[]> getParameterMap(); @JsonGetter @Override diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/SlingHttpServletRequestMixin.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/SlingHttpServletRequestMixin.java index b21b0f7..e186846 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/SlingHttpServletRequestMixin.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/SlingHttpServletRequestMixin.java @@ -1,34 +1,38 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; import java.util.Enumeration; -import org.apache.sling.api.request.RequestPathInfo; -import org.apache.sling.api.resource.Resource; - import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonGetter; +import org.apache.sling.api.request.RequestPathInfo; +import org.apache.sling.api.resource.Resource; /** * This "mixin" interface instructs the Jackson ObjectMapper what properties should be included in JSON view of a SlingHttpServletRequest object. * Without it, the auto-detection may lead to unexpected results, e.g. StackOverflow errors. */ -@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE) +@JsonAutoDetect( + getterVisibility = JsonAutoDetect.Visibility.NONE, + fieldVisibility = JsonAutoDetect.Visibility.NONE, + isGetterVisibility = JsonAutoDetect.Visibility.NONE) public interface SlingHttpServletRequestMixin extends HttpServletRequestMixin { @JsonGetter @@ -42,5 +46,4 @@ public interface SlingHttpServletRequestMixin extends HttpServletRequestMixin { @JsonGetter() public Enumeration<String> getResponseContentTypes(); - } diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/WarningResourceResolverMixin.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/WarningResourceResolverMixin.java index 51b4ac0..b9ad87f 100644 --- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/WarningResourceResolverMixin.java +++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/WarningResourceResolverMixin.java @@ -20,53 +20,40 @@ package org.apache.sling.models.jacksonexporter.impl; import java.io.IOException; -import org.apache.sling.api.resource.ResourceResolver; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.annotation.JsonSerialize; - +import org.apache.sling.api.resource.ResourceResolver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This mixin exports data which Jackson would export by default (so not change in the default behaviour), * but prints a warning whenever it does that. */ - - @JsonAutoDetect public interface WarningResourceResolverMixin extends ResourceResolver { - + public static final String MESSAGE = "A ResourceResolver is serialized with all its private fields containing " + "implementation details you should not disclose. Please review your Sling Model implementation(s) and remove " + "all public accessors to a ResourceResolver."; - + public static final Logger LOG = LoggerFactory.getLogger(JacksonExporter.class); - - // This method is explicitly mentioned so we provide a custom serializer which prints the warning - + @Override - @JsonSerialize(using=WarningBooleanSerializer.class) + @JsonSerialize(using = WarningBooleanSerializer.class) boolean isLive(); - - - + public class WarningBooleanSerializer extends JsonSerializer<Boolean> { @Override - public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider) - throws IOException { + public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider) throws IOException { LOG.warn(MESSAGE); jgen.writeObject(value); - } - } - - } diff --git a/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterLimitSerializationTest.java b/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterLimitSerializationTest.java index 3e9b944..4325ee9 100644 --- a/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterLimitSerializationTest.java +++ b/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterLimitSerializationTest.java @@ -18,16 +18,16 @@ */ package org.apache.sling.models.jacksonexporter.impl; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import javax.servlet.http.HttpServletRequest; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import javax.servlet.http.HttpServletRequest; - +import ch.qos.logback.classic.Level; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.Resource; @@ -40,77 +40,76 @@ import org.apache.sling.testing.mock.osgi.junit5.OsgiContextExtension; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import ch.qos.logback.classic.Level; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; @ExtendWith(OsgiContextExtension.class) class JacksonExporterLimitSerializationTest { private OsgiContext context = new OsgiContext(); - + @Test void testWarnLogWhenSerializingResourceResolver() throws Exception { - - LogCapture capture = new LogCapture(JacksonExporter.class.getName(),false); - + + LogCapture capture = new LogCapture(JacksonExporter.class.getName(), false); + PojoWithResourceResolver pojo = new PojoWithResourceResolver("text", new EmptyResourceResolver()); - + context.registerInjectActivateService(new ConfigurableSerializationModuleProvider()); JacksonExporter underTest = context.registerInjectActivateService(JacksonExporter.class); - Map<String,String> options = Collections.emptyMap(); + Map<String, String> options = Collections.emptyMap(); String json = underTest.export(pojo, String.class, options); ObjectMapper mapper = new ObjectMapper(); JsonNode result = mapper.readTree(json); assertTrue(result.path("resolver").isContainerNode()); assertTrue(capture.anyMatch(event -> { - return event.getFormattedMessage().equals(WarningResourceResolverMixin.MESSAGE) && - event.getLevel().equals(Level.WARN); + return event.getFormattedMessage().equals(WarningResourceResolverMixin.MESSAGE) + && event.getLevel().equals(Level.WARN); })); } - + @Test void testNotSerializingResourceResolverWhenDisabled() throws Exception { - - LogCapture capture = new LogCapture(ConfigurableSerializationModuleProvider.class.getName(),false); - - PojoWithResourceResolver pojo = new PojoWithResourceResolver("text",new EmptyResourceResolver()); - Map<String,Object> config = Collections.singletonMap("disable.serialization", ResourceResolver.class.getName()); - context.registerInjectActivateService(new ConfigurableSerializationModuleProvider(),config); - + + LogCapture capture = new LogCapture(ConfigurableSerializationModuleProvider.class.getName(), false); + + PojoWithResourceResolver pojo = new PojoWithResourceResolver("text", new EmptyResourceResolver()); + Map<String, Object> config = + Collections.singletonMap("disable.serialization", ResourceResolver.class.getName()); + context.registerInjectActivateService(new ConfigurableSerializationModuleProvider(), config); + JacksonExporter underTest = context.registerInjectActivateService(JacksonExporter.class); - Map<String,String> options = Collections.emptyMap(); + Map<String, String> options = Collections.emptyMap(); String expectedJson = "{\"msg\":\"text\"}"; assertEquals(expectedJson, underTest.export(pojo, String.class, options)); // no log is written in this case - assertEquals(0,capture.list.size()); + assertEquals(0, capture.list.size()); } - - + @Test void test_givenInvalidTypes_whenActivate_thenWarnLogStatements() throws ExportException { - LogCapture capture = new LogCapture(ConfigurableSerializationModuleProvider.class.getName(),false); - - Map<String,Object> config = new HashMap<>(); + LogCapture capture = new LogCapture(ConfigurableSerializationModuleProvider.class.getName(), false); + + Map<String, Object> config = new HashMap<>(); config.put("disable.serialization", "foo.bar.disable"); config.put("enable.warn.logging", "foo.bar.logging"); - - context.registerInjectActivateService(new ConfigurableSerializationModuleProvider(),config); + + context.registerInjectActivateService(new ConfigurableSerializationModuleProvider(), config); assertTrue(capture.anyMatch(event -> { - return event.getFormattedMessage().equals("Support to disable the serialization of type foo.bar.disable is not implemented") && - event.getLevel().equals(Level.WARN); + return event.getFormattedMessage() + .equals("Support to disable the serialization of type foo.bar.disable is not implemented") + && event.getLevel().equals(Level.WARN); })); assertTrue(capture.anyMatch(event -> { - return event.getFormattedMessage().equals("Support to log any serialization of type foo.bar.logging is not implemented") && - event.getLevel().equals(Level.WARN); - })); + return event.getFormattedMessage() + .equals("Support to log any serialization of type foo.bar.logging is not implemented") + && event.getLevel().equals(Level.WARN); + })); } - - + /** * A very simple ResourceResolver implementation which does not lead to any issues with any mocking framework * when trying to export it with Jackson. @@ -182,7 +181,6 @@ class JacksonExporterLimitSerializationTest { return null; } - @Override public ResourceResolver clone(Map<String, Object> authenticationInfo) throws LoginException { return null; diff --git a/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterMappingOptionsTest.java b/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterMappingOptionsTest.java index 356e62a..b37a5ea 100644 --- a/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterMappingOptionsTest.java +++ b/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterMappingOptionsTest.java @@ -18,8 +18,6 @@ */ package org.apache.sling.models.jacksonexporter.impl; -import static org.junit.jupiter.api.Assertions.assertEquals; - import java.util.Collections; import java.util.Map; @@ -31,6 +29,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import static org.junit.jupiter.api.Assertions.assertEquals; + /** * Tests passing in and configuring mapping options for {@link JacksonExporter}. */ @@ -43,16 +43,13 @@ class JacksonExporterMappingOptionsTest { @BeforeEach void setUp() { - pojoWithData = new ExamplePojo() - .stringProp("value1") - .numberProp(1) - .booleanProp(true); + pojoWithData = new ExamplePojo().stringProp("value1").numberProp(1).booleanProp(true); } @Test void testDefaultConfig() throws ExportException { JacksonExporter underTest = context.registerInjectActivateService(JacksonExporter.class); - Map<String,String> options = Collections.emptyMap(); + Map<String, String> options = Collections.emptyMap(); String expectedJson = "{\"stringProp\":\"value1\",\"numberProp\":1,\"booleanProp\":true}"; assertEquals(expectedJson, underTest.export(pojoWithData, String.class, options)); @@ -61,7 +58,7 @@ class JacksonExporterMappingOptionsTest { @Test void testPassedInOptions() throws ExportException { JacksonExporter underTest = context.registerInjectActivateService(JacksonExporter.class); - Map<String,String> options = Collections.singletonMap("MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", "true"); + Map<String, String> options = Collections.singletonMap("MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", "true"); String expectedJson = "{\"booleanProp\":true,\"numberProp\":1,\"stringProp\":\"value1\"}"; assertEquals(expectedJson, underTest.export(pojoWithData, String.class, options)); @@ -69,9 +66,11 @@ class JacksonExporterMappingOptionsTest { @Test void testConfiguredOptions() throws ExportException { - JacksonExporter underTest = context.registerInjectActivateService(JacksonExporter.class, - "mapping.options", new String[] { "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY=true" }); - Map<String,String> options = Collections.emptyMap(); + JacksonExporter underTest = + context.registerInjectActivateService(JacksonExporter.class, "mapping.options", new String[] { + "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY=true" + }); + Map<String, String> options = Collections.emptyMap(); String expectedJson = "{\"booleanProp\":true,\"numberProp\":1,\"stringProp\":\"value1\"}"; assertEquals(expectedJson, underTest.export(pojoWithData, String.class, options)); @@ -79,12 +78,13 @@ class JacksonExporterMappingOptionsTest { @Test void testPassedInOptionsOverlayConfiguredOptions() throws ExportException { - JacksonExporter underTest = context.registerInjectActivateService(JacksonExporter.class, - "mapping.options", new String[] { "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY=true" }); - Map<String,String> options = Collections.singletonMap("MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", "false"); + JacksonExporter underTest = + context.registerInjectActivateService(JacksonExporter.class, "mapping.options", new String[] { + "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY=true" + }); + Map<String, String> options = Collections.singletonMap("MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", "false"); String expectedJson = "{\"stringProp\":\"value1\",\"numberProp\":1,\"booleanProp\":true}"; assertEquals(expectedJson, underTest.export(pojoWithData, String.class, options)); } - } diff --git a/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java b/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java index 35675ad..081417e 100644 --- a/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java +++ b/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java @@ -1,26 +1,27 @@ /* - * 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 + * 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 + * 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. + * 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 org.apache.sling.models.jacksonexporter.impl; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.util.Iterator; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.type.TypeFactory; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.AbstractResource; import org.apache.sling.api.resource.Resource; @@ -30,8 +31,8 @@ import org.apache.sling.models.annotations.Model; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.type.TypeFactory; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; class ModelSkippingSerializersTest { diff --git a/src/test/java/org/apache/sling/models/jacksonexporter/impl/PropertiesUtilTest.java b/src/test/java/org/apache/sling/models/jacksonexporter/impl/PropertiesUtilTest.java index da069ed..654a87d 100644 --- a/src/test/java/org/apache/sling/models/jacksonexporter/impl/PropertiesUtilTest.java +++ b/src/test/java/org/apache/sling/models/jacksonexporter/impl/PropertiesUtilTest.java @@ -18,15 +18,15 @@ */ package org.apache.sling.models.jacksonexporter.impl; -import static org.apache.sling.models.jacksonexporter.impl.PropertiesUtil.toMap; -import static org.junit.jupiter.api.Assertions.assertEquals; - import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import org.junit.jupiter.api.Test; +import static org.apache.sling.models.jacksonexporter.impl.PropertiesUtil.toMap; +import static org.junit.jupiter.api.Assertions.assertEquals; + class PropertiesUtilTest { @Test @@ -37,21 +37,14 @@ class PropertiesUtilTest { @Test void testToMap_VariousEntries() { String[] input = new String[] { - "prop1=value1", - " prop.2 = value.2 ", - "prop3=", - "prop4", - "", - null, - "prop5=value5a=value5b" + "prop1=value1", " prop.2 = value.2 ", "prop3=", "prop4", "", null, "prop5=value5a=value5b" }; - Map<String,String> expectedOutput = new LinkedHashMap<>(); + Map<String, String> expectedOutput = new LinkedHashMap<>(); expectedOutput.put("prop1", "value1"); expectedOutput.put("prop.2", "value.2"); expectedOutput.put("prop5", "value5a=value5b"); assertEquals(expectedOutput, toMap(input)); } - } diff --git a/src/test/java/org/apache/sling/models/jacksonexporter/impl/example/ExamplePojo.java b/src/test/java/org/apache/sling/models/jacksonexporter/impl/example/ExamplePojo.java index cf4e325..ba59779 100644 --- a/src/test/java/org/apache/sling/models/jacksonexporter/impl/example/ExamplePojo.java +++ b/src/test/java/org/apache/sling/models/jacksonexporter/impl/example/ExamplePojo.java @@ -50,5 +50,4 @@ public class ExamplePojo { this.booleanProp = value; return this; } - } diff --git a/src/test/java/org/apache/sling/models/jacksonexporter/impl/example/PojoWithResourceResolver.java b/src/test/java/org/apache/sling/models/jacksonexporter/impl/example/PojoWithResourceResolver.java index 3e544b7..186794f 100644 --- a/src/test/java/org/apache/sling/models/jacksonexporter/impl/example/PojoWithResourceResolver.java +++ b/src/test/java/org/apache/sling/models/jacksonexporter/impl/example/PojoWithResourceResolver.java @@ -21,16 +21,12 @@ package org.apache.sling.models.jacksonexporter.impl.example; import org.apache.sling.api.resource.ResourceResolver; public class PojoWithResourceResolver { - - + public String msg; public ResourceResolver resolver; - - public PojoWithResourceResolver (String msg, ResourceResolver resolver) { + + public PojoWithResourceResolver(String msg, ResourceResolver resolver) { this.msg = msg; this.resolver = resolver; } - - - } diff --git a/src/test/java/org/apache/sling/models/jacksonexporter/impl/util/LogCapture.java b/src/test/java/org/apache/sling/models/jacksonexporter/impl/util/LogCapture.java index be328b0..d252fe9 100644 --- a/src/test/java/org/apache/sling/models/jacksonexporter/impl/util/LogCapture.java +++ b/src/test/java/org/apache/sling/models/jacksonexporter/impl/util/LogCapture.java @@ -18,21 +18,20 @@ */ package org.apache.sling.models.jacksonexporter.impl.util; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.io.Closeable; import java.io.IOException; import java.util.function.Predicate; -import org.slf4j.LoggerFactory; - import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; +import org.slf4j.LoggerFactory; + +import static org.junit.jupiter.api.Assertions.assertTrue; -public class LogCapture extends ListAppender<ILoggingEvent> implements Closeable { +public class LogCapture extends ListAppender<ILoggingEvent> implements Closeable { private final boolean verboseFailure; /** Setup the capture and start it */ @@ -54,16 +53,9 @@ public class LogCapture extends ListAppender<ILoggingEvent> implements Closeabl return event.getFormattedMessage().equals(message); })); } - - - - @Override public void close() throws IOException { stop(); } - - - }