Commit f09e50c5 authored by michael.simon's avatar michael.simon
Browse files

first attribute source workflow stub

parent 0ebdceba
package edu.kit.scc.webreg.as;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.exc.RegisterException;
import edu.kit.scc.webreg.service.reg.ldap.PropertyReader;
public abstract class AbstractAttributeSourceWorkflow implements AttributeSourceWorkflow {
private static final long serialVersionUID = 1L;
protected static final Logger logger = LoggerFactory.getLogger(AbstractAttributeSourceWorkflow.class);
protected PropertyReader prop;
public void init(AttributeSourceEntity attributeSource)
throws RegisterException {
prop = new PropertyReader(attributeSource.getAsProps());
}
}
package edu.kit.scc.webreg.as;
import java.io.Serializable;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.exc.RegisterException;
public interface AttributeSourceWorkflow extends Serializable {
public void pollUserAttributes(AttributeSourceEntity attributeSource, UserEntity user) throws RegisterException;
}
package edu.kit.scc.webreg.as;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.exc.RegisterException;
public class HttpUrlSingleAttributeSource extends
AbstractAttributeSourceWorkflow {
private static final long serialVersionUID = 1L;
@Override
public void pollUserAttributes(AttributeSourceEntity attributeSource,
UserEntity user) throws RegisterException {
init(attributeSource);
String urlTemplate = prop.readProp("url_template");
VelocityEngine engine = new VelocityEngine();
Map<String, Object> context = new HashMap<String, Object>();
context.put("user", user);
VelocityContext velocityContext = new VelocityContext(context);
StringWriter out = new StringWriter();
try {
engine.evaluate(velocityContext, out, "log", urlTemplate);
} catch (ParseErrorException e) {
logger.warn("Velocity problem", e);
return;
} catch (MethodInvocationException e) {
logger.warn("Velocity problem", e);
return;
} catch (ResourceNotFoundException e) {
logger.warn("Velocity problem", e);
return;
}
String url = out.toString();
RequestConfig config = RequestConfig.custom()
.setSocketTimeout(1000)
.setConnectTimeout(1000)
.build();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(config).build();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response;
try {
response = httpclient.execute(httpget);
} catch (ClientProtocolException e) {
throw new RegisterException(e);
} catch (IOException e) {
throw new RegisterException(e);
}
HttpEntity entity = response.getEntity();
if (response.getStatusLine() != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
if (entity != null) {
try {
String r = EntityUtils.toString(entity);
} catch (ParseException e) {
throw new RegisterException(e);
} catch (IOException e) {
throw new RegisterException(e);
}
}
}
}
}
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