[DURACOM-70] Resolved change requests

JavaDoc description on HdlResolverRestController
Replaced Gson with Jackson
Added exception logging and rethrowing
This commit is contained in:
Vincenzo Mecca
2022-06-21 11:58:54 +02:00
parent d4fb343f1e
commit 589ddd1a0d
3 changed files with 21 additions and 8 deletions

View File

@@ -10,8 +10,11 @@ package org.dspace.app.rest.hdlresolver;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import com.google.gson.Gson; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.utils.ContextUtil; import org.dspace.app.rest.utils.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@@ -21,6 +24,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* This controller is public and is useful for handler resolving,
* wheter a target handle identifier will be resolved into the
* corresponding URL (if found), otherwise will respond a null string.
*
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.it) * @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.it)
* *
*/ */
@@ -29,6 +36,8 @@ import org.springframework.web.bind.annotation.RestController;
public class HdlResolverRestController { public class HdlResolverRestController {
static final String BASE_PATH = "/hdlResolver/"; static final String BASE_PATH = "/hdlResolver/";
private static final Logger log = LogManager.getLogger();
@Autowired @Autowired
private HdlResolverService hdlResolverService; private HdlResolverService hdlResolverService;
@@ -66,13 +75,17 @@ public class HdlResolverRestController {
* @return One element list using String if found, else null String. * @return One element list using String if found, else null String.
*/ */
private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleResolver) { private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleResolver) {
String json = "null";
final String resolvedUrl = this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request), final String resolvedUrl = this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request),
handleResolver); handleResolver);
if (StringUtils.isNotEmpty(resolvedUrl)) { if (StringUtils.isNotEmpty(resolvedUrl)) {
return new Gson().toJson(List.of(resolvedUrl)); try {
} else { json = new ObjectMapper().writeValueAsString(List.of(resolvedUrl));
return "null"; } catch (JsonProcessingException e) {
log.error("Error during conversion of response!", e);
} }
} }
return json;
}
} }

View File

@@ -42,9 +42,9 @@ public class HdlResolverServiceImpl implements HdlResolverService {
try { try {
return this.handleService.resolveToURL(context, hdlResolver.getHandle()); return this.handleService.resolveToURL(context, hdlResolver.getHandle());
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error while resolving Handle: " + hdlResolver.getHandle()); log.error("Error while resolving Handle: " + hdlResolver.getHandle(), e);
throw new RuntimeException("Error while resolving Handle: " + hdlResolver.getHandle(), e);
} }
return null;
} }
} }

View File

@@ -65,6 +65,8 @@ public class HdlResolverRestControllerIT extends AbstractControllerIntegrationTe
.withAuthor("Smith, Donald").withAuthor("Doe, John").withSubject("ExtraEntry") .withAuthor("Smith, Donald").withAuthor("Doe, John").withSubject("ExtraEntry")
.withHandle("123456789/testHdlResolver").build(); .withHandle("123456789/testHdlResolver").build();
context.restoreAuthSystemState();
// ** END GIVEN ** // ** END GIVEN **
getClient() getClient()
@@ -72,8 +74,6 @@ public class HdlResolverRestControllerIT extends AbstractControllerIntegrationTe
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$[0]", StringContains.containsString("123456789/testHdlResolver"))); .andExpect(jsonPath("$[0]", StringContains.containsString("123456789/testHdlResolver")));
context.restoreAuthSystemState();
} }
/** /**