/* * Copyright (C) 2003-2017 eXo Platform SAS. * * 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.exoplatform.management.organization.role; import java.io.InputStream; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.exoplatform.management.common.AbstractOperationHandler; import org.exoplatform.management.organization.OrganizationManagementExtension; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; import org.exoplatform.services.organization.MembershipType; import org.exoplatform.services.organization.OrganizationService; import org.exoplatform.services.organization.impl.MembershipTypeImpl; import org.gatein.management.api.exceptions.OperationException; import org.gatein.management.api.operation.OperationAttachment; import org.gatein.management.api.operation.OperationAttributes; import org.gatein.management.api.operation.OperationContext; import org.gatein.management.api.operation.OperationNames; import org.gatein.management.api.operation.ResultHandler; import org.gatein.management.api.operation.model.NoResultModel; import com.thoughtworks.xstream.XStream; /** * The Class RoleImportResource. * * @author <a href="mailto:boubaker.khanfir@exoplatform.com">Boubaker * Khanfir</a> */ public class RoleImportResource extends AbstractOperationHandler { /** The Constant log. */ private static final Log log = ExoLogger.getLogger(RoleImportResource.class); /** The organization service. */ private OrganizationService organizationService = null; /** * {@inheritDoc} */ @Override public void execute(OperationContext operationContext, ResultHandler resultHandler) throws OperationException { if (organizationService == null) { organizationService = operationContext.getRuntimeContext().getRuntimeComponent(OrganizationService.class); } InputStream attachmentInputStream = null; OperationAttributes attributes = operationContext.getAttributes(); List<String> filters = attributes.getValues("filter"); // "replace-existing" attribute. Defaults to false. boolean replaceExisting = filters.contains("replace-existing:true"); // get attachement input stream OperationAttachment attachment = operationContext.getAttachment(false); attachmentInputStream = attachment.getStream(); final ZipInputStream zin = new ZipInputStream(attachmentInputStream); try { ZipEntry entry; try { while ((entry = zin.getNextEntry()) != null) { try { String filePath = entry.getName(); if (!filePath.startsWith(OrganizationManagementExtension.PATH_ORGANIZATION + "/" + OrganizationManagementExtension.PATH_ORGANIZATION_ROLE + "/")) { continue; } if (entry.isDirectory() || filePath.trim().isEmpty() || !filePath.endsWith(".xml")) { continue; } if (filePath.endsWith("_role.xml")) { log.debug("Parsing : " + filePath); createRole(zin, replaceExisting); } } finally { try { zin.closeEntry(); } catch (Exception e) { // Already closed, expected } } } } finally { zin.close(); } resultHandler.completed(NoResultModel.INSTANCE); } catch (Exception e) { throw new OperationException(OperationNames.IMPORT_RESOURCE, "Error while reading group from Stream.", e); } } /** * Creates the role. * * @param zin the zin * @param replaceExisting the replace existing * @throws Exception the exception */ private void createRole(final ZipInputStream zin, Boolean replaceExisting) throws Exception { MembershipType membershipType = deserializeObject(zin, MembershipTypeImpl.class); MembershipType oldMembershipType = organizationService.getMembershipTypeHandler().findMembershipType(membershipType.getName()); boolean alreadyExists = (oldMembershipType != null); if (alreadyExists && replaceExisting) { log.info("ReplaceExisting is On: Deleting role '" + membershipType.getName() + "'"); organizationService.getMembershipTypeHandler().removeMembershipType(membershipType.getName(), true); oldMembershipType = null; } if (oldMembershipType == null) { organizationService.getMembershipTypeHandler().createMembershipType(membershipType, true); } else { log.info("ReplaceExisting is Off: Ignoring role '" + membershipType.getName() + "'"); } } /** * Deserialize object. * * @param <T> the generic type * @param zin the zin * @param objectClass the object class * @return the t */ private <T> T deserializeObject(final ZipInputStream zin, Class<T> objectClass) { XStream xStream = new XStream(); xStream.alias("organization", objectClass); @SuppressWarnings("unchecked") T object = (T) xStream.fromXML(zin); return object; } }