Commit 35b7b4d5 authored by michael.simon's avatar michael.simon
Browse files

implement create account method

parent 57a11443
......@@ -3,6 +3,8 @@ package edu.kit.scc.nextcloud;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLException;
import javax.xml.bind.JAXBContext;
......@@ -11,6 +13,7 @@ import javax.xml.bind.Unmarshaller;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
......@@ -18,8 +21,10 @@ import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
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.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.auth.BasicScheme;
......@@ -27,6 +32,7 @@ import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -91,10 +97,90 @@ public class NextcloudWorker {
}
protected HttpGet buildHttpGet(URI uri) {
HttpGet httpGet = new HttpGet(uri);
httpGet.setHeader("OCS-APIRequest", "true");
httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
return httpGet;
HttpGet http = new HttpGet(uri);
http.setHeader("OCS-APIRequest", "true");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
return http;
}
protected HttpPost buildHttpPost(URI uri) {
HttpPost http = new HttpPost(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");
String displayName = registry.getRegistryValues().get("displayName");
String email = registry.getRegistryValues().get("email");
URI uri;
try {
URIBuilder uriBuilder = new URIBuilder(apiUrl + "users");
uri = uriBuilder.build();
} catch (URISyntaxException e) {
throw new RegisterException(e);
}
HttpClientContext context = buildHttpContext(uri);
CloseableHttpResponse response;
try {
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("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 {
......
......@@ -87,7 +87,10 @@ public class NextcloudRegisterWorkflow implements RegisterUserWorkflow, Infotai
if (statusCode == 404) {
// user not found, needs to be created
answer = worker.createAccount(registry);
if (answer.getMeta().getStatusCode() != 100) {
throw new RegisterException("Registration failed! " + answer.getMeta().getMessage());
}
} else if (statusCode == 100) {
// user found, ok
} else {
......@@ -121,6 +124,9 @@ public class NextcloudRegisterWorkflow implements RegisterUserWorkflow, Infotai
logger.warn("Nextcloud User ID for user {} would change from {} to {}!", registry.getRegistryValues().get("id"), id);
}
}
registry.getRegistryValues().put("displayName", user.getGivenName() + " " + user.getSurName());
registry.getRegistryValues().put("email", user.getEmail());
return false;
}
......
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