Strabon

changeset 603:e1ab6f5bf125

Corresponds to changset 570:e53c5ec92c0a. Added files BrowseBean and browse.jsp.
author Stella Giannakopoulou <sgian@di.uoa.gr>
date Tue Oct 02 11:42:36 2012 +0300 (2012-10-02)
parents cc95bcf87eaa
children 06a1beb9b9c3
files endpoint/WebContent/browse.jsp endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/BrowseBean.java
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/endpoint/WebContent/browse.jsp	Tue Oct 02 11:42:36 2012 +0300
     1.3 @@ -0,0 +1,49 @@
     1.4 +<%@page import="java.net.URLEncoder"%>
     1.5 +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     1.6 +<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
     1.7 +<%@page import="org.springframework.web.context.WebApplicationContext"%>
     1.8 +<%@page import="eu.earthobservatory.org.StrabonEndpoint.StrabonBeanWrapper"%>
     1.9 +<%@page import="eu.earthobservatory.org.StrabonEndpoint.StrabonBeanWrapperConfiguration"%>
    1.10 +<%@page import="java.util.List"%>
    1.11 +<%@page import="java.util.Iterator"%>
    1.12 +<jsp:directive.page import="eu.earthobservatory.org.StrabonEndpoint.Common"/>
    1.13 +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    1.14 +<html>
    1.15 +<head>
    1.16 +	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    1.17 +	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    1.18 +	<link rel="stylesheet" href="style.css" type="text/css" />
    1.19 +	<title>TELEIOS: Strabon Endpoint</title>
    1.20 +</head>
    1.21 +<body topmargin="0" leftmargin="0" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">
    1.22 +<TABLE width="80%" BORDER=0 CELLPADDING=0 CELLSPACING=0>  
    1.23 +	<TR>
    1.24 +      <TD height="1"></TD>
    1.25 +	</TR>		
    1.26 +	<TR>
    1.27 +      <TD height="60" background="images/nav2_bg.gif"> 
    1.28 +        <table width="100%" border=0>
    1.29 +         <tr>
    1.30 +            <td width="1"><img src="images/nav2_bg.gif" width="1" height="60"></td>
    1.31 +            <td valign="top" width="50px"><a href="query.jsp"><img border="0" src="images/teleios_	logo.png" /></a></td>
    1.32 +            <td valign="top" align="left">
    1.33 +            <span id="logo"><a href="query.jsp">&nbsp;stSPARQL Endpoint</a></span><br><span class="style4"></span></td>
    1.34 +          </tr>
    1.35 +        </table>
    1.36 +      </TD>
    1.37 +	</TR>
    1.38 +</TABLE>
    1.39 +	<h1>About: </h1>
    1.40 +	<a href=""><%=request.getAttribute("resource")%></a>
    1.41 +	<div id="response">
    1.42 +	<!-- Response -->
    1.43 +<% if (request.getAttribute("response") != null) {
    1.44 +	if (Common.getHTMLFormat().equals(request.getParameter("format"))) {%>
    1.45 +		<%=request.getAttribute("response")%>
    1.46 +	<%} else { %>
    1.47 +	<PRE><%=request.getAttribute("response") %></PRE>
    1.48 +	<%}%>
    1.49 +<%}%>
    1.50 +</div>
    1.51 +</body>
    1.52 +</html>
    1.53 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/BrowseBean.java	Tue Oct 02 11:42:36 2012 +0300
     2.3 @@ -0,0 +1,212 @@
     2.4 +/**
     2.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     2.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     2.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     2.8 + * 
     2.9 + * Copyright (C) 2010, 2011, 2012, Pyravlos Team
    2.10 + * 
    2.11 + * http://www.strabon.di.uoa.gr/
    2.12 + */
    2.13 +package eu.earthobservatory.org.StrabonEndpoint;
    2.14 +
    2.15 +import java.io.ByteArrayOutputStream;
    2.16 +import java.io.IOException;
    2.17 +import java.net.URLDecoder;
    2.18 +
    2.19 +import javax.servlet.RequestDispatcher;
    2.20 +import javax.servlet.ServletConfig;
    2.21 +import javax.servlet.ServletContext;
    2.22 +import javax.servlet.ServletException;
    2.23 +import javax.servlet.ServletOutputStream;
    2.24 +import javax.servlet.http.HttpServlet;
    2.25 +import javax.servlet.http.HttpServletRequest;
    2.26 +import javax.servlet.http.HttpServletResponse;
    2.27 +
    2.28 +import org.apache.commons.lang.StringEscapeUtils;
    2.29 +import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
    2.30 +import org.slf4j.Logger;
    2.31 +import org.slf4j.LoggerFactory;
    2.32 +import org.springframework.web.context.WebApplicationContext;
    2.33 +import org.springframework.web.context.support.WebApplicationContextUtils;
    2.34 +
    2.35 +/*@author Stella Giannakopoulou <sgian@di.uoa.gr>*/
    2.36 +
    2.37 +public class BrowseBean extends HttpServlet {
    2.38 +
    2.39 +	private static final long serialVersionUID = -378175118289907707L;
    2.40 +
    2.41 +	private static Logger logger = LoggerFactory
    2.42 +			.getLogger(eu.earthobservatory.org.StrabonEndpoint.BrowseBean.class);
    2.43 +
    2.44 +	/**
    2.45 +	 * Attributes carrying values to be rendered by the browse.jsp file
    2.46 +	 */
    2.47 +	private static final String ERROR = "error";
    2.48 +	private static final String RESPONSE = "response";
    2.49 +
    2.50 +	/**
    2.51 +	 * Error returned by BrowseBean
    2.52 +	 */
    2.53 +	private static final String PARAM_ERROR = "stSPARQL Query Results Format or SPARQL query are not set or are invalid.";
    2.54 +
    2.55 +	/**
    2.56 +	 * The context of the servlet
    2.57 +	 */
    2.58 +	private ServletContext context;
    2.59 +
    2.60 +	/**
    2.61 +	 * Wrapper over Strabon
    2.62 +	 */
    2.63 +	private StrabonBeanWrapper strabonWrapper;
    2.64 +
    2.65 +
    2.66 +	public void doGet(HttpServletRequest request, HttpServletResponse response)
    2.67 +			throws ServletException, IOException {
    2.68 +		doPost(request, response);
    2.69 +	}
    2.70 +
    2.71 +	public void init(ServletConfig servletConfig) throws ServletException {
    2.72 +		super.init(servletConfig);
    2.73 +
    2.74 +		// get the context of the servlet
    2.75 +		context = getServletContext();
    2.76 +
    2.77 +		// get the context of the application
    2.78 +		WebApplicationContext applicationContext = WebApplicationContextUtils
    2.79 +				.getWebApplicationContext(context);
    2.80 +
    2.81 +		// the the strabon wrapper
    2.82 +		strabonWrapper = (StrabonBeanWrapper) applicationContext
    2.83 +				.getBean("strabonBean");
    2.84 +
    2.85 +	}
    2.86 +
    2.87 +	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    2.88 +		request.setCharacterEncoding("UTF-8");
    2.89 +
    2.90 +		// check connection details
    2.91 +		if (strabonWrapper.getStrabon() == null) {
    2.92 +			RequestDispatcher dispatcher = request
    2.93 +					.getRequestDispatcher("/connection.jsp");
    2.94 +
    2.95 +			// pass the current details of the connection
    2.96 +			request.setAttribute("username", strabonWrapper.getUsername());
    2.97 +			request.setAttribute("password", strabonWrapper.getPassword());
    2.98 +			request.setAttribute("dbname", strabonWrapper.getDatabaseName());
    2.99 +			request.setAttribute("hostname", strabonWrapper.getHostName());
   2.100 +			request.setAttribute("port", strabonWrapper.getPort());
   2.101 +			request.setAttribute("dbengine", strabonWrapper.getDBEngine());
   2.102 +
   2.103 +			// pass the other parameters as well
   2.104 +			request.setAttribute("query", request.getParameter("query"));
   2.105 +			request.setAttribute("format", request.getParameter("format"));
   2.106 +			request.setAttribute("handle", request.getParameter("handle"));
   2.107 +
   2.108 +			// forward the request
   2.109 +			dispatcher.forward(request, response);
   2.110 +
   2.111 +		} else {
   2.112 +
   2.113 +			if (Common.VIEW_TYPE.equals(request.getParameter(Common.VIEW))) {
   2.114 +				// HTML visual interface
   2.115 +				processVIEWRequest(request, response);
   2.116 +
   2.117 +			} else {// invoked as a service
   2.118 +				processRequest(request, response);
   2.119 +			}
   2.120 +		}
   2.121 +	}
   2.122 +
   2.123 +	/**
   2.124 +	 * Processes the request made by a client of the endpoint that uses it as a
   2.125 +	 * service.
   2.126 +	 * 
   2.127 +	 * @param request
   2.128 +	 * @param response
   2.129 +	 * @throws IOException
   2.130 +	 */
   2.131 +	private void processRequest(HttpServletRequest request,
   2.132 +			HttpServletResponse response) throws IOException {
   2.133 +		ServletOutputStream out = response.getOutputStream();
   2.134 +
   2.135 +		// get the stSPARQL Query Result format (we check only the Accept
   2.136 +		// header)
   2.137 +		stSPARQLQueryResultFormat format = stSPARQLQueryResultFormat.forMIMEType(request.getHeader("accept"));
   2.138 +
   2.139 +		// get the query
   2.140 +		String query = request.getParameter("query");
   2.141 +
   2.142 +		// check for required parameters
   2.143 +		if (format == null || query == null) {
   2.144 +			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
   2.145 +			out.print(ResponseMessages.getXMLHeader());
   2.146 +			out.print(ResponseMessages.getXMLException(PARAM_ERROR));
   2.147 +			out.print(ResponseMessages.getXMLFooter());
   2.148 +
   2.149 +		} else {
   2.150 +			// decode the query
   2.151 +			query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
   2.152 +
   2.153 +			response.setContentType(format.getDefaultMIMEType());
   2.154 +			try {
   2.155 +				strabonWrapper.query(query, format.getName(), out);
   2.156 +				response.setStatus(HttpServletResponse.SC_OK);
   2.157 +
   2.158 +			} catch (Exception e) {
   2.159 +				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
   2.160 +				out.print(ResponseMessages.getXMLHeader());
   2.161 +				out.print(ResponseMessages.getXMLException(e.getMessage()));
   2.162 +				out.print(ResponseMessages.getXMLFooter());
   2.163 +			}
   2.164 +		}
   2.165 +
   2.166 +		out.flush();
   2.167 +	}
   2.168 +
   2.169 +	/**
   2.170 +	 * Processes the request made from the HTML visual interface of Strabon
   2.171 +	 * Endpoint.
   2.172 +	 * 
   2.173 +	 * @param request
   2.174 +	 * @param response
   2.175 +	 * @throws ServletException
   2.176 +	 * @throws IOException
   2.177 +	 */
   2.178 +	private void processVIEWRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   2.179 +		RequestDispatcher dispatcher;
   2.180 +
   2.181 +		String query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
   2.182 +		String format = request.getParameter("format");
   2.183 +		
   2.184 +		// get stSPARQLQueryResultFormat from given format name
   2.185 +		stSPARQLQueryResultFormat queryResultFormat = stSPARQLQueryResultFormat.valueOf(format);
   2.186 +
   2.187 +		if (query == null || format == null || queryResultFormat == null) {
   2.188 +			dispatcher = request.getRequestDispatcher("browse.jsp");
   2.189 +			request.setAttribute(ERROR, PARAM_ERROR);
   2.190 +			dispatcher.forward(request, response);
   2.191 +
   2.192 +		} else {		
   2.193 +			dispatcher = request.getRequestDispatcher("browse.jsp");
   2.194 +			ByteArrayOutputStream bos = new ByteArrayOutputStream();
   2.195 +			request.setAttribute("resource",
   2.196 +					request.getParameter("resource"));
   2.197 +
   2.198 +			try {
   2.199 +				strabonWrapper.query(query, format, bos);
   2.200 +				if (format.equals(Common.getHTMLFormat())) {
   2.201 +					request.setAttribute(RESPONSE, bos.toString());
   2.202 +				} else {
   2.203 +					request.setAttribute(RESPONSE, StringEscapeUtils.escapeHtml(bos.toString()));
   2.204 +				}
   2.205 +
   2.206 +			} catch (Exception e) {
   2.207 +				logger.error("[StrabonEndpoint.BrowseBean] Error during querying.", e);
   2.208 +				request.setAttribute(ERROR, e.getMessage());
   2.209 +
   2.210 +			} finally {
   2.211 +				dispatcher.forward(request, response);
   2.212 +			}			
   2.213 +		}
   2.214 +	}
   2.215 +}
   2.216 \ No newline at end of file