1 /* 2 * #%L 3 * wcm.io 4 * %% 5 * Copyright (C) 2014 wcm.io 6 * %% 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * #L% 19 */ 20 package io.wcm.handler.link; 21 22 import java.util.HashMap; 23 import java.util.Map; 24 25 import org.apache.sling.api.resource.ResourceResolver; 26 import org.apache.sling.api.resource.SyntheticResource; 27 import org.apache.sling.api.resource.ValueMap; 28 import org.apache.sling.api.wrappers.ValueMapDecorator; 29 import org.jetbrains.annotations.NotNull; 30 import org.osgi.annotation.versioning.ProviderType; 31 32 /** 33 * Synthetic resource for building links via {@link LinkHandler}. 34 * Use properties with names provided by {@link LinkNameConstants}. 35 */ 36 @ProviderType 37 public final class SyntheticLinkResource extends SyntheticResource { 38 39 private static final String RESOURCE_TYPE = "wcm-io/handler/link/synthetic/resource"; 40 41 private final ValueMap properties; 42 43 /** 44 * Instantiate resource with static path/resource type 45 * @param resourceResolver Resource resolver 46 * @param path Resource path. Can be a non-existing path, but the path should be located somewhere within the 47 * applications content paths to make sure the handler configuration looked up via context-aware services 48 * is the expected one. 49 */ 50 public SyntheticLinkResource(@NotNull ResourceResolver resourceResolver, 51 @NotNull String path) { 52 this(resourceResolver, path, new HashMap<>()); 53 } 54 55 /** 56 * Instantiate resource with static path/resource type 57 * @param resourceResolver Resource resolver 58 * @param path Resource path. Can be a non-existing path, but the path should be located somewhere within the 59 * applications content paths to make sure the handler configuration looked up via context-aware services 60 * is the expected one. 61 * @param properties Properties for resource 62 */ 63 public SyntheticLinkResource(@NotNull ResourceResolver resourceResolver, 64 @NotNull String path, @NotNull Map<String, Object> properties) { 65 super(resourceResolver, path, RESOURCE_TYPE); 66 this.properties = new ValueMapDecorator(properties); 67 } 68 69 @Override 70 @SuppressWarnings({ "unchecked", "null" }) 71 public <Type> Type adaptTo(Class<Type> type) { 72 if (type == ValueMap.class) { 73 return (Type)this.properties; 74 } 75 else { 76 return super.adaptTo(type); 77 } 78 } 79 80 }