-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into EA-3596_addReterivalMethods
- Loading branch information
Showing
22 changed files
with
1,080 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
record-api-common/src/main/java/eu/europeana/api/config/MediaTypeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
record-api-common/src/main/java/eu/europeana/api/config/SerialisationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
record-api-common/src/main/java/eu/europeana/api/error/EuropeanaApiErrorAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
record-api-common/src/main/java/eu/europeana/api/error/EuropeanaApiErrorController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.