RESTful API for Import and Export
parent
bebe653cce
commit
9aea37c68b
|
|
@ -20,6 +20,7 @@ import org.libreccm.api.DefaultResponseHeaders;
|
||||||
import org.libreccm.api.PreflightRequestFilter;
|
import org.libreccm.api.PreflightRequestFilter;
|
||||||
import org.libreccm.api.admin.categorization.CategoriesApi;
|
import org.libreccm.api.admin.categorization.CategoriesApi;
|
||||||
import org.libreccm.api.admin.categorization.DomainsApi;
|
import org.libreccm.api.admin.categorization.DomainsApi;
|
||||||
|
import org.libreccm.api.admin.imexport.ImExportApi;
|
||||||
import org.libreccm.api.admin.sites.SitesApi;
|
import org.libreccm.api.admin.sites.SitesApi;
|
||||||
import org.libreccm.api.admin.web.CcmApplicationsApi;
|
import org.libreccm.api.admin.web.CcmApplicationsApi;
|
||||||
import org.libreccm.configuration.Configuration;
|
import org.libreccm.configuration.Configuration;
|
||||||
|
|
@ -46,6 +47,9 @@ public class AdminApi extends Application {
|
||||||
// Configuration API
|
// Configuration API
|
||||||
classes.add(Configuration.class);
|
classes.add(Configuration.class);
|
||||||
|
|
||||||
|
// Import and Export of entities
|
||||||
|
classes.add(ImExportApi.class);
|
||||||
|
|
||||||
// Security API
|
// Security API
|
||||||
classes.add(GroupsApi.class);
|
classes.add(GroupsApi.class);
|
||||||
classes.add(RolesApi.class);
|
classes.add(RolesApi.class);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,131 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2020 LibreCCM Foundation.
|
||||||
|
*
|
||||||
|
* This library 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 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.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.libreccm.api.admin.imexport;
|
||||||
|
|
||||||
|
import org.libreccm.api.Identifier;
|
||||||
|
import org.libreccm.api.IdentifierParser;
|
||||||
|
import org.libreccm.core.CcmObject;
|
||||||
|
import org.libreccm.core.CcmObjectRepository;
|
||||||
|
import org.libreccm.core.CoreConstants;
|
||||||
|
import org.libreccm.imexport.Exportable;
|
||||||
|
import org.libreccm.imexport.ImportExport;
|
||||||
|
import org.libreccm.imexport.ImportManifest;
|
||||||
|
import org.libreccm.security.AuthorizationRequired;
|
||||||
|
import org.libreccm.security.RequiresPrivilege;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import javax.enterprise.context.RequestScoped;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
*/
|
||||||
|
@RequestScoped
|
||||||
|
@Path("imexport")
|
||||||
|
public class ImExportApi {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private CcmObjectRepository ccmObjectRepository;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private IdentifierParser identifierParser;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ImportExport importExport;
|
||||||
|
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("/imports/")
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@AuthorizationRequired
|
||||||
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
public List<ImportManifest> listImports() {
|
||||||
|
return importExport.listAvailableImportArchivies();
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("/imports/{importName}")
|
||||||
|
@AuthorizationRequired
|
||||||
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
public Response importArchive(
|
||||||
|
@PathParam("importName") final String importName
|
||||||
|
) {
|
||||||
|
importExport.importEntities(importName);
|
||||||
|
|
||||||
|
return Response.ok(String.format(
|
||||||
|
"Archive %s successfully imported.", importName)
|
||||||
|
).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("/imports/{exportName}")
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
@AuthorizationRequired
|
||||||
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
public Response exportEntities(
|
||||||
|
final String exportName, final List<String> entityIds
|
||||||
|
) {
|
||||||
|
final List<Exportable> entities = entityIds
|
||||||
|
.stream()
|
||||||
|
.map(this::findEntity)
|
||||||
|
.filter(Optional::isPresent)
|
||||||
|
.map(Optional::get)
|
||||||
|
.filter(obj -> obj instanceof Exportable)
|
||||||
|
.map(obj -> (Exportable) obj)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
importExport.exportEntities(entities, exportName);
|
||||||
|
|
||||||
|
return Response.ok(
|
||||||
|
String.format("Export %s successfully created.", exportName)
|
||||||
|
).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<CcmObject> findEntity(final String entityId) {
|
||||||
|
final Identifier identifier = identifierParser.parseIdentifier(entityId);
|
||||||
|
switch(identifier.getType()) {
|
||||||
|
case ID:
|
||||||
|
return ccmObjectRepository.findObjectById(
|
||||||
|
Long.parseLong(identifier.getIdentifier())
|
||||||
|
);
|
||||||
|
case UUID:
|
||||||
|
return ccmObjectRepository.findObjectByUuid(
|
||||||
|
identifier.getIdentifier()
|
||||||
|
);
|
||||||
|
default:
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue