Search in sources :

Example 11 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project carbon-business-process by wso2.

the class BaseExecutionService method createExecutionVariable.

protected Response createExecutionVariable(Execution execution, boolean override, int variableType, HttpServletRequest httpServletRequest, UriInfo uriInfo) {
    Object result = null;
    Response.ResponseBuilder responseBuilder = Response.ok();
    List<RestVariable> inputVariables = new ArrayList<>();
    List<RestVariable> resultVariables = new ArrayList<>();
    if (Utils.isApplicationJsonRequest(httpServletRequest)) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            @SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) objectMapper.readValue(httpServletRequest.getInputStream(), List.class);
            for (Object restObject : variableObjects) {
                RestVariable restVariable = objectMapper.convertValue(restObject, RestVariable.class);
                inputVariables.add(restVariable);
            }
        } catch (Exception e) {
            throw new ActivitiIllegalArgumentException("Failed to serialize to a RestVariable instance", e);
        }
    } else if (Utils.isApplicationXmlRequest(httpServletRequest)) {
        JAXBContext jaxbContext = null;
        try {
            jaxbContext = JAXBContext.newInstance(RestVariableCollection.class);
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
            inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
            XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(new StreamSource(httpServletRequest.getInputStream()));
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            RestVariableCollection restVariableCollection = (RestVariableCollection) jaxbUnmarshaller.unmarshal(xmlReader);
            if (restVariableCollection == null) {
                throw new ActivitiIllegalArgumentException("xml request body could not be transformed to a " + "RestVariable Collection instance.");
            }
            List<RestVariable> restVariableList = restVariableCollection.getRestVariables();
            if (restVariableList.size() == 0) {
                throw new ActivitiIllegalArgumentException("xml request body could not identify any rest " + "variables to be updated");
            }
            for (RestVariable restVariable : restVariableList) {
                inputVariables.add(restVariable);
            }
        } catch (JAXBException | IOException | XMLStreamException e) {
            throw new ActivitiIllegalArgumentException("xml request body could not be transformed to a " + "RestVariable instance.", e);
        }
    }
    if (inputVariables.size() == 0) {
        throw new ActivitiIllegalArgumentException("Request didn't contain a list of variables to create.");
    }
    RestVariable.RestVariableScope sharedScope = null;
    RestVariable.RestVariableScope varScope = null;
    Map<String, Object> variablesToSet = new HashMap<String, Object>();
    for (RestVariable var : inputVariables) {
        // Validate if scopes match
        varScope = var.getVariableScope();
        if (var.getName() == null) {
            throw new ActivitiIllegalArgumentException("Variable name is required");
        }
        if (varScope == null) {
            varScope = RestVariable.RestVariableScope.LOCAL;
        }
        if (sharedScope == null) {
            sharedScope = varScope;
        }
        if (varScope != sharedScope) {
            throw new ActivitiIllegalArgumentException("Only allowed to update multiple variables in the same scope.");
        }
        if (!override && hasVariableOnScope(execution, var.getName(), varScope)) {
            throw new BPMNConflictException("Variable '" + var.getName() + "' is already present on execution '" + execution.getId() + "'.");
        }
        Object actualVariableValue = new RestResponseFactory().getVariableValue(var);
        variablesToSet.put(var.getName(), actualVariableValue);
        resultVariables.add(new RestResponseFactory().createRestVariable(var.getName(), actualVariableValue, varScope, execution.getId(), variableType, false, uriInfo.getBaseUri().toString()));
    }
    if (!variablesToSet.isEmpty()) {
        RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
        if (sharedScope == RestVariable.RestVariableScope.LOCAL) {
            runtimeService.setVariablesLocal(execution.getId(), variablesToSet);
        } else {
            if (execution.getParentId() != null) {
                // Explicitly set on parent, setting non-local variables on execution itself will override local-variables if exists
                runtimeService.setVariables(execution.getParentId(), variablesToSet);
            } else {
                // Standalone task, no global variables possible
                throw new ActivitiIllegalArgumentException("Cannot set global variables on execution '" + execution.getId() + "', task is not part of process.");
            }
        }
    }
    RestVariableCollection restVariableCollection = new RestVariableCollection();
    restVariableCollection.setRestVariables(resultVariables);
    responseBuilder.entity(restVariableCollection);
    return responseBuilder.status(Response.Status.CREATED).build();
}
Also used : BPMNConflictException(org.wso2.carbon.bpmn.rest.common.exception.BPMNConflictException) XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBContext(javax.xml.bind.JAXBContext) RestVariable(org.wso2.carbon.bpmn.rest.engine.variable.RestVariable) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) Unmarshaller(javax.xml.bind.Unmarshaller) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) RuntimeService(org.activiti.engine.RuntimeService) StreamSource(javax.xml.transform.stream.StreamSource) ActivitiException(org.activiti.engine.ActivitiException) BPMNContentNotSupportedException(org.wso2.carbon.bpmn.rest.common.exception.BPMNContentNotSupportedException) XMLStreamException(javax.xml.stream.XMLStreamException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) BPMNConflictException(org.wso2.carbon.bpmn.rest.common.exception.BPMNConflictException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) JAXBException(javax.xml.bind.JAXBException) DataResponse(org.wso2.carbon.bpmn.rest.model.common.DataResponse) Response(javax.ws.rs.core.Response) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 12 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangXMLNS xmlnsNode) {
    BLangXMLNS generatedXMLNSNode;
    xmlnsNode.namespaceURI = rewriteExpr(xmlnsNode.namespaceURI);
    BSymbol ownerSymbol = xmlnsNode.symbol.owner;
    // Local namespace declaration in a function/resource/action/worker
    if ((ownerSymbol.tag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        generatedXMLNSNode = new BLangLocalXMLNS();
    } else {
        generatedXMLNSNode = new BLangPackageXMLNS();
    }
    generatedXMLNSNode.namespaceURI = xmlnsNode.namespaceURI;
    generatedXMLNSNode.prefix = xmlnsNode.prefix;
    generatedXMLNSNode.symbol = xmlnsNode.symbol;
    result = generatedXMLNSNode;
}
Also used : BLangPackageXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangPackageXMLNS) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangLocalXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS)

