Strabon
changeset 1357:c37086c4a55f
Fix for Bug #64: the endpoint now parses Accept headers with multiple values and uses the first mimetype corresponding to a valid stSPARQLQueryResultFormat. If preferences are attached to such values (using qvalues, i.e., text/plain;q=0.5), then these preferences are ignored.
author | Babis Nikolaou <charnik@di.uoa.gr> |
---|---|
date | Thu Sep 11 19:00:22 2014 +0300 (2014-09-11) |
parents | c4055a32472c |
children | 65db010f01ac |
files | endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java |
line diff
1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java Thu Sep 11 17:33:38 2014 +0300 1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java Thu Sep 11 19:00:22 2014 +0300 1.3 @@ -16,10 +16,11 @@ 1.4 import java.math.BigInteger; 1.5 import java.net.URLDecoder; 1.6 import java.security.SecureRandom; 1.7 +import java.util.ArrayList; 1.8 import java.util.Date; 1.9 import java.util.Iterator; 1.10 -import java.util.ArrayList; 1.11 import java.util.List; 1.12 +import java.util.StringTokenizer; 1.13 1.14 import javax.servlet.RequestDispatcher; 1.15 import javax.servlet.ServletConfig; 1.16 @@ -33,7 +34,6 @@ 1.17 import org.apache.commons.io.FileUtils; 1.18 import org.apache.commons.lang.StringEscapeUtils; 1.19 import org.openrdf.query.MalformedQueryException; 1.20 -import org.openrdf.query.TupleQueryResult; 1.21 import org.openrdf.query.resultio.TupleQueryResultFormat; 1.22 import org.openrdf.query.resultio.stSPARQLQueryResultFormat; 1.23 import org.slf4j.Logger; 1.24 @@ -115,9 +115,6 @@ 1.25 // get the name of this web application 1.26 appName = context.getContextPath().replace("/", ""); 1.27 1.28 - 1.29 - 1.30 - 1.31 } 1.32 1.33 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 1.34 @@ -173,26 +170,30 @@ 1.35 private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { 1.36 ServletOutputStream out = response.getOutputStream(); 1.37 1.38 - // get the stSPARQL Query Result format (we check only the Accept header) 1.39 - stSPARQLQueryResultFormat format = stSPARQLQueryResultFormat.forMIMEType(request.getHeader("accept")); 1.40 - 1.41 - // get the query 1.42 + // get desired formats (we check only the Accept header) 1.43 + List<stSPARQLQueryResultFormat> formats = parseMultiValuedAcceptHeader(request.getHeader("accept")); 1.44 + 1.45 + // get the query and the limit 1.46 String query = request.getParameter("query"); 1.47 String maxLimit = request.getParameter("maxLimit"); 1.48 - 1.49 - // check for required parameters 1.50 - if (format == null || query == null) { 1.51 - logger.error("[StrabonEndpoint.QueryBean] {}", PARAM_ERROR); 1.52 - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 1.53 + 1.54 + // check for required parameters 1.55 + if (formats.size() == 0 || query == null) { 1.56 + logger.error("[StrabonEndpoint.QueryBean] {}", PARAM_ERROR); 1.57 + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 1.58 out.print(ResponseMessages.getXMLHeader()); 1.59 out.print(ResponseMessages.getXMLException(PARAM_ERROR)); 1.60 out.print(ResponseMessages.getXMLFooter()); 1.61 - 1.62 - } else { 1.63 - // decode the query 1.64 - query = URLDecoder.decode(request.getParameter("query"), "UTF-8"); 1.65 - 1.66 + 1.67 + } else { 1.68 + // just use the first specified format 1.69 + stSPARQLQueryResultFormat format = formats.get(0); 1.70 + 1.71 + // decode the query 1.72 + query = URLDecoder.decode(request.getParameter("query"), "UTF-8"); 1.73 + 1.74 response.setContentType(format.getDefaultMIMEType()); 1.75 + 1.76 try { 1.77 query = strabonWrapper.addLimit(query, maxLimit); 1.78 strabonWrapper.query(query, format.getName(), out); 1.79 @@ -205,11 +206,11 @@ 1.80 out.print(ResponseMessages.getXMLException(e.getMessage())); 1.81 out.print(ResponseMessages.getXMLFooter()); 1.82 } 1.83 - } 1.84 - 1.85 - out.flush(); 1.86 + } 1.87 + 1.88 + out.flush(); 1.89 } 1.90 - 1.91 + 1.92 /** 1.93 * Processes the request made from the HTML visual interface of Strabon Endpoint. 1.94 * 1.95 @@ -385,4 +386,43 @@ 1.96 } 1.97 } 1.98 } 1.99 + 1.100 + /** 1.101 + * Given an Accept header, it parses it and extracts the mime types for the accepted formats. 1.102 + * The header might contain multiple accepted values and qvalues as well, however, qvalues 1.103 + * are ignored. The extracted mime types are then transformed to stSPARQLQueryResultFormat 1.104 + * and a list of such objects is returned. If a mimetype is not valid, then it is ignored. 1.105 + * If all mimetypes are invalid, then the returned list has zero elements, but it is not 1.106 + * null. 1.107 + * 1.108 + * @param header 1.109 + * @return 1.110 + */ 1.111 + private List<stSPARQLQueryResultFormat> parseMultiValuedAcceptHeader(String header) { 1.112 + List<stSPARQLQueryResultFormat> formats = new ArrayList<stSPARQLQueryResultFormat>(); 1.113 + 1.114 + StringTokenizer token = new StringTokenizer(header, ", "); 1.115 + 1.116 + while (token.hasMoreTokens()) { 1.117 + String value = token.nextToken(); 1.118 + 1.119 + // value might contain qvalues (e.g., "text/plain; q=0.2") 1.120 + // for the time being, we just discard them 1.121 + int idx_sep_cut = value.indexOf(';'); 1.122 + if (idx_sep_cut > 0) { 1.123 + value = value.substring(0, idx_sep_cut); 1.124 + } 1.125 + 1.126 + // get the stSPARQL Query Result format 1.127 + stSPARQLQueryResultFormat format = stSPARQLQueryResultFormat.forMIMEType(value); 1.128 + 1.129 + // keep only the valid formats (non-null) 1.130 + if (format != null) { 1.131 + formats.add(format); 1.132 + } 1.133 + } 1.134 + 1.135 + return formats; 1.136 + } 1.137 + 1.138 }