This is an automated email from the ASF dual-hosted git repository. jsedding pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-xss.git
commit ab7618bcff4b086455ba073cfa91b94dd905ae54 Author: Julian Sedding <[email protected]> AuthorDate: Wed Nov 4 14:40:21 2020 +0100 SLING-9874 - Allow adapting SlingHttpServletRequest and ResourceResolver to XSSAPI --- .../sling/xss/impl/XSSAPIAdapterFactory.java | 56 +++++++++++++++++ .../sling/xss/impl/XSSAPIAdapterFactoryTest.java | 72 ++++++++++++++++++++++ 2 files changed, 128 insertions(+) diff --git a/src/main/java/org/apache/sling/xss/impl/XSSAPIAdapterFactory.java b/src/main/java/org/apache/sling/xss/impl/XSSAPIAdapterFactory.java new file mode 100644 index 0000000..8bf4735 --- /dev/null +++ b/src/main/java/org/apache/sling/xss/impl/XSSAPIAdapterFactory.java @@ -0,0 +1,56 @@ +/* + * 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 + * + * 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.xss.impl; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.adapter.AdapterFactory; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.xss.XSSAPI; +import org.jetbrains.annotations.NotNull; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferencePolicyOption; + +@Component( + service = AdapterFactory.class, + property = { + AdapterFactory.ADAPTER_CLASSES + "=org.apache.sling.xss.XSSAPI", + AdapterFactory.ADAPTABLE_CLASSES + "=org.apache.sling.api.resource.ResourceResolver", + AdapterFactory.ADAPTABLE_CLASSES + "=org.apache.sling.api.SlingHttpServletRequest" + } +) +public class XSSAPIAdapterFactory implements AdapterFactory { + + @Reference(policyOption = ReferencePolicyOption.GREEDY) + private XSSAPI xssapi; + + // constructor for testing, could use constructor injection with OSGi R7 + XSSAPIAdapterFactory(XSSAPI xssapi) { + this.xssapi = xssapi; + } + + @Override + public <AdapterType> AdapterType getAdapter(@NotNull Object adaptable, @NotNull Class<AdapterType> type) { + if (type == XSSAPI.class + && (adaptable instanceof ResourceResolver || adaptable instanceof SlingHttpServletRequest)) { + return type.cast(xssapi); + } + return null; + } +} diff --git a/src/test/java/org/apache/sling/xss/impl/XSSAPIAdapterFactoryTest.java b/src/test/java/org/apache/sling/xss/impl/XSSAPIAdapterFactoryTest.java new file mode 100644 index 0000000..df1e4af --- /dev/null +++ b/src/test/java/org/apache/sling/xss/impl/XSSAPIAdapterFactoryTest.java @@ -0,0 +1,72 @@ +/* + * 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 + * + * 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.xss.impl; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.xss.XSSAPI; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; + +public class XSSAPIAdapterFactoryTest { + + private XSSAPIAdapterFactory factory; + + @Before + public void setup() { + final XSSAPI xssapi = mock(XSSAPI.class); + factory = new XSSAPIAdapterFactory(xssapi); + } + + @Test + public void testAdaptFromResourceResolver() { + final ResourceResolver resolver = mock(ResourceResolver.class); + + assertThat("should adapt ResourceResolver to XSSAPI", + factory.getAdapter(resolver, XSSAPI.class), isA(XSSAPI.class)); + } + + @Test + public void testAdaptFromRequest() { + final SlingHttpServletRequest request = mock(SlingHttpServletRequest.class); + + assertThat("should adapt SlingHttpServletRequest to XSSAPI", + factory.getAdapter(request, XSSAPI.class), isA(XSSAPI.class)); + } + + @Test + public void testNoAdaptionFromArbitraryObject() { + assertThat("should not adapt Object to XSSAPI", + factory.getAdapter(new Object(), XSSAPI.class), nullValue()); + } + + @Test + public void testNoAdaptionFromRequestToResource() { + final SlingHttpServletRequest request = mock(SlingHttpServletRequest.class); + + assertThat("should not adapt SlingHttpServletRequest to Resource", + factory.getAdapter(request, Resource.class), nullValue()); + } +} \ No newline at end of file