Example 13 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangSimpleVarRef varRefExpr) {
    BLangSimpleVarRef genVarRefExpr = varRefExpr;
    // XML qualified name reference. e.g: ns0:foo
    if (varRefExpr.pkgSymbol != null && varRefExpr.pkgSymbol.tag == SymTag.XMLNS) {
        BLangXMLQName qnameExpr = new BLangXMLQName(varRefExpr.variableName);
        qnameExpr.nsSymbol = (BXMLNSSymbol) varRefExpr.pkgSymbol;
        qnameExpr.localname = varRefExpr.variableName;
        qnameExpr.prefix = varRefExpr.pkgAlias;
        qnameExpr.namespaceURI = qnameExpr.nsSymbol.namespaceURI;
        qnameExpr.isUsedInXML = false;
        qnameExpr.pos = varRefExpr.pos;
        qnameExpr.type = symTable.stringType;
        result = qnameExpr;
        return;
    }
    BSymbol ownerSymbol = varRefExpr.symbol.owner;
    if ((varRefExpr.symbol.tag & SymTag.FUNCTION) == SymTag.FUNCTION && varRefExpr.symbol.type.tag == TypeTags.INVOKABLE) {
        genVarRefExpr = new BLangFunctionVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        // Local variable in a function/resource/action/worker
        genVarRefExpr = new BLangLocalVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
        // Field variable in a receiver
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.STRUCT) == SymTag.STRUCT) {
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymbol.tag & SymTag.SERVICE) == SymTag.SERVICE) {
        // Package variable | service variable
        // We consider both of them as package level variables
        genVarRefExpr = new BLangPackageVarRef(varRefExpr.symbol);
        // Only locking service level and package level variables
        if (!enclLocks.isEmpty()) {
            enclLocks.peek().addLockVariable(varRefExpr.symbol);
        }
    }
    genVarRefExpr.type = varRefExpr.type;
    result = genVarRefExpr;
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangPackageVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangPackageVarRef) BLangFunctionVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFunctionVarRef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) BLangFieldVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFieldVarRef)

