use of org.wso2.carbon.CarbonException 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.CarbonException 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;
}
use of org.wso2.carbon.CarbonException in project jaggery by wso2.
the class RegistryHostObject method jsFunction_search.
public static Scriptable jsFunction_search(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "search";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof NativeObject)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "json", args[0], false);
}
NativeObject options = (NativeObject) args[0];
CustomSearchParameterBean parameters = new CustomSearchParameterBean();
String path = null;
List<String[]> values = new ArrayList<String[]>();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String val;
for (Object idObj : options.getIds()) {
String id = (String) idObj;
Object value = options.get(id, options);
if (value == null || value instanceof Undefined) {
continue;
}
if ("path".equals(id)) {
path = (String) value;
continue;
}
if ("createdBefore".equals(id) || "createdAfter".equals(id) || "updatedBefore".equals(id) || "updatedAfter".equals(id)) {
long t;
if (value instanceof Number) {
t = ((Number) value).longValue();
} else {
t = Long.parseLong(HostObjectUtil.serializeObject(value));
}
val = new String(dateFormat.format(new Date(t)).getBytes());
} else {
val = HostObjectUtil.serializeObject(value);
}
values.add(new String[] { id, val });
}
parameters.setParameterValues(values.toArray(new String[0][0]));
RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
UserRegistry userRegistry = RegistryHostObjectContext.getRegistryService().getRegistry(registryHostObject.registry.getUserName(), tenantId);
Registry configRegistry = RegistryHostObjectContext.getRegistryService().getConfigSystemRegistry(tenantId);
AdvancedSearchResultsBean resultsBean = registryHostObject.search(configRegistry, userRegistry, parameters);
if (resultsBean.getResourceDataList() == null) {
ScriptableObject error = (ScriptableObject) cx.newObject(thisObj);
error.put("error", error, true);
error.put("description", error, resultsBean.getErrorMessage());
return error;
}
List<ScriptableObject> results = new ArrayList<ScriptableObject>();
for (ResourceData resourceData : resultsBean.getResourceDataList()) {
String resourcePath = resourceData.getResourcePath();
if (path != null && !resourcePath.startsWith(path)) {
continue;
}
ScriptableObject result = (ScriptableObject) cx.newObject(thisObj);
result.put("author", result, resourceData.getAuthorUserName());
result.put("rating", result, resourceData.getAverageRating());
result.put("created", result, resourceData.getCreatedOn().getTime().getTime());
result.put("description", result, resourceData.getDescription());
result.put("name", result, resourceData.getName());
result.put("path", result, resourceData.getResourcePath());
List<ScriptableObject> tags = new ArrayList<ScriptableObject>();
if (resourceData.getTagCounts() != null) {
for (TagCount tagCount : resourceData.getTagCounts()) {
ScriptableObject tag = (ScriptableObject) cx.newObject(thisObj);
tag.put("name", tag, tagCount.getKey());
tag.put("count", tag, tagCount.getValue());
tags.add(tag);
}
}
result.put("tags", result, cx.newArray(thisObj, tags.toArray()));
results.add(result);
}
return cx.newArray(thisObj, results.toArray());
} catch (RegistryException e) {
throw new ScriptException(e);
} catch (CarbonException e) {
throw new ScriptException(e);
}
}
use of org.wso2.carbon.CarbonException in project jaggery by wso2.
the class RegistryHostObject method search.
private AdvancedSearchResultsBean search(Registry configSystemRegistry, UserRegistry registry, CustomSearchParameterBean parameters) throws CarbonException {
RegistryUtils.recordStatistics(parameters);
AdvancedSearchResultsBean metaDataSearchResultsBean;
ResourceData[] contentSearchResourceData;
String[][] tempParameterValues = parameters.getParameterValues();
// Doing a validation of all the values sent
boolean allEmpty = true;
for (String[] tempParameterValue : tempParameterValues) {
if (tempParameterValue[1] != null & tempParameterValue[1].trim().length() > 0) {
allEmpty = false;
// Validating all the dates
if (tempParameterValue[0].equals("createdAfter") || tempParameterValue[0].equals("createdBefore") || tempParameterValue[0].equals("updatedAfter") || tempParameterValue[0].equals("updatedBefore")) {
if (!SearchUtils.validateDateInput(tempParameterValue[1])) {
String message = tempParameterValue[0] + " contains illegal characters";
return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
}
} else if (tempParameterValue[0].equals("mediaType")) {
if (SearchUtils.validateMediaTypeInput(tempParameterValue[1])) {
String message = tempParameterValue[0] + " contains illegal characters";
return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
}
} else if (tempParameterValue[0].equals("content")) {
if (SearchUtils.validateContentInput(tempParameterValue[1])) {
String message = tempParameterValue[0] + " contains illegal characters";
return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
}
} else if (tempParameterValue[0].equals("tags")) {
boolean containsTag = false;
for (String str : tempParameterValue[1].split(",")) {
if (str.trim().length() > 0) {
containsTag = true;
break;
}
}
if (!containsTag) {
String message = tempParameterValue[0] + " contains illegal characters";
return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
}
if (SearchUtils.validateTagsInput(tempParameterValue[1])) {
String message = tempParameterValue[0] + " contains illegal characters";
return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
}
} else {
if (SearchUtils.validatePathInput(tempParameterValue[1])) {
String message = tempParameterValue[0] + " contains illegal characters";
return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
}
}
}
}
if (allEmpty) {
return SearchUtils.getEmptyResultBeanWithErrorMsg("At least one field must be filled");
}
boolean onlyContent = true;
for (String[] tempParameterValue : tempParameterValues) {
if (!tempParameterValue[0].equals("content") && !tempParameterValue[0].equals("leftOp") && !tempParameterValue[0].equals("rightOp") && tempParameterValue[1] != null && tempParameterValue[1].length() > 0) {
onlyContent = false;
break;
}
}
for (String[] tempParameterValue : tempParameterValues) {
if (tempParameterValue[0].equals("content") && tempParameterValue[1] != null && tempParameterValue[1].length() > 0) {
try {
contentSearchResourceData = search(registry, tempParameterValue[1]);
} catch (Exception e) {
metaDataSearchResultsBean = new AdvancedSearchResultsBean();
metaDataSearchResultsBean.setErrorMessage(e.getMessage());
return metaDataSearchResultsBean;
}
// If there are no resource paths returned from content, then there is no point of searching for more
if (contentSearchResourceData != null && contentSearchResourceData.length > 0) {
// Map<String, ResourceData> resourceDataMap = new HashMap<String, ResourceData>();
Map<String, ResourceData> aggregatedMap = new HashMap<String, ResourceData>();
for (ResourceData resourceData : contentSearchResourceData) {
aggregatedMap.put(resourceData.getResourcePath(), resourceData);
}
metaDataSearchResultsBean = AdvancedSearchResultsBeanPopulator.populate(configSystemRegistry, registry, parameters);
if (metaDataSearchResultsBean != null) {
ResourceData[] metaDataResourceData = metaDataSearchResultsBean.getResourceDataList();
if (metaDataResourceData != null && metaDataResourceData.length > 0) {
List<String> invalidKeys = new ArrayList<String>();
for (String key : aggregatedMap.keySet()) {
boolean keyFound = false;
for (ResourceData resourceData : metaDataResourceData) {
if (resourceData.getResourcePath().equals(key)) {
keyFound = true;
break;
}
}
if (!keyFound) {
invalidKeys.add(key);
}
}
for (String invalidKey : invalidKeys) {
aggregatedMap.remove(invalidKey);
}
} else if (!onlyContent) {
aggregatedMap.clear();
}
}
ArrayList<ResourceData> sortedList = new ArrayList<ResourceData>(aggregatedMap.values());
SearchUtils.sortResourceDataList(sortedList);
metaDataSearchResultsBean = new AdvancedSearchResultsBean();
metaDataSearchResultsBean.setResourceDataList(sortedList.toArray(new ResourceData[sortedList.size()]));
return metaDataSearchResultsBean;
} else {
metaDataSearchResultsBean = new AdvancedSearchResultsBean();
metaDataSearchResultsBean.setResourceDataList(contentSearchResourceData);
return metaDataSearchResultsBean;
}
}
}
return AdvancedSearchResultsBeanPopulator.populate(configSystemRegistry, registry, parameters);
}
Aggregations