Strabon

changeset 501:74f346ca104f

DescribeBean: changes to conform to changeset 499:ef9c43dd2c78.
* Added into endpoint/pom.xml dependency to JSTL, which is required in jsp pages for accessing attributes of a request (set by a Bean)
* Completed describe.jsp and DescribeBean
- "format" parameter is now only checked when using the HTML visual interface
- the value of Accept header is inspected when the DescribeBean is used as a service
- "SPARQLquery" parameter is not checked at all; we moved to the standard "query" parameter
* Updated endpoint script to set the accept header with respect to the format used

Similar changes have to be made for the other beans too. Please have a look and comment on this approach (is it better, simpler, does not worth the cause, etc.)!
author Babis Nikolaou <charnik@di.uoa.gr>
date Tue Jul 24 22:29:54 2012 +0300 (2012-07-24)
parents 4a75a1fdd1c1
children 362b91bf1b97
files endpoint/WebContent/describe.jsp endpoint/pom.xml endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/DescribeBean.java scripts/endpoint
line diff
     1.1 --- a/endpoint/WebContent/describe.jsp	Tue Jul 24 18:54:40 2012 +0300
     1.2 +++ b/endpoint/WebContent/describe.jsp	Tue Jul 24 22:29:54 2012 +0300
     1.3 @@ -1,5 +1,8 @@
     1.4  <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     1.5 +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
     1.6 +
     1.7  <jsp:directive.page import="eu.earthobservatory.org.StrabonEndpoint.StoreBean"/>
     1.8 +<jsp:directive.page import="eu.earthobservatory.org.StrabonEndpoint.DescribeBean"/>
     1.9  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    1.10  <html>
    1.11  	<head>
    1.12 @@ -28,7 +31,9 @@
    1.13  <%@ include file="teleios-header.html"%>
    1.14  <!-- include TELEIOS header and description -->
    1.15  
    1.16 -<form enctype="UTF-8" accept-charset="UTF-8" method="post">
    1.17 +<FORM enctype="UTF-8" accept-charset="UTF-8" method="post" action="Describe">
    1.18 +<input type="hidden" name="<%=DescribeBean.VIEW%>" value="<%=DescribeBean.VIEW_TYPE%>">
    1.19 +
    1.20  <table border="0" width="100%"><tr> 
    1.21  <td width="90" valign="top" class="style4"> 
    1.22  <table border="0" cellspacing="0" cellpadding="0" width="165" id="navigation"> 
    1.23 @@ -40,8 +45,7 @@
    1.24  <table cellspacing="5">
    1.25  <tr>
    1.26  <td id="output">stSPARQL Query:</td>
    1.27 -<td id="output"><textarea name="SPARQLQuery" title="pose your DESCRIBE query here" rows="15" cols="100">
    1.28 -</textarea></td>
    1.29 +<td id="output"><textarea name="query" title="pose your DESCRIBE query here" rows="15" cols="100">${query}</textarea></td>
    1.30  </tr>
    1.31  <tr>
    1.32  <td id="output"><center>Output Format:<br/>
    1.33 @@ -55,7 +59,22 @@
    1.34  	<input type="submit" title="execute DESCRIBE query" value="Describe" name="submit" style="width: 400px"/><br/>
    1.35  </center><br/></td>
    1.36  </tr>
    1.37 +
    1.38 +<!-- Error Message -->
    1.39 +<c:if test="${error != null}">
    1.40 +<TR>
    1.41 +		<TD id="output">Result: </TD><TD id="output">${error}</TD>
    1.42 +</TR>
    1.43 +</c:if> 
    1.44 +<!-- Error Message -->
    1.45 +
    1.46  </table></td></tr></table><br/><br/>
    1.47 +
    1.48 +<!-- Response -->
    1.49 +<c:if test="${response != null}">
    1.50 +<PRE>${response}</PRE>
    1.51 +</c:if>
    1.52 +<!-- Response -->
    1.53  </form>
    1.54  </body>
    1.55  </html>
    1.56 \ No newline at end of file
     2.1 --- a/endpoint/pom.xml	Tue Jul 24 18:54:40 2012 +0300
     2.2 +++ b/endpoint/pom.xml	Tue Jul 24 22:29:54 2012 +0300
     2.3 @@ -41,6 +41,12 @@
     2.4  			</exclusions>
     2.5  		</dependency>
     2.6  
     2.7 +		<dependency>
     2.8 +			<groupId>jstl</groupId>
     2.9 +			<artifactId>jstl</artifactId>
    2.10 +			<version>1.2</version>
    2.11 +		</dependency>
    2.12 +		
    2.13  		<!-- GeoTools -->
    2.14  		<dependency>
    2.15  			<groupId>org.geotools.xsd</groupId>
     3.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/DescribeBean.java	Tue Jul 24 18:54:40 2012 +0300
     3.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/DescribeBean.java	Tue Jul 24 22:29:54 2012 +0300
     3.3 @@ -2,18 +2,13 @@
     3.4  
     3.5  import java.io.ByteArrayOutputStream;
     3.6  import java.io.IOException;
     3.7 -import java.io.PrintWriter;
     3.8 -import java.io.UnsupportedEncodingException;
     3.9  import java.net.URLDecoder;
    3.10 -import java.net.URLEncoder;
    3.11 -import java.util.ArrayList;
    3.12 -import java.util.Arrays;
    3.13 -import java.util.Iterator;
    3.14 -import java.util.List;
    3.15  
    3.16 +import javax.servlet.RequestDispatcher;
    3.17  import javax.servlet.ServletConfig;
    3.18  import javax.servlet.ServletContext;
    3.19  import javax.servlet.ServletException;
    3.20 +import javax.servlet.ServletOutputStream;
    3.21  import javax.servlet.http.HttpServlet;
    3.22  import javax.servlet.http.HttpServletRequest;
    3.23  import javax.servlet.http.HttpServletResponse;
    3.24 @@ -23,150 +18,30 @@
    3.25  import org.springframework.web.context.WebApplicationContext;
    3.26  import org.springframework.web.context.support.WebApplicationContextUtils;
    3.27  
    3.28 -import eu.earthobservatory.org.StrabonEndpoint.StrabonBeanWrapper.Entry;
    3.29 -
    3.30  public class DescribeBean extends HttpServlet{
    3.31  
    3.32 -    private static final long serialVersionUID = -7541662133934957148L;
    3.33 +	private static final long serialVersionUID = -7541662133934957148L;
    3.34  
    3.35 +    /**
    3.36 +	 * Parameters used in the describe.jsp file
    3.37 +	 */
    3.38 +	public static final String VIEW 		= "view";
    3.39 +	public static final String VIEW_TYPE 	= "HTML";
    3.40 +	
    3.41 +	/**
    3.42 +	 * Attributes carrying values to be rendered by the describe.jsp file 
    3.43 +	 */
    3.44 +	private static final String ERROR		= "error";
    3.45 +	private static final String RESPONSE	= "response";
    3.46 +	
    3.47 +	/**
    3.48 +	 * Error returned by DescribeBean
    3.49 +	 */
    3.50 +	private static final String PARAM_ERROR = "RDF format or SPARQL query are not set or are invalid.";
    3.51 +	
    3.52  	private StrabonBeanWrapper strabonWrapper;
    3.53  
    3.54      @Override
    3.55 -	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    3.56 -		doPost(request, response);
    3.57 -	}
    3.58 -
    3.59 -    @Override
    3.60 -	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    3.61 -		request.setCharacterEncoding("UTF-8");
    3.62 -
    3.63 -		String query = request.getParameter("SPARQLQuery");
    3.64 -		
    3.65 -		if (query != null) {
    3.66 -			query = URLDecoder.decode(request.getParameter("SPARQLQuery"), "UTF-8");
    3.67 -			
    3.68 -		} else { // check for SPARQL protocol "query" parameter
    3.69 -			query = request.getParameter("query");
    3.70 -			
    3.71 -			if (query != null) {
    3.72 -				query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
    3.73 -			}
    3.74 -		}
    3.75 -		
    3.76 -		// get the RDF format (checks the "format" and then the "accept" parameters)
    3.77 -        String reqFormat = getFormat(request);
    3.78 -		
    3.79 -		if (reqFormat == null || reqFormat.equals("HTML")) { // browser view
    3.80 -			response.setContentType("text/html; charset=UTF-8");
    3.81 -		    reqFormat = "HTML";
    3.82 -		    
    3.83 -		    PrintWriter out = response.getWriter();
    3.84 -
    3.85 -			appendHTML1a(out, "");
    3.86 -			appendHTMLQ(out, strabonWrapper);
    3.87 -			appendHTML1b(out);
    3.88 -
    3.89 -			if (query != null) {
    3.90 -				out.write(query);
    3.91 -			}
    3.92 -
    3.93 -			appendHTML2(out, reqFormat);
    3.94 -
    3.95 -			String answer = "";
    3.96 -			if (query != null) {
    3.97 -				ByteArrayOutputStream bos = new ByteArrayOutputStream();
    3.98 -				
    3.99 -				try {
   3.100 -					// we use the default N-Triples format in case of HTML output
   3.101 -					strabonWrapper.describe(query, "N-Triples", bos);
   3.102 -					answer = bos.toString();
   3.103 -						
   3.104 -				} catch (Exception e) {
   3.105 -						appendHTML3(out, e.getMessage());
   3.106 -				}
   3.107 -			}
   3.108 -
   3.109 -			out.append("</table></td></tr></table>");
   3.110 -			if (!answer.equals("")) {
   3.111 -				out.println("<style type=\"text/css\">");
   3.112 -				out.println("table.result    {border:1px solid #777777;}");
   3.113 -				out.println("table.result tr {border:1px dashed grey;}");
   3.114 -				out.println("table.result th {background-color:grey; color:black;}");
   3.115 -				out.println("</style>");
   3.116 -				out.println("<PRE>");
   3.117 -				out.append(StringEscapeUtils.escapeHtml(answer));
   3.118 -				out.println("</PRE>");
   3.119 -			}
   3.120 -			
   3.121 -			appendHTML4(out);
   3.122 -			appendHTML5(out);
   3.123 -		    
   3.124 -			response.setStatus(HttpServletResponse.SC_OK);
   3.125 -			out.flush();
   3.126 -			
   3.127 -	    } else {	    	
   3.128 -	    	RDFFormat rdfFormat = RDFFormat.valueOf(reqFormat);
   3.129 -	    	
   3.130 -	    	// assuming N-Triples
   3.131 -	    	if (rdfFormat == null) {
   3.132 -	    		rdfFormat = RDFFormat.valueOf("N-Triples");
   3.133 -	    	}
   3.134 -	    	
   3.135 -	    	response.setContentType(rdfFormat.getDefaultMIMEType());
   3.136 -		    response.setHeader("Content-Disposition", 
   3.137 -		    		"attachment; filename=describe." + rdfFormat.getDefaultFileExtension() + "; " + rdfFormat.getCharset());
   3.138 -		    
   3.139 -		    if (query != null) {
   3.140 -				try {
   3.141 -					strabonWrapper.describe(query, rdfFormat.getName(), response.getOutputStream());
   3.142 -					response.setStatus(HttpServletResponse.SC_OK);
   3.143 -					
   3.144 -				} catch (Exception e) {
   3.145 -					
   3.146 -					response.getOutputStream().print(ResponseMessages.getXMLHeader());
   3.147 -					response.getOutputStream().print(ResponseMessages.getXMLException(e.getMessage()));
   3.148 -					response.getOutputStream().print(ResponseMessages.getXMLFooter());
   3.149 -				}
   3.150 -			}
   3.151 -		    
   3.152 -		    response.getOutputStream().flush();
   3.153 -	    }
   3.154 -	}
   3.155 -	
   3.156 -    /**
   3.157 -     * Determines the RDF format to use. If the client has specified
   3.158 -     * the "format" parameter, then it is favored over the "accept"
   3.159 -     * parameter. If "format" is not specified, we determine the RDF 
   3.160 -     * format according to the "accept" parameter. 
   3.161 -     * 
   3.162 -     * @param request
   3.163 -     * @return
   3.164 -     */
   3.165 -    private String getFormat(HttpServletRequest request) {
   3.166 -        
   3.167 -        String reqFormat = request.getParameter("format");
   3.168 -        
   3.169 -        if (reqFormat == null) {
   3.170 -                String reqAccept = request.getHeader("accept");
   3.171 -                
   3.172 -                if (reqAccept != null) {
   3.173 -                        // check whether the "accept" parameter contains any 
   3.174 -                        // of the mime types of any RDF format
   3.175 -                        for (RDFFormat format : RDFFormat.values()) {
   3.176 -                                for (String mimeType : format.getMIMETypes()) {
   3.177 -                                        if (reqAccept.contains(mimeType)) {
   3.178 -                                                reqFormat = format.getName();
   3.179 -                                                break;
   3.180 -                                        }
   3.181 -                                }
   3.182 -                        }
   3.183 -                }
   3.184 -        }
   3.185 -                
   3.186 -        return reqFormat;
   3.187 -    }
   3.188 -
   3.189 -    @Override
   3.190  	public void init(ServletConfig servletConfig) throws ServletException {
   3.191  		super.init(servletConfig);
   3.192  
   3.193 @@ -176,160 +51,135 @@
   3.194  
   3.195  		strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean");
   3.196  	}
   3.197 -
   3.198 -	protected static void appendHTML1a(PrintWriter out, String pathToKML) {
   3.199 -		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
   3.200 -		out.println("<html>");
   3.201 -		out.println("<head>");
   3.202 -		out.println("<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" />");
   3.203 -		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");		
   3.204 -		out.println("<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" /> ");
   3.205 -		out.println("<style type=\"text/css\"> ");
   3.206 -		out.println("<!--");
   3.207 -		out.println(".style3 {font-size: 15px}");
   3.208 -		out.println(".style4 {font-size: 12px}");
   3.209 -		out.println(".style5 {font-size: 15px;font-weight: bold;}");
   3.210 -		out.println(".style6 {color: #FF0000}");
   3.211 -		out.println(".style7 {font-size: 14px}");
   3.212 -		out.println(" .hidden { visibility: hidden }");
   3.213 -		out.println("-->");
   3.214 -		out.println("</style> ");
   3.215 -		out.println("<title>TELEIOS: Strabon Endpoint</title>");
   3.216 -		out.println("</head>");
   3.217 -		out.println("<body topmargin=\"0\" leftmargin=\"0\" link=\"#FFFFFF\" vlink=\"#FFFFFF\" alink=\"#FFFFFF\" onload=\"initialize()\">");
   3.218 -		out.println("  <TABLE width=\"100%\" BORDER=0 CELLPADDING=0 CELLSPACING=0>");
   3.219 -		out.println("    <TR>");
   3.220 -		out.println("		");
   3.221 -		out.println("      <TD height=\"16\" background=\"images/pixi_white.gif\"><img src=\"images/pixi_white.gif\" width=\"16\" height=\"16\"></TD>");
   3.222 -		out.println("	</TR>");
   3.223 -		out.println("	<TR>");
   3.224 -		out.println("      <TD height=\"1\"></TD>");
   3.225 -		out.println("	</TR>		");
   3.226 -		out.println("	<TR>");
   3.227 -		out.println("      <TD height=\"60\" background=\"images/nav2_bg.gif\"> ");
   3.228 -		out.println("        <table width=\"100%\" border=\"0\">");
   3.229 -		out.println("         <tr>");
   3.230 -		out.println("            <td width=\"1\"><img src=\"images/nav2_bg.gif\" width=\"1\" height=\"60\"></td>");
   3.231 -		out.println("            <td valign=\"top\" width=\"80px\"><img border=\"0\" src=\"images/teleios_logo.png\" /></td>");
   3.232 -		out.println("            <td valign=\"top\" align=\"left\">");
   3.233 -		out.println("            <span class=\"logo\"> &nbsp stSPARQL Endpoint</span><br><span class=\"style4\"></span></td>");
   3.234 -		out.println("          </tr>");
   3.235 -		out.println("        </table> </TD>");
   3.236 -		out.println("	</TR>");
   3.237 -		out.println("	<TR>");
   3.238 -		out.println("      <TD height=\"50\" id=\"intro\">");
   3.239 -		out.println("On this page you can execute stSPARQL queries against the Strabon backend. " +
   3.240 -				"The dataset is based on  the following ontologies: " +
   3.241 -				"<a href=\"http://harmonisa.uni-klu.ac.at/content/land-use-land-cover-ontologies\" > Corine Land Cover </a>, " +
   3.242 -				"<a > Greek Administrative Geography(Kallikratis), </a>" +
   3.243 -				"<a href=\"http://labs.mondeca.com/dataset/lov/details/vocabulary_lgdo.html\" > Linked Geodata </a> " +
   3.244 -				" and <a href=\"http://www.geonames.org/search.html?q=ontology&country=\" >  geonames </a>." +
   3.245 -				"We also use the <a href=\"images/graph.png\">NOA ontology</a> we developed for the <a href=\"http://www.space.noa.gr/ \">NOA </a> use case of the European FP7 project " +
   3.246 -				"<a href=\"http://www.earthobservatory.eu/\" >TELEIOS </a>. ") ;
   3.247 -		out.println("<a onclick=\"return toggleMe('par')\" />(More) </a> <br>");
   3.248 -		out.println(" <p id=\"par\"> In this context NOA has been developing a real-time fire hotspot detection service for effectively monitoring a " +
   3.249 -				"fire-front. The technique is based on the use of acquisitions originating from the SEVIRI (Spinning Enhanced Visible and " +
   3.250 -				"Infrared Imager) sensor, on top of MSG-1 (Meteosat Second Generation satellite, renamed to Meteosat-8) and MSG-2 (renamed to " +
   3.251 -				"Meteosat-9) satellite platforms. Since 2007, NOA operates an MSG/SEVIRI acquisition station, and has been systematically archiving" +
   3.252 -				" raw satellite images on a 5 and 15 minutes basis, the respective temporal resolutions of MSG-1 and MSG-2. The acquired data are then annotated " +
   3.253 -				"using the stRDF model and can be queried using the stSPARQL query language. </p>  ");
   3.254 -		out.println("On the left sidebar, some example stSPARQL queries are provided. The NOA use case is described in more detail in the VLDB application paper " +
   3.255 -				"<a href=\"\"> here. </a> ");
   3.256 -		out.println("      </TD>");
   3.257 -		out.println("	</TR>");
   3.258 -		out.println("</TABLE>");
   3.259 -		out.println("<form " +
   3.260 -				"enctype=\"UTF-8\" " +
   3.261 -				"accept-charset=\"UTF-8\" "+
   3.262 -				"method=\"post\"" +
   3.263 -				">");
   3.264 -
   3.265 -		out.println("<table border=\"0\" width=\"100%\"><tr> ");
   3.266 -		out.println("<td width=\"90\" valign=\"top\" bgcolor=\"#dfe8f0\"> ");
   3.267 -		out.println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"165\" id=\"navigation\"> ");
   3.268 -		out.println("<tr><td width=\"90\" class=\"style4\"><a href=\"Query\" class=\"navText\">Query</a></td></tr> ");
   3.269 -        out.println("<tr><td width=\"90\" class=\"style4\"><a href=\"Describe?format=HTML\" class=\"navText\">Describe</a></td></tr> ");
   3.270 +    
   3.271 +    @Override
   3.272 +	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   3.273 +		doPost(request, response);
   3.274  	}
   3.275  
   3.276 -	protected static void appendHTML1b(PrintWriter out) {	
   3.277 -		out.println("<tr><td width=\"90\" class=\"style4\"><a href=\"store.jsp\" class=\"navText\" title=\"Store triples\">Store</a></td></tr> ");;
   3.278 -		out.println("<tr><td width=\"90\" class=\"style4\"><a href=\"javascript:history.go(0)\" class=\"navText\" title=\"Clear editor\">Clear</a></td></tr> ");
   3.279 -		out.println("</table>");
   3.280 -		out.println("</td>");
   3.281 -		out.println("<td width=\"*\" valign=\"top\" >"); 
   3.282 +    @Override
   3.283 +	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   3.284 +		request.setCharacterEncoding("UTF-8");
   3.285  
   3.286 -		out.println("<table cellspacing=\"5\">");
   3.287 -		out.println("<tr>");
   3.288 -		out.println("<td id=\"output\" \">stSPARQL Query:</td>");
   3.289 -		out.println("<td id=\"output\" \"><textarea name=\"SPARQLQuery\" title=\"pose your query/update here\" rows=\"15\" cols=\"100\">");
   3.290 +		if (VIEW_TYPE.equals(request.getParameter(VIEW))) {
   3.291 +			// HTML visual interface
   3.292 +			processVIEWRequest(request, response);
   3.293 +			
   3.294 +		} else {// invoked as a service
   3.295 +			processRequest(request, response);
   3.296 +	    }
   3.297  	}
   3.298 +    
   3.299 +    /**
   3.300 +     * Processes the request made from the HTML visual interface of Strabon Endpoint.
   3.301 +     * 
   3.302 +     * @param request
   3.303 +     * @param response
   3.304 +     * @throws ServletException
   3.305 +     * @throws IOException
   3.306 +     */
   3.307 +    private void processVIEWRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   3.308 +		// get the dispatcher for forwarding the rendering of the response
   3.309 +		RequestDispatcher dispatcher = request.getRequestDispatcher("describe.jsp");
   3.310 +		
   3.311 +		String query = request.getParameter("query");
   3.312 +		String format = request.getParameter("format");
   3.313 +		
   3.314 +		if (format == null || query == null) {
   3.315 +			request.setAttribute(ERROR, PARAM_ERROR);
   3.316 +			dispatcher.forward(request, response);
   3.317 +			
   3.318 +		} else {
   3.319 +			// set the query and format to be selected in the rendered page
   3.320 +			request.setAttribute("query", URLDecoder.decode(query, "UTF-8"));
   3.321 +			//request.setAttribute("format", URLDecoder.decode(reqFormat, "UTF-8"));
   3.322 +		
   3.323 +			try {
   3.324 +				ByteArrayOutputStream bos = new ByteArrayOutputStream();
   3.325  
   3.326 -	protected static void appendHTML2(PrintWriter out, String format) {
   3.327 -		out.println("</textarea></td>");
   3.328 -		out.println("</tr>");
   3.329 -		out.println("<tr>");
   3.330 -
   3.331 -		out.println("<td id=\"output\";\"><center>Output Format:<br/><select name=\"format\" title=\"select one of the following output format types\">");
   3.332 -		
   3.333 -		ArrayList<String> formats = new ArrayList<String>(Arrays.asList("HTML", "N-Triples", "RDF/XML", "N3", "TURTLE", "TRIG", "TRIX", "BinaryRDF"));
   3.334 -		
   3.335 -		for (String rdfFormat: formats) {
   3.336 -			out.print("<option ");
   3.337 -			if (rdfFormat.equalsIgnoreCase(format)) {
   3.338 -				out.print("selected");
   3.339 +				strabonWrapper.describe(query, format, bos);
   3.340 +				
   3.341 +				request.setAttribute(RESPONSE, StringEscapeUtils.escapeHtml(bos.toString()));
   3.342 +				
   3.343 +			} catch (Exception e) {
   3.344 +				request.setAttribute(ERROR, e.getMessage());
   3.345  			}
   3.346  			
   3.347 -			out.println(" value=\"" + rdfFormat + "\">" + rdfFormat + "</option>");
   3.348 +			dispatcher.forward(request, response);
   3.349  		}
   3.350 +
   3.351 +    }
   3.352 +    
   3.353 +    /**
   3.354 +     * Processes the request made by a client of the endpoint that uses it as a service. 
   3.355 +     * 
   3.356 +     * @param request
   3.357 +     * @param response
   3.358 +     * @throws IOException 
   3.359 +     */
   3.360 +    private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
   3.361 +		ServletOutputStream out = response.getOutputStream();
   3.362  		
   3.363 -		out.println("</select></center></td>");
   3.364 -		out.println("<td colspan=2 \"><br/><center><input type=\"submit\" title=\"execute query\" value=\"Describe\" name=\"submit\" /></center><br/></td>");
   3.365 -		out.println("</tr>");
   3.366 -	}
   3.367 -
   3.368 -	protected static void appendHTML3(PrintWriter out, String errorMessage) {
   3.369 -		out.println("<tr>");
   3.370 -		out.println("<td id=\"output\" \">Result: </td>");
   3.371 -		out.println("<td id=\"output\" \">");
   3.372 -		out.println(errorMessage);
   3.373 -		out.println("</td>");
   3.374 -		out.println("</tr>");
   3.375 -	}
   3.376 -
   3.377 -	protected static void appendHTML4(PrintWriter out) {
   3.378 -		out.println("<br/><br/>");
   3.379 -		out.println("</form>");
   3.380 -	}
   3.381 -
   3.382 -	protected static void appendHTML5(PrintWriter out) {
   3.383 -		out.println("</body>");
   3.384 -		out.println("</html>");
   3.385 -	}
   3.386 -
   3.387 -	protected static void appendHTMLQ(PrintWriter out, StrabonBeanWrapper strabonWrapper) throws UnsupportedEncodingException {
   3.388 -		out.println("<tr><td id=\"twidth\">");
   3.389 -		List<Entry> entries = strabonWrapper.getEntries();
   3.390 -		Iterator<Entry> it = entries.iterator();
   3.391 -		while (it.hasNext()) {
   3.392 -			Entry entry = it.next();
   3.393 -			out.println(createLink(entry));
   3.394 -		}
   3.395 -		out.println("</td></tr> ");
   3.396 -	}
   3.397 +		// get the RDF format (we check only the Accept header)
   3.398 +        RDFFormat format = getFormat(request.getHeader("accept"));
   3.399 +        
   3.400 +        // get the query
   3.401 +		String query = request.getParameter("query");
   3.402 +    	
   3.403 +    	// check for required parameters
   3.404 +    	if (format == null || query == null) {
   3.405 +    		response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
   3.406 +			out.print(ResponseMessages.getXMLHeader());
   3.407 +			out.print(ResponseMessages.getXMLException(PARAM_ERROR));
   3.408 +			out.print(ResponseMessages.getXMLFooter());
   3.409 +    		
   3.410 +    	} else {
   3.411 +    		// decode the query
   3.412 +    		query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
   3.413 +    		
   3.414 +	    	response.setContentType(format.getDefaultMIMEType());
   3.415 +		    response.setHeader("Content-Disposition", 
   3.416 +		    		"attachment; filename=describe." + format.getDefaultFileExtension() + "; " + format.getCharset());
   3.417 +		    
   3.418 +			try {
   3.419 +				strabonWrapper.describe(query, format.getName(), out);
   3.420 +				response.setStatus(HttpServletResponse.SC_OK);
   3.421 +				
   3.422 +			} catch (Exception e) {
   3.423 +				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
   3.424 +				out.print(ResponseMessages.getXMLHeader());
   3.425 +				out.print(ResponseMessages.getXMLException(e.getMessage()));
   3.426 +				out.print(ResponseMessages.getXMLFooter());
   3.427 +			}
   3.428 +    	}
   3.429 +    	
   3.430 +    	out.flush();
   3.431 +    }
   3.432  	
   3.433 -	private static String createLink(Entry entry) throws UnsupportedEncodingException {
   3.434 -		StringBuffer buf = new StringBuffer(1024);
   3.435 -		buf.append("<a href=\"");
   3.436 -		buf.append(entry.getBean());
   3.437 -		buf.append("?SPARQLQuery=");
   3.438 -		buf.append(URLEncoder.encode(entry.getStatement(), "UTF-8"));
   3.439 -		buf.append("&format=");
   3.440 -		buf.append(entry.getFormat()+"\"");
   3.441 -		buf.append("title="+"\""+entry.getTitle());
   3.442 -		buf.append("\">&nbsp;&middot;&nbsp;");
   3.443 -		buf.append(entry.getLabel());
   3.444 -		buf.append("</a><br/>");
   3.445 -		
   3.446 -		return buf.toString();
   3.447 -	}
   3.448 +    /**
   3.449 +     * Determines the RDF format to use. We check only for "accept"
   3.450 +     * parameter (present in the header). 
   3.451 +     * 
   3.452 +     * The use of "format" parameter is now deprecated for using the
   3.453 +     * DescribeBean as a service. It is only used through the HTML
   3.454 +     * visual interface, provided with Strabon Endpoint.
   3.455 +     * 
   3.456 +     * @param request
   3.457 +     * @return
   3.458 +     */
   3.459 +    private RDFFormat getFormat(String reqAccept) {
   3.460 +        if (reqAccept != null) {
   3.461 +            // check whether the "accept" parameter contains any 
   3.462 +            // of the mime types of any RDF format
   3.463 +            for (RDFFormat format : RDFFormat.values()) {
   3.464 +                    for (String mimeType : format.getMIMETypes()) {
   3.465 +                            if (reqAccept.contains(mimeType)) {
   3.466 +                                    return format;
   3.467 +                            }
   3.468 +                    }
   3.469 +            }
   3.470 +        }
   3.471 +                
   3.472 +        return null;
   3.473 +    }
   3.474  }
     4.1 --- a/scripts/endpoint	Tue Jul 24 18:54:40 2012 +0300
     4.2 +++ b/scripts/endpoint	Tue Jul 24 22:29:54 2012 +0300
     4.3 @@ -257,9 +257,28 @@
     4.4  			FORMAT="${1}"
     4.5  		fi
     4.6  
     4.7 +		MIME_TYPE=
     4.8  		case "${FORMAT}" in
     4.9 -			N-Triples|RDF/XML|N3|TURTLE|TRIG|TRIX|BinaryRDF)
    4.10 -				shift
    4.11 +			N-Triples)
    4.12 +				MIME_TYPE="text/plain"
    4.13 +				;;
    4.14 +			RDF/XML)
    4.15 +				MIME_TYPE="application/rdf+xml"
    4.16 +				;;
    4.17 +			N3)
    4.18 +				MIME_TYPE="text/rdf+n3"
    4.19 +				;;
    4.20 +			TURTLE)
    4.21 +				MIME_TYPE="text/turtle"
    4.22 +				;;
    4.23 +			TRIG)
    4.24 +				MIME_TYPE="application/x-trig"
    4.25 +				;;
    4.26 +			TRIX)
    4.27 +				MIME_TYPE="application/trix"
    4.28 +				;;
    4.29 +			BinaryRDF)
    4.30 +				MIME_TYPE="application/x-binary-rdf"
    4.31  				;;
    4.32  			*)
    4.33  				echo "${CMD}: unknown format \"${FORMAT}\"."
    4.34 @@ -268,7 +287,7 @@
    4.35  				;;
    4.36  		esac
    4.37  
    4.38 -		EXEC="curl ${CURL_OPTS} -d format='${FORMAT}' --data-urlencode SPARQLQuery='${PREFIXES}${QUERY}' ${URL}"
    4.39 +		EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
    4.40  		;;
    4.41  	*) 
    4.42  		help