Search in sources :

Example 26 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class HtmlDocTest method testFunctionsWithoutStructBindings.

@Test(description = "One function without a struct bindings in a package should not be grouped together with the" + "structs shown in the constructs")
public void testFunctionsWithoutStructBindings() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "public function hello(){} " + "public struct Message { string message; int id;}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 2);
    Assert.assertEquals(page.constructs.get(0).name, "Message");
    Assert.assertEquals(page.constructs.get(1).name, "hello");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) Test(org.testng.annotations.Test)

Example 27 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.

the class HtmlDocTest method testFunctionsWithStructBindings.

@Test(description = "One function with a struct bindings in a package should be grouped together shown in the " + "constructs")
public void testFunctionsWithStructBindings() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "public function <Message m>hello(){} " + "public struct Message { string message; int id;}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 1);
    Assert.assertEquals(page.constructs.get(0).name, "Message");
    Assert.assertEquals(page.constructs.get(0).children.get(0).name, "hello");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) Test(org.testng.annotations.Test)

Example 28 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project carbon-business-process by wso2.

the class XMLDocument method set.

/**
 * Function to set/replace/update an object (String / Element) to matching the xPath provided. (In case new element
 * is added, this api will clone it and merge the new node to the target location pointed by xPath and return the new cloned node)
 *
 * @param xPathStr xPath to the location object need to set
 * @param obj      String or Node
 * @return returns the node get updated when the set object is String, or returns newly added Node in case object is Element
 * @throws XPathExpressionException If expression cannot be evaluated
 * @throws BPMNXmlException         is thrown due to : Provided XPath and object does not match, provided object is not a Node or String
 *                                  result is NodeList, not a Text node or Element
 */
public Node set(String xPathStr, Object obj) throws XPathExpressionException, BPMNXmlException {
    NodeList evalResult = (NodeList) Utils.evaluateXPath(this.doc, xPathStr, XPathConstants.NODESET);
    if (evalResult.getLength() == 1) {
        Node targetNode = evalResult.item(0);
        if (obj instanceof String && targetNode instanceof Text) {
            // if string is provided, assume that user
            // need to replace the node value
            targetNode.setNodeValue((String) obj);
            // return updated Text Node
            return targetNode;
        } else if ((obj instanceof Integer || obj instanceof Byte || obj instanceof Character || obj instanceof Short || obj instanceof Long || obj instanceof Float || obj instanceof Double || obj instanceof Boolean) && targetNode instanceof Text) {
            // need to replace the node value
            targetNode.setNodeValue(obj.toString());
            // return updated Text Node
            return targetNode;
        } else if (obj instanceof Element && targetNode instanceof Element && targetNode.getParentNode() != null) {
            // if the user provides Node object,
            // assume that need to replace the target node
            Node targetParent = targetNode.getParentNode();
            Node nextSibling = targetNode.getNextSibling();
            // remove the target node
            targetParent.removeChild(targetNode);
            // add new node
            Node newNode = doc.importNode((Node) obj, true);
            if (nextSibling != null) {
                // If next sibling exists we have to put the new node before it
                targetParent.insertBefore(newNode, nextSibling);
            } else {
                targetParent.appendChild(newNode);
            }
            // return new node
            return newNode;
        } else {
            // provided XPath and object to set does not match
            throw new BPMNXmlException("Provided XPath and provided object does not match");
        }
    } else if (evalResult.getLength() > 0) {
        throw new BPMNXmlException("Error in provided xPath. Evaluation result is NodeList, not a Text node or Element");
    } else {
        throw new BPMNXmlException("Error in provided xPath. Evaluation result is not a Text node or Element");
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) BPMNXmlException(org.wso2.carbon.bpmn.core.types.datatypes.xml.BPMNXmlException)

Example 29 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project carbon-business-process by wso2.

the class BPELUploadExecutor method execute.

public boolean execute(HttpServletRequest request, HttpServletResponse response) throws CarbonException, IOException {
    String errMsg;
    response.setContentType("text/html; charset=utf-8");
    PrintWriter out = response.getWriter();
    String webContext = (String) request.getAttribute(CarbonConstants.WEB_CONTEXT);
    String serverURL = (String) request.getAttribute(CarbonConstants.SERVER_URL);
    String cookie = (String) request.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    Map<String, ArrayList<FileItemData>> fileItemsMap = getFileItemsMap();
    if (fileItemsMap == null || fileItemsMap.isEmpty()) {
        String msg = "File uploading failed.";
        log.error(msg);
        out.write("<textarea>" + "(function(){i18n.fileUplodedFailed();})();" + "</textarea>");
        return true;
    }
    BPELUploaderClient uploaderClient = new BPELUploaderClient(configurationContext, serverURL + "BPELUploader", cookie);
    SaveExtractReturn uploadedFiles = null;
    ArrayList<String> extractedFiles = new ArrayList<String>();
    try {
        for (FileItemData fieldData : fileItemsMap.get("bpelFileName")) {
            String fileName = getFileName(fieldData.getFileItem().getName());
            // Check filename for \ charactors. This cannot be handled at the lower stages.
            if (fileName.matches("(.*[\\\\].*[/].*|.*[/].*[\\\\].*)")) {
                log.error("BPEL Package Validation Failure: one or many of the following illegal characters are " + "in " + "the package.\n ~!@#$;%^*()+={}[]| \\<>");
                throw new Exception("BPEL Package Validation Failure: one or many of the following illegal " + "characters " + "are in the package. ~!@#$;%^*()+={}[]| \\<>");
            }
            // Check file extension.
            checkServiceFileExtensionValidity(fileName, ALLOWED_FILE_EXTENSIONS);
            if (fileName.lastIndexOf('\\') != -1) {
                int indexOfColon = fileName.lastIndexOf('\\') + 1;
                fileName = fileName.substring(indexOfColon, fileName.length());
            }
            if (fieldData.getFileItem().getFieldName().equals("bpelFileName")) {
                uploadedFiles = saveAndExtractUploadedFile(fieldData.getFileItem());
                extractedFiles.add(uploadedFiles.extractedFile);
                validateBPELPackage(uploadedFiles.extractedFile);
                DataSource dataSource = new FileDataSource(uploadedFiles.zipFile);
                uploaderClient.addUploadedFileItem(new DataHandler(dataSource), fileName, "zip");
            }
        }
        uploaderClient.uploadFileItems();
        String msg = "Your BPEL package been uploaded successfully. Please refresh this page in a" + " while to see the status of the new process.";
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request, response, getContextRoot(request) + "/" + webContext + "/bpel/process_list.jsp");
        return true;
    } catch (Exception e) {
        errMsg = "File upload failed :" + e.getMessage();
        log.error(errMsg, e);
        CarbonUIMessage.sendCarbonUIMessage(errMsg, CarbonUIMessage.ERROR, request, response, getContextRoot(request) + "/" + webContext + "/bpel/upload_bpel.jsp");
    } finally {
        for (String s : extractedFiles) {
            File extractedFile = new File(s);
            if (log.isDebugEnabled()) {
                log.debug("Cleaning temporarily extracted BPEL artifacts in " + extractedFile.getParent());
            }
            try {
                FileUtils.cleanDirectory(new File(extractedFile.getParent()));
            } catch (IOException ex) {
                log.warn("Failed to clean temporary extractedFile.", ex);
            }
        }
    }
    return false;
}
Also used : FileItemData(org.wso2.carbon.utils.FileItemData) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) IOException(java.io.IOException) CarbonException(org.wso2.carbon.CarbonException) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) FileDataSource(javax.activation.FileDataSource) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 30 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project carbon-business-process by wso2.

