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;
}
Aggregations