forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created endpoint for fetching IP address with tests.
- Loading branch information
1 parent
6eb3726
commit 431e145
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinUserInfoController.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,49 @@ | ||
/** | ||
* 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.google.gson.JsonObject; | ||
import org.apache.commons.lang3.StringUtils; | ||
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 { | ||
|
||
/** | ||
* 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)) { | ||
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Cannot get user's IP address"); | ||
} | ||
// Create JSON object | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("ipAddress", ipAddress); | ||
return ResponseEntity.ok(jsonObject.toString()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserInfoControllerIT.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,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\"}")); | ||
} | ||
} |