bema/func/log4j_logging #6
| @@ -14,6 +14,7 @@ | ||||
|     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||
|     <jackson.version>2.14.1</jackson.version> | ||||
|     <slf4j.version>2.0.6</slf4j.version> | ||||
|     <log4j.version>2.19.0</log4j.version> | ||||
|   </properties> | ||||
|  | ||||
|   <build> | ||||
| @@ -77,7 +78,7 @@ | ||||
|     <dependency> | ||||
|       <groupId>com.sparkjava</groupId> | ||||
|       <artifactId>spark-core</artifactId> | ||||
|       <version>2.9.3</version> | ||||
|       <version>2.9.4</version> | ||||
|     </dependency> | ||||
|  | ||||
|     <!--    JSON    --> | ||||
| @@ -133,8 +134,15 @@ | ||||
|     <dependency> | ||||
|       <groupId>org.apache.logging.log4j</groupId> | ||||
|       <artifactId>log4j-core</artifactId> | ||||
|       <version>2.19.0</version> | ||||
|       <version>${log4j.version}</version> | ||||
|     </dependency> | ||||
|  | ||||
|     <dependency> | ||||
|       <groupId>org.apache.logging.log4j</groupId> | ||||
|       <artifactId>log4j-api</artifactId> | ||||
|       <version>${log4j.version}</version> | ||||
|     </dependency> | ||||
|  | ||||
|  | ||||
|   </dependencies> | ||||
| </project> | ||||
| @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonMappingException; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import com.r11.tools.xslt.processors.Saxon; | ||||
| import com.r11.tools.xslt.processors.Xalan; | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.*; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| @@ -14,6 +16,9 @@ import java.util.Map; | ||||
|  * @author Wojciech Czop | ||||
|  */ | ||||
| public class SparkInitializer { | ||||
|  | ||||
|     private static final Logger LOG = LogManager.getLogger(SparkInitializer.class); | ||||
|  | ||||
|     /** | ||||
|      * Initializes spark framework | ||||
|      */ | ||||
| @@ -32,17 +37,19 @@ public class SparkInitializer { | ||||
|         Spark.post("/xpathpost", xpathHandler); | ||||
|         Spark.post("/xsdpost", xsdHandler); | ||||
|         Spark.get("/procinfo", procinfoHandler); | ||||
|         System.out.println("Server is online"); | ||||
|  | ||||
|         LOG.info("Server is online at port: " + Spark.port()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Handler that returns processor version | ||||
|      */ | ||||
|     private static Route procinfoHandler = (Request req, Response resp) -> { | ||||
|     private static final Route procinfoHandler = (Request req, Response resp) -> { | ||||
|         try { | ||||
|             resp.header("processor", "Saxon " + Saxon.getVersion() + " over s9api"); | ||||
|             return Saxon.getVersion(); | ||||
|         } catch (Exception ex) { | ||||
|             LOG.error("Error on retrieving engine version. " + ex); | ||||
|             return ex.getMessage(); | ||||
|         } | ||||
|     }; | ||||
| @@ -51,11 +58,10 @@ public class SparkInitializer { | ||||
|      * Handler that returns info if document is valid | ||||
|      * Also provides info about request time and processor | ||||
|      */ | ||||
|     private static Route xsdHandler = (Request req, Response resp) -> { | ||||
|     private static final Route xsdHandler = (Request req, Response resp) -> { | ||||
|         String body = req.body(); | ||||
|         System.out.println("Request: " + body); | ||||
|         ObjectMapper mapper = new ObjectMapper(); | ||||
|         Map<String, String> requestMap = null; | ||||
|         Map<String, String> requestMap = new HashMap<>(); | ||||
|         Map<String, String> responseMap = new HashMap<>(); | ||||
|         try { | ||||
|             requestMap = mapper.readValue(body, Map.class); | ||||
| @@ -65,8 +71,6 @@ public class SparkInitializer { | ||||
|  | ||||
|         String data = requestMap.get("data"); | ||||
|         String xsd = requestMap.get("process"); | ||||
|         String processor = requestMap.get("processor"); | ||||
|         String version = requestMap.get("version"); | ||||
|  | ||||
|         resp.header("processor", Xalan.getVersion()); | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
| @@ -76,12 +80,13 @@ public class SparkInitializer { | ||||
|             responseMap.put("result", tmp); | ||||
|             responseMap.put("status", "OK"); | ||||
|         } catch (Exception ex) { | ||||
|             System.out.println("Exception: "+ex.getMessage()); | ||||
|             LOG.error("Error on validation against XSD using Xalan. " + ex); | ||||
|             responseMap.put("result", ex.getMessage()); | ||||
|             responseMap.put("status", "ERR"); | ||||
|             resp.status(400); | ||||
|         } | ||||
|         long duration = System.currentTimeMillis() - timeStart; | ||||
|         LOG.info("Request: " + body + " processed in " + duration + " ms."); | ||||
|         responseMap.put("processor", Xalan.getVersion()); | ||||
|         responseMap.put("time", "" + duration); | ||||
|         resp.body(mapper.writeValueAsString(responseMap)); | ||||
| @@ -91,17 +96,15 @@ public class SparkInitializer { | ||||
|     /** | ||||
|      * Handler that returns output of xpath query and processor data | ||||
|      */ | ||||
|     private static Route xpathHandler = (Request req, Response resp) -> { | ||||
|  | ||||
|     private static final Route xpathHandler = (Request req, Response resp) -> { | ||||
|         String body = req.body(); | ||||
|         System.out.println("Request: " + body); | ||||
|         ObjectMapper mapper = new ObjectMapper(); | ||||
|         Map<String, String> requestMap = null; | ||||
|         Map<String, String> requestMap = new HashMap<>(); | ||||
|         Map<String, String> responseMap = new HashMap<>(); | ||||
|         try { | ||||
|             requestMap = mapper.readValue(body, Map.class); | ||||
|         } catch (JsonMappingException ex) { | ||||
|             ex.printStackTrace(); | ||||
|             LOG.error("JSON mapping error. " + ex); | ||||
|         } | ||||
|  | ||||
|         String data = requestMap.get("data"); | ||||
| @@ -109,6 +112,7 @@ public class SparkInitializer { | ||||
|         String processor = requestMap.get("processor"); | ||||
|         String version = requestMap.get("version"); | ||||
|  | ||||
|  | ||||
|         String tmp = ""; | ||||
|         long timeStart; | ||||
|         long duration; | ||||
| @@ -116,70 +120,66 @@ public class SparkInitializer { | ||||
|         if (processor == null) { | ||||
|             return "saxon, xalan"; | ||||
|         } | ||||
|         try { | ||||
|             switch (processor) { | ||||
|                 case "saxon": | ||||
|                     resp.header("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api"); | ||||
|                     System.out.print("Processing start..."); | ||||
|                     timeStart = System.currentTimeMillis(); | ||||
|                     try { | ||||
|                         tmp = Saxon.processXPath(data, query, version).trim(); | ||||
|                         responseMap.put("result", tmp); | ||||
|                         responseMap.put("status", "OK"); | ||||
|                     } catch (Exception ex) { | ||||
|                         responseMap.put("result", ex.getMessage()); | ||||
|                         responseMap.put("status", "ERR"); | ||||
|                         resp.status(400); | ||||
|                     } | ||||
|                     duration = System.currentTimeMillis() - timeStart; | ||||
|                     System.out.println("end(" + duration + ")"); | ||||
|                     responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api"); | ||||
|                     responseMap.put("time", "" + duration); | ||||
|                     resp.body(mapper.writeValueAsString(responseMap)); | ||||
|                     return resp; | ||||
|  | ||||
|                 case "xalan": | ||||
|                     resp.header("processor", Xalan.getVersion()); | ||||
|                     timeStart = System.currentTimeMillis(); | ||||
|                     try { | ||||
|                         tmp = Xalan.processXPath(data, query).trim(); | ||||
|                         responseMap.put("result", tmp); | ||||
|                         responseMap.put("status", "OK"); | ||||
|                     } catch (Exception ex) { | ||||
|                         responseMap.put("result", ex.getMessage()); | ||||
|                         responseMap.put("status", "ERR"); | ||||
|                         resp.status(400); | ||||
|                     } | ||||
|                     duration = System.currentTimeMillis() - timeStart; | ||||
|                     responseMap.put("processor", Xalan.getVersion()); | ||||
|         switch (processor) { | ||||
|             case "saxon": | ||||
|                 resp.header("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api"); | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|                 try { | ||||
|                     tmp = Saxon.processXPath(data, query, version).trim(); | ||||
|                     responseMap.put("result", tmp); | ||||
|                     responseMap.put("time", Long.toString(duration)); | ||||
|                     resp.body(mapper.writeValueAsString(responseMap)); | ||||
|                     return resp; | ||||
|                     responseMap.put("status", "OK"); | ||||
|                 } catch (Exception ex) { | ||||
|                     LOG.error("Error on processing XPath using Saxon. " + ex); | ||||
|                     responseMap.put("result", ex.getMessage()); | ||||
|                     responseMap.put("status", "ERR"); | ||||
|                     resp.status(400); | ||||
|                 } | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 LOG.info("Request" + body + " processed in " + duration + " ms."); | ||||
|                 responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api"); | ||||
|                 responseMap.put("time", "" + duration); | ||||
|                 resp.body(mapper.writeValueAsString(responseMap)); | ||||
|                 return resp; | ||||
|  | ||||
|                 default: | ||||
|                     return "saxon, xalan"; | ||||
|             } | ||||
|             case "xalan": | ||||
|                 resp.header("processor", Xalan.getVersion()); | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|                 try { | ||||
|                     tmp = Xalan.processXPath(data, query).trim(); | ||||
|                     responseMap.put("result", tmp); | ||||
|                     responseMap.put("status", "OK"); | ||||
|                 } catch (Exception ex) { | ||||
|                     LOG.error("Error on processing XPath using Xalan. " + ex); | ||||
|                     responseMap.put("result", ex.getMessage()); | ||||
|                     responseMap.put("status", "ERR"); | ||||
|                     resp.status(400); | ||||
|                 } | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 LOG.info("Request: " + body + " processed in " + duration + " ms."); | ||||
|                 responseMap.put("processor", Xalan.getVersion()); | ||||
|                 responseMap.put("result", tmp); | ||||
|                 responseMap.put("time", Long.toString(duration)); | ||||
|                 resp.body(mapper.writeValueAsString(responseMap)); | ||||
|                 return resp; | ||||
|  | ||||
|         } catch (Exception ex) { | ||||
|             ex.printStackTrace(); | ||||
|             return ex.getMessage(); | ||||
|             default: | ||||
|                 return "saxon, xalan"; | ||||
|         } | ||||
|     }; | ||||
|  | ||||
|     /** | ||||
|      * Handler that returns outcome of xslt transformation and processor data | ||||
|      */ | ||||
|     private static Route xsltHandler = (Request req, Response resp) -> { | ||||
|     private static final Route xsltHandler = (Request req, Response resp) -> { | ||||
|         String body = req.body(); | ||||
|         ObjectMapper mapper = new ObjectMapper(); | ||||
|         Map<String, String> jsonMap = null; | ||||
|         Map<String, String> jsonMap = new HashMap<>(); | ||||
|         Map<String, String> responseMap = new HashMap<>(); | ||||
|         try { | ||||
|             jsonMap = mapper.readValue(body, Map.class); | ||||
|             System.out.println(jsonMap); | ||||
|         } catch (JsonMappingException ex) { | ||||
|             ex.printStackTrace(); | ||||
|             LOG.error("JSON mapping error. " + ex); | ||||
|         } | ||||
|         String data = jsonMap.get("data"); | ||||
|         String query = jsonMap.get("process"); | ||||
| @@ -189,52 +189,51 @@ public class SparkInitializer { | ||||
|         if (processor == null) { | ||||
|             return "saxon, xalan"; | ||||
|         } | ||||
|         try { | ||||
|             String tmp; | ||||
|             long timeStart; | ||||
|             long duration; | ||||
|             switch (processor) { | ||||
|                 case "saxon": | ||||
|                     timeStart = System.currentTimeMillis(); | ||||
|                     try { | ||||
|                         tmp = Saxon.processXSLT(data, query); | ||||
|                         responseMap.put("result", tmp); | ||||
|                         responseMap.put("status", "OK"); | ||||
|                     } catch (Exception ex) { | ||||
|                         responseMap.put("result", ex.getMessage()); | ||||
|                         responseMap.put("status", "ERR"); | ||||
|                         resp.status(400); | ||||
|                     } | ||||
|  | ||||
|         String tmp; | ||||
|         long timeStart; | ||||
|         long duration; | ||||
|         switch (processor) { | ||||
|             case "saxon": | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|                 try { | ||||
|                     tmp = Saxon.processXSLT(data, query); | ||||
|                     responseMap.put("result", tmp); | ||||
|                     responseMap.put("status", "OK"); | ||||
|                 } catch (Exception ex) { | ||||
|                     LOG.error("Error on processing XSLT using Saxon. " + ex); | ||||
|                     responseMap.put("result", ex.getMessage()); | ||||
|                     responseMap.put("status", "ERR"); | ||||
|                     resp.status(400); | ||||
|                 } | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 LOG.info("Request: " + body + " processed in " + duration + " ms."); | ||||
|                 responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version); | ||||
|                 responseMap.put("time", Long.toString(duration)); | ||||
|                 resp.body(mapper.writeValueAsString(responseMap)); | ||||
|                 return resp; | ||||
|  | ||||
|                     duration = System.currentTimeMillis() - timeStart; | ||||
|                     responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version); | ||||
|                     responseMap.put("time", Long.toString(duration)); | ||||
|                     resp.body(mapper.writeValueAsString(responseMap)); | ||||
|                     return resp; | ||||
|             case "xalan": | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|                 try { | ||||
|                     tmp = Xalan.processXSLT(data, query); | ||||
|                     responseMap.put("result", tmp); | ||||
|                     responseMap.put("status", "OK"); | ||||
|                 } catch (Exception ex) { | ||||
|                     LOG.error("Error on processing XSLT using Xalan. " + ex); | ||||
|                     responseMap.put("result", ex.getMessage()); | ||||
|                     responseMap.put("status", "ERR"); | ||||
|                     resp.status(400); | ||||
|                 } | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 LOG.info("Request: " + body + " processed in " + duration + " ms."); | ||||
|                 responseMap.put("processor", Xalan.getVersion()); | ||||
|                 responseMap.put("time", Long.toString(duration)); | ||||
|                 resp.body(mapper.writeValueAsString(responseMap)); | ||||
|                 return resp; | ||||
|  | ||||
|                 case "xalan": | ||||
|                     timeStart = System.currentTimeMillis(); | ||||
|                     try { | ||||
|                         tmp = Xalan.processXSLT(data, query); | ||||
|                         responseMap.put("result", tmp); | ||||
|                         responseMap.put("status", "OK"); | ||||
|                     } catch (Exception ex) { | ||||
|                         responseMap.put("result", ex.getMessage()); | ||||
|                         responseMap.put("status", "ERR"); | ||||
|                         resp.status(400); | ||||
|                     } | ||||
|                     duration = System.currentTimeMillis() - timeStart; | ||||
|                     responseMap.put("processor", Xalan.getVersion()); | ||||
|                     responseMap.put("time", Long.toString(duration)); | ||||
|                     resp.body(mapper.writeValueAsString(responseMap)); | ||||
|                     return resp; | ||||
|  | ||||
|                 default: | ||||
|                     return "saxon, xalan"; | ||||
|             } | ||||
|         } catch (Exception ex) { | ||||
|             return ex.getMessage(); | ||||
|             default: | ||||
|                 return "saxon, xalan"; | ||||
|         } | ||||
|     }; | ||||
| } | ||||
|   | ||||
| @@ -1,11 +1,10 @@ | ||||
| package com.r11.tools.xslt.processors; | ||||
|  | ||||
| import net.sf.saxon.om.NamespaceBinding; | ||||
| import net.sf.saxon.om.NamespaceMap; | ||||
| import net.sf.saxon.s9api.XPathCompiler; | ||||
| import net.sf.saxon.s9api.XdmNode; | ||||
|  | ||||
| import java.util.Iterator; | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
|  | ||||
| /** | ||||
|  * Handler for saxon namespace scan engine. | ||||
| @@ -13,6 +12,7 @@ import java.util.Iterator; | ||||
|  * @author Wojciech Czop | ||||
|  */ | ||||
| public class NewNamespaceResolver { | ||||
|     private static final Logger LOG = LogManager.getLogger("NewNamespaceResolver"); | ||||
|  | ||||
|     private NamespaceMap namespaceMap; | ||||
|  | ||||
| @@ -21,12 +21,13 @@ public class NewNamespaceResolver { | ||||
|      * @param doc dom structure object | ||||
|      * @return map of namespaces | ||||
|      */ | ||||
|  | ||||
|     // TODO: Closer inspection. Return value is never used according to IntelliJ | ||||
|     // | ||||
|     public NamespaceMap process(XdmNode doc) { | ||||
|         namespaceMap = NamespaceMap.emptyMap(); | ||||
|         Iterator<XdmNode> it = doc.children().iterator(); | ||||
| //        TODO: remove | ||||
|         while (it.hasNext()) { | ||||
|             XdmNode tmp = it.next(); | ||||
|         //        TODO: remove | ||||
|         for (XdmNode tmp : doc.children()) { | ||||
|             extractNamespace(tmp); | ||||
|         } | ||||
| //        end | ||||
| @@ -38,12 +39,6 @@ public class NewNamespaceResolver { | ||||
|      * @param compiler compiler used to compile xpath statements | ||||
|      */ | ||||
|     public void exportNamespaces(XPathCompiler compiler){ | ||||
|         Iterator<NamespaceBinding> it = namespaceMap.iterator(); | ||||
| //        TODO: remove | ||||
|         while(it.hasNext()){ | ||||
|             System.out.println(it.next()); | ||||
|         } | ||||
| //        end | ||||
|         namespaceMap.forEach(namespaceBinding -> compiler.declareNamespace(namespaceBinding.getPrefix(), namespaceBinding.getURI())); | ||||
|     } | ||||
|  | ||||
| @@ -58,17 +53,11 @@ public class NewNamespaceResolver { | ||||
|         } | ||||
|         if (node.children().iterator().hasNext()) { | ||||
|  | ||||
|             Iterator<XdmNode> it = node.children().iterator(); | ||||
|             while (it.hasNext()) { | ||||
|                 XdmNode rNode = it.next(); | ||||
| //                TODO: remove | ||||
|                 if(rNode.getUnderlyingNode().getPrefix().isEmpty() && !rNode.getParent().getUnderlyingNode().getPrefix().isEmpty()){ | ||||
|  | ||||
|                     System.out.println("prefix missing, parent has "+rNode.getParent().getUnderlyingNode().getPrefix() + ", but child has none"); | ||||
|  | ||||
|                     System.out.println(); | ||||
|             for (XdmNode rNode : node.children()) { | ||||
|                 if (rNode.getUnderlyingNode().getPrefix().isEmpty() && !rNode.getParent().getUnderlyingNode().getPrefix().isEmpty()) { | ||||
|                     LOG.warn("Missing prefix. Parent has " + rNode.getParent().getUnderlyingNode().getPrefix() + ", but child has none"); | ||||
|                 } | ||||
| //                end | ||||
|  | ||||
|                 extractNamespace(rNode); | ||||
|             } | ||||
|         } | ||||
|   | ||||
| @@ -1,6 +1,9 @@ | ||||
| package com.r11.tools.xslt.processors; | ||||
|  | ||||
| import net.sf.saxon.s9api.*; | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
|  | ||||
| import javax.xml.transform.stream.StreamSource; | ||||
| import java.io.StringReader; | ||||
| import java.io.StringWriter; | ||||
|   | ||||
| @@ -1,5 +1,7 @@ | ||||
| package com.r11.tools.xslt.processors; | ||||
|  | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import org.w3c.dom.Document; | ||||
| import org.xml.sax.InputSource; | ||||
|  | ||||
| @@ -25,6 +27,7 @@ import java.io.StringWriter; | ||||
|  * @author Wojciech Czop | ||||
|  */ | ||||
| public class Xalan { | ||||
|  | ||||
|     /** | ||||
|      * Transforms string containing xml document via xslt | ||||
|      * @param data xml to be transformed | ||||
|   | ||||
							
								
								
									
										21
									
								
								Backend/xslt-rest/src/main/resources/log4j2.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Backend/xslt-rest/src/main/resources/log4j2.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <Configuration status="WARN"> | ||||
| 	<Appenders> | ||||
| 		<Console name="Console" target="SYSTEM_OUT"> | ||||
| 			<PatternLayout pattern="%d{HH:mm:ss.SSS} [%c] %-5level - %msg%n"/> | ||||
| 		</Console> | ||||
| 		<File name="File" fileName="/tmp/xml_tools_java_backend.log" append="true"> | ||||
| 			<PatternLayout> | ||||
| 				<Pattern>%d{HH:mm:ss.SSS} [%c] %-5level - %msg%n</Pattern> | ||||
| 			</PatternLayout> | ||||
| 		</File> | ||||
| 	</Appenders> | ||||
| 	<Loggers> | ||||
| 		<Logger name="com.r11.tools.xslt.SparkInitializer" level="info" additivity="true"> | ||||
| 			<AppenderRef ref="Console"/> | ||||
| 		</Logger> | ||||
| 		<Root level="info"> | ||||
| 			<AppenderRef ref="File"/> | ||||
| 		</Root> | ||||
| 	</Loggers> | ||||
| </Configuration> | ||||
		Reference in New Issue
	
	Block a user