-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added REST Implementation with Privilege check for the Server logs en…
…d point. Added REST Implementation with Privilege check for the Server logs end point. Added REST Implementation with Privilege check for the Server logs end point. Added REST Implementation with Privilege check for the Server logs end point. Added REST Implementation with Privilege check for Server Logs end point. Added REST Implementation for Server logs Privilege check added for the REST end point updated the privilege check Comments updated Changed support class for the resource Minor fix in REST Constants
- Loading branch information
suthagar23
committed
Mar 18, 2018
1 parent
6fcc43c
commit 94805ff
Showing
5 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...rg/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ServerLogResource1_8.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,44 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; | ||
|
||
import org.openmrs.module.webservices.helper.ServerLogActionWrapper; | ||
import org.openmrs.module.webservices.rest.SimpleObject; | ||
import org.openmrs.module.webservices.rest.web.RequestContext; | ||
import org.openmrs.module.webservices.rest.web.RestConstants; | ||
import org.openmrs.module.webservices.rest.web.annotation.Resource; | ||
import org.openmrs.module.webservices.rest.web.resource.api.Listable; | ||
import org.openmrs.module.webservices.rest.web.response.ResponseException; | ||
|
||
/** | ||
* {@link Resource} for ServerLogController, supporting standard CRUD operations | ||
*/ | ||
@Resource(name = RestConstants.VERSION_1 + "/serverlog", supportedClass = ServerLogActionWrapper.class, supportedOpenmrsVersions = { | ||
"1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*", "2.2.*" }) | ||
public class ServerLogResource1_8 implements Listable { | ||
|
||
private ServerLogActionWrapper serverLogActionWrapper = new ServerLogActionWrapper(); | ||
|
||
public void setServerLogActionWrapper(ServerLogActionWrapper serverLogActionWrapper) { | ||
this.serverLogActionWrapper = serverLogActionWrapper; | ||
} | ||
|
||
@Override | ||
public SimpleObject getAll(RequestContext context) throws ResponseException { | ||
SimpleObject rest = new SimpleObject(); | ||
rest.put("serverLog", serverLogActionWrapper.getServerLogs()); | ||
return rest; | ||
} | ||
|
||
@Override | ||
public String getUri(Object instance) { | ||
return RestConstants.URI_PREFIX + "/serverlog"; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...1.8/src/test/java/org/openmrs/module/webservices/rest/web/MockServerLogActionWrapper.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,41 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.webservices.rest.web; | ||
|
||
import org.openmrs.module.webservices.helper.ServerLogActionWrapper; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/*** | ||
* MockServerLogActionWrapper used to run the Unit tests for ServerLogResource | ||
*/ | ||
public class MockServerLogActionWrapper extends ServerLogActionWrapper { | ||
|
||
public List<String> mockMemoryAppenderBuffer = new ArrayList<String>(); | ||
|
||
private ServerLogActionWrapper serverLogActionWrapper; | ||
|
||
/*** | ||
* Override method from ServerLogActionWrapper to get the logs from mockMemoryAppender | ||
* | ||
* @return List of log lines | ||
*/ | ||
@Override | ||
public List<String[]> getServerLogs() { | ||
List<String> logLines = mockMemoryAppenderBuffer; | ||
List<String[]> finalOutput = new ArrayList<String[]>(); | ||
serverLogActionWrapper = new ServerLogActionWrapper(); | ||
for (String logLine : logLines) { | ||
String[] logElements = serverLogActionWrapper.logLinePatternMatcher(logLine); | ||
finalOutput.add(logElements); | ||
} | ||
return finalOutput; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...penmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ServerLogResource1_8Test.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,69 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; | ||
|
||
import org.apache.struts.mock.MockHttpServletResponse; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.openmrs.module.webservices.helper.ServerLogActionWrapper; | ||
import org.openmrs.module.webservices.rest.SimpleObject; | ||
import org.openmrs.module.webservices.rest.web.MockServerLogActionWrapper; | ||
import org.openmrs.module.webservices.rest.web.api.RestService; | ||
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController; | ||
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Integration tests for the ServerLogResource class | ||
*/ | ||
public class ServerLogResource1_8Test extends BaseModuleWebContextSensitiveTest { | ||
|
||
@Autowired | ||
RestService restService; | ||
|
||
@Autowired | ||
private MainResourceController mainResourceController; | ||
|
||
private MockServerLogActionWrapper mockServerLogActionWrapper = new MockServerLogActionWrapper(); | ||
|
||
public String getURI() { | ||
return "serverlog"; | ||
} | ||
|
||
@Test | ||
public void testGetAll() { | ||
ServerLogResource1_8 serverLogResource = (ServerLogResource1_8) restService | ||
.getResourceBySupportedClass(ServerLogActionWrapper.class); | ||
serverLogResource.setServerLogActionWrapper(mockServerLogActionWrapper); | ||
|
||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
request.setMethod("GET"); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
SimpleObject result = mainResourceController.get(getURI(), request, response); | ||
|
||
ArrayList<String[]> serverLog = result.get("serverLog"); | ||
Assert.assertEquals(serverLog.size(), 0); | ||
|
||
String mockLogLine1 = "INFO - Simple.appender(115) |2018-03-03 15:44:54,834| Info Message"; | ||
// Add some mock log lines to mockMemoryAppenderBuffer | ||
mockServerLogActionWrapper.mockMemoryAppenderBuffer.add(mockLogLine1); | ||
result = mainResourceController.get(getURI(), request, response); | ||
serverLog = result.get("serverLog"); | ||
Assert.assertNotEquals(serverLog.size(), 0); | ||
|
||
String[] logLine1 = serverLog.get(0); | ||
Assert.assertNotEquals(logLine1[0], null); | ||
Assert.assertNotEquals(logLine1[1], null); | ||
Assert.assertNotEquals(logLine1[2], null); | ||
Assert.assertNotEquals(logLine1[3], null); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
omod-common/src/main/java/org/openmrs/module/webservices/helper/ServerLogActionWrapper.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,80 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.webservices.helper; | ||
|
||
import org.apache.log4j.Appender; | ||
import org.apache.log4j.Logger; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.module.webservices.rest.web.RestConstants; | ||
import org.openmrs.util.MemoryAppender; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
/*** | ||
* ServerLogActionWrapper used to serve the Server logs | ||
*/ | ||
public class ServerLogActionWrapper { | ||
|
||
/*** | ||
* Get server logs | ||
* | ||
* @return List of last hundred server logs | ||
*/ | ||
public List<String[]> getServerLogs() { | ||
// Check the GET_SERVER_LOGS privilege to serve the server logs | ||
Context.requirePrivilege(RestConstants.GET_SERVER_LOGS); | ||
// Use the Memory Appender to retrieve the logs | ||
Appender appender = Logger.getRootLogger().getAppender("MEMORY_APPENDER"); | ||
if (appender instanceof MemoryAppender) { | ||
MemoryAppender memoryAppender = (MemoryAppender) appender; | ||
List<String> logLines = memoryAppender.getLogLines(); | ||
List<String[]> finalOutput = new ArrayList<String[]>(); | ||
for (String logLine : logLines) { | ||
String[] logElements = logLinePatternMatcher(logLine); | ||
finalOutput.add(logElements); | ||
} | ||
return finalOutput; | ||
} | ||
else { | ||
return new ArrayList<String[]>(); | ||
} | ||
} | ||
|
||
/*** | ||
* Match and find the patterns for log line | ||
* | ||
* @param logLine Log lines from the terminal | ||
* @return Array of matched patterns | ||
*/ | ||
public String[] logLinePatternMatcher(String logLine) { | ||
String[] logElements = new String[4]; | ||
// Defined Pattern to analyze | ||
String regExPatternType = "(INFO|ERROR|WARN|DEBUG)\\s.*?[-].*?\\s((?:[A-z][A-z].+))\\s[|](.*?)[|]\\s((.*\\n*)+)"; | ||
try { | ||
Pattern pattern = Pattern.compile(regExPatternType); | ||
Matcher matcher = pattern.matcher(logLine); | ||
if (matcher.find()) { | ||
// If pattern matches to the message | ||
logElements[0] = matcher.group(1); | ||
logElements[1] = matcher.group(2); | ||
logElements[2] = matcher.group(3); | ||
logElements[3] = matcher.group(4); | ||
} | ||
return logElements; | ||
} | ||
catch (PatternSyntaxException e) { | ||
// In case of Exception, It will return empty array | ||
return logElements; | ||
} | ||
} | ||
} |
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