private void setFile()

in src/main/java/org/apache/sling/servlets/post/impl/helper/SlingFileUploadHandler.java [118:183]


    private void setFile(final Resource parentResource,
            final RequestProperty prop,
            final RequestParameter value,
            final List<Modification> changes, String name,
            final String contentType)
    throws PersistenceException {
        // check type hint. if the type is ok and extends from nt:file,
        // create an nt:file with that type. if it's invalid, drop it and let
        // the parent node type decide.
        boolean createNtFile = parentResource.isResourceType(JcrConstants.NT_FOLDER) || this.jcrSupport.isNodeType(parentResource, JcrConstants.NT_FOLDER);
        String typeHint = prop.getTypeHint();
        if (typeHint != null) {
            Boolean isFileNodeType = this.jcrSupport.isFileNodeType(parentResource.getResourceResolver(), typeHint);
            if ( isFileNodeType == null ) {
                // assuming type not valid.
                createNtFile = false;
                typeHint = null;
            } else {
                createNtFile = isFileNodeType;
            }
        }

        // also create an nt:file if the name contains an extension
        // the rationale is that if the file name is "important" we want
        // an nt:file, and an image name with an extension is probably "important"
        if (!createNtFile && name.indexOf('.') > 0) {
            createNtFile = true;
        }

        // set empty type
        if (typeHint == null) {
            typeHint = createNtFile ? JcrConstants.NT_FILE : JcrConstants.NT_RESOURCE;
        }

        // create nt:file resource if needed
        Resource resParent;
        if (createNtFile) {
            // create nt:file
            resParent = getOrCreateChildResource(parentResource, name, typeHint, changes);
            name = JcrConstants.JCR_CONTENT;
            typeHint = JcrConstants.NT_RESOURCE;
        } else {
            resParent = parentResource;
        }

        // create resource
        final Resource newResource = getOrCreateChildResource(resParent, name, typeHint, changes);
        final ModifiableValueMap mvm = newResource.adaptTo(ModifiableValueMap.class);
        // set properties
        mvm.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
        mvm.put(JcrConstants.JCR_MIMETYPE, contentType);
        changes.add(Modification.onModified(newResource.getPath() + "/" + JcrConstants.JCR_LASTMODIFIED));
        changes.add(Modification.onModified(newResource.getPath() + "/" + JcrConstants.JCR_MIMETYPE));

        try {
            // process chunk upload request separately
            if (prop.isChunkUpload()) {
                processChunk(resParent, newResource, prop, value, changes);
            } else {
                mvm.put(JcrConstants.JCR_DATA, value.getInputStream());
                changes.add(Modification.onModified(newResource.getPath() + "/" + JcrConstants.JCR_DATA));
            }
        } catch (IOException e) {
            throw new PersistenceException("Error while retrieving inputstream from parameter value.", e);
        }
    }