Strabon
changeset 1314:3e3fe6213ac1
A new tab to configure the database and a function to check if you run in localhost. If you do the endpoint doesnt ask for credentials.
author | George Stamoulis <gstam@di.uoa.gr> |
---|---|
date | Tue Mar 11 10:50:45 2014 +0200 (2014-03-11) |
parents | 3f3fc8237460 |
children | 9bfbe553dfec |
files | endpoint/WebContent/WEB-INF/beans.xml endpoint/WebContent/connection.jsp endpoint/WebContent/query.jsp endpoint/WebContent/store.jsp endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/ConnectionBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StoreBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/UpdateBean.java |
line diff
1.1 --- a/endpoint/WebContent/WEB-INF/beans.xml Wed Feb 26 16:31:08 2014 +0200 1.2 +++ b/endpoint/WebContent/WEB-INF/beans.xml Tue Mar 11 10:50:45 2014 +0200 1.3 @@ -186,7 +186,7 @@ 1.4 <!-- Statement --> 1.5 <value><![CDATA[PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 1.6 1.7 -SELECT (?o AS ?class) 1.8 +SELECT DISTINCT (?o AS ?class) 1.9 WHERE { 1.10 ?s rdf:type ?o 1.11 } 1.12 @@ -225,7 +225,7 @@ 1.13 <list> 1.14 <!-- Link to bean: two arguemnts --> 1.15 <value>Configuration</value> 1.16 - <value>ChangeConnection</value> 1.17 + <value>connection.jsp</value> 1.18 </list> 1.19 </list> 1.20 </constructor-arg>
2.1 --- a/endpoint/WebContent/connection.jsp Wed Feb 26 16:31:08 2014 +0200 2.2 +++ b/endpoint/WebContent/connection.jsp Tue Mar 11 10:50:45 2014 +0200 2.3 @@ -44,7 +44,29 @@ 2.4 <input type="hidden" name="query" value="<%=request.getAttribute("query")%>"/> 2.5 <input type="hidden" name="handle" value="<%=request.getAttribute("handle")%>"/> 2.6 <input type="hidden" name="format" value="<%=request.getAttribute("format")%>"/> 2.7 -<TABLE class="style4"> 2.8 +<TABLE class="style4"> 2.9 + <% if (request.getAttribute("error") != null) {%> 2.10 + <!-- Error Message --> 2.11 + <TR><TD colspan=2> 2.12 + <CENTER><P style="color: red;"><%=request.getAttribute("error") %></P></CENTER> 2.13 + </TD></TR> 2.14 + <!-- Error Message --> 2.15 + <%}%> 2.16 + 2.17 + <%if (request.getAttribute("info") != null) { %> 2.18 + <!-- Info Message --> 2.19 + <TR><TD colspan=2> 2.20 + <CENTER><P><%=request.getAttribute("info") %></P></CENTER> 2.21 + </TD></TR> 2.22 + <!-- Info Message --> 2.23 + <%}%> 2.24 + <tr> 2.25 + <td colspan=2 id="output"> 2.26 + <div style="font-size:13px"> 2.27 + You must be logged in to change configuration, or run in localhost. 2.28 + </div> 2.29 + </td> 2.30 + </tr> 2.31 <TR> 2.32 <TD valign="top" class="style4">Database Name:</TD> 2.33 <TD><input type="text" name="dbname" value="<%=request.getAttribute("dbname")%>"/></TD> 2.34 @@ -78,6 +100,7 @@ 2.35 <TD colspan=2><input type="submit" value="Connect"/></TD> 2.36 </TR> 2.37 </TABLE> 2.38 + 2.39 </FORM> 2.40 <br/><br/><br/><br/><br/> 2.41 </BODY>
3.1 --- a/endpoint/WebContent/query.jsp Wed Feb 26 16:31:08 2014 +0200 3.2 +++ b/endpoint/WebContent/query.jsp Tue Mar 11 10:50:45 2014 +0200 3.3 @@ -306,7 +306,7 @@ 3.4 3.5 </script> 3.6 3.7 - </script> 3.8 + 3.9 <link href="js/timemap/examples.css" type="text/css" rel="stylesheet"/> 3.10 <style> 3.11 div#timelinecontainer{ height: 310px; } 3.12 @@ -438,7 +438,7 @@ 3.13 <tr> 3.14 <td id="output" colspan=2> 3.15 <div style="font-size:13px"> 3.16 - You must be logged in to perform update queries. 3.17 + You must be logged in to perform update queries, or run in localhost. 3.18 </div> 3.19 </td> 3.20 </tr>
4.1 --- a/endpoint/WebContent/store.jsp Wed Feb 26 16:31:08 2014 +0200 4.2 +++ b/endpoint/WebContent/store.jsp Tue Mar 11 10:50:45 2014 +0200 4.3 @@ -159,7 +159,7 @@ 4.4 <tr> 4.5 <td colspan=2 id="output"> 4.6 <div style="font-size:13px"> 4.7 - You must be logged in to store. 4.8 + You must be logged in to store, or run in localhost. 4.9 </div> 4.10 </td> 4.11 </tr>
5.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/ConnectionBean.java Wed Feb 26 16:31:08 2014 +0200 5.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/ConnectionBean.java Tue Mar 11 10:50:45 2014 +0200 5.3 @@ -14,6 +14,7 @@ 5.4 import java.io.FileOutputStream; 5.5 import java.io.IOException; 5.6 import java.io.InputStream; 5.7 +import java.net.InetAddress; 5.8 import java.util.Properties; 5.9 5.10 import javax.servlet.RequestDispatcher; 5.11 @@ -59,6 +60,22 @@ 5.12 */ 5.13 private ServletContext context; 5.14 5.15 + //Check for localHost. Works with ipV4 and ipV6 5.16 + public static boolean isLocalClient(HttpServletRequest request) { 5.17 + HttpServletRequest testRequest = request; 5.18 + try { 5.19 + InetAddress remote = InetAddress.getByName(testRequest.getRemoteAddr()); 5.20 + if (remote.isLoopbackAddress()) { 5.21 + return true; 5.22 + } 5.23 + InetAddress localHost = InetAddress.getLocalHost(); 5.24 + String localAddress = localHost.getHostAddress(); 5.25 + String remoteAddress = remote.getHostAddress(); 5.26 + return (remoteAddress != null && remoteAddress.equalsIgnoreCase(localAddress)); 5.27 + } catch (Exception e) { } 5.28 + return false; 5.29 + } 5.30 + 5.31 @Override 5.32 public void init(ServletConfig servletConfig) throws ServletException { 5.33 super.init(servletConfig); 5.34 @@ -85,63 +102,83 @@ 5.35 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 5.36 RequestDispatcher dispatcher; 5.37 5.38 - // set new connection details 5.39 - strabonWrapper.setConnectionDetails(request.getParameter("dbname"), 5.40 - request.getParameter("username"), 5.41 - request.getParameter("password"), 5.42 - request.getParameter("port"), 5.43 - request.getParameter("hostname"), 5.44 - request.getParameter("dbengine")); 5.45 + //Authorization 5.46 + boolean authorized; 5.47 5.48 - // pass the query, format, and the handle that had been issued to the dispatcher 5.49 - request.setAttribute("query", request.getAttribute("query")); 5.50 - request.setAttribute("format", request.getAttribute("format")); 5.51 - request.setAttribute("handle", request.getAttribute("handle")); 5.52 + if(!isLocalClient(request)) { 5.53 + Authenticate authenticate = new Authenticate(); 5.54 + String authorization = request.getHeader("Authorization"); 5.55 + 5.56 + authorized = authenticate.authenticateUser(authorization, context); 5.57 + } 5.58 + else 5.59 + authorized = true; 5.60 + 5.61 + if (!authorized) { 5.62 + // not allowed, so report he's unauthorized 5.63 + response.setHeader("WWW-Authenticate", "BASIC realm=\"Please login\""); 5.64 + response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 5.65 + } 5.66 + else { 5.67 + // set new connection details 5.68 + strabonWrapper.setConnectionDetails(request.getParameter("dbname"), 5.69 + request.getParameter("username"), 5.70 + request.getParameter("password"), 5.71 + request.getParameter("port"), 5.72 + request.getParameter("hostname"), 5.73 + request.getParameter("dbengine")); 5.74 + 5.75 + // pass the query, format, and the handle that had been issued to the dispatcher 5.76 + request.setAttribute("query", request.getAttribute("query")); 5.77 + request.setAttribute("format", request.getAttribute("format")); 5.78 + request.setAttribute("handle", request.getAttribute("handle")); 5.79 5.80 - if (logger.isInfoEnabled()) { 5.81 - logger.info("[StrabonEndpoint.ConnectionBean] Establishing connection with database using new connection details."); 5.82 - } 5.83 - 5.84 - // establish connection 5.85 - if (strabonWrapper.init()) { // successfully connected, go to query.jsp 5.86 - if (logger.isInfoEnabled()) { 5.87 - logger.info("[StrabonEndpoint.ConnectionBean] Connection with database established."); 5.88 - logger.info("[StrabonEndpoint.ConnectionBean] Saving new connection details in {}.", CONNECTION_PROPERTIES_FILE); 5.89 - } 5.90 + if (logger.isInfoEnabled()) { 5.91 + logger.info("[StrabonEndpoint.ConnectionBean] Establishing connection with database using new connection details."); 5.92 + } 5.93 + 5.94 + // establish connection 5.95 + if (strabonWrapper.init()) { // successfully connected, go to query.jsp 5.96 + if (logger.isInfoEnabled()) { 5.97 + logger.info("[StrabonEndpoint.ConnectionBean] Connection with database established."); 5.98 + logger.info("[StrabonEndpoint.ConnectionBean] Saving new connection details in {}.", CONNECTION_PROPERTIES_FILE); 5.99 + } 5.100 + 5.101 + // save the new connection details 5.102 + saveNewConnectionDetails(request.getParameter("dbname"), 5.103 + request.getParameter("username"), 5.104 + request.getParameter("password"), 5.105 + request.getParameter("port"), 5.106 + request.getParameter("hostname"), 5.107 + request.getParameter("dbengine")); 5.108 + 5.109 + if (logger.isInfoEnabled()) { 5.110 + logger.info("[StrabonEndpoint.ConnectionBean] New connection details succesfully saved."); 5.111 + } 5.112 + 5.113 + // go to query.jsp 5.114 + dispatcher = request.getRequestDispatcher("/query.jsp"); 5.115 + 5.116 + } else { // try again 5.117 + if (logger.isInfoEnabled()) { 5.118 + logger.info("[StrabonEndpoint.ConnectionBean] Cannot establish connection with database."); 5.119 + } 5.120 + 5.121 + // pass the current details of the connection 5.122 + request.setAttribute("dbname", request.getParameter("dbname")); 5.123 + request.setAttribute("username", request.getParameter("username")); 5.124 + request.setAttribute("password", request.getParameter("password")); 5.125 + request.setAttribute("port", request.getParameter("port")); 5.126 + request.setAttribute("hostname", request.getParameter("hostname")); 5.127 + request.setAttribute("dbengine", request.getParameter("dbengine")); 5.128 + 5.129 + dispatcher = request.getRequestDispatcher("/connection.jsp"); 5.130 + 5.131 + } 5.132 + 5.133 + dispatcher.forward(request, response); 5.134 + } 5.135 5.136 - // save the new connection details 5.137 - saveNewConnectionDetails(request.getParameter("dbname"), 5.138 - request.getParameter("username"), 5.139 - request.getParameter("password"), 5.140 - request.getParameter("port"), 5.141 - request.getParameter("hostname"), 5.142 - request.getParameter("dbengine")); 5.143 - 5.144 - if (logger.isInfoEnabled()) { 5.145 - logger.info("[StrabonEndpoint.ConnectionBean] New connection details succesfully saved."); 5.146 - } 5.147 - 5.148 - // go to query.jsp 5.149 - dispatcher = request.getRequestDispatcher("/query.jsp"); 5.150 - 5.151 - } else { // try again 5.152 - if (logger.isInfoEnabled()) { 5.153 - logger.info("[StrabonEndpoint.ConnectionBean] Cannot establish connection with database."); 5.154 - } 5.155 - 5.156 - // pass the current details of the connection 5.157 - request.setAttribute("dbname", request.getParameter("dbname")); 5.158 - request.setAttribute("username", request.getParameter("username")); 5.159 - request.setAttribute("password", request.getParameter("password")); 5.160 - request.setAttribute("port", request.getParameter("port")); 5.161 - request.setAttribute("hostname", request.getParameter("hostname")); 5.162 - request.setAttribute("dbengine", request.getParameter("dbengine")); 5.163 - 5.164 - dispatcher = request.getRequestDispatcher("/connection.jsp"); 5.165 - 5.166 - } 5.167 - 5.168 - dispatcher.forward(request, response); 5.169 } 5.170 5.171 private void saveNewConnectionDetails(String dbname, String username, String password,
6.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StoreBean.java Wed Feb 26 16:31:08 2014 +0200 6.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StoreBean.java Tue Mar 11 10:50:45 2014 +0200 6.3 @@ -11,6 +11,7 @@ 6.4 6.5 import java.io.IOException; 6.6 import java.io.UnsupportedEncodingException; 6.7 +import java.net.InetAddress; 6.8 import java.net.MalformedURLException; 6.9 import java.net.URLDecoder; 6.10 import java.util.Map; 6.11 @@ -65,7 +66,23 @@ 6.12 * The context of the servlet 6.13 */ 6.14 private ServletContext context; 6.15 - 6.16 + 6.17 + //Check for localHost. Works with ipV4 and ipV6 6.18 + public static boolean isLocalClient(HttpServletRequest request) { 6.19 + HttpServletRequest testRequest = request; 6.20 + try { 6.21 + InetAddress remote = InetAddress.getByName(testRequest.getRemoteAddr()); 6.22 + if (remote.isLoopbackAddress()) { 6.23 + return true; 6.24 + } 6.25 + InetAddress localHost = InetAddress.getLocalHost(); 6.26 + String localAddress = localHost.getHostAddress(); 6.27 + String remoteAddress = remote.getHostAddress(); 6.28 + return (remoteAddress != null && remoteAddress.equalsIgnoreCase(localAddress)); 6.29 + } catch (Exception e) { } 6.30 + return false; 6.31 + } 6.32 + 6.33 @Override 6.34 public void init(ServletConfig servletConfig) throws ServletException { 6.35 super.init(servletConfig); 6.36 @@ -95,7 +112,7 @@ 6.37 6.38 boolean authorized; 6.39 6.40 - if(!request.getLocalAddr().equals("127.0.0.1")) { 6.41 + if(!isLocalClient(request)) { 6.42 Authenticate authenticate = new Authenticate(); 6.43 String authorization = request.getHeader("Authorization"); 6.44
7.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/UpdateBean.java Wed Feb 26 16:31:08 2014 +0200 7.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/UpdateBean.java Tue Mar 11 10:50:45 2014 +0200 7.3 @@ -10,6 +10,7 @@ 7.4 package eu.earthobservatory.org.StrabonEndpoint; 7.5 7.6 import java.io.IOException; 7.7 +import java.net.InetAddress; 7.8 import java.net.URLDecoder; 7.9 7.10 import javax.servlet.RequestDispatcher; 7.11 @@ -38,6 +39,22 @@ 7.12 7.13 private StrabonBeanWrapper strabonWrapper; 7.14 7.15 + //Check for localHost. Works with ipV4 and ipV6 7.16 + public static boolean isLocalClient(HttpServletRequest request) { 7.17 + HttpServletRequest testRequest = request; 7.18 + try { 7.19 + InetAddress remote = InetAddress.getByName(testRequest.getRemoteAddr()); 7.20 + if (remote.isLoopbackAddress()) { 7.21 + return true; 7.22 + } 7.23 + InetAddress localHost = InetAddress.getLocalHost(); 7.24 + String localAddress = localHost.getHostAddress(); 7.25 + String remoteAddress = remote.getHostAddress(); 7.26 + return (remoteAddress != null && remoteAddress.equalsIgnoreCase(localAddress)); 7.27 + } catch (Exception e) { } 7.28 + return false; 7.29 + } 7.30 + 7.31 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 7.32 doPost(request, response); 7.33 } 7.34 @@ -59,7 +76,7 @@ 7.35 7.36 request.setCharacterEncoding("UTF-8"); 7.37 ServletContext context = getServletContext(); 7.38 - if(!request.getLocalAddr().equals("127.0.0.1")) { 7.39 + if(!isLocalClient(request)) { 7.40 Authenticate authenticate = new Authenticate(); 7.41 String authorization = request.getHeader("Authorization"); 7.42