org.wso2.carbon.utils.FileItemData Java Examples

The following examples show how to use org.wso2.carbon.utils.FileItemData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: reportUploadExecutor.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
private void init(HttpServletRequest request) throws Exception {
    HttpSession session = request.getSession();
    String serverURL = CarbonUIUtil.getServerURL(session.getServletContext(), session);
    ConfigurationContext configContext =
            (ConfigurationContext) session.getServletContext().getAttribute(CarbonConstants.CONFIGURATION_CONTEXT);
    String cookie = (String) session.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);

    client = new ReportTemplateClient(configContext, serverURL, cookie);
    Map<String, ArrayList<FileItemData>> fileItemsMap = getFileItemsMap();
    formFieldsMap = getFormFieldsMap();

    images = fileItemsMap.get("logo");

    String type = null;
    if(formFieldsMap.get("reportType") != null){
       type = formFieldsMap.get("reportType").get(0);
    }

    if(type == null){
      tableReport= (TableReportDTO)session.getAttribute("table-report");
    }
    else {
      chartReport = (ChartReportDTO)session.getAttribute("chart-report");
    }
}
 
Example #2
Source File: reportUploadExecutor.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
private void handleLogo() throws ReportTemplateAdminReportingExceptionException {
     ReportHeaderInformationDTO header = null;
    if(tableReport != null){
       header = tableReport.getReportHeaderInformation();
    }else if(chartReport != null){
        header = chartReport.getReportHeaderInformation();
    }

    if (images != null && images.size() > 0)
    {
        FileItemData image = images.get(0);
        if(image.getFileItem().getContentType().contains("image/")){
        Resource resource = new Resource();
        resource.setDataHandler(image.getDataHandler());
        resource.setFileName(formFieldsMap.get("imageName").get(0));
        header.setLogo(resource);
        }
        else {
           throw new ReportTemplateAdminReportingExceptionException("Unsupported file format. Only Image can be uploaded here.");
        }
    }
}
 
Example #3
Source File: AbstractFileUploadExecutor.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
protected List<FileItemData> getAllFileItems() {
    Collection<ArrayList<FileItemData>> listCollection = fileItemsMap.get().values();
    List<FileItemData> fileItems = new ArrayList<FileItemData>();
    for (ArrayList<FileItemData> fileItemData : listCollection) {
        fileItems.addAll(fileItemData);
    }
    return fileItems;
}
 
Example #4
Source File: CarbonAppUploadExecutor.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
protected boolean uploadArtifacts(HttpServletRequest request,
                                  HttpServletResponse response,
                                  String uploadDirName,
                                  String[] extensions,
                                  String utilityString)
        throws IOException {

    response.setContentType("text/html; charset=utf-8");

    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);

    String msg;
    Map<String, ArrayList<FileItemData>> fileItemsMap = getFileItemsMap();
    if (fileItemsMap == null || fileItemsMap.isEmpty()) {
        msg = "File uploading failed. No files are specified";
        log.error(msg);
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.ERROR, request,
                response, getContextRoot(request) + "/" + webContext + "/carbonapps/app_upload.jsp");
    }

    //Creating the stub to call the back-end service
    CarbonAppUploaderClient uploaderClient = new CarbonAppUploaderClient(
            configurationContext, serverURL + "CarbonAppUploader", cookie);

    try {
        for (Object o : fileItemsMap.keySet()) {
            String fieldName = (String) o;
            FileItemData fileItemData = fileItemsMap.get(fieldName).get(0);
            String fileName = getFileName(fileItemData.getFileItem().getName());
            uploaderClient.addUploadedFileItem(fileItemData.getDataHandler(), fileName, "jar");
        }

        //Uploading files to back end service
        uploaderClient.uploadFileItems();
        msg = "Your Application has been uploaded successfully. Please refresh this page in a" +
                " while to see the status of the new Application.";
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request,
                response, getContextRoot(request) + "/" + webContext + "/carbonapps/index.jsp" );
        return true;
    } catch (Exception e) {
        msg = "File upload failed.";
        log.error(msg, e);
        CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.ERROR, request,
                response, getContextRoot(request) + "/" + webContext + "/carbonapps/app_upload.jsp");
    }
    return false;
}
 
Example #5
Source File: ToolsFileUploadExecutor.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public boolean execute(HttpServletRequest request, HttpServletResponse response)
        throws CarbonException, IOException {
    PrintWriter out = response.getWriter();
    try {
        List<FileItemData> fileItems = getAllFileItems();
        
        StringBuffer filePathsStrBuffer = new StringBuffer();
        
        for (FileItemData fileItem : fileItems) {
            String uuid = String.valueOf(
                    System.currentTimeMillis() + Math.random());
            String serviceUploadDir =
                    configurationContext
                            .getProperty(ServerConstants.WORK_DIR) +
                            File.separator +
                            "extra" + File
                            .separator +
                            uuid + File.separator;
            File dir = new File(serviceUploadDir);
            if (!dir.exists()) {
                boolean dirCreated = dir.mkdirs();
                if (!dirCreated) {
                	log.error("Error creating dir " + dir.getPath());
                	return false;
                }
            }
            File uploadedFile = new File(dir, uuid);
            FileOutputStream fileOutStream = new FileOutputStream(uploadedFile);
            fileItem.getDataHandler().writeTo(fileOutStream);
            fileOutStream.flush();
            fileOutStream.close();
            response.setContentType("text/plain; charset=utf-8");
            filePathsStrBuffer.append(uploadedFile.getAbsolutePath());
            filePathsStrBuffer.append(',');                
        }

        out.write(filePathsStrBuffer.substring(0, filePathsStrBuffer.length() - 1));
        out.flush();
    } catch (Exception e) {
        log.error("File upload FAILED", e);
        out.write("<script type=\"text/javascript\">" +
                "top.wso2.wsf.Util.alertWarning('File upload FAILED. File may be non-existent or invalid.');" +
                "</script>");
    } finally {
        out.close();
    }
    return true;
}
 
