-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NXP-15638: handle Part conversion to serializable Blob for state mana…
…gement in case of validation error, or within lists, and avoid crash on file submit inside an ajax request (display a specific validation error instead) + add non regression test for validation error use case
- Loading branch information
1 parent
c76165d
commit a6490f2
Showing
8 changed files
with
265 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...orm-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/component/file/NXFileRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the GNU Lesser General Public License | ||
* (LGPL) version 2.1 which accompanies this distribution, and is available at | ||
* http://www.gnu.org/licenses/lgpl-2.1.html | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* Contributors: | ||
* Anahide Tchertchian | ||
*/ | ||
package org.nuxeo.ecm.platform.ui.web.component.file; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import javax.faces.FacesException; | ||
import javax.faces.component.UIComponent; | ||
import javax.faces.context.ExternalContext; | ||
import javax.faces.context.FacesContext; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.Part; | ||
|
||
import org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException; | ||
import org.nuxeo.ecm.core.api.impl.blob.StringBlob; | ||
import org.nuxeo.ecm.platform.ui.web.util.files.FileUtils; | ||
|
||
import com.sun.faces.renderkit.html_basic.FileRenderer; | ||
|
||
/** | ||
* Renderer for file input. | ||
* <p> | ||
* Overrides the base JSF renderer for files to ignore error when submitting file inside a non multipart form. | ||
* <p> | ||
* Component {@link UIInputFile} will handle error message management in UI, if validation phase occurs. | ||
* | ||
* @since 7.1 | ||
*/ | ||
public class NXFileRenderer extends FileRenderer { | ||
|
||
public static final String RENDERER_TYPE = "javax.faces.NXFile"; | ||
|
||
@Override | ||
public void decode(FacesContext context, UIComponent component) { | ||
|
||
rendererParamsNotNull(context, component); | ||
|
||
if (!shouldDecode(component)) { | ||
return; | ||
} | ||
|
||
String clientId = decodeBehaviors(context, component); | ||
|
||
if (clientId == null) { | ||
clientId = component.getClientId(context); | ||
} | ||
|
||
assert (clientId != null); | ||
ExternalContext externalContext = context.getExternalContext(); | ||
Map<String, String> requestMap = externalContext.getRequestParameterMap(); | ||
|
||
if (requestMap.containsKey(clientId)) { | ||
setSubmittedValue(component, requestMap.get(clientId)); | ||
} | ||
|
||
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); | ||
try { | ||
Collection<Part> parts = request.getParts(); | ||
for (Part cur : parts) { | ||
if (clientId.equals(cur.getName())) { | ||
// Nuxeo patch: transform into serializable blob right away, and do not set component transient | ||
// component.setTransient(true); | ||
// setSubmittedValue(component, cur); | ||
String filename = FileUtils.retrieveFilename(cur); | ||
String mimetype = cur.getContentType(); | ||
setSubmittedValue(component, | ||
FileUtils.createSerializableBlob(cur.getInputStream(), filename, mimetype)); | ||
} | ||
} | ||
} catch (IOException ioe) { | ||
throw new FacesException(ioe); | ||
} catch (ServletException se) { | ||
Throwable cause = se.getCause(); | ||
// Nuxeo specific error management | ||
if ((cause instanceof InvalidContentTypeException) | ||
|| (cause != null && cause.getClass().getName().contains("InvalidContentTypeException"))) { | ||
setSubmittedValue(component, new StringBlob("")); | ||
} else { | ||
throw new FacesException(se); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.