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

nextcloud register workflow simple version is working

parent f15bc2db
......@@ -25,6 +25,7 @@ import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.auth.BasicScheme;
......@@ -110,6 +111,13 @@ public class NextcloudWorker {
return http;
}
protected HttpPut buildHttpPut(URI uri) {
HttpPut http = new HttpPut(uri);
http.setHeader("OCS-APIRequest", "true");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
return http;
}
public NextcloudAnswer createAccount(RegistryEntity registry) throws RegisterException {
String id = registry.getRegistryValues().get("id");
......@@ -132,7 +140,7 @@ public class NextcloudWorker {
HttpPost http = buildHttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userid", id));
params.add(new BasicNameValuePair("displayName", displayName));
params.add(new BasicNameValuePair("displayname", displayName));
params.add(new BasicNameValuePair("email", email));
http.setEntity(new UrlEncodedFormEntity(params));
......@@ -183,6 +191,79 @@ public class NextcloudWorker {
}
}
public NextcloudAnswer updateAccount(RegistryEntity registry) throws RegisterException {
String id = registry.getRegistryValues().get("id");
String displayName = registry.getRegistryValues().get("displayName");
String email = registry.getRegistryValues().get("email");
URI uri;
try {
URIBuilder uriBuilder = new URIBuilder(apiUrl + "users/" + id);
uri = uriBuilder.build();
} catch (URISyntaxException e) {
throw new RegisterException(e);
}
HttpClientContext context = buildHttpContext(uri);
CloseableHttpResponse response;
try {
HttpPut http = buildHttpPut(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userid", id));
params.add(new BasicNameValuePair("displayname", displayName));
params.add(new BasicNameValuePair("email", email));
http.setEntity(new UrlEncodedFormEntity(params));
response = httpClient.execute(http, context);
} catch (ClientProtocolException e) {
logger.warn("Client protocol problem", e);
throw new RegisterException(e);
} catch (SSLException e) {
logger.error("SSL Certificate problem with SNS Server: {}", e.toString());
throw new RegisterException(e);
} catch (IOException e) {
logger.warn("Connection", e);
throw new RegisterException(e);
}
logger.debug("Status line of response: {}", response.getStatusLine());
if (response.getStatusLine() != null && response.getStatusLine().getStatusCode() == 404) {
logger.warn("Status answer is 404, Account not found.");
return null;
} else if (response.getStatusLine() == null || response.getStatusLine().getStatusCode() != 200) {
logger.warn("Status answer was not HTTP OK 200");
throw new RegisterException("Nexcloud: " + response.getStatusLine());
}
HttpEntity entity = response.getEntity();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(NextcloudAnswer.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
NextcloudAnswer answer = (NextcloudAnswer) unmarshaller.unmarshal(entity.getContent());
logger.debug("{} {}", answer.getMeta().getStatusCode(), answer.getMeta().getStatus());
return answer;
} catch (ParseException e) {
logger.warn("Parse problem", e);
throw new RegisterException(e);
} catch (IOException e) {
logger.warn("Connection", e);
throw new RegisterException(e);
} catch (IllegalStateException e) {
logger.warn("Parse problem", e);
throw new RegisterException(e);
} catch (JAXBException e) {
logger.warn("Parse problem", e);
throw new RegisterException(e);
}
}
public NextcloudAnswer loadAccount(RegistryEntity registry) throws RegisterException {
String id = registry.getRegistryValues().get("id");
......
......@@ -66,7 +66,7 @@ public class NextcloudRegisterWorkflow implements RegisterUserWorkflow, Infotai
new InfotainmentTreeNode("ID", answer.getUser().getId(), node);
new InfotainmentTreeNode("Name", answer.getUser().getDisplayName(), node);
new InfotainmentTreeNode("E-Mail", answer.getUser().getEmail(), node);
if (answer.getUser().getQuota() != null) {
if (answer.getUser().getQuota() != null && answer.getUser().getQuota().getRelative() != null) {
new InfotainmentTreeNode("Verbrauchter Platz", "" + answer.getUser().getQuota().getRelative() + "%", node);
}
if (answer.getUser().getGroups() != null) {
......@@ -113,10 +113,7 @@ public class NextcloudRegisterWorkflow implements RegisterUserWorkflow, Infotai
// load the created account
answer = worker.loadAccount(registry);
if (answer.getMeta().getStatusCode() != 100) {
// this should not fail, the account was created with status 100
throw new RegisterException("Registration failed! Could not load newly created account: " + answer.getMeta().getMessage());
}
statusCode = answer.getMeta().getStatusCode();
}
if (statusCode != 100) {
......@@ -129,6 +126,8 @@ public class NextcloudRegisterWorkflow implements RegisterUserWorkflow, Infotai
logger.debug("Account for registry {} successfully loaded", registry.getId());
worker.updateAccount(registry);
if (registry.getRegistryValues().containsKey("primaryGroup")) {
logger.debug("Creating group {} for registry {}",
registry.getRegistryValues().containsKey("primaryGroup"), registry.getId());
......
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