-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from HHS/develop
Revise logging to use a JSON log and add version
- Loading branch information
Showing
18 changed files
with
247 additions
and
32 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
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
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 @@ | ||
${project.version} |
45 changes: 45 additions & 0 deletions
45
webui/src/main/java/gov/nih/nlm/lode/servlet/CountingServletOutputStream.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,45 @@ | ||
package gov.nih.nlm.lode.servlet; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.WriteListener; | ||
|
||
|
||
public class CountingServletOutputStream extends ServletOutputStream { | ||
private ServletOutputStream originalStream; | ||
private int count = 0; | ||
|
||
public CountingServletOutputStream(ServletOutputStream wrappedStream) { | ||
this.originalStream = wrappedStream; | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return originalStream.isReady(); | ||
} | ||
|
||
@Override | ||
public void setWriteListener(WriteListener writeListener) { | ||
originalStream.setWriteListener(writeListener); | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
count += 1; | ||
originalStream.write(b); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
originalStream.close(); | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
|
||
public void resetCount() { | ||
count = 0; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
webui/src/main/java/gov/nih/nlm/lode/servlet/DiagnosticHttpServletResponseWrapper.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 @@ | ||
package gov.nih.nlm.lode.servlet; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpServletResponseWrapper; | ||
|
||
public class DiagnosticHttpServletResponseWrapper extends HttpServletResponseWrapper { | ||
|
||
private CountingServletOutputStream countingStream; | ||
private long startTime; | ||
|
||
public DiagnosticHttpServletResponseWrapper(HttpServletResponse response) { | ||
super(response); | ||
countingStream = null; | ||
startTime = System.currentTimeMillis(); | ||
} | ||
|
||
public int getCount() { | ||
return (countingStream != null ? countingStream.getCount(): 0); | ||
} | ||
|
||
public long getResponseTime() { | ||
return System.currentTimeMillis() - startTime; | ||
} | ||
|
||
@Override | ||
public PrintWriter getWriter() throws IOException { | ||
return new PrintWriter(getOutputStream()); | ||
} | ||
|
||
@Override | ||
public ServletOutputStream getOutputStream() throws IOException { | ||
if (countingStream == null) { | ||
countingStream = new CountingServletOutputStream(super.getOutputStream()); | ||
} | ||
return countingStream; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
webui/src/main/java/gov/nih/nlm/lode/servlet/ServletUtils.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,16 @@ | ||
package gov.nih.nlm.lode.servlet; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
public class ServletUtils { | ||
|
||
public static String getClientAddress(HttpServletRequest request) { | ||
String v; | ||
if ((v = request.getHeader("X-Forwarded-For")) != null) { | ||
// parse and return | ||
return v.split(",")[0].trim(); | ||
} | ||
return request.getRemoteAddr(); | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
webui/src/main/java/gov/nih/nlm/lode/servlet/SparqlController.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 @@ | ||
package gov.nih.nlm.lode.servlet; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
import org.apache.log4j.MDC; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import com.hp.hpl.jena.query.QueryParseException; | ||
|
||
import uk.ac.ebi.fgpt.lode.exception.LodeException; | ||
import uk.ac.ebi.fgpt.lode.servlet.SparqlServlet; | ||
|
||
|
||
@Controller | ||
@RequestMapping("/query") | ||
public class SparqlController extends SparqlServlet { | ||
|
||
private Logger apilog = LoggerFactory.getLogger("gov.nih.nlm.lode.api"); | ||
|
||
@Override | ||
@RequestMapping | ||
public @ResponseBody | ||
void query( | ||
@RequestParam(value = "query", required = false) String query, | ||
@RequestParam(value = "format", required = false) String format, | ||
@RequestParam(value = "offset", required = false) Integer offset, | ||
@RequestParam(value = "limit", required = false) Integer limit, | ||
@RequestParam(value = "inference", required = false) boolean inference, | ||
HttpServletRequest request, | ||
HttpServletResponse response) throws QueryParseException, LodeException, IOException { | ||
String v; | ||
if ((v = request.getHeader("User-Agent")) != null) | ||
MDC.put("ua", v); | ||
if ((v = ServletUtils.getClientAddress(request)) != null) | ||
MDC.put("cliaddr", v); | ||
if ((v = request.getRequestedSessionId()) != null) | ||
MDC.put("requestedsession", v); | ||
v = request.getHeader("Referer"); | ||
MDC.put("webui", v != null && v.contains("/mesh/query")); | ||
if (query != null) | ||
MDC.put("query", query); | ||
if (format != null) | ||
MDC.put("format", format); | ||
if (limit != null) | ||
MDC.put("limit", limit); | ||
if (offset != null) | ||
MDC.put("offset", offset); | ||
MDC.put("inference", inference); | ||
HttpSession session = request.getSession(); | ||
if (session != null) { | ||
// Convert session datetime into a zoned date time | ||
ZonedDateTime zdt = ZonedDateTime.ofInstant( | ||
Instant.ofEpochMilli(session.getCreationTime()), | ||
ZoneId.systemDefault() | ||
); | ||
// print that as ISO8601 formatted timestamp | ||
MDC.put("sessiontime", zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); | ||
MDC.put("sessionid", session.getId()); | ||
} | ||
DiagnosticHttpServletResponseWrapper wrappedResponse = new DiagnosticHttpServletResponseWrapper(response); | ||
super.query(query, format, offset, limit, inference, request, wrappedResponse); | ||
MDC.put("responsesize", wrappedResponse.getCount()); | ||
MDC.put("responsetime", wrappedResponse.getResponseTime()); | ||
apilog.info("sparql query"); | ||
MDC.clear(); | ||
} | ||
} |
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
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
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
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 |
---|---|---|
|
@@ -159,3 +159,8 @@ ul#queries_list { | |
#example_queries h3 { | ||
margin-top: 0; | ||
} | ||
|
||
.version { | ||
float: right; | ||
padding-right: 15px; | ||
} |
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
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
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
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
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
Oops, something went wrong.