Example 14 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangXMLElementLiteral xmlElementLiteral) {
    xmlElementLiteral.startTagName = rewriteExpr(xmlElementLiteral.startTagName);
    xmlElementLiteral.endTagName = rewriteExpr(xmlElementLiteral.endTagName);
    xmlElementLiteral.modifiedChildren = rewriteExprs(xmlElementLiteral.modifiedChildren);
    xmlElementLiteral.attributes = rewriteExprs(xmlElementLiteral.attributes);
    // Separate the in-line namepsace declarations and attributes.
    Iterator<BLangXMLAttribute> attributesItr = xmlElementLiteral.attributes.iterator();
    while (attributesItr.hasNext()) {
        BLangXMLAttribute attribute = attributesItr.next();
        if (!attribute.isNamespaceDeclr) {
            continue;
        }
        // Create local namepace declaration for all in-line namespace declarations
        BLangLocalXMLNS xmlns = new BLangLocalXMLNS();
        xmlns.namespaceURI = attribute.value.concatExpr;
        xmlns.prefix = ((BLangXMLQName) attribute.name).localname;
        xmlns.symbol = (BXMLNSSymbol) attribute.symbol;
        xmlElementLiteral.inlineNamespaces.add(xmlns);
        attributesItr.remove();
    }
    result = xmlElementLiteral;
}
Also used : BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangLocalXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS)

Example 15 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project identity-outbound-auth-sms-otp by wso2-extensions.

the class SMSOTPAuthenticator method processFirstStepOnly.

/**
 * In SMSOTP optional case proceed with first step only.It can be basic or federated.
 *
 * @param authenticatedUser the name of authenticatedUser
 * @param context           the AuthenticationContext
 */
private void processFirstStepOnly(AuthenticatedUser authenticatedUser, AuthenticationContext context) {
    if (log.isDebugEnabled()) {
        log.debug("Processing First step only. Skipping SMSOTP");
    }
    // the authentication flow happens with basic authentication.
    StepConfig stepConfig = context.getSequenceConfig().getStepMap().get(context.getCurrentStep() - 1);
    if (stepConfig.getAuthenticatedAutenticator().getApplicationAuthenticator() instanceof LocalApplicationAuthenticator) {
        if (log.isDebugEnabled()) {
            log.debug("Found local authenticator in previous step. Hence setting a local user");
        }
        FederatedAuthenticatorUtil.updateLocalAuthenticatedUserInStepConfig(context, authenticatedUser);
        context.setProperty(SMSOTPConstants.AUTHENTICATION, SMSOTPConstants.BASIC);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Found federated authenticator in previous step. Hence setting a local user");
        }
        FederatedAuthenticatorUtil.updateAuthenticatedUserInStepConfig(context, authenticatedUser);
        context.setProperty(SMSOTPConstants.AUTHENTICATION, SMSOTPConstants.FEDERETOR);
    }
}
Also used : StepConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig) LocalApplicationAuthenticator(org.wso2.carbon.identity.application.authentication.framework.LocalApplicationAuthenticator)

Aggregations

RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)14 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 Map (java.util.Map)6 HashMap (java.util.HashMap)5 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)5 RuntimeService (org.activiti.engine.RuntimeService)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 TreeMap (java.util.TreeMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Response (javax.ws.rs.core.Response)3 JAXBContext (javax.xml.bind.JAXBContext)3 JAXBException (javax.xml.bind.JAXBException)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)3 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3