Skip to content
Snippets Groups Projects
Unverified Commit 96ee52b8 authored by Martin Weise's avatar Martin Weise
Browse files

Templateinterceptor

parent 03665705
No related branches found
No related tags found
3 merge requests!81New stable release,!43Merge dev to master,!27Draft: Resolve "Zenodo Sandbox integration for PID (e.g. DOI)"
...@@ -14,6 +14,8 @@ COPY --from=dependency /root/.m2/repository/at/tuwien /root/.m2/repository/at/tu ...@@ -14,6 +14,8 @@ COPY --from=dependency /root/.m2/repository/at/tuwien /root/.m2/repository/at/tu
COPY ./rest-service ./rest-service COPY ./rest-service ./rest-service
COPY ./services ./services COPY ./services ./services
COPY ./report ./report COPY ./report ./report
COPY ./gateway ./gateway
COPY ./api ./api
RUN mvn -q clean package -DskipTests RUN mvn -q clean package -DskipTests
......
...@@ -11,9 +11,4 @@ ...@@ -11,9 +11,4 @@
<artifactId>api</artifactId> <artifactId>api</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project> </project>
\ No newline at end of file
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId> <artifactId>gateway</artifactId>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>at.tuwien</groupId> <groupId>at.tuwien</groupId>
...@@ -19,9 +20,4 @@ ...@@ -19,9 +20,4 @@
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project> </project>
\ No newline at end of file
package at.tuwien.config; package at.tuwien.config;
import at.tuwien.gateway.ZenodoTemplateInterceptor;
import lombok.Getter; import lombok.Getter;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
...@@ -9,6 +10,7 @@ import org.springframework.web.client.RestTemplate; ...@@ -9,6 +10,7 @@ import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriTemplateHandler; import org.springframework.web.util.DefaultUriTemplateHandler;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.List;
@Configuration @Configuration
public class ZenodoConfig { public class ZenodoConfig {
...@@ -27,9 +29,11 @@ public class ZenodoConfig { ...@@ -27,9 +29,11 @@ public class ZenodoConfig {
public RestTemplate zenodo() { public RestTemplate zenodo() {
DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler(); DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler();
defaultUriTemplateHandler.setBaseUrl(zenodoEndpoint); defaultUriTemplateHandler.setBaseUrl(zenodoEndpoint);
return new RestTemplateBuilder() final RestTemplate template = new RestTemplateBuilder()
.uriTemplateHandler(defaultUriTemplateHandler) .uriTemplateHandler(defaultUriTemplateHandler)
.build(); .build();
template.setInterceptors(List.of(new ZenodoTemplateInterceptor()));
return template;
} }
} }
...@@ -5,12 +5,12 @@ import at.tuwien.config.ZenodoConfig; ...@@ -5,12 +5,12 @@ import at.tuwien.config.ZenodoConfig;
import at.tuwien.exception.ZenodoApiException; import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException; import at.tuwien.exception.ZenodoAuthenticationException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod; import org.springframework.http.*;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
@Component @Component
...@@ -37,4 +37,17 @@ public class ZenodoGateway { ...@@ -37,4 +37,17 @@ public class ZenodoGateway {
return Arrays.asList(response.getBody()); return Arrays.asList(response.getBody());
} }
public DepositDto createDeposit() throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<DepositDto> response = zenodoRestTemplate.exchange("/api/deposit/depositions?access_token={token}",
HttpMethod.POST, null, DepositDto.class, zenodoConfig.getZenodoApiKey());
if (response.getStatusCode().is4xxClientError()) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
return response.getBody();
}
} }
package at.tuwien.gateway;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class ZenodoTemplateInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
ClientHttpResponse response = execution.execute(request, body);
response.getHeaders().add("Content-Type", "application/json");
response.getHeaders().add("Accept", "application/json");
return response;
}
}
...@@ -45,8 +45,13 @@ public class ZenodoIntegrationTest extends BaseUnitTest { ...@@ -45,8 +45,13 @@ public class ZenodoIntegrationTest extends BaseUnitTest {
/* test */ /* test */
final List<DepositDto> response = zenodoGateway.listDeposits(); final List<DepositDto> response = zenodoGateway.listDeposits();
assertEquals(1, response.size()); }
assertEquals(DEPOSIT_1_ID, response.get(0).getId());
@Test
public void createDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException {
/* test */
final DepositDto response = zenodoGateway.createDeposit();
} }
} }
\ No newline at end of file
...@@ -74,4 +74,24 @@ public class ZenodoUnitTest extends BaseUnitTest { ...@@ -74,4 +74,24 @@ public class ZenodoUnitTest extends BaseUnitTest {
}); });
} }
@Test
public void createDeposit_succeed() throws ZenodoApiException, ZenodoAuthenticationException {
when(zenodoTemplate.exchange(anyString(), eq(HttpMethod.POST), eq(null), eq(DepositDto.class), anyString()))
.thenReturn(ResponseEntity.ok(DEPOSIT_1_DTO));
/* test */
final DepositDto response = zenodoGateway.createDeposit();
}
@Test
public void createDeposit_noToken_fails() {
when(zenodoTemplate.exchange(anyString(), eq(HttpMethod.POST), eq(null), eq(DepositDto.class), anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
/* test */
assertThrows(ZenodoAuthenticationException.class, () -> {
zenodoGateway.createDeposit();
});
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment