Search in sources :

Example 6 with FileItemData

use of org.wso2.carbon.utils.FileItemData in project carbon-business-process by wso2.

the class AttachmentUploadExecutor method execute.

@Override
public boolean execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
    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<String>> formFieldsMap = getFormFieldsMap();
    String taskID = null;
    String redirect = null;
    try {
        if (formFieldsMap.get("taskID") != null) {
            taskID = formFieldsMap.get("taskID").get(0);
        }
        if (formFieldsMap.get("redirect") != null) {
            redirect = formFieldsMap.get("redirect").get(0);
        }
        ArrayList<FileItemData> fileItemsMap = getFileItemsMap().get("fileToUpload");
        FileItemData fileToBeUpload = fileItemsMap.get(0);
        AttachmentUploadClient attachmentUploadClient = new AttachmentUploadClient(configurationContext, serverURL, cookie);
        HumanTaskClientAPIServiceClient taskOperationClient = new HumanTaskClientAPIServiceClient(cookie, serverURL, configurationContext);
        response.setContentType("text/html; charset=utf-8");
        String attachmentID = attachmentUploadClient.addUploadedFileItem(fileToBeUpload);
        String attachmentName = fileToBeUpload.getDataHandler().getName();
        String contentType = fileToBeUpload.getDataHandler().getContentType();
        boolean isAdded = taskOperationClient.addAttachment(taskID, attachmentName, contentType, attachmentID);
        String msg = "Your attachment has been uploaded successfully.";
        if (!isAdded) {
            throw new Exception("Attachment was added successfully with id:" + attachmentID + ". But the task " + "with id: " + taskID + " was not associated with it correctly.");
        } else {
            if (redirect != null && redirect.contains("humantask/basic_task_view.jsp")) {
                // redirection is going to the carbon mgt console
                CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request, response, getContextRoot(request) + "/" + webContext + "/" + redirect);
            } else if (redirect != null) {
                // redirection exists, not to carbon mgt console
                out.write(msg);
                response.sendRedirect(redirect);
            } else {
                CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request);
            }
            return true;
        }
    } catch (Exception ex) {
        String errMsg = "File upload failed. Reason :" + ex.getLocalizedMessage();
        log.error(errMsg, ex);
        if (redirect != null) {
            CarbonUIMessage.sendCarbonUIMessage(errMsg, CarbonUIMessage.ERROR, request, response, getContextRoot(request) + "/" + webContext + "/" + redirect);
        } else {
            CarbonUIMessage.sendCarbonUIMessage(errMsg, CarbonUIMessage.ERROR, request);
        }
    }
    return false;
}
Also used : HumanTaskClientAPIServiceClient(org.wso2.carbon.humantask.ui.clients.HumanTaskClientAPIServiceClient) FileItemData(org.wso2.carbon.utils.FileItemData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 7 with FileItemData

use of org.wso2.carbon.utils.FileItemData 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)

Example 8 with FileItemData

use of org.wso2.carbon.utils.FileItemData in project jaggery by wso2.

the class WarFileUploadExecutor method execute.

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings({ "FILE_UPLOAD_FILENAME", "FILE_UPLOAD_FILENAME" })
public boolean execute(HttpServletRequest request, HttpServletResponse response) throws CarbonException, IOException {
    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 = "Jaggery application uploading failed. No file specified.";
        log.error(msg);
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.ERROR, request, response, "../" + webContext + "/jaggeryapp-mgt/uploadjaggeryapp.jsp");
        return false;
    }
    JaggeryAdminClient client = new JaggeryAdminClient(cookie, serverURL, configurationContext, request.getLocale());
    String msg;
    List<FileItemData> tempDataList = fileItemsMap.get("warFileName");
    if (tempDataList == null) {
        return false;
    }
    List<WebappUploadData> webappUploadDataList = new ArrayList<WebappUploadData>();
    try {
        for (FileItemData filedata : tempDataList) {
            WebappUploadData tempData = new WebappUploadData();
            checkServiceFileExtensionValidity(getFileName(filedata.getFileItem().getName()), ALLOWED_FILE_EXTENSIONS);
            tempData.setFileName(getFileName(filedata.getFileItem().getName()));
            tempData.setDataHandler(filedata.getDataHandler());
            webappUploadDataList.add(tempData);
        }
        client.uploadWebapp(webappUploadDataList.toArray(new WebappUploadData[webappUploadDataList.size()]));
        response.setContentType("text/html; charset=utf-8");
        if (tempDataList.size() > 1) {
            msg = "Jaggery applications have been uploaded " + "successfully. Please refresh this page in a while to see " + "the status of the running Jaggery apps.";
        } else {
            msg = "Jaggery application has been uploaded " + "successfully. Please refresh this page in a while to see " + "the status of the running Jaggery apps.";
        }
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request, response, "../" + webContext + "/webapp-list/index.jsp");
        return true;
    } catch (Exception e) {
        msg = "Jaggery application upload failed. " + e.getMessage();
        log.error(msg, e);
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.ERROR, request, response, "../" + webContext + "/jaggeryapp-mgt/uploadjaggeryapp.jsp");
    }
    return false;
}
Also used : FileItemData(org.wso2.carbon.utils.FileItemData) ArrayList(java.util.ArrayList) WebappUploadData(org.jaggeryjs.jaggery.app.mgt.stub.types.carbon.WebappUploadData) IOException(java.io.IOException) CarbonException(org.wso2.carbon.CarbonException)

Aggregations

ArrayList (java.util.ArrayList)6 FileItemData (org.wso2.carbon.utils.FileItemData)6 IOException (java.io.IOException)5 DataHandler (javax.activation.DataHandler)5 PrintWriter (java.io.PrintWriter)4 CarbonException (org.wso2.carbon.CarbonException)4 DataSource (javax.activation.DataSource)3 FileDataSource (javax.activation.FileDataSource)3 File (java.io.File)2 TAttachment (org.wso2.carbon.attachment.mgt.stub.types.TAttachment)2 Calendar (java.util.Calendar)1 Date (java.util.Date)1 WebappUploadData (org.jaggeryjs.jaggery.app.mgt.stub.types.carbon.WebappUploadData)1 HumanTaskClientAPIServiceClient (org.wso2.carbon.humantask.ui.clients.HumanTaskClientAPIServiceClient)1