You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by js...@apache.org on 2020/11/04 13:49:26 UTC
[sling-org-apache-sling-xss] 01/01: SLING-9874 - Allow adapting
SlingHttpServletRequest and ResourceResolver to XSSAPI
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 1d7559ef58dc1093c25f0b8ef360ab677b9c0d92
Author: Julian Sedding <js...@apache.org>
AuthorDate: Wed Nov 4 14:40:21 2020 +0100
SLING-9874 - Allow adapting SlingHttpServletRequest and ResourceResolver to XSSAPI
---
.../sling/xss/impl/XSSAPIAdapterFactory.java | 60 ++++++++++++++++++
.../sling/xss/impl/XSSAPIAdapterFactoryTest.java | 72 ++++++++++++++++++++++
2 files changed, 132 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..f996c2d
--- /dev/null
+++ b/src/main/java/org/apache/sling/xss/impl/XSSAPIAdapterFactory.java
@@ -0,0 +1,60 @@
+/*
+ * 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;
+
+ public XSSAPIAdapterFactory() {
+ // default constructor for SCR
+ }
+
+ // 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