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

added metadata functionality§

parent 2ff48340
Branches
Tags
3 merge requests!81New stable release,!43Merge dev to master,!27Draft: Resolve "Zenodo Sandbox integration for PID (e.g. DOI)"
Showing with 149 additions and 22 deletions
......@@ -4,6 +4,7 @@ import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.utils.ZenodoTemplateInterceptor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.Getter;
......@@ -17,9 +18,7 @@ import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilderFactory;
import javax.validation.constraints.NotNull;
import java.time.ZoneId;
import java.util.List;
import java.util.TimeZone;
@Log4j2
@Configuration
......@@ -46,8 +45,6 @@ public class ZenodoConfig {
public ObjectMapper objectMapper() {
return new ObjectMapper()
.findAndRegisterModules()
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
.setTimeZone(TimeZone.getTimeZone(ZoneId.of("Europe/Vienna")))
.registerModule(new JavaTimeModule())
.registerModule(new Jdk8Module());
}
......
......@@ -3,7 +3,7 @@ package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.FORBIDDEN)
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public class ZenodoApiException extends Exception {
public ZenodoApiException(String msg) {
......
package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class ZenodoNotFoundException extends Exception {
public ZenodoNotFoundException(String msg) {
super(msg);
}
public ZenodoNotFoundException(String msg, Throwable thr) {
super(msg, thr);
}
public ZenodoNotFoundException(Throwable thr) {
super(thr);
}
}
package at.tuwien.service;
import at.tuwien.api.zenodo.deposit.DepositDto;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface CitationService {
List<DepositDto> listCitations() throws ZenodoAuthenticationException, ZenodoApiException;
DepositDto storeCitation() throws ZenodoAuthenticationException, ZenodoApiException;
DepositDto deleteCitation(Long id) throws ZenodoAuthenticationException, ZenodoApiException;
}
package at.tuwien.service;
import org.springframework.stereotype.Service;
@Service
public interface FileService {
}
package at.tuwien.service;
import at.tuwien.api.zenodo.deposit.*;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoNotFoundException;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface MetadataService {
List<DepositResponseDto> listCitations() throws ZenodoAuthenticationException, ZenodoApiException;
DepositChangeResponseDto storeCitation() throws ZenodoAuthenticationException, ZenodoApiException;
DepositChangeResponseDto updateCitation(Long id, DepositChangeRequestDto data) throws ZenodoAuthenticationException,
ZenodoApiException, ZenodoNotFoundException;
void deleteCitation(Long id) throws ZenodoAuthenticationException, ZenodoApiException;
}
package at.tuwien.service;
import org.springframework.stereotype.Service;
@Service
public class ZenodoFileService implements FileService {
}
package at.tuwien.service;
import at.tuwien.api.zenodo.deposit.DepositDto;
import at.tuwien.api.zenodo.deposit.*;
import at.tuwien.config.ZenodoConfig;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
......@@ -13,21 +14,21 @@ import java.util.Arrays;
import java.util.List;
@Service
public class ZenodoService implements CitationService {
public class ZenodoMetadataService implements MetadataService {
private final RestTemplate zenodoRestTemplate;
private final ZenodoConfig zenodoConfig;
@Autowired
public ZenodoService(RestTemplate zenodoRestTemplate, ZenodoConfig zenodoConfig) {
public ZenodoMetadataService(RestTemplate zenodoRestTemplate, ZenodoConfig zenodoConfig) {
this.zenodoRestTemplate = zenodoRestTemplate;
this.zenodoConfig = zenodoConfig;
}
@Override
public List<DepositDto> listCitations() throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<DepositDto[]> response = zenodoRestTemplate.exchange("/api/deposit/depositions?access_token={token}",
HttpMethod.GET, null, DepositDto[].class, zenodoConfig.getApiKey());
public List<DepositResponseDto> listCitations() throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<DepositResponseDto[]> response = zenodoRestTemplate.exchange("/api/deposit/depositions?access_token={token}",
HttpMethod.GET, null, DepositResponseDto[].class, zenodoConfig.getApiKey());
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
......@@ -38,34 +39,50 @@ public class ZenodoService implements CitationService {
}
@Override
public DepositDto storeCitation() throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<DepositDto> response = zenodoRestTemplate.exchange("/api/deposit/depositions?access_token={token}",
HttpMethod.POST, new HttpEntity<>("{}"), DepositDto.class, zenodoConfig.getApiKey());
public DepositChangeResponseDto storeCitation() throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<DepositChangeResponseDto> response = zenodoRestTemplate.exchange("/api/deposit/depositions?access_token={token}",
HttpMethod.POST, new HttpEntity<>("{}"), DepositChangeResponseDto.class, zenodoConfig.getApiKey());
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
if (response.getStatusCode().equals(HttpStatus.BAD_REQUEST)) { // 400
throw new ZenodoApiException("Failed to store citation.");
}
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
if (response.getBody().getState().equals("error")) {
throw new ZenodoApiException("Status returned error or is unknown");
}
return response.getBody();
}
@Override
public DepositDto deleteCitation(Long id) throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<DepositDto> response = zenodoRestTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}",
HttpMethod.POST, null, DepositDto.class, id, zenodoConfig.getApiKey());
public DepositChangeResponseDto updateCitation(Long id, DepositChangeRequestDto data) throws ZenodoAuthenticationException,
ZenodoApiException, ZenodoNotFoundException {
final ResponseEntity<DepositChangeResponseDto> response = zenodoRestTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}",
HttpMethod.PUT, new HttpEntity<>(data), DepositChangeResponseDto.class, id, zenodoConfig.getApiKey());
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
if (response.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
throw new ZenodoNotFoundException("Could not get the citation.");
}
if (!response.getStatusCode().equals(HttpStatus.OK)) {
throw new ZenodoAuthenticationException("Could not update the citation.");
}
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
if (response.getBody().getState().equals("error")) {
throw new ZenodoApiException("Status returned error or is unknown");
}
return response.getBody();
}
@Override
public void deleteCitation(Long id) throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<String> response = zenodoRestTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}",
HttpMethod.DELETE, null, String.class, id, zenodoConfig.getApiKey());
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
throw new ZenodoApiException("Could not delete the deposit");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment