package org.apache.sling.servlets.post.impl.helper;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.servlets.HtmlResponse;
Handles file uploads.
Simple example:
<form action="/home/admin" method="POST" enctype="multipart/form-data">
<input type="file" name="./portrait" />
</form>
this will create a nt:file node below "/home/admin" if the node type of
"admin" is (derived from) nt:folder, a nt:resource node otherwise.
Filename example:
<form action="/home/admin" method="POST" enctype="multipart/form-data">
<input type="file" name="./*" />
</form>
same as above, but uses the filename of the uploaded file as name for the
new node.
Type hint example:
<form action="/home/admin" method="POST" enctype="multipart/form-data">
<input type="file" name="./portrait" />
<input type="hidden" name="./portrait@TypeHint" value="my:file" />
</form>
this will create a new node with the type my:file below admin. if the hinted
type extends from nt:file an intermediate file node is created otherwise
directly a resource node.
Constructs file upload handler
- Parameters:
servletCtx the post processor
this.servletContext = servletCtx;
Uses the file(s) in the request parameter for creation of new nodes.
if the parent node is a nt:folder a new nt:file is created. otherwise
just a nt:resource. if the
name is '*', the filename of
the uploaded file is used.
- Parameters:
parent the parent nodeprop the assembled property info- Throws:
- RepositoryException if an error occurs
RequestParameter value = prop.getValues()[0];
assert !value.isFormField();
if (value.getSize() <= 0) { String name = prop.getName();
name = value.getFileName();
name = name.substring(name.lastIndexOf('/') + 1); name = name.substring(name.lastIndexOf('\\') + 1); name = Text.escapeIllegalJcrChars(name);
boolean createNtFile = parent.isNodeType(NT_FOLDER);
String typeHint = prop.getTypeHint();
NodeTypeManager ntMgr = parent.getSession().getWorkspace().getNodeTypeManager();
NodeType nt = ntMgr.getNodeType(typeHint);
createNtFile = nt.isNodeType(NT_FILE);
} catch (RepositoryException e) { if(!createNtFile && name.indexOf('.') > 0) { typeHint = createNtFile ? NT_FILE : NT_RESOURCE;
if (parent.hasNode(name)) { parent.getNode(name).remove();
parent = parent.addNode(name, typeHint);
response.onCreated(parent.getPath());
Node res = parent.addNode(name, typeHint);
response.onCreated(res.getPath());
String contentType = value.getContentType();
if (contentType != null) { int idx = contentType.indexOf(';'); contentType = contentType.substring(0, idx);
if (contentType == null || contentType.equals("application/octet-stream")) { contentType = this.servletContext.getMimeType(value.getFileName());
if (contentType == null || contentType.equals("application/octet-stream")) { contentType = "application/octet-stream";
res.setProperty(JCR_LASTMODIFIED, Calendar.getInstance()).getPath()
res.setProperty(JCR_MIMETYPE, contentType).getPath()
res.setProperty(JCR_DATA, value.getInputStream()).getPath()
} catch (IOException e) { throw new RepositoryException("Error while retrieving inputstream from parameter value.", e);