ExpEditorServiceManager.java 22.7 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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aem.dermis.api.IFormDataModelManager
 *  com.adobe.aem.dermis.api.bridge.DataContext
 *  com.adobe.aem.dermis.api.bridge.IDermisBridge
 *  com.adobe.aem.dermis.api.bridge.OperationOptions
 *  com.adobe.aem.dermis.api.bridge.Query
 *  com.adobe.aem.dermis.exception.DermisException
 *  com.adobe.aem.dermis.model.value.ITypeValue
 *  com.adobe.aem.dermis.model.value.IValue
 *  com.adobe.aem.dermis.slingmodel.EntityOperation
 *  com.adobe.aem.dermis.slingmodel.FormDataModel
 *  com.adobe.aem.dermis.util.SlingJSONWriter
 *  com.adobe.aemds.guide.service.AdaptiveFormConfigurationService
 *  com.adobe.forms.foundation.wsdl.WSDLInvokerParams
 *  com.adobe.forms.foundation.wsdl.WSDLInvokerService
 *  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
 *  javax.jcr.Session
 *  org.apache.commons.lang.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.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.addon.expeditor.servlet;

import com.adobe.aem.dermis.api.IFormDataModelManager;
import com.adobe.aem.dermis.api.bridge.DataContext;
import com.adobe.aem.dermis.api.bridge.IDermisBridge;
import com.adobe.aem.dermis.api.bridge.OperationOptions;
import com.adobe.aem.dermis.api.bridge.Query;
import com.adobe.aem.dermis.exception.DermisException;
import com.adobe.aem.dermis.model.value.ITypeValue;
import com.adobe.aem.dermis.model.value.IValue;
import com.adobe.aem.dermis.slingmodel.EntityOperation;
import com.adobe.aem.dermis.slingmodel.FormDataModel;
import com.adobe.aem.dermis.util.SlingJSONWriter;
import com.adobe.aemds.guide.service.AdaptiveFormConfigurationService;
import com.adobe.forms.foundation.wsdl.WSDLInvokerParams;
import com.adobe.forms.foundation.wsdl.WSDLInvokerService;
import com.day.cq.search.Predicate;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.SearchResult;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Session;
import org.apache.commons.lang.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.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
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"fd/af/components/guideContainer"}), @Property(name="sling.servlet.selectors", value={"af.dermis"}), @Property(name="sling.servlet.methods", value={"GET", "POST"})})
public class ExpEditorServiceManager
extends SlingAllMethodsServlet {
    @Reference(referenceInterface=WSDLInvokerService.class)
    private WSDLInvokerService wsdlInvokerService;
    @Reference
    private IDermisBridge dermisBridge;
    @Reference
    private AdaptiveFormConfigurationService configService;
    @Reference
    private QueryBuilder queryBuilder;
    @Reference
    private IFormDataModelManager formDataModelManager;
    private static final long serialVersionUID = 193653482338551669L;
    private final Logger logger = LoggerFactory.getLogger(ExpEditorServiceManager.class);
    private final String paramFunc = "functionToExecute";
    private final String GET_ALL_SERVICES_FUNCTION = "getAllServices";
    private final String GET_ALL_OPERATIONS_FUNCTION = "getAllOperations";
    private final String INVOKE_WSDL_OPERATION = "invokeWsdlOperation";
    private final String INVOKE_FDM_OPERATION = "invokeFDMOperation";
    private final String paramWSDLEndPoint = "wsdlEndPoint";
    private final String paramOperationName = "operationName";
    private final String paramPORT = "port";
    private final String paramSOAPURI = "soapActionURI";
    private final String paramServiceEndPoint = "serviceEndPoint";
    private final String paramGuideNodePath = "guideNodePath";
    private final String paramOperationInput = "input";
    private final String paramNamespace = "namespace";
    private final String paramInputRoot = "inputRoot";
    private static final String WEBSERVICES_CFG_NODE_TYPE = "fd/af/webServices";

    private void writeResult(SlingHttpServletResponse response, String output) throws IOException, JSONException {
        PrintWriter writer = response.getWriter();
        writer.write(output);
        writer.close();
    }

    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 Predicate createPropertyPredicate(String propName, String value) {
        Predicate propPred = new Predicate("property");
        propPred.set("property", propName);
        propPred.set("value", "fd/af/webServices");
        return propPred;
    }

    private boolean isValidSearchPath(String path) {
        return StringUtils.isNotBlank((String)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 PredicateGroup webServicesPredicate() {
        PredicateGroup searchPathPredicates = this.webServicesSearchPathPredicate();
        if (searchPathPredicates.size() < 1) {
            this.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 com.day.cq.search.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 webServiceJsonCreator(Resource resource, JSONWriter writer) {
        try {
            writer.object();
            writer.key("id").value((Object)resource.getPath());
            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) {
            this.logger.error(e.getMessage(), (Throwable)e);
        }
    }

    private void getAllServices(SlingHttpServletRequest request, SlingHttpServletResponse response) throws Exception {
        JSONWriter writer = new JSONWriter((Writer)response.getWriter());
        ResourceResolver resolver = request.getResourceResolver();
        com.day.cq.search.Query query = this.webServiceQuery(resolver);
        Iterator webServiceResources = null;
        List formDataModelList = null;
        if (query != null) {
            SearchResult searchResult = query.getResult();
            webServiceResources = searchResult.getResources();
            try {
                formDataModelList = this.formDataModelManager.getAllOperations();
            }
            catch (DermisException e) {
                this.logger.error(e.getMessage(), (Throwable)e);
            }
            writer.object();
            if (formDataModelList != null) {
                for (FormDataModel formDataModel : formDataModelList) {
                    List operations = formDataModel.getOperations();
                    if (operations == null || operations.size() <= 0) continue;
                    try {
                        writer.key(formDataModel.getPath());
                        writer.object();
                        writer.key("title").value((Object)formDataModel.getTitle());
                        writer.key("path").value((Object)formDataModel.getPath());
                        writer.key("items");
                        writer.array();
                        for (EntityOperation operation : operations) {
                            writer.object();
                            writer.key("operation").value((Object)operation.getId());
                            writer.key("jcr:title").value((Object)operation.getTitle());
                            writer.key("formDataModelId").value((Object)formDataModel.getPath());
                            writer.key("id").value((Object)(formDataModel.getPath() + ":" + operation.getId()));
                            writer.endObject();
                        }
                        writer.endArray();
                        writer.endObject();
                    }
                    catch (JSONException e) {
                        this.logger.error(e.getMessage(), (Throwable)e);
                    }
                }
            }
            if (webServiceResources != null) {
                try {
                    writer.key("others");
                    writer.object();
                    writer.key("title").value((Object)"Others");
                    writer.key("items");
                    writer.array();
                    while (webServiceResources.hasNext()) {
                        Resource resource = (Resource)webServiceResources.next();
                        this.webServiceJsonCreator(resource, writer);
                    }
                    writer.endArray();
                    writer.endObject();
                }
                catch (JSONException e) {
                    this.logger.error(e.getMessage(), (Throwable)e);
                }
            }
            writer.endObject();
        }
    }

    private void getWSDLOperations(SlingHttpServletRequest request, SlingHttpServletResponse response) throws Exception {
        try {
            String serverEndPoint = request.getParameter("wsdlEndPoint");
            if (serverEndPoint.isEmpty()) {
                throw new UnsupportedOperationException("Invalid Param: wsdlEndPoint empty");
            }
            WSDLInvokerParams params = new WSDLInvokerParams();
            String jsonOutput = this.wsdlInvokerService.expandAllOperations(serverEndPoint, params);
            this.writeResult(response, jsonOutput);
        }
        catch (IOException e) {
            throw new Exception("getWSDLOperations: IOException", e);
        }
        catch (JSONException e) {
            throw new Exception("getWSDLOperations: JSONException", (Throwable)e);
        }
        catch (Exception e) {
            this.logger.error("Error getAllOperations:", (Throwable)e);
            throw new Exception("getAllOperations: ", e);
        }
    }

    private boolean validateWebServiceEndPoint(SlingHttpServletRequest request, String guideNodePath, String requiredString) {
        int index;
        if (requiredString == null || requiredString.isEmpty() || guideNodePath == null || guideNodePath.isEmpty()) {
            return false;
        }
        String[] oldEventProperties = new String[]{"calcExp", "visibleExp", "initScript", "clickExp", "valueCommitScript", "enabledExp", "validateExp", "completionExp", "summaryExp", "optionsExp", "navigationChangeExp"};
        String[] newEventProperties = new String[]{"fd:calc", "fd:visible", "fd:init", "fd:click", "fd:valueCommit", "fd:enabled", "fd:validate", "fd:completion", "fd:summary", "fd:options", "fd:navigationChange"};
        Resource guideContainerResource = request.getResourceResolver().getResource(guideNodePath);
        ValueMap valueMap = ResourceUtil.getValueMap((Resource)guideContainerResource);
        boolean whiteListed = false;
        for (index = 0; !whiteListed && index < oldEventProperties.length; ++index) {
            String scriptWithoutSpaces;
            String finalScript;
            String script = (String)valueMap.get((Object)oldEventProperties[index]);
            if (script == null || script.isEmpty() || (finalScript = (scriptWithoutSpaces = script.replaceAll("\\s", "")).replaceAll("\\\\", "")) == null || finalScript.isEmpty()) continue;
            whiteListed = finalScript.contains(requiredString);
        }
        Resource rulesNode = request.getResourceResolver().getResource(guideNodePath + "/fd:rules");
        Resource scriptsNode = request.getResourceResolver().getResource(guideNodePath + "/fd:scripts");
        ValueMap rulesValueMap = ResourceUtil.getValueMap((Resource)rulesNode);
        ValueMap scriptsValueMap = ResourceUtil.getValueMap((Resource)scriptsNode);
        for (index = 0; !whiteListed && index < newEventProperties.length; ++index) {
            String scriptWithoutSpaces;
            String finalScript;
            int i;
            String[] rules = (String[])rulesValueMap.get((Object)newEventProperties[index]);
            String[] scripts = (String[])scriptsValueMap.get((Object)newEventProperties[index]);
            if (rules != null) {
                for (i = 0; !whiteListed && i < rules.length; ++i) {
                    String rule = rules[i];
                    scriptWithoutSpaces = rule.replaceAll("\\s", "");
                    finalScript = scriptWithoutSpaces.replaceAll("\\\\", "");
                    if (finalScript == null || finalScript.isEmpty()) continue;
                    whiteListed = finalScript.contains(requiredString);
                }
            }
            if (scripts == null) continue;
            for (i = 0; !whiteListed && i < scripts.length; ++i) {
                String script = scripts[i];
                scriptWithoutSpaces = script.replaceAll("\\s", "");
                finalScript = scriptWithoutSpaces.replaceAll("\\\\", "");
                if (finalScript == null || finalScript.isEmpty()) continue;
                whiteListed = finalScript.contains(requiredString);
            }
        }
        return whiteListed;
    }

    private void invokeWsdlOperation(SlingHttpServletRequest request, SlingHttpServletResponse response) throws Exception {
        String serverEndPoint = request.getParameter("wsdlEndPoint");
        if (serverEndPoint.isEmpty()) {
            throw new UnsupportedOperationException("Invalid Param: wsdlEndPoint empty");
        }
        String operationName = request.getParameter("operationName");
        if (operationName.isEmpty()) {
            throw new UnsupportedOperationException("Invalid Param: " + operationName + " empty");
        }
        String operationInput = request.getParameter("input");
        if (operationInput.isEmpty()) {
            throw new UnsupportedOperationException("Invalid Param: input empty");
        }
        String operationSoapURI = request.getParameter("soapActionURI");
        String namespace = request.getParameter("namespace");
        String port = request.getParameter("port");
        String inputRoot = request.getParameter("inputRoot");
        String serviceEndPoint = request.getParameter("serviceEndPoint");
        String guideNodePath = request.getParameter("guideNodePath");
        if (!this.validateWebServiceEndPoint(request, guideNodePath, "\"wsdlEndPoint\":\"" + serverEndPoint + "\"")) {
            throw new UnsupportedOperationException("Invalid Param");
        }
        WSDLInvokerParams params = new WSDLInvokerParams();
        params.setPort(port);
        params.setOpSoapUri(operationSoapURI);
        params.setNamespace(namespace);
        params.setOpInputRoot(inputRoot);
        params.setServiceEndPoint(serviceEndPoint);
        String jsonOutput = this.wsdlInvokerService.executeOperation(serverEndPoint, operationName, operationInput, params);
        this.writeResult(response, jsonOutput);
    }

    private void invokeFDMOperation(SlingHttpServletRequest request, SlingHttpServletResponse response) throws Exception {
        String formDataModelId = request.getParameter("formDataModelId");
        if (StringUtils.isEmpty((String)formDataModelId)) {
            throw new UnsupportedOperationException("Invalid Param: wsdlEndPoint empty");
        }
        String operationName = request.getParameter("operationName");
        if (StringUtils.isEmpty((String)operationName)) {
            throw new UnsupportedOperationException("Invalid Param: operationName empty");
        }
        String operationArguments = request.getParameter("input");
        if (StringUtils.isEmpty((String)operationArguments)) {
            throw new UnsupportedOperationException("Invalid Param: input empty");
        }
        String guideNodePath = request.getParameter("guideNodePath");
        if (!this.validateWebServiceEndPoint(request, guideNodePath, "\"formDataModelId\":\"" + formDataModelId + "\"")) {
            throw new UnsupportedOperationException("Invalid Param");
        }
        JSONObject arguments = new JSONObject(operationArguments);
        OperationOptions operationOptions = new OperationOptions(operationName, arguments);
        Query query = new Query(formDataModelId, null, operationOptions);
        IValue value = this.dermisBridge.executeOperation(query);
        SlingJSONWriter slingJSONWriter = new SlingJSONWriter((Writer)response.getWriter());
        if (value != null) {
            if (value instanceof ITypeValue) {
                response.getWriter().flush();
                response.getWriter().write(((ITypeValue)value).get().toString());
            } else {
                slingJSONWriter.write((Object)value);
            }
        }
    }

    public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        try {
            String functionToExecute = request.getParameter("functionToExecute");
            if (functionToExecute.isEmpty()) {
                throw new Exception("doPost: Invalid Param");
            }
            if (functionToExecute.compareToIgnoreCase("invokeFDMOperation") == 0) {
                this.invokeFDMOperation(request, response);
            } else if (functionToExecute.compareToIgnoreCase("invokeWsdlOperation") == 0) {
                this.invokeWsdlOperation(request, response);
            }
        }
        catch (Exception e) {
            this.logger.error("Error while making web service related call", (Throwable)e);
        }
    }

    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        try {
            String functionToExecute = request.getParameter("functionToExecute");
            if (functionToExecute.isEmpty()) {
                throw new Exception("doGet: Invalid Param");
            }
            if (functionToExecute.compareToIgnoreCase("getAllOperations") == 0) {
                this.getWSDLOperations(request, response);
            } else if (functionToExecute.compareToIgnoreCase("getAllServices") == 0) {
                this.getAllServices(request, response);
            }
        }
        catch (Exception e) {
            this.logger.error("Error while retrieving data", (Throwable)e);
        }
    }

    protected void bindWsdlInvokerService(WSDLInvokerService wSDLInvokerService) {
        this.wsdlInvokerService = wSDLInvokerService;
    }

    protected void unbindWsdlInvokerService(WSDLInvokerService wSDLInvokerService) {
        if (this.wsdlInvokerService == wSDLInvokerService) {
            this.wsdlInvokerService = null;
        }
    }

    protected void bindDermisBridge(IDermisBridge iDermisBridge) {
        this.dermisBridge = iDermisBridge;
    }

    protected void unbindDermisBridge(IDermisBridge iDermisBridge) {
        if (this.dermisBridge == iDermisBridge) {
            this.dermisBridge = 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;
        }
    }

    protected void bindFormDataModelManager(IFormDataModelManager iFormDataModelManager) {
        this.formDataModelManager = iFormDataModelManager;
    }

    protected void unbindFormDataModelManager(IFormDataModelManager iFormDataModelManager) {
        if (this.formDataModelManager == iFormDataModelManager) {
            this.formDataModelManager = null;
        }
    }
}