Commit cc8bd127 authored by Gerrit Hübbers's avatar Gerrit Hübbers 🃏
Browse files

WIP. Implement login and create-new-xmlworkflowitem methods

parent 509b21cf
package org.gesis.dda.feeder.ssoar;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor {
final static Logger log = LoggerFactory.getLogger(LoggingRequestInterceptor.class);
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
traceRequest(request, body);
ClientHttpResponse response = execution.execute(request, body);
traceResponse(response);
return response;
}
private void traceRequest(HttpRequest request, byte[] body) throws IOException {
log.debug("===========================request begin================================================");
log.debug("URI : {}", request.getURI());
log.debug("Method : {}", request.getMethod());
log.debug("Headers : {}", request.getHeaders() );
log.debug("Request body: {}", new String(body, "UTF-8"));
log.debug("==========================request end================================================");
}
private void traceResponse(ClientHttpResponse response) throws IOException {
StringBuilder inputStringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8"));
String line = bufferedReader.readLine();
while (line != null) {
inputStringBuilder.append(line);
inputStringBuilder.append('\n');
line = bufferedReader.readLine();
}
log.debug("============================response begin==========================================");
log.debug("Status code : {}", response.getStatusCode());
log.debug("Status text : {}", response.getStatusText());
log.debug("Headers : {}", response.getHeaders());
log.debug("Response body: {}", inputStringBuilder.toString());
log.debug("=======================response end=================================================");
}
}
package org.gesis.dda.feeder.ssoar;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.gesis.dda.feeder.ssoar.domain.Collection;
import org.gesis.dda.feeder.ssoar.domain.User;
import org.gesis.dda.feeder.ssoar.domain.XmlWorkflowItem;
import org.gesis.dda.wizard.config.IngesterProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class SsoarRestService {
private final static Logger log = LoggerFactory.getLogger(SsoarRestService.class);
private RestTemplate restTemplate;
private String endpoint;
private User user;
private String authenticationToken;
private boolean isAuthenticated = false;
@Inject
public SsoarRestService(IngesterProperties ingesterProperties) {
restTemplate = new RestTemplate();
this.endpoint = ingesterProperties.getEndpoint();
log.debug("SSOAR REST endpoint: {}", endpoint);
this.user = new User( ingesterProperties.getEmail(), ingesterProperties.getPassword() );
//restTemplate = new RestTemplate();
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new LoggingRequestInterceptor());
restTemplate.setInterceptors(interceptors);
}
public boolean login() {
boolean wasSuccessful = false;
isAuthenticated = false;
ResponseEntity<String> authenticationTokenResponseEntity = restTemplate.postForEntity( endpoint + "/login", user, String.class );
HttpStatus loginStatus = authenticationTokenResponseEntity.getStatusCode();
log.debug("Login status: {}", loginStatus);
if ( loginStatus.is2xxSuccessful() ) {
isAuthenticated = true;
authenticationToken = authenticationTokenResponseEntity.getBody();
}
log.debug("Authentication token: {}", authenticationToken);
return isAuthenticated;
}
public XmlWorkflowItem createNewXmlWorkflowItemInCollection(Collection targetCollection) {
//ResponseEntity<XmlWorkflowItem> xmlWorkflowItemResponseEntity = restTemplate.postForEntity( endpoint + "/xmlworkflowitems?collection_id={}", user, XmlWorkflowItem.class, targetCollection.getId() );
// .addHeader("rest-dspace-token", DUMMY_EDITOR_AUTHENTICATION_TOKEN_LOCALHOST)
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
return wasSuccessful;
HttpEntity<Collection> entity = new HttpEntity<>(targetCollection, headers);
ResponseEntity<XmlWorkflowItem> xmlWorkflowItemResponseEntity = restTemplate.postForEntity( endpoint + "/xmlworkflowitems?collection_id={targetCollectionId}", entity, XmlWorkflowItem.class, targetCollection.getId() );
XmlWorkflowItem xmlWorkflowItem = xmlWorkflowItemResponseEntity.getBody();
log.debug("new XmlWorkflowItem: {}", xmlWorkflowItem);
return xmlWorkflowItem;
}
}
......@@ -66,6 +66,7 @@ public class DSpaceObject {
this.expand = expand;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
......
package org.gesis.dda.feeder.ssoar.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
private String email;
private String password;
public User(String email, String password) {
this.email = email;
this.password = password;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
}
package org.gesis.dda.feeder.ssoar.domain;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
......@@ -21,4 +23,9 @@ public class XmlWorkflowItem {
this.item = item;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
package org.gesis.dda.wizard;
import org.gesis.dda.wizard.config.Constants;
import org.gesis.dda.wizard.config.IngesterProperties;
import org.gesis.dda.wizard.config.JHipsterProperties;
import org.slf4j.Logger;
......@@ -23,9 +24,9 @@ import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collection;
@ComponentScan
@ComponentScan(basePackages="org.gesis.dda")
@EnableAutoConfiguration(exclude = { MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class })
@EnableConfigurationProperties({ JHipsterProperties.class, LiquibaseProperties.class })
@EnableConfigurationProperties({ JHipsterProperties.class, LiquibaseProperties.class, IngesterProperties.class })
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
......
......@@ -5,8 +5,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "ingester", ignoreUnknownFields = false)
public class IngesterProperties {
// for a REST endpoint such as "http://www.ssoar.info/rest, *this* endpoint field must be set to "http://www.ssoar.info", i.e. without trailing slash
private String endpoint;
private String username;
private String email;
private String password;
public String getEndpoint() {
......@@ -17,12 +18,12 @@ public class IngesterProperties {
this.endpoint = endpoint;
}
public String getUsername() {
return username;
public String getEmail() {
return email;
}
public void setUsername(String username) {
this.username = username;
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
......
......@@ -4,15 +4,21 @@ import static org.junit.Assert.assertEquals;
import javax.inject.Inject;
import org.gesis.dda.feeder.ssoar.domain.Collection;
import org.gesis.dda.feeder.ssoar.domain.XmlWorkflowItem;
import org.gesis.dda.wizard.Application;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
......@@ -20,12 +26,30 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional
public class SsoarRestServiceIntTest {
private final static Logger log = LoggerFactory.getLogger(SsoarRestServiceIntTest.class);
private final static Collection MONOGRAPHY_COLLECTION = new Collection();
static {
MONOGRAPHY_COLLECTION.setId(2);
}
@Inject
private SsoarRestService ssoarRestService;
@Before
public void login() {
ssoarRestService.login();
}
@Test
private void logInSuccessfully() {
public void logInSuccessfully() {
boolean successfullyLoggedIn = ssoarRestService.login();
assertEquals(true, successfullyLoggedIn);
}
@Test
public void createNewXmlWorkflowItemInMonographyCollection() {
XmlWorkflowItem createdXmlWorkflowItem = ssoarRestService.createNewXmlWorkflowItemInCollection(MONOGRAPHY_COLLECTION);
log.debug("created XmlWorkflowItem: {}", createdXmlWorkflowItem);
}
}
......@@ -78,6 +78,6 @@ jhipster:
# ===================================================================
ingester:
endpoint: http://svko-dda-test.gesis.intra
username: dummy-editor@example.com
endpoint: http://ssoar.svko-dda-test.gesis.intra/rest
email: dummy-editor@example.com
password: dummy-editor
......@@ -4,6 +4,7 @@
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.gesis" level="DEBUG"/>
<logger name="org.springframework.web.client.RestTemplate" level="DEBUG"/>
<logger name="org.hibernate.ejb.HibernatePersistence" level="OFF"/>
<logger name="org.apache.catalina.startup.DigesterFactory" level="OFF"/>
......
Supports Markdown
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