Search in sources :

Example 36 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project carbon-business-process by wso2.

the class HumanTaskUploadExecutor 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;
    }
    HIUploaderClient uploaderClient = new HIUploaderClient(configurationContext, serverURL + HumanTaskUIConstants.SERVICE_NAMES.HUMANTASK_UPLOADER_SERVICE_NAME, cookie);
    try {
        for (FileItemData fieldData : fileItemsMap.get("humantaskFileName")) {
            String fileName = getFileName(fieldData.getFileItem().getName());
            // Check filename for \ charactors. This cannot be handled at the lower stages.
            if (fileName.matches("(.*[\\\\].*[/].*|.*[/].*[\\\\].*)")) {
                log.error("HumanTask Package Validation Failure: one or many of the following illegal characters are in " + "the package.\n ~!@#$;%^*()+={}[]| \\<>");
                throw new Exception("HumanTask 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 ("humantaskFileName".equals(fieldData.getFileItem().getFieldName())) {
                SaveExtractReturn uploadedFiles = saveAndExtractUploadedFile(fieldData.getFileItem());
                validateHumanTaskPackage(uploadedFiles.extractedFile);
                DataSource dataSource = new FileDataSource(uploadedFiles.zipFile);
                uploaderClient.addUploadedFileItem(new DataHandler(dataSource), fileName, "zip");
            }
        }
        uploaderClient.uploadFileItems();
        String msg = "Your HumanTask package been uploaded successfully. Please refresh this page in a" + " while to see the status of the new package.";
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request, response, getContextRoot(request) + "/" + webContext + HumanTaskUIConstants.PAGES.PACKAGE_LIST_PAGE);
        return true;
    } catch (Exception e) {
        errMsg = "File upload failed :" + e.getMessage();
        log.error(errMsg, e);
        // Removing <, > and </ characters from Error message, in order to provide accurate error message.
        // TODO : FIX this correctly. Identify why latest browsers unable to render HTML encoded string. Eg: &lt; with <
        String encodedErrMsg = errMsg.replace("</", " ").replace(">", " ").replace("<", " ");
        CarbonUIMessage.sendCarbonUIMessage(encodedErrMsg, CarbonUIMessage.ERROR, request, response, getContextRoot(request) + "/" + webContext + HumanTaskUIConstants.PAGES.UPLOAD_PAGE);
    }
    return false;
}
Also used : FileItemData(org.wso2.carbon.utils.FileItemData) ArrayList(java.util.ArrayList) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) CarbonException(org.wso2.carbon.CarbonException) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Aggregations

ArrayList (java.util.ArrayList)12 Attribute (org.wso2.charon3.core.attributes.Attribute)10 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)10 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)10 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)10 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)7 Extension (org.wso2.siddhi.query.api.extension.Extension)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 List (java.util.List)6 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)6 ConfigReader (org.wso2.siddhi.core.util.config.ConfigReader)6 Test (org.testng.annotations.Test)5 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)5 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)5 Event (org.wso2.siddhi.core.event.Event)5 StringConcatAggregatorString (org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString)5 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)5 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)5 Map (java.util.Map)4