Search in sources :

Example 1 with PortletParameters

use of javax.portlet.PortletParameters in project liferay-faces-bridge-impl by liferay.

the class MultiPartFormDataProcessorCompatImpl method iterateOver.

/* package-private */
Map<String, List<UploadedFile>> iterateOver(ClientDataRequest clientDataRequest, PortletConfig portletConfig, FacesRequestParameterMap facesRequestParameterMap, File uploadedFilesPath) {
    // Parse the request parameters and save all uploaded files in a map.
    Map<String, List<UploadedFile>> uploadedFileMap = new HashMap<>();
    // Determine the max file upload size threshold (in bytes).
    long defaultMaxFileSize = PortletConfigParam.UploadedFileMaxSize.getDefaultLongValue();
    long uploadedFileMaxSize = PortletConfigParam.UploadedFileMaxSize.getLongValue(portletConfig);
    if (defaultMaxFileSize != uploadedFileMaxSize) {
        logger.warn("Ignoring init param {0}=[{1}] since it has been replaced by <multipart-config> in web.xml", PortletConfigParam.UploadedFileMaxSize.getName(), uploadedFileMaxSize);
    }
    // FACES-271: Include name+value pairs found in the ActionRequest/ResourceRequest.
    PortletParameters portletParameters;
    boolean actionPhase;
    if (clientDataRequest instanceof ActionRequest) {
        actionPhase = true;
        ActionRequest actionRequest = (ActionRequest) clientDataRequest;
        portletParameters = actionRequest.getActionParameters();
    } else {
        actionPhase = false;
        ResourceRequest resourceRequest = (ResourceRequest) clientDataRequest;
        portletParameters = resourceRequest.getResourceParameters();
    }
    Set<String> fullyQualifiedParameterNames = new HashSet<>(Collections.list(clientDataRequest.getParameterNames()));
    Set<String> portletParameterNames = portletParameters.getNames();
    for (String parameterName : portletParameterNames) {
        // the portlet namespace.
        if (!fullyQualifiedParameterNames.contains(parameterName)) {
            String fullyQualifiedParameterName = facesRequestParameterMap.getNamespace() + parameterName;
            if (fullyQualifiedParameterNames.contains(fullyQualifiedParameterName)) {
                parameterName = fullyQualifiedParameterName;
            }
        }
        String[] parameterValues = portletParameters.getValues(parameterName);
        if (parameterValues.length > 0) {
            for (String parameterValue : parameterValues) {
                facesRequestParameterMap.addValue(parameterName, parameterValue);
                if (actionPhase) {
                    logger.debug("Added action parameter name={0} value={1}", parameterName, parameterValue);
                } else {
                    logger.debug("Added resource parameter name={0} value={1}", parameterName, parameterValue);
                }
            }
        }
    }
    UploadedFileFactory uploadedFileFactory = (UploadedFileFactory) BridgeFactoryFinder.getFactory(portletConfig.getPortletContext(), UploadedFileFactory.class);
    // Begin parsing the request for file parts:
    try {
        Collection<Part> parts = clientDataRequest.getParts();
        List<String> fileUploadFieldNames = new ArrayList<String>();
        int totalFiles = 0;
        // For each field found in the request:
        for (Part part : parts) {
            String fieldName = part.getName();
            fileUploadFieldNames.add(fieldName);
            try {
                totalFiles++;
                String characterEncoding = clientDataRequest.getCharacterEncoding();
                String contentDispositionHeader = part.getHeader("content-disposition");
                String fileName = getValidFileName(contentDispositionHeader);
                // If the current field is a simple form-field, then save the form field value in the map.
                if ((fileName != null) && (fileName.length() > 0)) {
                    File uploadedFilePath = new File(uploadedFilesPath, fileName);
                    String uploadedFilePathAbsolutePath = uploadedFilePath.getAbsolutePath();
                    part.write(uploadedFilePathAbsolutePath);
                    // If the copy was successful, then
                    if (uploadedFilePath.exists()) {
                        // If present, build up a map of headers. According to Hypertext Transfer Protocol --
                        // HTTP/1.1 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2), header names
                        // are case-insensitive. In order to support this, use a TreeMap with case insensitive
                        // keys.
                        Map<String, List<String>> headersMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
                        Collection<String> headerNames = part.getHeaderNames();
                        for (String headerName : headerNames) {
                            Collection<String> headerValues = part.getHeaders(headerName);
                            List<String> headerValueList = new ArrayList<>();
                            for (String headerValue : headerValues) {
                                headerValueList.add(headerValue);
                            }
                            headersMap.put(headerName, headerValueList);
                        }
                        // Put a valid UploadedFile instance into the map that contains all of the
                        // uploaded file's attributes, along with a successful status.
                        Map<String, Object> attributeMap = new HashMap<>();
                        String id = Long.toString(((long) hashCode()) + System.currentTimeMillis());
                        com.liferay.faces.util.model.UploadedFile uploadedFile = uploadedFileFactory.getUploadedFile(uploadedFilePathAbsolutePath, attributeMap, characterEncoding, part.getContentType(), headersMap, id, null, fileName, part.getSize(), com.liferay.faces.util.model.UploadedFile.Status.FILE_SAVED);
                        facesRequestParameterMap.addValue(fieldName, uploadedFilePathAbsolutePath);
                        addUploadedFile(uploadedFileMap, fieldName, uploadedFile);
                        logger.debug("Received uploaded file fieldName=[{0}] fileName=[{1}]", fieldName, fileName);
                    } else {
                        if (fileName.trim().length() > 0) {
                            Exception e = new IOException("Failed to copy the stream of uploaded file=[" + fileName + "] to a temporary file (possibly a zero-length uploaded file)");
                            com.liferay.faces.util.model.UploadedFile uploadedFile = uploadedFileFactory.getUploadedFile(e);
                            addUploadedFile(uploadedFileMap, fieldName, uploadedFile);
                        }
                    }
                }
            } catch (Exception e) {
                logger.error(e);
                com.liferay.faces.util.model.UploadedFile uploadedFile = uploadedFileFactory.getUploadedFile(e);
                String totalFilesfieldName = Integer.toString(totalFiles);
                addUploadedFile(uploadedFileMap, totalFilesfieldName, uploadedFile);
            }
        }
        for (String fileUploadFieldName : fileUploadFieldNames) {
            // value.
            if (!uploadedFileMap.containsKey(fileUploadFieldName)) {
                uploadedFileMap.put(fileUploadFieldName, Collections.<UploadedFile>emptyList());
            }
        }
    }// the map so that the developer can have some idea that something went wrong.
     catch (Exception e) {
        logger.error(e);
        com.liferay.faces.util.model.UploadedFile uploadedFile = uploadedFileFactory.getUploadedFile(e);
        addUploadedFile(uploadedFileMap, "unknown", uploadedFile);
    }
    return uploadedFileMap;
}
Also used : HashMap(java.util.HashMap) UploadedFile(com.liferay.faces.util.model.UploadedFile) ArrayList(java.util.ArrayList) PortletParameters(javax.portlet.PortletParameters) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) UploadedFileFactory(com.liferay.faces.util.model.UploadedFileFactory) IOException(java.io.IOException) TreeMap(java.util.TreeMap) IOException(java.io.IOException) UploadedFile(com.liferay.faces.util.model.UploadedFile) ActionRequest(javax.portlet.ActionRequest) Part(javax.servlet.http.Part) ResourceRequest(javax.portlet.ResourceRequest) UploadedFile(com.liferay.faces.util.model.UploadedFile) File(java.io.File)

Aggregations

UploadedFile (com.liferay.faces.util.model.UploadedFile)1 UploadedFileFactory (com.liferay.faces.util.model.UploadedFileFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 ActionRequest (javax.portlet.ActionRequest)1 PortletParameters (javax.portlet.PortletParameters)1 ResourceRequest (javax.portlet.ResourceRequest)1 Part (javax.servlet.http.Part)1