Skip to content

Commit f149adc

Browse files
committed
SLING-9745 Sling Uri Mapping SPI
1 parent 36307d3 commit f149adc

5 files changed

Lines changed: 233 additions & 1 deletion

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sling.api.resource.mapping;
20+
21+
import java.util.Map;
22+
23+
import javax.servlet.http.HttpServletRequest;
24+
25+
import org.apache.sling.api.uri.SlingUri;
26+
import org.jetbrains.annotations.NotNull;
27+
import org.jetbrains.annotations.Nullable;
28+
import org.osgi.annotation.versioning.ProviderType;
29+
30+
/**
31+
* Provides a way to resolve and map paths to Sling URIs. Both operations are extensible by
32+
* {@link org.apache.sling.spi.urimapping.SlingUriMapper} OSGi services.
33+
*
34+
* @since 1.1.0 (Sling API Bundle 2.23.0)
35+
*/
36+
@ProviderType
37+
public interface PathToUriMappingService {
38+
39+
/** The result of a map or resolve operation */
40+
@ProviderType
41+
public interface Result {
42+
/**
43+
* The Sling URI as result of the resolve or map operation.
44+
*
45+
* @return the Sling URI
46+
*/
47+
@NotNull
48+
SlingUri getUri();
49+
50+
/**
51+
* Returns all intermediate mappings as produced by {@link org.apache.sling.spi.urimapping.SlingUriMapper} services
52+
*
53+
* @return the intermediate mappings
54+
*/
55+
@NotNull
56+
Map<String, SlingUri> getIntermediateMappings();
57+
}
58+
59+
/**
60+
* Maps a path to a Sling URI.
61+
*
62+
* @param referenceRequest the reference request with the same properties as the actual request that will have to resolve the produced
63+
* URI.
64+
* @param unmappedPath the path that is not mapped yet (may or may not contain selector, extension and suffix)
65+
* @return a @{link PathToUriMappingService.Result}
66+
*/
67+
Result map(@Nullable HttpServletRequest referenceRequest, @NotNull String unmappedPath);
68+
69+
/**
70+
* Resolves a path relative to the given request.
71+
*
72+
* @param request the request
73+
* @param path the path to be resolved or null for which case the information from request is used
74+
* @return a @{link PathToUriMappingService.Result}
75+
*/
76+
Result resolve(@Nullable HttpServletRequest request, @Nullable String path);
77+
}

src/main/java/org/apache/sling/api/resource/mapping/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
@Version("1.0.1")
20+
@Version("1.1.0")
2121
package org.apache.sling.api.resource.mapping;
2222

2323
import org.osgi.annotation.versioning.Version;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sling.spi.urimapping;
20+
21+
import java.util.Map;
22+
23+
import org.apache.sling.api.resource.ResourceResolver;
24+
import org.apache.sling.api.uri.SlingUri;
25+
import org.jetbrains.annotations.NotNull;
26+
import org.osgi.annotation.versioning.ProviderType;
27+
28+
/**
29+
* Provides SlingUriMapper instances with additional context.
30+
*
31+
* @since 1.0.0 (Sling API Bundle 2.23.0)
32+
*/
33+
@ProviderType
34+
public interface MappingChainContext {
35+
36+
/**
37+
* May be called by any SlingUriMapper in the chain to indicate that the rest of the chain should be skipped.
38+
*/
39+
void skipRemainingChain();
40+
41+
/**
42+
* A service resource resolver with read permissions.
43+
*
44+
* @return a resource resolver
45+
*/
46+
@NotNull
47+
ResourceResolver getResourceResolver();
48+
49+
/**
50+
* Allows to share state between SlingUriMapper instances in the chain.
51+
*
52+
* @return a mutable map to share state (never null).
53+
*/
54+
@NotNull
55+
Map<String, Object> getAttributes();
56+
57+
/**
58+
* Provides access to intermediate mappings as already created by SlingUriMapper instances earlier in the chain.
59+
*
60+
* @return the URI mappings
61+
*/
62+
@NotNull
63+
Map<String, SlingUri> getIntermediateMappings();
64+
65+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sling.spi.urimapping;
20+
21+
import javax.servlet.http.HttpServletRequest;
22+
23+
import org.apache.sling.api.uri.SlingUri;
24+
import org.jetbrains.annotations.NotNull;
25+
import org.jetbrains.annotations.Nullable;
26+
import org.osgi.annotation.versioning.ConsumerType;
27+
28+
/**
29+
* <p>
30+
* SPI interface that contributes to the resolving and mapping of Sling URIs. All registered services build a conceptual chain sorted by
31+
* service ranking. The Sling URI is passed through the chain while any SlingUriMapper chain member may or may not make adjustments to the
32+
* Sling URI.
33+
* </p>
34+
* <p>
35+
* The {@link org.apache.sling.api.resource.mapping.PathToUriMappingService} allows to call the resolve() (however normally called by
36+
* request only) and map() methods. resolve() passes through the chain starting at the SlingUriMapper with the <strong>highest</strong>
37+
* service ranking and map() passes through the chain starting at the SlingUriMapper with the <strong>lowest</strong> service ranking.
38+
* </p>
39+
* <p>
40+
* The resource resolver's map() and resolve() methods also use PathToUriMappingService as implementation.
41+
* </p>
42+
*/
43+
@ConsumerType
44+
public interface SlingUriMapper {
45+
46+
/**
47+
* Contributes to the resolve process (forward mapping), may or may not make adjustments to the Sling URI
48+
*
49+
* @param resourceUri the URI to be mapped for resolution
50+
* @param request the request context that may or may not influence the resolution process (request may be null)
51+
* @param context can be used to skip further processing of the chain or for sharing state between instances of SlingUriMapper services
52+
* @return the adjusted SlingUri or if no adjustments are necessary, just return resourceUri as passed in by first parameter
53+
*/
54+
SlingUri resolve(@NotNull SlingUri resourceUri, @Nullable HttpServletRequest request, @NotNull MappingChainContext context);
55+
56+
/**
57+
* Contributes to the reverse mapping process, may or may not make adjustments to the Sling URI.
58+
*
59+
* @param resourceUri the URI to be mapped
60+
* @param request the request to be taken as reference
61+
* @param context can be used to skip further processing of the chain or for sharing state between instances of SlingUriMapper services
62+
* @return the adjusted SlingUri or if no adjustments are necessary, just return resourceUri as passed in by first parameter
63+
*/
64+
SlingUri map(@NotNull SlingUri resourceUri, @Nullable HttpServletRequest request, @NotNull MappingChainContext context);
65+
66+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
@Version("1.0.0")
21+
package org.apache.sling.spi.urimapping;
22+
23+
import org.osgi.annotation.versioning.Version;
24+

0 commit comments

Comments
 (0)