use of com.liferay.faces.util.model.UploadedFile in project liferay-faces-alloy by liferay.
the class InputFileBackingBean method deleteUploadedFile.
public void deleteUploadedFile(ActionEvent actionEvent) {
UICommand uiCommand = (UICommand) actionEvent.getComponent();
String fileId = (String) uiCommand.getValue();
try {
List<UploadedFile> uploadedFiles = inputFileModelBean.getUploadedFiles();
UploadedFile uploadedFileToDelete = null;
for (UploadedFile uploadedFile : uploadedFiles) {
if (uploadedFile.getId().equals(fileId)) {
uploadedFileToDelete = uploadedFile;
break;
}
}
if (uploadedFileToDelete != null) {
uploadedFileToDelete.delete();
uploadedFiles.remove(uploadedFileToDelete);
logger.debug("Deleted file=[{0}]", uploadedFileToDelete.getName());
}
} catch (Exception e) {
logger.error(e);
}
}
use of com.liferay.faces.util.model.UploadedFile in project liferay-faces-alloy by liferay.
the class InputFileRenderer method decode.
@Override
public void decode(FacesContext facesContext, UIComponent uiComponent) {
InputFile inputFile = (InputFile) uiComponent;
Map<String, List<UploadedFile>> uploadedFileMap = getUploadedFileMap(facesContext, inputFile.getLocation());
if (uploadedFileMap != null) {
String clientId = uiComponent.getClientId(facesContext);
List<UploadedFile> uploadedFiles = uploadedFileMap.get(clientId);
if ((uploadedFiles != null) && (uploadedFiles.size() > 0)) {
inputFile.setSubmittedValue(uploadedFiles);
// ActionListener.
for (UploadedFile uploadedFile : uploadedFiles) {
FileUploadEvent fileUploadEvent = new FileUploadEvent(uiComponent, uploadedFile);
uiComponent.queueEvent(fileUploadEvent);
}
} else // FACES-3136: Ensure that the required attribute is enforced.
{
inputFile.setSubmittedValue(Collections.emptyList());
}
} else // FACES-3136: Ensure that the required attribute is enforced.
{
inputFile.setSubmittedValue(Collections.emptyList());
}
}
use of com.liferay.faces.util.model.UploadedFile in project liferay-faces-alloy by liferay.
the class InputFile method validateValue.
@Override
protected void validateValue(FacesContext facesContext, Object value) {
super.validateValue(facesContext, value);
if (isValid()) {
Long maxFileSize = getMaxFileSize();
String contentTypeSet = getContentTypes();
if ((maxFileSize != null) || (contentTypeSet != null)) {
Locale locale = facesContext.getViewRoot().getLocale();
ExternalContext externalContext = facesContext.getExternalContext();
I18n i18n = I18nFactory.getI18nInstance(externalContext);
String clientId = getClientId(facesContext);
@SuppressWarnings("unchecked") List<UploadedFile> uploadedFiles = (List<UploadedFile>) value;
for (UploadedFile uploadedFile : uploadedFiles) {
if ((maxFileSize != null) && (maxFileSize >= 0) && (uploadedFile.getSize() > maxFileSize)) {
String errorMessage = i18n.getMessage(facesContext, locale, "file-x-is-y-bytes-but-may-not-exceed-z-bytes", uploadedFile.getName(), uploadedFile.getSize(), maxFileSize);
handleInvalidFile(facesContext, clientId, uploadedFile, errorMessage);
}
String contentType = uploadedFile.getContentType();
if ((contentType == null) || ((contentTypeSet != null) && !contentTypeSet.contains(contentType))) {
String errorMessage = i18n.getMessage(facesContext, locale, "file-x-has-an-invalid-content-type-y", uploadedFile.getName(), contentType);
handleInvalidFile(facesContext, clientId, uploadedFile, errorMessage);
}
}
}
}
}
use of com.liferay.faces.util.model.UploadedFile 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;
}
use of com.liferay.faces.util.model.UploadedFile in project liferay-faces-bridge-impl by liferay.
the class UploadedFileWrapper method getFile.
/**
* Since the PrimeFaces UploadedFile interface does not provide a method for deleting the file, Liferay Faces Bridge
* automatically deletes it when the wrappedUploadedFile.getContents() method is called.
*/
protected File getFile(String uniqueFolderName) {
File file = null;
try {
File tempFolder = new File(System.getProperty("java.io.tmpdir"));
File uniqueFolder = new File(tempFolder, uniqueFolderName);
if (!uniqueFolder.exists()) {
uniqueFolder.mkdirs();
}
String fileNamePrefix = "attachment" + getId();
String fileNameSuffix = ".dat";
file = File.createTempFile(fileNamePrefix, fileNameSuffix, uniqueFolder);
OutputStream outputStream = new FileOutputStream(file);
outputStream.write(wrappedUploadedFile.getContent());
outputStream.close();
// Temporary file maintained by PrimeFaces is automatically deleted. See JavaDoc comments above.
} catch (Exception e) {
logger.error(e);
}
return file;
}
Aggregations