the class BPMNUploadExecutor method execute.

@Override
public boolean execute(HttpServletRequest request, HttpServletResponse response) throws CarbonException, IOException {
    String errMsg;
    response.setContentType("text/html; charset=utf-8");
    PrintWriter out = response.getWriter();
    String webContext = (String) request.getAttribute(CarbonConstants.WEB_CONTEXT);
    String serverURL = (String) request.getAttribute(CarbonConstants.SERVER_URL);
    String cookie = (String) request.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    Map<String, ArrayList<FileItemData>> fileItemsMap = getFileItemsMap();
    if (fileItemsMap == null || fileItemsMap.isEmpty()) {
        String msg = "File uploading failed.";
        log.error(msg);
        out.write("<textarea>" + "(function(){i18n.fileUplodedFailed();})();" + "</textarea>");
        return true;
    }
    BPMNUploaderClient uploaderClient = new BPMNUploaderClient(configurationContext, serverURL + "BPMNUploaderService", cookie);
    File uploadedTempFile;
    try {
        for (FileItemData fileData : fileItemsMap.get("bpmnFileName")) {
            String fileName = getFileName(fileData.getFileItem().getName());
            // Check filename for \ charactors. This cannot be handled at the lower stages.
            if (fileName.matches("(.*[\\\\].*[/].*|.*[/].*[\\\\].*)")) {
                log.error("BPMN Package Validation Failure: one or more of the following illegal characters are in " + "the package.\n ~!@#$;%^*()+={}[]| \\<>");
                throw new Exception("BPMN Package Validation Failure: one or more of the following illegal characters " + "are in the package. ~!@#$;%^*()+={}[]| \\<>");
            }
            // Check file extension.
            checkServiceFileExtensionValidity(fileName, ALLOWED_FILE_EXTENSIONS);
            if (fileName.lastIndexOf('\\') != -1) {
                int indexOfColon = fileName.lastIndexOf('\\') + 1;
                fileName = fileName.substring(indexOfColon, fileName.length());
            }
            if (fileData.getFileItem().getFieldName().equals("bpmnFileName")) {
                uploadedTempFile = new File(CarbonUtils.getTmpDir(), fileName);
                fileData.getFileItem().write(uploadedTempFile);
                DataSource dataSource = new FileDataSource(uploadedTempFile);
                uploaderClient.addUploadedFileItem(new DataHandler(dataSource), fileName, "bar");
            }
        }
        uploaderClient.uploadFileItems();
        String msg = "Your BPMN package has been uploaded successfully. Please refresh this page in a" + " while to see the status of the new process.";
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request, response, getContextRoot(request) + "/" + webContext + "/bpmn/process_list_view.jsp");
        return true;
    } catch (Exception e) {
        errMsg = "File upload failed :" + e.getMessage();
        log.error(errMsg, e);
        CarbonUIMessage.sendCarbonUIMessage(errMsg, CarbonUIMessage.ERROR, request, response, getContextRoot(request) + "/" + webContext + "/bpmn/process_list_view.jsp");
    }
    return false;
}
Also used : FileItemData(org.wso2.carbon.utils.FileItemData) ArrayList(java.util.ArrayList) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) File(java.io.File) IOException(java.io.IOException) CarbonException(org.wso2.carbon.CarbonException) PrintWriter(java.io.PrintWriter) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Aggregations

Test (org.testng.annotations.Test)94 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)49 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)44 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)39 ArrayList (java.util.ArrayList)37 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)35 BString (org.ballerinalang.model.values.BString)30 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)29 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)25 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)20 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)20 BJSON (org.ballerinalang.model.values.BJSON)19 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)19 List (java.util.List)18 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)18 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)16 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)15 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)14 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)14