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;
}
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: < with <
String encodedErrMsg = errMsg.replace("</", " ").replace(">", " ").replace("<", " ");
CarbonUIMessage.sendCarbonUIMessage(encodedErrMsg, CarbonUIMessage.ERROR, request, response, getContextRoot(request) + "/" + webContext + HumanTaskUIConstants.PAGES.UPLOAD_PAGE);
}
return false;
}
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;
}
Aggregations