From 51868b2493c5ccf4a75af6363d855466917a890b Mon Sep 17 00:00:00 2001 From: milanmajchrak <90026355+milanmajchrak@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:29:40 +0100 Subject: [PATCH] ufal/be-get-user-ip-address (#468) * Created endpoint for fetching IP address with tests. * Made the code more understable --- .../app/rest/ClarinUserInfoController.java | 56 +++++++++++++++++++ .../app/rest/ClarinUserInfoControllerIT.java | 34 +++++++++++ 2 files changed, 90 insertions(+) create mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinUserInfoController.java create mode 100644 dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserInfoControllerIT.java diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinUserInfoController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinUserInfoController.java new file mode 100644 index 000000000000..fc4bf5f19310 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinUserInfoController.java @@ -0,0 +1,56 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest; + +import java.io.IOException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * This class is a REST controller that returns information about the client user. + * E.g. the client's IP address. + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ +@RequestMapping(value = "/api/userinfo") +@RestController +public class ClarinUserInfoController { + + private final ObjectMapper objectMapper = new ObjectMapper(); + /** + * This method returns the client's IP address. + * @param request The HttpServletRequest object. + * @return The client's IP address. + */ + @RequestMapping(method = RequestMethod.GET, path = "/ipaddress") + public ResponseEntity getUserIPAddress(HttpServletRequest request, HttpServletResponse response) + throws IOException { + // Get client's IP address + String ipAddress = request.getRemoteAddr(); + if (StringUtils.isBlank(ipAddress)) { + String errorMessage = "Cannot get user's IP address"; + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMessage); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage); + } + + // Create JSON object using Jackson's ObjectNode + ObjectNode jsonObject = objectMapper.createObjectNode(); + jsonObject.put("ipAddress", ipAddress); + + return ResponseEntity.ok().body(jsonObject); + } +} diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserInfoControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserInfoControllerIT.java new file mode 100644 index 000000000000..be32cc55a656 --- /dev/null +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserInfoControllerIT.java @@ -0,0 +1,34 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import javax.ws.rs.core.MediaType; + +import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.junit.Test; + +/** + * This class test the REST controller that returns information about the client user. + * E.g. the client's IP address. + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ +public class ClarinUserInfoControllerIT extends AbstractControllerIntegrationTest { + + @Test + public void getUserIPAddress() throws Exception { + getClient().perform(get("/api/userinfo/ipaddress") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json("{\"ipAddress\":\"127.0.0.1\"}")); + } +}