Skip to content

Commit

Permalink
Merge branch 'develop' into EA-3596_addReterivalMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
SrishtiSingh-eu authored Nov 17, 2023
2 parents 0ff5e70 + 50f9d1e commit 243500e
Show file tree
Hide file tree
Showing 22 changed files with 1,080 additions and 155 deletions.
60 changes: 60 additions & 0 deletions record-api-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,64 @@
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

<dependencies>
<!-- StringUtils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commomLang3.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.3</version>
</dependency>

<!-- Error module dependency-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-framework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>

<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<!-- latest log4j version included in spring boot starter 2.20.0-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package eu.europeana.api.config;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import eu.europeana.api.model.MediaType;
import eu.europeana.api.model.MediaTypes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


/**
* @author srishti singh
* @since 18 October 2023
*/
@Configuration
public class MediaTypeConfig {

private static final Logger LOG = LogManager.getLogger(MediaTypeConfig.class);

@Resource(name = "msXmlMapper")
private XmlMapper xmlMapper;


@Bean(name = "msMediaTypes")
public MediaTypes getMediaTypes() throws IOException {

MediaTypes mediaTypes;
try (InputStream inputStream = getClass().getResourceAsStream("/mediacategories.xml")) {
assert inputStream != null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String contents = reader.lines().collect(Collectors.joining(System.lineSeparator()));
mediaTypes = xmlMapper.readValue(contents, MediaTypes.class);
}
}

if (!mediaTypes.mediaTypeCategories.isEmpty()) {
mediaTypes.getMap().putAll(mediaTypes.mediaTypeCategories.stream().filter(media -> !media.isEuScreen()).collect(Collectors.toMap(MediaType::getMimeType, e-> e)));
} else {
LOG.error("media Categories not configured at startup. mediacategories.xml file not added or is empty");
}
return mediaTypes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package eu.europeana.api.config;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

@Configuration
public class SerialisationConfig {

private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX");

@Bean("msXmlMapper")
public XmlMapper xmlMapper() {
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setDateFormat(dateFormat);
return xmlMapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package eu.europeana.api.error;


import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.WebRequest;

import java.time.OffsetDateTime;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import static eu.europeana.api.error.EuropeanaErrorConstants.*;

@Component
public class EuropeanaApiErrorAttributes extends DefaultErrorAttributes {

/**
* Used by Spring to display errors with no custom handler.
* Since we explicitly return {@link EuropeanaApiErrorResponse} on errors within controllers, this method is only invoked when
* a request isn't handled by any controller.
*/
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions sbOptions) {
final Map<String, Object> defaultErrorAttributes = super.getErrorAttributes(webRequest, sbOptions);

// use LinkedHashMap to guarantee display order
LinkedHashMap<String, Object> europeanaErrorAttributes = new LinkedHashMap<>();
europeanaErrorAttributes.put(SUCCESS, false);
europeanaErrorAttributes.put(STATUS, defaultErrorAttributes.get(STATUS));
europeanaErrorAttributes.put(ERROR, defaultErrorAttributes.get(ERROR));
// message not shown
europeanaErrorAttributes.put(TIMESTAMP, OffsetDateTime.now());
addPathRequestParameters(europeanaErrorAttributes, webRequest);
return europeanaErrorAttributes;
}


/**
* Spring errors only return the error path and not the parameters, so we add those ourselves.
* The original parameter string is not available in WebRequest so we rebuild it.
*/
private void addPathRequestParameters(Map<String, Object> errorAttributes, WebRequest webRequest) {
Iterator<String> it = webRequest.getParameterNames();
StringBuilder s = new StringBuilder();
while (it.hasNext()) {
if (s.length() == 0) {
s.append('?');
} else {
s.append("&");
}
String paramName = it.next();
s.append(paramName);
String paramValue = webRequest.getParameter(paramName);
if (StringUtils.hasText(paramValue)) {
s.append("=").append(paramValue);
}
}

if (s.length() > 0) {
errorAttributes.put(PATH, errorAttributes.get(PATH) + s.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package eu.europeana.api.error;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import jakarta.servlet.http.HttpServletRequest;

import javax.annotation.PostConstruct;;

@RestController
public class EuropeanaApiErrorController extends AbstractErrorController {

private final EuropeanaApiErrorAttributes errorAttributes;
private ErrorAttributeOptions errorAttributeOptions = ErrorAttributeOptions.defaults();

@Value("${server.error.include-message}")
private ErrorProperties.IncludeAttribute includeMessage;
@Value("${server.error.include-exception}")
private Boolean includeException;
@Value("${server.error.include-stacktrace}")
private ErrorProperties.IncludeStacktrace includeStacktrace;

/**
* Initialize a new controller to handle error output
* @param errorAttributes auto-wired ApiErrorAttributes (error fields)
*/
@Autowired
public EuropeanaApiErrorController(EuropeanaApiErrorAttributes errorAttributes) {
super(errorAttributes);
this.errorAttributes = errorAttributes;
}

@PostConstruct
private void init() {
if (ErrorProperties.IncludeAttribute.ALWAYS.equals(includeMessage)) {
errorAttributeOptions = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE);
}
if (includeException) {
errorAttributeOptions = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.EXCEPTION);
}
if (ErrorProperties.IncludeStacktrace.ALWAYS.equals(includeStacktrace)) {
errorAttributeOptions = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.STACK_TRACE);
}
}

// @Override
// public String getErrorPath() {
// return "/error";
// }

/**
* Override default Spring-Boot error endpoint
* @param request incoming request
* @return error object to serialize
*/
@GetMapping("/error")
public Map<String, Object> error(HttpServletRequest request) {
ErrorAttributeOptions options = errorAttributeOptions;
if (ErrorProperties.IncludeAttribute.ON_PARAM.equals(includeMessage) && this.getMessageParameter(request)) {
options = errorAttributeOptions.including(ErrorAttributeOptions.Include.MESSAGE);
}
if (ErrorProperties.IncludeStacktrace.ON_PARAM.equals(includeStacktrace) && this.getTraceParameter(request)) {
options = errorAttributeOptions.including(ErrorAttributeOptions.Include.STACK_TRACE);
}
WebRequest webRequest = new ServletWebRequest(request);
return this.errorAttributes.getErrorAttributes(webRequest, options);
}

}
Loading

0 comments on commit 243500e

Please sign in to comment.