Example #6
Source File: AbstractFileUploadExecutor.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
protected void parseRequest(HttpServletRequest request) throws FileUploadFailedException,
                                                             FileSizeLimitExceededException {
    fileItemsMap.set(new HashMap<String, ArrayList<FileItemData>>());
    formFieldsMap.set(new HashMap<String, ArrayList<String>>());

    ServletRequestContext servletRequestContext = new ServletRequestContext(request);
    boolean isMultipart = ServletFileUpload.isMultipartContent(servletRequestContext);
    Long totalFileSize = 0L;

    if (isMultipart) {

        List items;
        try {
            items = parseRequest(servletRequestContext);
        } catch (FileUploadException e) {
            String msg = "File upload failed";
            log.error(msg, e);
            throw new FileUploadFailedException(msg, e);
        }
        boolean multiItems = false;
        if (items.size() > 1) {
            multiItems = true;
        }

        // Add the uploaded items to the corresponding maps.
        for (Iterator iter = items.iterator(); iter.hasNext();) {
            FileItem item = (FileItem) iter.next();
            String fieldName = item.getFieldName().trim();
            if (item.isFormField()) {
                if (formFieldsMap.get().get(fieldName) == null) {
                    formFieldsMap.get().put(fieldName, new ArrayList<String>());
                }
                try {
                    formFieldsMap.get().get(fieldName).add(new String(item.get(), "UTF-8"));
                } catch (UnsupportedEncodingException ignore) {
                }
            } else {
                String fileName = item.getName();
                if ((fileName == null || fileName.length() == 0) && multiItems) {
                    continue;
                }
                if (fileItemsMap.get().get(fieldName) == null) {
                    fileItemsMap.get().put(fieldName, new ArrayList<FileItemData>());
                }
                totalFileSize += item.getSize();
                if (totalFileSize < totalFileUploadSizeLimit) {
                    fileItemsMap.get().get(fieldName).add(new FileItemData(item));
                } else {
                    throw new FileSizeLimitExceededException(getFileSizeLimit() / 1024 / 1024);
                }
            }
        }
    }
}
 
Example #7
Source File: AbstractFileUploadExecutor.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
/**
 * This is the common method that can be used for Fileupload.
 * extraStoreDirUUID is the name of the javascript that's going to
 * execute on the client side at the secound run.
 *
 * @param request
 * @param response
 * @return Status true/fase.
 * @throws org.apache.commons.fileupload.FileUploadException
 *
 */
protected boolean executeCommon(HttpServletRequest request, HttpServletResponse response)
        throws FileUploadException {

    String serverURL = (String) request.getAttribute(CarbonConstants.SERVER_URL);
    HttpSession session = request.getSession();
    String cookie = (String) session.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);

    PrintWriter out = null;
    try {
        out = response.getWriter();
        FileUploadServiceClient client =
                new FileUploadServiceClient(configurationContext, serverURL, cookie);

        response.setContentType("text/plain; charset=utf-8");
        Set<String> keys = fileItemsMap.get().keySet();
        boolean multiItems = false;
        if (fileItemsMap.get().size() > 1) {
            multiItems = true;
        }
        // Process the uploaded items
        UploadedFileItem[] uploadedFileItems = new UploadedFileItem[fileItemsMap.get().size()];
        Iterator<String> iterator = keys.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            String fieldName = iterator.next();
            String fileName = fileItemsMap.get().get(fieldName).get(0).getFileItem().getName();
            if ((fileName == null || fileName.length() == 0) && multiItems) {
                continue;
            }
            FileItemData fileItemData = fileItemsMap.get().get(fieldName).get(0);
            UploadedFileItem uploadedFileItem = new UploadedFileItem();
            uploadedFileItem.setDataHandler(fileItemData.getDataHandler());
            uploadedFileItem.setFileName(fileName);
            uploadedFileItem.setFileType("");
            uploadedFileItems[i] = uploadedFileItem;
            i++;
        }
        String[] uuidArray = client.uploadFiles(uploadedFileItems);
        StringBuffer uuids = new StringBuffer();
        for (String uuid : uuidArray) {
            uuids.append(uuid).append(",");
        }
        out.write(uuids.toString().substring(0, uuids.length() - 1));
        out.flush();
    } catch (Exception e) {
        String msg = "File upload FAILED. File may be non-existent or invalid.";
        log.error(msg, e);
        throw new FileUploadException(msg, e);
    } finally {
        if (out != null) {
            out.close();
        }
    }
    return true;
}
 
Example #8
Source File: AbstractFileUploadExecutor.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
protected Map<String, ArrayList<FileItemData>> getFileItemsMap() {
    return fileItemsMap.get();
}