From 1b447b3f85bab7c66a857e740748156cca70f831 Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Wed, 12 Jun 2019 14:02:41 +0200
Subject: [PATCH] first pillar

---
 hsarback/src/de/hsadmin/pillar/JsonPillarServlet.java |   90 ++++++++++++++++++++++++++++++
 hsarback/conf/WEB-INF/test-web.xml                    |   20 +++---
 hsarback/conf/WEB-INF/prod-web.xml                    |   10 +++
 hsarback/conf/WEB-INF/dev-web.xml                     |   10 +++
 4 files changed, 120 insertions(+), 10 deletions(-)

diff --git a/hsarback/conf/WEB-INF/dev-web.xml b/hsarback/conf/WEB-INF/dev-web.xml
index 29fffc6..8aaecc2 100644
--- a/hsarback/conf/WEB-INF/dev-web.xml
+++ b/hsarback/conf/WEB-INF/dev-web.xml
@@ -37,12 +37,22 @@
         </init-param>
     </servlet>
 
+	<servlet>
+		<servlet-name>PillarServlet</servlet-name>
+		<servlet-class>de.hsadmin.pillar.JsonPillarServlet</servlet-class>
+	</servlet>
+
     <servlet-mapping>
         <servlet-name>Queue Status Servlet</servlet-name>
         <url-pattern>/queueStatus</url-pattern>
     </servlet-mapping>
 
     <servlet-mapping>
+        <servlet-name>PillarServlet</servlet-name>
+        <url-pattern>/pillar</url-pattern>
+    </servlet-mapping>
+
+    <servlet-mapping>
         <servlet-name>Queue PacTasks Servlet</servlet-name>
         <url-pattern>/queuePacTasks</url-pattern>
     </servlet-mapping>
diff --git a/hsarback/conf/WEB-INF/prod-web.xml b/hsarback/conf/WEB-INF/prod-web.xml
index 2431cfa..4a24530 100644
--- a/hsarback/conf/WEB-INF/prod-web.xml
+++ b/hsarback/conf/WEB-INF/prod-web.xml
@@ -39,11 +39,21 @@
         </init-param>
     </servlet>
 
+	<servlet>
+		<servlet-name>PillarServlet</servlet-name>
+		<servlet-class>de.hsadmin.pillar.JsonPillarServlet</servlet-class>
+	</servlet>
+
     <servlet-mapping>
         <servlet-name>Queue Status Servlet</servlet-name>
         <url-pattern>/queueStatus</url-pattern>
     </servlet-mapping>
 
+    <servlet-mapping>
+        <servlet-name>PillarServlet</servlet-name>
+        <url-pattern>/pillar</url-pattern>
+    </servlet-mapping>
+
 	<!--
     <servlet-mapping>
         <servlet-name>Queue PacTasks Servlet</servlet-name>
diff --git a/hsarback/conf/WEB-INF/test-web.xml b/hsarback/conf/WEB-INF/test-web.xml
index cb180a5..c341f4f 100644
--- a/hsarback/conf/WEB-INF/test-web.xml
+++ b/hsarback/conf/WEB-INF/test-web.xml
@@ -37,9 +37,19 @@
         </init-param>
     </servlet>
 
+	<servlet>
+		<servlet-name>PillarServlet</servlet-name>
+		<servlet-class>de.hsadmin.pillar.JsonPillarServlet</servlet-class>
+	</servlet>
+
     <servlet-mapping>
         <servlet-name>Queue Status Servlet</servlet-name>
         <url-pattern>/queueStatus</url-pattern>
+    </servlet-mapping>
+
+    <servlet-mapping>
+        <servlet-name>PillarServlet</servlet-name>
+        <url-pattern>/pillar</url-pattern>
     </servlet-mapping>
 
     <servlet-mapping>
@@ -96,14 +106,4 @@
         <res-sharing-scope>Shareable</res-sharing-scope>
     </resource-ref>
  	    
