diff --git a/xwiki-platform-core/xwiki-platform-oldcore/pom.xml b/xwiki-platform-core/xwiki-platform-oldcore/pom.xml index 4e856119006..95213cec9ed 100644 --- a/xwiki-platform-core/xwiki-platform-oldcore/pom.xml +++ b/xwiki-platform-core/xwiki-platform-oldcore/pom.xml @@ -887,7 +887,6 @@ **/web/ExportAction.java, **/web/ExportURLFactory.java, **/web/HomePageRedirectServlet.java, - **/web/ImportAction.java, **/web/includeservletasstring/BufferedResponse.java, **/web/includeservletasstring/BufferOutputStream.java, **/web/includeservletasstring/IncludeServletAsString.java, diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/objects/BaseElement.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/objects/BaseElement.java index 05ce2e47c68..c7887316cd3 100644 --- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/objects/BaseElement.java +++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/objects/BaseElement.java @@ -101,7 +101,7 @@ public abstract class BaseElement implements ElementI private EntityReferenceSerializer localUidStringEntityReferenceSerializer; private ContextualLocalizationManager localization; - + /** * @return a merge manager instance. * @since 11.8RC1 diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/ImportAction.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/ImportAction.java index 2f76984a776..b14c49b579a 100644 --- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/ImportAction.java +++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/ImportAction.java @@ -20,44 +20,21 @@ package com.xpn.xwiki.web; import java.io.IOException; -import java.lang.reflect.Type; -import java.util.UUID; import javax.inject.Named; import javax.inject.Singleton; import org.apache.commons.lang3.StringUtils; import org.apache.tika.mime.MediaType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.Marker; import org.xwiki.component.annotation.Component; import org.xwiki.filter.FilterException; -import org.xwiki.filter.event.model.WikiDocumentFilter; -import org.xwiki.filter.input.BeanInputFilterStream; -import org.xwiki.filter.input.BeanInputFilterStreamFactory; -import org.xwiki.filter.input.InputFilterStreamFactory; import org.xwiki.filter.input.InputSource; -import org.xwiki.filter.instance.output.DocumentInstanceOutputProperties; -import org.xwiki.filter.instance.output.InstanceOutputProperties; -import org.xwiki.filter.output.BeanOutputFilterStream; -import org.xwiki.filter.output.BeanOutputFilterStreamFactory; -import org.xwiki.filter.output.OutputFilterStreamFactory; -import org.xwiki.filter.type.FilterStreamType; -import org.xwiki.filter.xar.input.XARInputProperties; +import org.xwiki.internal.filter.Importer; import org.xwiki.localization.LocaleUtils; -import org.xwiki.logging.LogLevel; -import org.xwiki.logging.LogQueue; -import org.xwiki.logging.LoggerManager; -import org.xwiki.logging.event.LogEvent; -import org.xwiki.logging.event.LoggerListener; import org.xwiki.model.EntityType; -import org.xwiki.model.reference.EntityReference; import org.xwiki.model.reference.EntityReferenceResolver; -import org.xwiki.model.reference.EntityReferenceSerializer; import org.xwiki.model.reference.EntityReferenceSet; import org.xwiki.model.reference.LocalDocumentReference; -import org.xwiki.observation.ObservationManager; import org.xwiki.xar.XarException; import org.xwiki.xar.XarPackage; @@ -65,11 +42,8 @@ import com.xpn.xwiki.XWikiException; import com.xpn.xwiki.doc.XWikiAttachment; import com.xpn.xwiki.doc.XWikiDocument; -import com.xpn.xwiki.internal.event.XARImportedEvent; -import com.xpn.xwiki.internal.event.XARImportingEvent; import com.xpn.xwiki.internal.filter.input.XWikiAttachmentContentInputSource; import com.xpn.xwiki.plugin.packaging.DocumentInfo; -import com.xpn.xwiki.plugin.packaging.Package; import com.xpn.xwiki.util.Util; /** @@ -82,7 +56,9 @@ @Singleton public class ImportAction extends XWikiAction { - private static final Logger LOGGER = LoggerFactory.getLogger(ImportAction.class); + private static final String TEMPLATE_ADMIN = "admin"; + + private static final String TEMPLATE_IMPORTED = "imported"; @Override public String render(XWikiContext context) throws XWikiException @@ -102,7 +78,7 @@ public String render(XWikiContext context) throws XWikiException } if (name == null) { - return "admin"; + return TEMPLATE_ADMIN; } if ("getPackageInfos".equals(action)) { @@ -140,9 +116,9 @@ private String importPackage(XWikiAttachment packFile, XWikiRequest request, XWi // If the import is done from an AJAX request we don't want to return a whole HTML page, // instead we return "inline" the list of imported documents, // evaluating imported.vm template. - return "imported"; + return TEMPLATE_IMPORTED; } else { - return "admin"; + return TEMPLATE_ADMIN; } } @@ -183,14 +159,21 @@ private String getDocumentReference(String pageEntry) private void importPackageFilterStream(XWikiAttachment packFile, XWikiRequest request, XWikiContext context) throws IOException, XWikiException, FilterException { - String[] pages = request.getParameterValues("pages"); + // Define the data source to import + @SuppressWarnings("resource") + InputSource source = new XWikiAttachmentContentInputSource(packFile.getAttachmentContent(context)); - XARInputProperties xarProperties = new XARInputProperties(); - DocumentInstanceOutputProperties instanceProperties = new DocumentInstanceOutputProperties(); - instanceProperties.setSaveComment("Imported from XAR"); + // Get the history handling stategy + String historyStrategy = request.getParameter("historyStrategy"); + // Get the backup switch value + boolean importAsBackup = StringUtils.equals(request.getParameter("importAsBackup"), "true"); + + // Configure pages to import + EntityReferenceSet entities; + String[] pages = request.getParameterValues("pages"); if (pages != null) { - EntityReferenceSet entities = new EntityReferenceSet(); + entities = new EntityReferenceSet(); EntityReferenceResolver resolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING, "relative"); @@ -207,116 +190,11 @@ private void importPackageFilterStream(XWikiAttachment packFile, XWikiRequest re } } } - - xarProperties.setEntities(entities); - } - - // Set the appropriate strategy to handle versions - if (StringUtils.equals(request.getParameter("historyStrategy"), "reset")) { - instanceProperties.setPreviousDeleted(true); - instanceProperties.setVersionPreserved(false); - xarProperties.setWithHistory(false); - } else if (StringUtils.equals(request.getParameter("historyStrategy"), "replace")) { - instanceProperties.setPreviousDeleted(true); - instanceProperties.setVersionPreserved(true); - xarProperties.setWithHistory(true); - } else { - instanceProperties.setPreviousDeleted(false); - instanceProperties.setVersionPreserved(false); - xarProperties.setWithHistory(false); - } - - // Set the backup pack option - if (StringUtils.equals(request.getParameter("importAsBackup"), "true")) { - instanceProperties.setAuthorPreserved(true); } else { - instanceProperties.setAuthorPreserved(false); - } - - BeanInputFilterStreamFactory xarFilterStreamFactory = - Utils.getComponent((Type) InputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize()); - BeanInputFilterStream xarFilterStream = - xarFilterStreamFactory.createInputFilterStream(xarProperties); - - BeanOutputFilterStreamFactory instanceFilterStreamFactory = - Utils.getComponent((Type) OutputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize()); - BeanOutputFilterStream instanceFilterStream = - instanceFilterStreamFactory.createOutputFilterStream(instanceProperties); - - // Notify all the listeners about import - ObservationManager observation = Utils.getComponent(ObservationManager.class); - - InputSource source = new XWikiAttachmentContentInputSource(packFile.getAttachmentContent(context)); - xarProperties.setSource(source); - - // Setup log - xarProperties.setVerbose(true); - instanceProperties.setVerbose(true); - instanceProperties.setStoppedWhenSaveFail(false); - LoggerManager loggerManager = Utils.getComponent(LoggerManager.class); - LogQueue logger = new LogQueue(); - if (loggerManager != null) { - // Isolate log - loggerManager.pushLogListener(new LoggerListener(UUID.randomUUID().toString(), logger)); + entities = null; } - observation.notify(new XARImportingEvent(), null, context); - - try { - xarFilterStream.read(instanceFilterStream.getFilter()); - - xarFilterStream.close(); - instanceFilterStream.close(); - } finally { - if (loggerManager != null) { - // Stop isolating log - loggerManager.popLogListener(); - } - - // Print the import log - if (LOGGER.isDebugEnabled()) { - logger.log(LOGGER); - } else { - // TODO: remove when the UI show the log properly - for (LogEvent logEvent : logger.getLogsFrom(LogLevel.ERROR)) { - logEvent.log(LOGGER); - } - } - - // Make sure to free any resource use by the input source in case the input filter does not do it - source.close(); - - observation.notify(new XARImportedEvent(), null, context); - } - - // Generate import report - // Emulate old packager report (for retro compatibility) - Package oldImporter = new Package(); - if (logger.containLogsFrom(LogLevel.ERROR)) { - context.put("install_status", DocumentInfo.INSTALL_ERROR); - } else { - context.put("install_status", DocumentInfo.INSTALL_OK); - } - EntityReferenceSerializer serializer = - Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local"); - for (LogEvent log : logger) { - Marker marker = log.getMarker(); - if (marker != null) { - if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_CREATED.getName()) - || marker.contains(WikiDocumentFilter.LOG_DOCUMENT_UPDATED.getName())) { - oldImporter.getInstalled(context) - .add(serializer.serialize((EntityReference) log.getArgumentArray()[0])); - } else if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_SKIPPED.getName())) { - oldImporter.getSkipped(context) - .add(serializer.serialize((EntityReference) log.getArgumentArray()[0])); - } else if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_ERROR.getName())) { - Object entity = log.getArgumentArray()[0]; - if (entity != null) { - oldImporter.getErrors(context).add(entity instanceof EntityReference - ? serializer.serialize((EntityReference) log.getArgumentArray()[0]) : entity.toString()); - } - } - } - } + // Execute the import + Utils.getComponent(Importer.class).importXAR(source, entities, historyStrategy, importAsBackup, context); } } diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/org/xwiki/internal/filter/Importer.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/org/xwiki/internal/filter/Importer.java new file mode 100644 index 00000000000..95da4709ce2 --- /dev/null +++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/org/xwiki/internal/filter/Importer.java @@ -0,0 +1,232 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.internal.filter; + +import java.io.IOException; +import java.util.UUID; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.Marker; +import org.xwiki.component.annotation.Component; +import org.xwiki.filter.FilterException; +import org.xwiki.filter.event.model.WikiDocumentFilter; +import org.xwiki.filter.input.BeanInputFilterStream; +import org.xwiki.filter.input.BeanInputFilterStreamFactory; +import org.xwiki.filter.input.InputFilterStreamFactory; +import org.xwiki.filter.input.InputSource; +import org.xwiki.filter.instance.internal.InstanceUtils; +import org.xwiki.filter.instance.output.DocumentInstanceOutputProperties; +import org.xwiki.filter.instance.output.InstanceOutputProperties; +import org.xwiki.filter.output.BeanOutputFilterStream; +import org.xwiki.filter.output.BeanOutputFilterStreamFactory; +import org.xwiki.filter.output.OutputFilterStreamFactory; +import org.xwiki.filter.xar.input.XARInputProperties; +import org.xwiki.filter.xar.internal.XARFilterUtils; +import org.xwiki.logging.LogLevel; +import org.xwiki.logging.LogQueue; +import org.xwiki.logging.LoggerManager; +import org.xwiki.logging.event.LogEvent; +import org.xwiki.logging.event.LoggerListener; +import org.xwiki.model.reference.EntityReference; +import org.xwiki.model.reference.EntityReferenceSerializer; +import org.xwiki.model.reference.EntityReferenceSet; +import org.xwiki.observation.ObservationManager; + +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.internal.event.XARImportedEvent; +import com.xpn.xwiki.internal.event.XARImportingEvent; +import com.xpn.xwiki.plugin.packaging.DocumentInfo; +import com.xpn.xwiki.plugin.packaging.Package; +import com.xpn.xwiki.web.Utils; + +/** + * A helper to import a XAR package. + * + * @version $Id$ + * @since 16.10.2 + * @since 16.4.6 + * @since 15.10.16 + */ +@Component(roles = Importer.class) +@Singleton +@SuppressWarnings("checkstyle:ClassFanOutComplexity") +public class Importer +{ + @Inject + @Named(XARFilterUtils.ROLEHINT_CURRENT) + private InputFilterStreamFactory xarFilterStreamFactory; + + @Inject + @Named(InstanceUtils.ROLEHINT) + private OutputFilterStreamFactory instanceFilterStreamFactory; + + @Inject + private ObservationManager observation; + + @Inject + private Logger logger; + + /** + * @param source the XAR package to import + * @param entities the entities to import from the package or null for everything + * @param historyStrategy the history strategy to apply + * @param backup true if the package should be handled as a backup + * @param context the XWiki context + * @throws IOException when failing to import the package + * @throws FilterException when failing to import the package + */ + public void importXAR(InputSource source, EntityReferenceSet entities, String historyStrategy, boolean backup, + XWikiContext context) throws IOException, FilterException + { + XARInputProperties xarProperties = new XARInputProperties(); + xarProperties.setSource(source); + + DocumentInstanceOutputProperties instanceProperties = new DocumentInstanceOutputProperties(); + instanceProperties.setSaveComment("Imported from XAR"); + + if (entities != null) { + xarProperties.setEntities(entities); + } + + // Set the appropriate strategy to handle versions + setHistoryStrategy(historyStrategy, xarProperties, instanceProperties); + + // Set the backup pack option + instanceProperties.setAuthorPreserved(backup); + + // Setup log + xarProperties.setVerbose(true); + instanceProperties.setVerbose(true); + instanceProperties.setStoppedWhenSaveFail(false); + LoggerManager loggerManager = Utils.getComponent(LoggerManager.class); + LogQueue importLogger = new LogQueue(); + if (loggerManager != null) { + // Isolate log + loggerManager.pushLogListener(new LoggerListener(UUID.randomUUID().toString(), importLogger)); + } + + // Create the streams + BeanInputFilterStream xarFilterStream = createXARFilterStream(xarProperties); + BeanOutputFilterStream instanceFilterStream = + createInstanceFilterStream(instanceProperties); + + // Notify listeners about import beginning + this.observation.notify(new XARImportingEvent(), null, context); + + try { + xarFilterStream.read(instanceFilterStream.getFilter()); + + xarFilterStream.close(); + instanceFilterStream.close(); + } finally { + if (loggerManager != null) { + // Stop isolating log + loggerManager.popLogListener(); + } + + // Print the import log + if (this.logger.isDebugEnabled()) { + importLogger.log(this.logger); + } else { + // TODO: remove when the UI show the log properly + for (LogEvent logEvent : importLogger.getLogsFrom(LogLevel.ERROR)) { + logEvent.log(this.logger); + } + } + + // Make sure to free any resource use by the input source in case the input filter does not do it + source.close(); + + // Notify listeners about import end + this.observation.notify(new XARImportedEvent(), null, context); + } + + // Generate import report + generateReport(importLogger, context); + } + + private BeanInputFilterStream createXARFilterStream(XARInputProperties xarProperties) + throws FilterException + { + BeanInputFilterStreamFactory xarInputfactory = + (BeanInputFilterStreamFactory) this.xarFilterStreamFactory; + return xarInputfactory.createInputFilterStream(xarProperties); + } + + private BeanOutputFilterStream createInstanceFilterStream( + DocumentInstanceOutputProperties instanceProperties) throws FilterException + { + BeanOutputFilterStreamFactory instanceOutputFactory = + (BeanOutputFilterStreamFactory) this.instanceFilterStreamFactory; + return instanceOutputFactory.createOutputFilterStream(instanceProperties); + } + + private void setHistoryStrategy(String historyStrategy, XARInputProperties xarProperties, + DocumentInstanceOutputProperties instanceProperties) + { + if (StringUtils.equals(historyStrategy, "reset")) { + instanceProperties.setPreviousDeleted(true); + instanceProperties.setVersionPreserved(false); + xarProperties.setWithHistory(false); + } else if (StringUtils.equals(historyStrategy, "replace")) { + instanceProperties.setPreviousDeleted(true); + instanceProperties.setVersionPreserved(true); + xarProperties.setWithHistory(true); + } else { + instanceProperties.setPreviousDeleted(false); + instanceProperties.setVersionPreserved(false); + xarProperties.setWithHistory(false); + } + } + + private void generateReport(LogQueue importLogger, XWikiContext context) + { + // Emulate old packager report (for retro compatibility) + Package oldImporter = new Package(); + context.put("install_status", + importLogger.containLogsFrom(LogLevel.ERROR) ? DocumentInfo.INSTALL_ERROR : DocumentInfo.INSTALL_OK); + EntityReferenceSerializer serializer = + Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local"); + for (LogEvent log : importLogger) { + Marker marker = log.getMarker(); + if (marker != null) { + if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_CREATED.getName()) + || marker.contains(WikiDocumentFilter.LOG_DOCUMENT_UPDATED.getName())) { + oldImporter.getInstalled(context) + .add(serializer.serialize((EntityReference) log.getArgumentArray()[0])); + } else if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_SKIPPED.getName())) { + oldImporter.getSkipped(context) + .add(serializer.serialize((EntityReference) log.getArgumentArray()[0])); + } else if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_ERROR.getName())) { + Object entity = log.getArgumentArray()[0]; + if (entity != null) { + oldImporter.getErrors(context).add(entity instanceof EntityReference + ? serializer.serialize((EntityReference) log.getArgumentArray()[0]) : entity.toString()); + } + } + } + } + } +} diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/resources/META-INF/components.txt b/xwiki-platform-core/xwiki-platform-oldcore/src/main/resources/META-INF/components.txt index bc309b8e482..6d15471359f 100644 --- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/resources/META-INF/components.txt +++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/resources/META-INF/components.txt @@ -280,6 +280,7 @@ org.xwiki.evaluation.internal.DefaultObjectEvaluator org.xwiki.evaluation.internal.VelocityObjectPropertyEvaluator org.xwiki.internal.authentication.LoginLinkUIExtension org.xwiki.internal.authentication.RegisterLinkUIExtension +org.xwiki.internal.filter.Importer org.xwiki.internal.document.DocumentOverrideListener org.xwiki.internal.document.DocumentRequiredRightsReader org.xwiki.internal.document.RequiredRightClassMandatoryDocumentInitializer diff --git a/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/wikis/WikiResourceImpl.java b/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/wikis/WikiResourceImpl.java index 6ce1ba28904..eaacee5bebc 100644 --- a/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/wikis/WikiResourceImpl.java +++ b/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/wikis/WikiResourceImpl.java @@ -19,25 +19,27 @@ */ package org.xwiki.rest.internal.resources.wikis; -import java.io.IOException; import java.io.InputStream; -import java.util.List; +import javax.inject.Inject; import javax.inject.Named; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import org.xwiki.component.annotation.Component; +import org.xwiki.filter.input.DefaultInputStreamInputSource; +import org.xwiki.filter.input.InputSource; +import org.xwiki.internal.filter.Importer; +import org.xwiki.model.reference.WikiReference; import org.xwiki.rest.XWikiResource; import org.xwiki.rest.XWikiRestException; import org.xwiki.rest.internal.DomainObjectFactory; -import org.xwiki.rest.internal.Utils; import org.xwiki.rest.model.jaxb.Wiki; import org.xwiki.rest.resources.wikis.WikiResource; +import org.xwiki.wiki.descriptor.WikiDescriptorManager; +import org.xwiki.wiki.manager.WikiManagerException; import com.xpn.xwiki.XWikiContext; -import com.xpn.xwiki.XWikiException; -import com.xpn.xwiki.plugin.packaging.PackageAPI; /** * Resource for interacting with a specific wiki. @@ -48,141 +50,57 @@ @Named("org.xwiki.rest.internal.resources.wikis.WikiResourceImpl") public class WikiResourceImpl extends XWikiResource implements WikiResource { - /** - * The possible option for managing history when importing wiki documents. - */ - private enum HistoryOptions - { - /** - * Add a new version. - */ - ADD, - /** - * Reset the version to 1.1. - */ - RESET, - /** - * Replace the current version. - */ - REPLACE - } + @Inject + private Importer importer; + + @Inject + private WikiDescriptorManager wikis; @Override public Wiki get(String wikiName) throws XWikiRestException { try { - if (wikiExists(wikiName)) { - return DomainObjectFactory.createWiki(objectFactory, uriInfo.getBaseUri(), wikiName); + if (this.wikis.exists(wikiName)) { + return DomainObjectFactory.createWiki(this.objectFactory, this.uriInfo.getBaseUri(), wikiName); } throw new WebApplicationException(Response.Status.NOT_FOUND); - } catch (XWikiException e) { + } catch (WikiManagerException e) { throw new XWikiRestException(e); } } @Override - public Wiki importXAR(String wikiName, Boolean backup, String history, InputStream is) throws XWikiRestException + public Wiki importXAR(String wikiName, Boolean backup, String historyStrategy, InputStream is) + throws XWikiRestException { try { - if (!wikiExists(wikiName)) { + if (!this.wikis.exists(wikiName)) { throw new WebApplicationException(Response.Status.NOT_FOUND); } /* Use the package plugin for importing pages */ XWikiContext xwikiContext = getXWikiContext(); - PackageAPI importer = ((PackageAPI) xwikiContext.getWiki().getPluginApi("package", xwikiContext)); - if (importer == null) { - throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, - "Can't access Package plugin API. Generally mean you don't have enough rights."); - } - String database = xwikiContext.getWikiId(); + WikiReference currentWiki = xwikiContext.getWikiReference(); try { xwikiContext.setWikiId(wikiName); - importer.setBackupPack(backup); - - importer.Import(is); - - HistoryOptions historyOption = parseHistoryOption(history, HistoryOptions.ADD); - switch (historyOption) { - case RESET: - importer.setPreserveVersion(false); - importer.setWithVersions(false); - break; - case REPLACE: - importer.setPreserveVersion(false); - importer.setWithVersions(true); - break; - default: - case ADD: - importer.setPreserveVersion(true); - importer.setWithVersions(false); - break; - } + // Define the data source to import + InputSource source = new DefaultInputStreamInputSource(is, false); - // Set the backup pack option - importer.setBackupPack(backup); - - if (importer.install() == com.xpn.xwiki.plugin.packaging.DocumentInfo.INSTALL_IMPOSSIBLE) { - throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); - } - } catch (IOException e) { - throw new WebApplicationException(e); + // Execute the import + this.importer.importXAR(source, null, historyStrategy, backup == Boolean.TRUE, getXWikiContext()); + } catch (Exception e) { + throw new WebApplicationException("Failed to import the XAR package", e); } finally { - xwikiContext.setWikiId(database); + xwikiContext.setWikiReference(currentWiki); } - return DomainObjectFactory.createWiki(objectFactory, uriInfo.getBaseUri(), wikiName); - } catch (XWikiException e) { + return DomainObjectFactory.createWiki(this.objectFactory, this.uriInfo.getBaseUri(), wikiName); + } catch (WikiManagerException e) { throw new XWikiRestException(e); } } - - /** - * Check if a wiki exists. - * - * @param wikiName the wiki name. - * @return true if the wiki exists. - * @throws XWikiException if something goes wrong. - */ - protected boolean wikiExists(String wikiName) throws XWikiException - { - List databaseNames = - Utils.getXWiki(componentManager).getVirtualWikisDatabaseNames(Utils.getXWikiContext(componentManager)); - - if (databaseNames.isEmpty()) { - databaseNames.add("xwiki"); - } - - for (String databaseName : databaseNames) { - if (databaseName.equals(wikiName)) { - return true; - } - } - - return false; - } - - /** - * Return the HistoryOptions enum object corresponding to a string. - * - * @param value a string representing a history option. - * @param defaultValue the value to be returned in the case no corresponding history option is found. - * @return the history option enum object, - */ - protected HistoryOptions parseHistoryOption(String value, HistoryOptions defaultValue) - { - try { - if (value != null) { - return HistoryOptions.valueOf(value.toUpperCase()); - } - } catch (IllegalArgumentException e) { - // Invalid query type string. - } - - return defaultValue; - } }