Commit 358d813b authored by michael.simon's avatar michael.simon
Browse files

first caching with attribute sources

parent adc8ba18
......@@ -15,6 +15,8 @@ import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
......@@ -22,6 +24,7 @@ import javax.persistence.Table;
import edu.kit.scc.webreg.entity.AbstractBaseEntity;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.UserStatus;
@Entity(name = "ASUserAttrEntity")
@Table(name = "attribute_src_ua")
......@@ -37,12 +40,22 @@ public class ASUserAttrEntity extends AbstractBaseEntity {
@JoinColumn(name = "attribute_src_id", nullable = false)
private AttributeSourceEntity attributeSource;
@Column(name = "last_query")
private Date lastQuery;
@Column(name = "last_successful_query")
private Date lastSuccessfulQuery;
@OneToMany(targetEntity = ASUserAttrValueEntity.class, mappedBy="asUserAttr")
private Set<ASUserAttrValueEntity> values;
@Enumerated(EnumType.STRING)
@Column(name = "query_status")
private AttributeSourceQueryStatus queryStatus;
@Column(name = "queryMessage", length = 512)
private String queryMessage;
public UserEntity getUser() {
return user;
}
......@@ -74,4 +87,28 @@ public class ASUserAttrEntity extends AbstractBaseEntity {
public void setValues(Set<ASUserAttrValueEntity> values) {
this.values = values;
}
public Date getLastQuery() {
return lastQuery;
}
public void setLastQuery(Date lastQuery) {
this.lastQuery = lastQuery;
}
public AttributeSourceQueryStatus getQueryStatus() {
return queryStatus;
}
public void setQueryStatus(AttributeSourceQueryStatus queryStatus) {
this.queryStatus = queryStatus;
}
public String getQueryMessage() {
return queryMessage;
}
public void setQueryMessage(String queryMessage) {
this.queryMessage = queryMessage;
}
}
package edu.kit.scc.webreg.as;
import java.io.Serializable;
public class AttributeSourceQueryLog implements Serializable {
private static final long serialVersionUID = 1L;
private AttributeSourceQueryStatus status;
private String message;
public AttributeSourceQueryStatus getStatus() {
return status;
}
public void setStatus(AttributeSourceQueryStatus status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
......@@ -9,7 +9,7 @@ import edu.kit.scc.webreg.exc.RegisterException;
public interface AttributeSourceWorkflow extends Serializable {
AttributeSourceQueryLog pollUserAttributes(ASUserAttrEntity asUserAttr,
void pollUserAttributes(ASUserAttrEntity asUserAttr,
ASUserAttrValueDao asValueDao, AttributeSourceAuditor auditor)
throws RegisterException;
}
......@@ -30,6 +30,7 @@ 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;
import edu.kit.scc.webreg.entity.as.AttributeSourceQueryStatus;
import edu.kit.scc.webreg.exc.RegisterException;
public class HttpUrlSingleAttributeSource extends
......@@ -38,10 +39,8 @@ public class HttpUrlSingleAttributeSource extends
private static final long serialVersionUID = 1L;
@Override
public AttributeSourceQueryLog pollUserAttributes(ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao, AttributeSourceAuditor auditor) throws RegisterException {
public void pollUserAttributes(ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao, AttributeSourceAuditor auditor) throws RegisterException {
AttributeSourceQueryLog queryLog = new AttributeSourceQueryLog();
init(asUserAttr);
UserEntity user = asUserAttr.getUser();
......@@ -59,16 +58,19 @@ public class HttpUrlSingleAttributeSource extends
engine.evaluate(velocityContext, out, "log", urlTemplate);
} catch (ParseErrorException e) {
logger.warn("Velocity problem", e);
queryLog.setStatus(AttributeSourceQueryStatus.FAIL);
return queryLog;
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.FAIL);
asUserAttr.setQueryMessage(e.getMessage());
return;
} catch (MethodInvocationException e) {
logger.warn("Velocity problem", e);
queryLog.setStatus(AttributeSourceQueryStatus.FAIL);
return queryLog;
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.FAIL);
asUserAttr.setQueryMessage(e.getMessage());
return;
} catch (ResourceNotFoundException e) {
logger.warn("Velocity problem", e);
queryLog.setStatus(AttributeSourceQueryStatus.FAIL);
return queryLog;
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.FAIL);
asUserAttr.setQueryMessage(e.getMessage());
return;
}
String url = out.toString();
......@@ -84,12 +86,14 @@ public class HttpUrlSingleAttributeSource extends
response = httpclient.execute(httpget);
} catch (ClientProtocolException e) {
logger.info("Problem", e);
queryLog.setStatus(AttributeSourceQueryStatus.FAIL);
return queryLog;
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.FAIL);
asUserAttr.setQueryMessage(e.getMessage());
return;
} catch (IOException e) {
logger.info("Problem", e);
queryLog.setStatus(AttributeSourceQueryStatus.FAIL);
return queryLog;
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.FAIL);
asUserAttr.setQueryMessage(e.getMessage());
return;
}
HttpEntity entity = response.getEntity();
......@@ -109,14 +113,15 @@ public class HttpUrlSingleAttributeSource extends
createOrUpdateValue(entry.getKey(), entry.getValue(), asUserAttr, asValueDao, auditor);
}
queryLog.setStatus(AttributeSourceQueryStatus.SUCCESS);
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.SUCCESS);
} catch (JsonMappingException e) {
/*
* Datasource generates invalid JSON
*/
logger.warn("Json Parse failed: {}", e.getMessage());
queryLog.setStatus(AttributeSourceQueryStatus.FAIL);
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.FAIL);
asUserAttr.setQueryMessage(e.getMessage());
}
} catch (ParseException e) {
throw new RegisterException(e);
......@@ -130,10 +135,9 @@ public class HttpUrlSingleAttributeSource extends
* probably no info for this user from datasource
*/
logger.debug("Status HttpUrlSingleAS is not OK. It is {} - {}", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
queryLog.setStatus(AttributeSourceQueryStatus.USER_NOT_FOUND);
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.USER_NOT_FOUND);
asUserAttr.setQueryMessage("Status HttpUrlSingleAS is " + response.getStatusLine().getStatusCode());
}
return queryLog;
}
}
......@@ -2,14 +2,13 @@ package edu.kit.scc.webreg.service.reg;
import java.io.Serializable;
import edu.kit.scc.webreg.as.AttributeSourceQueryLog;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.exc.RegisterException;
public interface AttributeSourceQueryService extends Serializable {
AttributeSourceQueryLog updateUserAttributes(UserEntity user,
void updateUserAttributes(UserEntity user,
AttributeSourceEntity attributeSource, String executor)
throws RegisterException;
}
......@@ -7,8 +7,6 @@ import javax.inject.Inject;
import org.slf4j.Logger;
import edu.kit.scc.webreg.as.AttributeSourceQueryLog;
import edu.kit.scc.webreg.as.AttributeSourceQueryStatus;
import edu.kit.scc.webreg.as.AttributeSourceWorkflow;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
import edu.kit.scc.webreg.bootstrap.ApplicationConfig;
......@@ -21,6 +19,7 @@ import edu.kit.scc.webreg.dao.as.AttributeSourceDao;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceQueryStatus;
import edu.kit.scc.webreg.entity.as.AttributeSourceServiceEntity;
import edu.kit.scc.webreg.exc.RegisterException;
import edu.kit.scc.webreg.service.reg.AttributeSourceQueryService;
......@@ -55,14 +54,27 @@ public class AttributeSourceQueryServiceImpl implements AttributeSourceQueryServ
private ApplicationConfig appConfig;
@Override
public AttributeSourceQueryLog updateUserAttributes(UserEntity user, AttributeSourceEntity attributeSource, String executor)
public void updateUserAttributes(UserEntity user, AttributeSourceEntity attributeSource, String executor)
throws RegisterException {
attributeSource = attributeSourceDao.findById(attributeSource.getId());
attributeSource = attributeSourceDao.findByIdWithAttrs(attributeSource.getId(), "asProps");
user = userDao.findById(user.getId());
ASUserAttrEntity asUserAttr = asUserAttrDao.findASUserAttr(user, attributeSource);
// Default expiry Time after for Attribute Update
Long expireTime = 30000L;
if (attributeSource.getAsProps() != null && attributeSource.getAsProps().containsKey("attribute_expire_time")) {
expireTime = Long.parseLong(attributeSource.getAsProps().get("attribute_expire_time"));
}
if (asUserAttr != null && asUserAttr.getLastQuery() != null &&
(System.currentTimeMillis() - asUserAttr.getLastQuery().getTime()) < expireTime) {
logger.info("Skipping attribute source query {} for user {}. Data not expired.", attributeSource.getName(), user.getEppn());
return;
}
if (asUserAttr == null) {
asUserAttr = asUserAttrDao.createNew();
asUserAttr.setAttributeSource(attributeSource);
......@@ -82,16 +94,15 @@ public class AttributeSourceQueryServiceImpl implements AttributeSourceQueryServ
auditor.setDetail("Updating attributes for user " + user.getEppn());
auditor.setAsUserAttr(asUserAttr);
AttributeSourceQueryLog queryLog = workflow.pollUserAttributes(asUserAttr, asValueDao, auditor);
workflow.pollUserAttributes(asUserAttr, asValueDao, auditor);
if (AttributeSourceQueryStatus.SUCCESS.equals(queryLog.getStatus())) {
if (AttributeSourceQueryStatus.SUCCESS.equals(asUserAttr.getQueryStatus())) {
asUserAttr.setLastSuccessfulQuery(new Date());
}
asUserAttr.setLastQuery(new Date());
asUserAttr = asUserAttrDao.persist(asUserAttr);
auditor.finishAuditTrail();
return queryLog;
}
public AttributeSourceWorkflow getWorkflowInstance(String className) {
......
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