This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git
The following commit(s) were added to refs/heads/master by this push:
new 3878cd8 SLING-10871 - Add builder API for request/resource objects -
use SlingUriBuilder
3878cd8 is described below
commit 3878cd8cf74e79be2ce366b3bcf4a693e1195753
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Wed Oct 27 15:55:06 2021 +0200
SLING-10871 - Add builder API for request/resource objects - use
SlingUriBuilder
---
.../request/builder/impl/RequestPathInfoImpl.java | 80 ----------------
.../builder/impl/SlingHttpServletRequestImpl.java | 12 ++-
.../builder/impl/RequestPathInfoImplTest.java | 103 ---------------------
3 files changed, 10 insertions(+), 185 deletions(-)
diff --git
a/src/main/java/org/apache/sling/api/request/builder/impl/RequestPathInfoImpl.java
b/src/main/java/org/apache/sling/api/request/builder/impl/RequestPathInfoImpl.java
deleted file mode 100644
index 1181696..0000000
---
a/src/main/java/org/apache/sling/api/request/builder/impl/RequestPathInfoImpl.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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.api.request.builder.impl;
-
-import org.apache.sling.api.request.RequestPathInfo;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-
-/**
- * {@link RequestPathInfo} implementation.
- */
-public class RequestPathInfoImpl implements RequestPathInfo {
-
- private final String extension;
- private final String resourcePath;
- private final String[] selectors;
- private final String suffix;
-
- private final ResourceResolver resourceResolver;
-
- public RequestPathInfoImpl(final Resource resource,
- final String[] selectors,
- final String extension,
- final String suffix) {
- this.resourceResolver = resource.getResourceResolver();
- this.resourcePath = resource.getPath();
- this.selectors = selectors == null ? new String[0] : selectors;
- this.extension = extension;
- this.suffix = suffix;
- }
-
- @Override
- public String getExtension() {
- return this.extension;
- }
-
- @Override
- public String getResourcePath() {
- return this.resourcePath;
- }
-
- @Override
- public String[] getSelectors() {
- return this.selectors;
- }
-
- @Override
- public String getSelectorString() {
- return this.selectors.length == 0 ? null : String.join(".",
this.selectors);
- }
-
- @Override
- public String getSuffix() {
- return this.suffix;
- }
-
- @Override
- public Resource getSuffixResource() {
- if (suffix == null) {
- return null;
- }
- return this.resourceResolver.getResource(suffix);
- }
-}
diff --git
a/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
b/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
index 66d1562..598738b 100644
---
a/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
+++
b/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
@@ -66,6 +66,7 @@ import org.apache.sling.api.request.RequestProgressTracker;
import org.apache.sling.api.request.builder.SlingHttpServletRequestBuilder;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.uri.SlingUriBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -121,7 +122,7 @@ public class SlingHttpServletRequestImpl extends
SlingAdaptable
private final Map<String, String[]> parameters = new LinkedHashMap<>();
/** Request path info */
- private RequestPathInfoImpl requestPathInfo;
+ private RequestPathInfo requestPathInfo;
/** Optional query string */
private String queryString;
@@ -306,7 +307,14 @@ public class SlingHttpServletRequestImpl extends
SlingAdaptable
this.checkLocked();
this.locked = true;
- this.requestPathInfo = new RequestPathInfoImpl(this.resource,
this.selectors, this.extension, this.suffix);
+ final SlingUriBuilder builder =
SlingUriBuilder.createFrom(this.resource)
+ .setExtension(this.extension)
+ .setSuffix(this.suffix);
+ if ( this.selectors != null ) {
+ builder.setSelectors(this.selectors);
+ }
+ this.requestPathInfo = builder.build();
+
this.queryString = this.formatQueryString();
this.pathInfo = this.buildPathInfo();
diff --git
a/src/test/java/org/apache/sling/api/request/builder/impl/RequestPathInfoImplTest.java
b/src/test/java/org/apache/sling/api/request/builder/impl/RequestPathInfoImplTest.java
deleted file mode 100644
index 3450f0c..0000000
---
a/src/test/java/org/apache/sling/api/request/builder/impl/RequestPathInfoImplTest.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * 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.api.request.builder.impl;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-
-import org.apache.sling.api.request.RequestPathInfo;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.junit.Test;
-import org.mockito.Mockito;
-
-public class RequestPathInfoImplTest {
-
- @Test
- public void testExtension() {
- final ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
- final Resource resource = Mockito.mock(Resource.class);
- Mockito.when(resource.getPath()).thenReturn("/content/page");
- Mockito.when(resource.getResourceResolver()).thenReturn(resolver);
-
- RequestPathInfo requestPathInfo = new RequestPathInfoImpl(resource,
null, null, null);
- assertNull(requestPathInfo.getExtension());
- requestPathInfo = new RequestPathInfoImpl(resource, null, "ext", null);
- assertEquals("ext", requestPathInfo.getExtension());
- }
-
- @Test
- public void testResourcePath() {
- final ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
- final Resource resource = Mockito.mock(Resource.class);
- Mockito.when(resource.getPath()).thenReturn("/content/page");
- Mockito.when(resource.getResourceResolver()).thenReturn(resolver);
-
- RequestPathInfo requestPathInfo = new RequestPathInfoImpl(resource,
null, null, null);
- assertEquals("/content/page", requestPathInfo.getResourcePath());
- }
-
- @Test
- public void testSelector() {
- final ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
- final Resource resource = Mockito.mock(Resource.class);
- Mockito.when(resource.getPath()).thenReturn("/content/page");
- Mockito.when(resource.getResourceResolver()).thenReturn(resolver);
-
- RequestPathInfo requestPathInfo = new RequestPathInfoImpl(resource,
null, null, null);
- assertNull(requestPathInfo.getSelectorString());
- assertEquals(0, requestPathInfo.getSelectors().length);
-
- requestPathInfo = new RequestPathInfoImpl(resource, new String[]
{"aa", "bb"}, null, null);
- assertEquals("aa.bb", requestPathInfo.getSelectorString());
- assertArrayEquals(new String[] { "aa", "bb" },
requestPathInfo.getSelectors());
- }
-
- @Test
- public void testSuffix() {
- final ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
- final Resource resource = Mockito.mock(Resource.class);
- Mockito.when(resource.getPath()).thenReturn("/content/page");
- Mockito.when(resource.getResourceResolver()).thenReturn(resolver);
-
- RequestPathInfo requestPathInfo = new RequestPathInfoImpl(resource,
null, null, null);
- assertNull(requestPathInfo.getSuffix());
-
- requestPathInfo = new RequestPathInfoImpl(resource, null, null,
"/suffix");
- assertEquals("/suffix", requestPathInfo.getSuffix());
- }
-
- @Test
- public void testGetSuffixResource() {
- final ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
- final Resource resource = Mockito.mock(Resource.class);
- Mockito.when(resource.getPath()).thenReturn("/content/page");
- Mockito.when(resource.getResourceResolver()).thenReturn(resolver);
- final Resource suffixResource = Mockito.mock(Resource.class);
-
Mockito.when(resolver.getResource("/suffix")).thenReturn(suffixResource);
-
- RequestPathInfo requestPathInfo = new RequestPathInfoImpl(resource,
null, null, null);
- assertNull(requestPathInfo.getSuffixResource());
-
- requestPathInfo = new RequestPathInfoImpl(resource, null, null,
"/suffix");
- assertSame(suffixResource, requestPathInfo.getSuffixResource());
- }
-}