-	<security-constraint>
-       <web-resource-collection>
-           	<web-resource-name>Hostsharing Administrative Area</web-resource-name>
-        	<url-pattern>/*</url-pattern>
-       </web-resource-collection>
-       <user-data-constraint>
-           	<transport-guarantee>CONFIDENTIAL</transport-guarantee>
-       </user-data-constraint>
-   </security-constraint>
-
 </web-app>
diff --git a/hsarback/src/de/hsadmin/pillar/JsonPillarServlet.java b/hsarback/src/de/hsadmin/pillar/JsonPillarServlet.java
new file mode 100644
index 0000000..a90d863
--- /dev/null
+++ b/hsarback/src/de/hsadmin/pillar/JsonPillarServlet.java
@@ -0,0 +1,90 @@
+package de.hsadmin.pillar;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import de.hsadmin.core.model.Transaction;
+import de.hsadmin.core.qserv.ProcessorException;
+import de.hsadmin.mods.pac.Hive;
+import de.hsadmin.mods.pac.Pac;
+import de.hsadmin.mods.pac.PacComponent;
+
+public class JsonPillarServlet extends HttpServlet {
+
+	private static final long serialVersionUID = 1L;
+
+	@Override
+	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+		final Transaction transaction = new Transaction("pilalr");
+		final EntityManager em = transaction.getEntityManager();
+		final String hiveFqdn = req.getParameter("hive");
+		String hiveName = "";
+		if (hiveFqdn != null && hiveFqdn.length() == 19 && hiveFqdn.endsWith(".hostsharing.net") && hiveFqdn.startsWith("h")) {
+			hiveName = hiveFqdn.substring(0, 3);
+		} else {
+			throw new ServletException("error in hive parameter");
+		}
+		final Query hiveQuery = em.createQuery("SELECT h FROM Hives h WHERE h.name = :hive");
+		hiveQuery.setParameter("hive", hiveName);
+		final Object hiveResult = hiveQuery.getSingleResult();
+		final Hive hive = (Hive) hiveResult;
+		final Query pacsQuery = em.createQuery("SELECT p FROM Pacs p WHERE p.hive.name = :hive AND p.basepac.name = 'PAC/WEB'");
+		pacsQuery.setParameter("hive", hiveName);
+		final List<?> pacsResult = pacsQuery.getResultList();
+		resp.setContentType("text/plain");
+		final PrintWriter writer = resp.getWriter();
+		writer.println("{");
+		writer.println("  \"hivename\": \"" + hive.getHiveName() + "\"");
+		writer.println("  \"hiveipv4\": \"" + hive.getInetAddr().getInet4Addr() + "\"");
+		try {
+			writer.println("  \"hiveipv6\": \"" + hive.getInetAddr().getInet6Addr() + "\"");
+		} catch (ProcessorException e) {
+			// dont care
+		}
+		writer.println("  \"pacs\": [");
+		final Iterator<?> pacsIterator = pacsResult.iterator();
+		boolean firstLoop = true;
+		while (pacsIterator.hasNext()) {
+			final Object result = pacsIterator.next();
+			final Pac pac = (Pac) result;
+			if (firstLoop) {
+				firstLoop = false;
+				writer.println("    {");
+			} else {
+				writer.println("    , {");
+			}
+			writer.println("      \"pac\": \"" + pac.getName() + "\"");
+			writer.println("      , \"ipv4\": \"" + pac.getCurINetAddr().getInet4Addr() + "\"");
+			try {
+				writer.println("      , \"ipv6\": \"" + pac.getCurINetAddr().getInet6Addr() + "\"");
+			} catch (ProcessorException e) {
+				// dont care
+			}
+			final PacComponent pacQuota = pac.getPacComponent("QUOTA");
+			if (pacQuota != null) {
+				int quota = pacQuota.getQuantity();
+				writer.println("      , \"quota\": \"" + quota + "\"");
+			}
+			final PacComponent pacOffice = pac.getPacComponent("OFFICE");
+			if (pacOffice != null) {
+				int office = pacOffice.getQuantity();
+				writer.println("      , \"office\": \"" + office + "\"");
+			}
+			writer.println("    }");
+		}
+		writer.println("  ]");
+		writer.println("}");
+		writer.close();
+		em.close();
+	}
+	
+}

--
Gitblit v1.9.0-SNAPSHOT