GuidePropertyProvider.java 21.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.expeditor.service.ExpressionEditorService
 *  com.day.cq.search.Predicate
 *  com.day.cq.search.PredicateGroup
 *  com.day.cq.search.Query
 *  com.day.cq.search.QueryBuilder
 *  com.day.cq.search.result.SearchResult
 *  com.day.cq.widget.HtmlLibraryManager
 *  javax.jcr.Session
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.servlet;

import com.adobe.aemds.guide.service.AdaptiveFormConfigurationService;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.utils.GuidePropertyProviderUtils;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.NodeStructureUtils;
import com.adobe.aemfd.expeditor.service.ExpressionEditorService;
import com.day.cq.search.Predicate;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.SearchResult;
import com.day.cq.widget.HtmlLibraryManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"fd/af/components/infoprovider"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.methods", value={"GET"}), @Property(name="service.description", value={"Adaptive Form Component Info Provider"})})
public class GuidePropertyProvider
extends SlingAllMethodsServlet {
    private static final long serialVersionUID = -930058691346704417L;
    private static final String RT_AEM_FORM_CONTAINER = "fd/af/components/aemform";
    private static final String RT_AEM_APPS_FORM_CONTAINER = "fd/appsaddon/components/aemappsform";
    private static Logger logger = LoggerFactory.getLogger(GuidePropertyProvider.class);
    private static final String LAYOUT = "layout";
    private static final String ACTIONS = "action";
    private static final String FORMATTERS = "formatters";
    private static final String SUBMITACTION = "submitAction";
    private static final String AUTOSAVEACTION = "autoSaveAction";
    private static final String LCPROCESS = "lcProcess";
    private static final String MOBILELAYOUT = "mobileLayout";
    private static final String PROGRESSIVE_STRATEGIES = "progressiveStrategies";
    private static final String PROGRESSIVE_LAYOUT = "progressiveLayout";
    private static final String FRAG_FINDER = "fragfinder";
    private static final String ADAPTIVE_FORM_ASSET = "guide";
    private static final String ADAPTIVE_DOCUMENT_ASSET = "adaptivedocument";
    private static final String FRAGMENT_ASSET = "affragment";
    private static final String PANEL = "panel";
    private static final String AEM_APPS_FORM_CONTAINER = "aemappsform";
    private static final String REQUIRED_COMPONENT_TYPE_PARAM = "requiredComponentType";
    private static final String DOCUMENT_FRAGMENT_LAYOUT = "documentFragmentLayout";
    private static final String CHART_REDUCER = "chartReducer";
    private static final String THEME = "theme";
    private static final String APPEARANCE = "appearance";
    private static final String CUSTOM_FUNCTION = "customFunction";
    private static final String WEB_SERVICES = "webServices";
    private static final String GUIDE_CONTAINER_PATH = "guideContainerPath";
    private static final String CLIENT_LIB_REF = "clientLibRef";
    private static final String WEBSERVICES_CFG_NODE_TYPE = "fd/af/webServices";
    @Reference
    private ExpressionEditorService expressionEditorService;
    @Reference
    private HtmlLibraryManager htmlLibraryManager;
    @Reference
    private GuidePropertyProviderUtils guidePropertyProviderUtils;
    @Reference
    private AdaptiveFormConfigurationService configService;
    @Reference
    private QueryBuilder queryBuilder;

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        String type = request.getParameter("type");
        PrintWriter writer = new PrintWriter(response.getWriter());
        JSONWriter jsonWriter = new JSONWriter((Writer)response.getWriter());
        ResourceResolver resolver = request.getResourceResolver();
        String[] searchPaths = resolver.getSearchPath();
        if ("fragfinder".equals(type)) {
            this.queryFragFinder(request, writer, "affragment", "fd/af/components/panel");
        } else if ("guide".equals(type)) {
            String requiredComponentType = request.getParameter("requiredComponentType");
            requiredComponentType = "panel".equals(requiredComponentType) ? "fd/af/components/panel" : ("aemappsform".equals(requiredComponentType) ? "fd/appsaddon/components/aemappsform" : "fd/af/components/aemform");
            this.queryFragFinder(request, writer, "guide", requiredComponentType);
        } else if ("adaptivedocument".equals(type)) {
            String requiredComponentType = request.getParameter("requiredComponentType");
            requiredComponentType = "aemappsform".equals(requiredComponentType) ? "fd/appsaddon/components/aemappsform" : "fd/af/components/aemform";
            this.queryFragFinder(request, writer, "adaptivedocument", requiredComponentType);
        } else if ("layout".equals(type)) {
            this.queryLayout(request, writer, searchPaths);
        } else if (type.equalsIgnoreCase("theme")) {
            this.queryTheme(writer);
        } else if (type.equalsIgnoreCase("action")) {
            this.queryAction(writer, searchPaths);
        } else if (type.equalsIgnoreCase("progressiveStrategies")) {
            this.queryProgressiveStrategies(writer, searchPaths);
        } else if (type.equalsIgnoreCase("progressiveLayout")) {
            this.queryProgressiveLayout(writer, searchPaths);
        } else if (type.equalsIgnoreCase("formatters")) {
            this.queryFormatters(request, writer, searchPaths);
        } else if (type.equalsIgnoreCase("submitAction") || type.equalsIgnoreCase("autoSaveAction")) {
            this.querySubmitOrAutoSaveAction(request, writer, type, searchPaths);
        } else if (type.equalsIgnoreCase("lcProcess")) {
            this.queryLcProcess(writer);
        } else if (type.equalsIgnoreCase("mobileLayout")) {
            this.queryMobileLayout(writer, searchPaths);
        } else if (type.equalsIgnoreCase("documentFragmentLayout")) {
            this.queryDocumentFragmentLayout(jsonWriter, resolver, searchPaths);
        } else if (type.equalsIgnoreCase("chartReducer")) {
            this.queryChartReducer(writer, searchPaths);
        } else if (type.equalsIgnoreCase("customFunction")) {
            this.queryCustomFunction(request, jsonWriter);
        } else if (type.equalsIgnoreCase("webServices")) {
            this.queryWebServices(resolver, jsonWriter);
        } else if (type.equalsIgnoreCase("appearance")) {
            this.queryAppearance(request, writer, searchPaths);
        }
    }

    private void queryAppearance(SlingHttpServletRequest request, PrintWriter writer, String[] searchPaths) {
        HashMap<String, String> optionsMap = new HashMap<String, String>();
        optionsMap.put("guideNodeClass", request.getParameter("guideNodeClass"));
        JSONArray response = this.guidePropertyProviderUtils.queryAppearance(searchPaths, optionsMap);
        writer.write(response.toString());
        writer.close();
    }

    private void queryFragFinder(SlingHttpServletRequest request, PrintWriter writer, String formType, String resourceType) {
        String QUERY = "query";
        String START = "start";
        String LIMIT = "limit";
        HashMap<String, String> optionsMap = new HashMap<String, String>();
        optionsMap.put("assetType", request.getParameter("assetType"));
        optionsMap.put("query", request.getParameter("query"));
        optionsMap.put("start", request.getParameter("start"));
        optionsMap.put("limit", request.getParameter("limit"));
        JSONObject response = this.guidePropertyProviderUtils.queryFragFinder(formType, resourceType, optionsMap);
        writer.write(response.toString());
        writer.close();
    }

    private void queryLayout(SlingHttpServletRequest request, PrintWriter writer, String[] searchPaths) {
        HashMap<String, String> optionsMap = new HashMap<String, String>();
        optionsMap.put("layoutType", request.getParameter("layoutType"));
        optionsMap.put("isAfField", request.getParameter("isAfField"));
        JSONArray response = this.guidePropertyProviderUtils.queryLayout(searchPaths, optionsMap, request);
        writer.write(response.toString());
        writer.close();
    }

    private void queryTheme(PrintWriter writer) {
        JSONArray response = this.guidePropertyProviderUtils.queryTheme();
        writer.write(response.toString());
        writer.close();
    }

    private void queryAction(PrintWriter writer, String[] searchPaths) {
        JSONArray response = this.guidePropertyProviderUtils.queryAction(searchPaths);
        writer.write(response.toString());
        writer.close();
    }

    private void queryProgressiveStrategies(PrintWriter writer, String[] searchPaths) {
        JSONArray response = this.guidePropertyProviderUtils.queryProgressiveStrategies(searchPaths);
        writer.write(response.toString());
        writer.close();
    }

    private void queryProgressiveLayout(PrintWriter writer, String[] searchPaths) {
        JSONArray response = this.guidePropertyProviderUtils.queryProgressiveLayout(searchPaths);
        writer.write(response.toString());
        writer.close();
    }

    private void queryFormatters(SlingHttpServletRequest request, PrintWriter writer, String[] searchPaths) {
        String guideNodeClass = request.getParameter("guideNodeClass");
        JSONArray response = this.guidePropertyProviderUtils.queryFormatters(guideNodeClass, searchPaths);
        writer.write(response.toString());
        writer.close();
    }

    private void querySubmitOrAutoSaveAction(SlingHttpServletRequest request, PrintWriter writer, String type, String[] searchPaths) {
        HashMap<String, String> optionsMap = new HashMap<String, String>();
        String requestedDataModel = request.getParameter("guideDataModel");
        optionsMap.put("type", type);
        optionsMap.put("guideDataModel", requestedDataModel);
        JSONArray response = this.guidePropertyProviderUtils.querySubmitOrAutoSaveAction(searchPaths, optionsMap);
        writer.write(response.toString());
        writer.close();
    }

    private void queryLcProcess(PrintWriter writer) {
        JSONArray response = this.guidePropertyProviderUtils.queryLcProcess();
        writer.write(response.toString());
        writer.close();
    }

    private void queryMobileLayout(PrintWriter writer, String[] searchPaths) {
        JSONArray response = this.guidePropertyProviderUtils.queryMobileLayout(searchPaths);
        writer.write(response.toString());
        writer.close();
    }

    private void queryDocumentFragmentLayout(JSONWriter writer, ResourceResolver resolver, String[] searchPaths) {
        try {
            writer.array();
            for (int i = 0; i < searchPaths.length; ++i) {
                String query = "SELECT *  FROM nt:base  WHERE guideComponentType = 'fd/adaddon/documentFragmentLayout' AND jcr:path LIKE '" + searchPaths[i] + "%'";
                Iterator layouts = resolver.findResources(query, "sql");
                if (layouts == null) continue;
                while (layouts.hasNext()) {
                    Resource r = (Resource)layouts.next();
                    ValueMap properties = r.getValueMap();
                    String desc = NodeStructureUtils.getLayoutDescription(r);
                    writer.array();
                    writer.value(properties.get("layout", (Object)""));
                    writer.value((Object)(desc == null ? r.getPath() : desc));
                    writer.endArray();
                }
            }
            writer.endArray();
        }
        catch (JSONException e) {
            logger.error(e.getMessage(), (Throwable)e);
            throw new GuideException((Exception)e);
        }
    }

    private void queryChartReducer(PrintWriter writer, String[] searchPaths) {
        JSONArray response = this.guidePropertyProviderUtils.queryChartReducer(searchPaths);
        writer.write(response.toString());
        writer.close();
    }

    private void queryCustomFunction(SlingHttpServletRequest request, JSONWriter writer) {
        String guideContainerPath = request.getParameter("guideContainerPath");
        if (guideContainerPath != null && guideContainerPath.length() > 0) {
            try {
                ValueMap valueMap;
                Object clientLib;
                writer.object();
                JSONArray configJson = new JSONArray();
                ResourceResolver resourceResolver = request.getResourceResolver();
                Resource guideContainerResource = resourceResolver.getResource(guideContainerPath);
                if (guideContainerResource != null && (clientLib = (valueMap = guideContainerResource.getValueMap()).get((Object)"clientLibRef")) != null) {
                    String[] clientLibs = clientLib.toString().split(",");
                    ArrayList<String> scripts = GuideUtils.getScriptFromClientLibList(this.htmlLibraryManager, clientLibs);
                    for (String script : scripts) {
                        configJson = this.expressionEditorService.getFunctionsList(script, "eesJavascript");
                    }
                    if (configJson.length() > 0) {
                        writer.key("customFunction").value((Object)configJson);
                    }
                }
                writer.endObject();
            }
            catch (JSONException e) {
                logger.error(e.getMessage(), (Throwable)e);
                throw new GuideException((Exception)e);
            }
        }
    }

    private boolean isValidSearchPath(String path) {
        return StringUtils.isNotBlank((CharSequence)path) && path.trim().startsWith("/");
    }

    private PredicateGroup webServicesSearchPathPredicate() {
        String[] searchPaths = this.configService.getWSDLConfigSearchPaths();
        PredicateGroup predicate = new PredicateGroup();
        predicate.setAllRequired(false);
        for (String searchPath : searchPaths) {
            if (!this.isValidSearchPath(searchPath)) continue;
            Predicate pathPred = this.createPathPredicate(searchPath);
            predicate.add(pathPred);
        }
        return predicate;
    }

    private Predicate createPropertyPredicate(String propName, String value) {
        Predicate propPred = new Predicate("property");
        propPred.set("property", propName);
        propPred.set("value", "fd/af/webServices");
        return propPred;
    }

    private Predicate createTypePredicate(String type) {
        Predicate typePred = new Predicate("type");
        typePred.set("type", type);
        return typePred;
    }

    private Predicate createPathPredicate(String path) {
        Predicate pathPred = new Predicate("path");
        pathPred.set("path", path);
        return pathPred;
    }

    private PredicateGroup webServicesPredicate() {
        PredicateGroup searchPathPredicates = this.webServicesSearchPathPredicate();
        if (searchPathPredicates.size() < 1) {
            logger.warn("No search paths found, aborting webservices search");
            return null;
        }
        PredicateGroup predicate = new PredicateGroup();
        Predicate typePred = this.createTypePredicate("nt:unstructured");
        predicate.add(typePred);
        Predicate propPred = this.createPropertyPredicate("guideComponentType", "fd/af/webServices");
        predicate.add(propPred);
        predicate.add((Predicate)this.webServicesSearchPathPredicate());
        return predicate;
    }

    private Query webServiceQuery(ResourceResolver rr) {
        PredicateGroup predicate = this.webServicesPredicate();
        if (predicate == null) {
            return null;
        }
        return this.queryBuilder.createQuery(this.webServicesPredicate(), (Session)rr.adaptTo(Session.class));
    }

    private void queryWebServices(ResourceResolver resolver, JSONWriter writer) {
        SearchResult searchResult;
        Iterator webServiceResources;
        Query query = this.webServiceQuery(resolver);
        if (query != null && (webServiceResources = (searchResult = query.getResult()).getResources()) != null) {
            try {
                writer.array();
                while (webServiceResources.hasNext()) {
                    writer.object();
                    Resource resource = (Resource)webServiceResources.next();
                    String path = resource.getPath();
                    writer.key(path);
                    this.webServiceJsonCreator(resource, writer);
                    writer.endObject();
                }
                writer.endArray();
            }
            catch (JSONException e) {
                logger.error(e.getMessage(), (Throwable)e);
                throw new GuideException((Exception)e);
            }
        }
    }

    private void webServiceJsonCreator(Resource resource, JSONWriter writer) {
        try {
            writer.object();
            ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
            for (Map.Entry pairs : properties.entrySet()) {
                String key = (String)pairs.getKey();
                if (key.equals("guideComponentType") || key.equals("jcr:primaryType")) continue;
                writer.key(key).value(pairs.getValue());
            }
            Iterable children = resource.getChildren();
            for (Resource child : children) {
                String name = child.getName();
                writer.key(name);
                this.webServiceJsonCreator(child, writer);
            }
            writer.endObject();
        }
        catch (JSONException e) {
            logger.error(e.getMessage(), (Throwable)e);
            throw new GuideException((Exception)e);
        }
    }

    protected void bindExpressionEditorService(ExpressionEditorService expressionEditorService) {
        this.expressionEditorService = expressionEditorService;
    }

    protected void unbindExpressionEditorService(ExpressionEditorService expressionEditorService) {
        if (this.expressionEditorService == expressionEditorService) {
            this.expressionEditorService = null;
        }
    }

    protected void bindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
        this.htmlLibraryManager = htmlLibraryManager;
    }

    protected void unbindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
        if (this.htmlLibraryManager == htmlLibraryManager) {
            this.htmlLibraryManager = null;
        }
    }

    protected void bindGuidePropertyProviderUtils(GuidePropertyProviderUtils guidePropertyProviderUtils) {
        this.guidePropertyProviderUtils = guidePropertyProviderUtils;
    }

    protected void unbindGuidePropertyProviderUtils(GuidePropertyProviderUtils guidePropertyProviderUtils) {
        if (this.guidePropertyProviderUtils == guidePropertyProviderUtils) {
            this.guidePropertyProviderUtils = null;
        }
    }

    protected void bindConfigService(AdaptiveFormConfigurationService adaptiveFormConfigurationService) {
        this.configService = adaptiveFormConfigurationService;
    }

    protected void unbindConfigService(AdaptiveFormConfigurationService adaptiveFormConfigurationService) {
        if (this.configService == adaptiveFormConfigurationService) {
            this.configService = null;
        }
    }

    protected void bindQueryBuilder(QueryBuilder queryBuilder) {
        this.queryBuilder = queryBuilder;
    }

    protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
        if (this.queryBuilder == queryBuilder) {
            this.queryBuilder = null;
        }
    }
}