Commit 1ef32141 authored by michael.simon's avatar michael.simon
Browse files

Attribute Source developement

parent c6f37cff
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.dao.as;
import edu.kit.scc.webreg.dao.BaseDao;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueDateEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueLongEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueStringEntity;
public interface ASUserAttrValueDao extends BaseDao<ASUserAttrValueEntity, Long> {
ASUserAttrValueDateEntity createNewDate();
ASUserAttrValueLongEntity createNewLong();
ASUserAttrValueStringEntity createNewString();
ASUserAttrValueEntity findValueByKey(ASUserAttrEntity asUserAttr, String key);
}
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.dao.jpa.as;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.persistence.NoResultException;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.dao.jpa.JpaBaseDao;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueDateEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueLongEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueStringEntity;
@Named
@ApplicationScoped
public class JpaASUserAttrValueDao extends JpaBaseDao<ASUserAttrValueEntity, Long> implements ASUserAttrValueDao {
@Override
public ASUserAttrValueStringEntity createNewString() {
return new ASUserAttrValueStringEntity();
}
@Override
public ASUserAttrValueLongEntity createNewLong() {
return new ASUserAttrValueLongEntity();
}
@Override
public ASUserAttrValueDateEntity createNewDate() {
return new ASUserAttrValueDateEntity();
}
@Override
public ASUserAttrValueEntity findValueByKey(ASUserAttrEntity asUserAttr, String key) {
try {
return (ASUserAttrValueEntity) em.createQuery("select a from ASUserAttrValueEntity a where "
+ "a.asUserAttr = :asUserAttr and a.key = :key")
.setParameter("asUserAttr", asUserAttr).setParameter("key", key).getSingleResult();
}
catch (NoResultException e) {
return null;
}
}
@Override
public Class<ASUserAttrValueEntity> getEntityClass() {
return ASUserAttrValueEntity.class;
}
}
......@@ -11,17 +11,18 @@
package edu.kit.scc.webreg.entity.as;
import java.util.Date;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import edu.kit.scc.webreg.entity.AbstractBaseEntity;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.UserGroupEntity;
@Entity(name = "ASUserAttrEntity")
@Table(name = "attribute_src_ua")
......@@ -40,6 +41,9 @@ public class ASUserAttrEntity extends AbstractBaseEntity {
@Column(name = "last_successful_query")
private Date lastSuccessfulQuery;
@OneToMany(targetEntity = ASUserAttrValueEntity.class, mappedBy="asUserAttr")
private Set<ASUserAttrValueEntity> values;
public UserEntity getUser() {
return user;
}
......@@ -64,4 +68,12 @@ public class ASUserAttrEntity extends AbstractBaseEntity {
this.lastSuccessfulQuery = lastSuccessfulQuery;
}
public Set<ASUserAttrValueEntity> getValues() {
return values;
}
public void setValues(Set<ASUserAttrValueEntity> values) {
this.values = values;
}
}
package edu.kit.scc.webreg.as;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.entity.AuditStatus;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueStringEntity;
import edu.kit.scc.webreg.exc.RegisterException;
import edu.kit.scc.webreg.service.reg.ldap.PropertyReader;
......@@ -20,4 +27,40 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
prop = new PropertyReader(asUserAttr.getAttributeSource().getAsProps());
}
protected void createOrUpdateValue(String key, Object o, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao, AttributeSourceAuditor auditor) {
if (o == null || key == null) {
logger.warn("Null keys or values cannot be updated");
return;
}
if (o instanceof String) {
String value = (String) o;
ASUserAttrValueEntity asValue = asValueDao.findValueByKey(asUserAttr, key);
if (asValue == null) {
asValue = asValueDao.createNewString();
asValue.setKey(key);
asValue.setAsUserAttr(asUserAttr);
auditor.logAction("as-workflow", "CREATE VALUE (String)", key, "", AuditStatus.SUCCESS);
}
else if (! (asValue instanceof ASUserAttrValueStringEntity)) {
asValueDao.delete(asValue);
auditor.logAction("as-workflow", "DELETE VALUE", key, "", AuditStatus.SUCCESS);
asValue = asValueDao.createNewString();
asValue.setKey(key);
asValue.setAsUserAttr(asUserAttr);
auditor.logAction("as-workflow", "CREATE VALUE (String)", key, "", AuditStatus.SUCCESS);
}
ASUserAttrValueStringEntity asStringValue = (ASUserAttrValueStringEntity) asValue;
if (! value.equals(asStringValue.getValueString())) {
auditor.logAction("as-workflow", "UPDATE VALUE (String)", key, asStringValue.getValueString() + " -> " + value, AuditStatus.SUCCESS);
asStringValue.setValueString(value);
}
asValueDao.persist(asValue);
}
else {
logger.warn("Cannot process value of type {}", o.getClass());
}
}
}
......@@ -3,11 +3,13 @@ package edu.kit.scc.webreg.as;
import java.io.Serializable;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.exc.RegisterException;
public interface AttributeSourceWorkflow extends Serializable {
public void pollUserAttributes(ASUserAttrEntity asUserAttr,
AttributeSourceAuditor auditor) throws RegisterException;
void pollUserAttributes(ASUserAttrEntity asUserAttr,
ASUserAttrValueDao asValueDao, AttributeSourceAuditor auditor)
throws RegisterException;
}
......@@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
......@@ -21,7 +22,11 @@ import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
......@@ -33,7 +38,7 @@ public class HttpUrlSingleAttributeSource extends
private static final long serialVersionUID = 1L;
@Override
public void pollUserAttributes(ASUserAttrEntity asUserAttr, AttributeSourceAuditor auditor) throws RegisterException {
public void pollUserAttributes(ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao, AttributeSourceAuditor auditor) throws RegisterException {
init(asUserAttr);
......@@ -83,7 +88,23 @@ public class HttpUrlSingleAttributeSource extends
if (entity != null) {
try {
String r = EntityUtils.toString(entity);
logger.debug("Got String {}", r);
ObjectMapper om = new ObjectMapper();
try {
Map<String, Object> map = om.readValue(r, Map.class);
logger.debug("Got {} values", map.size());
for (Entry<String, Object> entry : map.entrySet()) {
logger.debug("Processing entry {}, value {}", entry.getKey(), entry.getValue());
createOrUpdateValue(entry.getKey(), entry.getValue(), asUserAttr, asValueDao, auditor);
}
} catch (JsonMappingException e) {
/*
* Datasource generates invalid JSON
*/
logger.warn("Json Parse failed: {}", e.getMessage());
}
} catch (ParseException e) {
throw new RegisterException(e);
} catch (IOException e) {
......@@ -92,6 +113,9 @@ public class HttpUrlSingleAttributeSource extends
}
}
else {
/*
* probably no info for this user from datasource
*/
logger.debug("Status HttpUrlSingleAS is not OK. It is {} - {}", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
}
}
......
......@@ -12,6 +12,7 @@ import edu.kit.scc.webreg.dao.AuditDetailDao;
import edu.kit.scc.webreg.dao.AuditEntryDao;
import edu.kit.scc.webreg.dao.UserDao;
import edu.kit.scc.webreg.dao.as.ASUserAttrDao;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.dao.as.AttributeSourceDao;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
......@@ -36,6 +37,9 @@ public class AttributeSourceQueryServiceImpl implements AttributeSourceQueryServ
@Inject
private ASUserAttrDao asUserAttrDao;
@Inject
private ASUserAttrValueDao asValueDao;
@Inject
private AuditEntryDao auditDao;
......@@ -69,7 +73,7 @@ public class AttributeSourceQueryServiceImpl implements AttributeSourceQueryServ
auditor.setDetail("Updateing attributes for user " + user.getEppn());
auditor.setAsUserAttr(asUserAttr);
workflow.pollUserAttributes(asUserAttr, auditor);
workflow.pollUserAttributes(asUserAttr, asValueDao, auditor);
auditor.finishAuditTrail();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment