View Javadoc

1   /*
2    * Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.squeakysand.sling;
17  
18  import com.squeakysand.jcr.SimpleNode;
19  import com.squeakysand.jcr.StringValue;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import javax.jcr.Node;
25  
26  import org.apache.sling.api.SlingException;
27  import org.apache.sling.api.SlingHttpServletRequest;
28  import org.apache.sling.api.request.RequestPathInfo;
29  import org.apache.sling.api.resource.Resource;
30  import org.apache.sling.api.resource.ResourceResolver;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public class SlingUtils {
35  
36      private static final Logger LOG = LoggerFactory.getLogger(SlingUtils.class);
37      private static final String SLING_RESOURCE_TYPE = "sling:resourceType";
38  
39      /**
40       * Utility method for walking the content tree to find an ancestor node with a specific sling:resourceType value.
41       */
42      public static SimpleNode findAncestorBySlingResourceType(Node descendentNode, String slingResourceType) {
43          SimpleNode simpleDescendentNode = SimpleNode.asSimpleNode(descendentNode);
44          return simpleDescendentNode.getAncestorByPropertyValue(SLING_RESOURCE_TYPE, new StringValue(slingResourceType));
45      }
46  
47      public static Resource getResource(String resourcePath, ResourceResolver resourceResolver) {
48          Resource result = null;
49          if ((resourcePath != null) && (resourceResolver != null)) {
50              try {
51                  result = resourceResolver.getResource(resourcePath);
52              } catch (SlingException e) {
53                  LOG.error(e.getMessage(), e);
54              }
55          }
56          return result;
57      }
58  
59      public static String getResourceType(Node node) {
60          String result = null;
61          if (node != null) {
62              SimpleNode simpleNode = SimpleNode.asSimpleNode(node);
63              result = simpleNode.getStringProperty(SLING_RESOURCE_TYPE);
64          }
65          LOG.debug("resource type for node {} is {}", node, result);
66          return result;
67      }
68  
69      public static List<String> getSelectors(SlingHttpServletRequest slingRequest) {
70          RequestPathInfo requestPathInfo = slingRequest.getRequestPathInfo();
71          String[] selectors = requestPathInfo.getSelectors();
72          return Arrays.asList(selectors);
73      }
74  
75      /**
76       * Returns true if the specific selector string is present in the request and false otherwise.
77       */
78      public static Boolean isSelectorPresent(String selector, SlingHttpServletRequest slingRequest) {
79          Boolean isPresent = Boolean.FALSE;
80          String selectors = slingRequest.getRequestPathInfo().getSelectorString();
81          if ((selectors != null) && selectors.contains(selector)) {
82              isPresent = Boolean.TRUE;
83          }
84          return isPresent;
85      }
86  }