
org.broadleafcommerce.openadmin.server.service.DynamicEntityRemoteService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of broadleaf-open-admin-platform Show documentation
Show all versions of broadleaf-open-admin-platform Show documentation
BroadleafCommerce Open Admin Platform
The newest version!
/*
* #%L
* BroadleafCommerce Open Admin Platform
* %%
* Copyright (C) 2009 - 2013 Broadleaf Commerce
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.broadleafcommerce.openadmin.server.service;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.common.security.service.CleanStringException;
import org.broadleafcommerce.common.security.service.ExploitProtectionService;
import org.broadleafcommerce.openadmin.dto.BatchDynamicResultSet;
import org.broadleafcommerce.openadmin.dto.BatchPersistencePackage;
import org.broadleafcommerce.openadmin.dto.CriteriaTransferObject;
import org.broadleafcommerce.openadmin.dto.Entity;
import org.broadleafcommerce.openadmin.dto.PersistencePackage;
import org.broadleafcommerce.openadmin.dto.Property;
import org.broadleafcommerce.openadmin.server.service.persistence.Persistable;
import org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager;
import org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManagerFactory;
import org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse;
import org.broadleafcommerce.openadmin.server.service.persistence.PersistenceThreadManager;
import org.broadleafcommerce.openadmin.server.service.persistence.TargetModeType;
import org.codehaus.jackson.map.util.LRUMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Constructor;
import java.util.Map;
import javax.annotation.Resource;
/**
* @author jfischer
*/
@Service("blDynamicEntityRemoteService")
public class DynamicEntityRemoteService implements DynamicEntityService {
private static final Log LOG = LogFactory.getLog(DynamicEntityRemoteService.class);
protected static final Map METADATA_CACHE = MapUtils.synchronizedMap(new LRUMap(100, 1000));
@Resource(name="blExploitProtectionService")
protected ExploitProtectionService exploitProtectionService;
@Resource(name="blPersistenceThreadManager")
protected PersistenceThreadManager persistenceThreadManager;
protected ServiceException recreateSpecificServiceException(ServiceException e, String message, Throwable cause) {
try {
ServiceException newException;
if (cause == null) {
Constructor constructor = e.getClass().getConstructor(String.class);
newException = (ServiceException) constructor.newInstance(message);
} else {
Constructor constructor = e.getClass().getConstructor(String.class, Throwable.class);
newException = (ServiceException) constructor.newInstance(message, cause);
}
return newException;
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
@Override
public PersistenceResponse inspect(final PersistencePackage persistencePackage) throws ServiceException {
return persistenceThreadManager.operation(TargetModeType.SANDBOX, new Persistable () {
@Override
public PersistenceResponse execute() throws ServiceException {
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
try {
PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
return persistenceManager.inspect(persistencePackage);
} catch (ServiceException e) {
String message = exploitProtectionService.cleanString(e.getMessage());
throw recreateSpecificServiceException(e, message, e.getCause());
} catch (Exception e) {
LOG.error("Problem inspecting results for " + ceilingEntityFullyQualifiedClassname, e);
throw new ServiceException(exploitProtectionService.cleanString("Unable to fetch results for " + ceilingEntityFullyQualifiedClassname), e);
}
}
});
}
@Override
public PersistenceResponse fetch(final PersistencePackage persistencePackage, final CriteriaTransferObject cto) throws ServiceException {
return persistenceThreadManager.operation(TargetModeType.SANDBOX, new Persistable() {
@Override
public PersistenceResponse execute() throws ServiceException {
try {
PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
return persistenceManager.fetch(persistencePackage, cto);
} catch (ServiceException e) {
LOG.error("Problem fetching results for " + persistencePackage.getCeilingEntityFullyQualifiedClassname(), e);
String message = exploitProtectionService.cleanString(e.getMessage());
throw recreateSpecificServiceException(e, message, e.getCause());
}
}
});
}
protected void cleanEntity(Entity entity) throws ServiceException {
Property currentProperty = null;
try {
for (Property property : entity.getProperties()) {
currentProperty = property;
property.setRawValue(property.getValue());
property.setValue(exploitProtectionService.cleanStringWithResults(property.getValue()));
property.setUnHtmlEncodedValue(StringEscapeUtils.unescapeHtml(property.getValue()));
}
} catch (CleanStringException e) {
StringBuilder sb = new StringBuilder();
for (int j=0;j() {
@Override
public PersistenceResponse execute() throws ServiceException {
cleanEntity(persistencePackage.getEntity());
if (persistencePackage.getEntity().isValidationFailure()) {
return new PersistenceResponse().withEntity(persistencePackage.getEntity());
}
try {
PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
return persistenceManager.add(persistencePackage);
} catch (ServiceException e) {
//immediately throw validation exceptions without printing a stack trace
if (e instanceof ValidationException) {
throw e;
} else if (e.getCause() instanceof ValidationException) {
throw (ValidationException) e.getCause();
}
String message = exploitProtectionService.cleanString(e.getMessage());
LOG.error("Problem adding new " + persistencePackage.getCeilingEntityFullyQualifiedClassname(), e);
throw recreateSpecificServiceException(e, message, e.getCause());
}
}
});
}
@Override
@Transactional(value="blTransactionManager", rollbackFor = ServiceException.class)
public PersistenceResponse update(final PersistencePackage persistencePackage) throws ServiceException {
return persistenceThreadManager.operation(TargetModeType.SANDBOX, new Persistable() {
@Override
public PersistenceResponse execute() throws ServiceException {
cleanEntity(persistencePackage.getEntity());
if (persistencePackage.getEntity().isValidationFailure()) {
return new PersistenceResponse().withEntity(persistencePackage.getEntity());
}
try {
PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
return persistenceManager.update(persistencePackage);
} catch (ServiceException e) {
//immediately throw validation exceptions without printing a stack trace
if (e instanceof ValidationException) {
throw e;
} else if (e.getCause() instanceof ValidationException) {
throw (ValidationException) e.getCause();
}
LOG.error("Problem updating " + persistencePackage.getCeilingEntityFullyQualifiedClassname(), e);
String message = exploitProtectionService.cleanString(e.getMessage());
throw recreateSpecificServiceException(e, message, e.getCause());
}
}
});
}
@Override
@Transactional(value="blTransactionManager", rollbackFor = ServiceException.class)
public PersistenceResponse remove(final PersistencePackage persistencePackage) throws ServiceException {
return persistenceThreadManager.operation(TargetModeType.SANDBOX, new Persistable() {
@Override
public PersistenceResponse execute() throws ServiceException {
try {
PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
return persistenceManager.remove(persistencePackage);
} catch (ServiceException e) {
//immediately throw validation exceptions without printing a stack trace
if (e instanceof ValidationException) {
throw e;
} else if (e.getCause() instanceof ValidationException) {
throw (ValidationException) e.getCause();
}
LOG.error("Problem removing " + persistencePackage.getCeilingEntityFullyQualifiedClassname(), e);
String message = exploitProtectionService.cleanString(e.getMessage());
throw recreateSpecificServiceException(e, message, e.getCause());
}
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy