}
@Override
protected void doRun(SlingHttpServletRequest request, PostResponse response, final List<Modification> changes)
throws RepositoryException {
ContentImporter importer = contentImporter;
if (importer == null) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Missing content importer for import");
return;
}
Map<String, RequestProperty> reqProperties = collectContent(request,
response);
VersioningConfiguration versioningConfiguration = getVersioningConfiguration(request);
// do not change order unless you have a very good reason.
Session session = request.getResourceResolver().adaptTo(Session.class);
try {
processCreate(request.getResourceResolver(), reqProperties, response, changes, versioningConfiguration);
} catch ( final PersistenceException pe) {
if ( pe.getCause() instanceof RepositoryException ) {
throw (RepositoryException)pe.getCause();
}
throw new RepositoryException(pe);
}
String path = response.getPath();
Node node = null;
try {
node = (Node) session.getItem(path);
} catch ( RepositoryException e ) {
log.warn(e.getMessage(),e);
// was not able to resolve the node
} catch ( ClassCastException e) {
log.warn(e.getMessage(),e);
// it was not a node
}
if (node == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND,
"Missing target node " + path + " for import");
return;
}
String contentType = getRequestParamAsString(request, SlingPostConstants.RP_CONTENT_TYPE);
if (contentType == null) {
response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED,
"Required :contentType parameter is missing");
return;
}
//import options passed as request parameters.
final boolean replace = "true".equalsIgnoreCase(getRequestParamAsString(request, SlingPostConstants.RP_REPLACE));
final boolean replaceProperties = "true".equalsIgnoreCase(getRequestParamAsString(request, SlingPostConstants.RP_REPLACE_PROPERTIES));
final boolean checkin = "true".equalsIgnoreCase(getRequestParamAsString(request, SlingPostConstants.RP_CHECKIN));
final boolean autoCheckout = "true".equalsIgnoreCase(getRequestParamAsString(request, SlingPostConstants.RP_AUTO_CHECKOUT));
String basePath = getItemPath(request);
basePath = removeAndValidateWorkspace(basePath, request.getResourceResolver().adaptTo(Session.class));
if (basePath.endsWith("/")) {
//remove the trailing slash
basePath = basePath.substring(0, basePath.length() - 1);
}
// default to creating content
response.setCreateRequest(true);
final String targetName;
//check if a name was posted to use as the name of the imported root node
if (getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME) != null) {
// exact name
targetName = getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME);
if (targetName.length() > 0 && node.hasNode(targetName)) {
if (replace) {
response.setCreateRequest(false);
} else {
response.setStatus(
HttpServletResponse.SC_PRECONDITION_FAILED,
"Cannot import " + path + "/" + targetName
+ ": node exists");
return;
}
}
} else if (getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME_HINT) != null) {
// node name hint only
String nodePath = generateName(request, basePath);
String name = nodePath.substring(nodePath.lastIndexOf('/') + 1);
targetName = name;
} else {
// no name posted, so the import won't create a root node
targetName = "";
}
final String contentRootName = targetName + "." + contentType;
try {
InputStream contentStream = null;
RequestParameter contentParameter = request.getRequestParameter(SlingPostConstants.RP_CONTENT);
if (contentParameter != null) {
contentStream = contentParameter.getInputStream();
} else {
RequestParameter contentFile = request.getRequestParameter(SlingPostConstants.RP_CONTENT_FILE);
if (contentFile != null) {
contentStream = contentFile.getInputStream();
}
}
if (contentStream == null) {
response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED,
"Missing content for import");
return;
} else {
importer.importContent(node, contentRootName, contentStream,
new ImportOptions() {
@Override
public boolean isCheckin() {
return checkin;