Strabon

changeset 628:9e0cad04ccd9

#6: Added a maximum limit for the number of results that are returned. If the query has already a limit which
is greater than the maximum limit it sets it to the maximum limit.
The application of the limit is controlled by the limitQuery parameter, which is passed to the QueryBean as a
hidden variable.
author Stella Giannakopoulou <sgian@di.uoa.gr>
date Mon Oct 08 21:15:46 2012 +0300 (2012-10-08)
parents a1a5e82e6844
children e6d414360f33
files endpoint/WebContent/query.jsp endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java
line diff
     1.1 --- a/endpoint/WebContent/query.jsp	Mon Oct 08 10:53:10 2012 +0300
     1.2 +++ b/endpoint/WebContent/query.jsp	Mon Oct 08 21:15:46 2012 +0300
     1.3 @@ -197,6 +197,7 @@
     1.4  
     1.5  <FORM enctype="UTF-8" accept-charset="UTF-8" method="post" action="Query">
     1.6  <INPUT type=hidden name="view" value="HTML"/>
     1.7 +<INPUT type=hidden name="limitQuery" value="true"/>
     1.8  
     1.9  <table border="0" width="100%">
    1.10  <tr> 
     2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Mon Oct 08 10:53:10 2012 +0300
     2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Mon Oct 08 21:15:46 2012 +0300
     2.3 @@ -208,16 +208,19 @@
     2.4  			String query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
     2.5  			String format = request.getParameter("format");
     2.6  			String handle = request.getParameter("handle");
     2.7 +			String limitQuery = request.getParameter("limitQuery");
     2.8  			
     2.9  			// get stSPARQLQueryResultFormat from given format name
    2.10  			stSPARQLQueryResultFormat queryResultFormat = stSPARQLQueryResultFormat.valueOf(format);
    2.11  			
    2.12 -			if (query == null || format == null || queryResultFormat == null) {
    2.13 +			if (query == null || format == null || queryResultFormat == null || limitQuery == null) {
    2.14  				dispatcher = request.getRequestDispatcher("query.jsp");
    2.15  				request.setAttribute(ERROR, PARAM_ERROR);
    2.16  				dispatcher.forward(request, response);
    2.17  				
    2.18  			} else {
    2.19 +				if(limitQuery.equals("true"))
    2.20 +					query = strabonWrapper.addLimit(query);
    2.21  				if ("download".equals(handle)) { // download as attachment
    2.22  					ServletOutputStream out = response.getOutputStream();
    2.23  					
     3.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Mon Oct 08 10:53:10 2012 +0300
     3.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Mon Oct 08 21:15:46 2012 +0300
     3.3 @@ -16,6 +16,8 @@
     3.4  import java.util.ArrayList;
     3.5  import java.util.Iterator;
     3.6  import java.util.List;
     3.7 +import java.util.regex.Matcher;
     3.8 +import java.util.regex.Pattern;
     3.9  
    3.10  import org.openrdf.model.Resource;
    3.11  import org.openrdf.query.MalformedQueryException;
    3.12 @@ -37,6 +39,7 @@
    3.13  	private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.org.StrabonEndpoint.StrabonBeanWrapper.class);
    3.14  	
    3.15  	private static final String FILE_PROTOCOL = "file";
    3.16 +	private static final int MAX_LIMIT = 300;
    3.17  	
    3.18  	private String serverName;
    3.19  	private int port;
    3.20 @@ -180,7 +183,6 @@
    3.21  		if ((this.strabon == null) && (!init())) {
    3.22  			throw new RepositoryException("Could not connect to Strabon.");
    3.23  		} 
    3.24 -
    3.25  		strabon.query(queryString, Format.fromString(answerFormatStrabon), strabon.getSailRepoConnection(), out);
    3.26  		
    3.27  	}
    3.28 @@ -338,5 +340,32 @@
    3.29  		return this.entries.get(i);
    3.30  	}
    3.31  	
    3.32 +	/*
    3.33 +	 * Limit the number of solutions returned.
    3.34 +	 * */
    3.35 +	public String addLimit(String queryString){
    3.36 +		String limitedQuery = queryString;
    3.37 +		Pattern limitPattern = Pattern.compile("limit \\d.*", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
    3.38 +		Matcher limitMatcher = limitPattern.matcher(queryString);
    3.39 +		
    3.40 +		// check whether the query contains a limit clause
    3.41 +		if(limitMatcher.find())		
    3.42 +		{
    3.43 +			String limitString = limitMatcher.group();
    3.44 +						
    3.45 +			Pattern rowsNumberPattern = Pattern.compile("\\d+");
    3.46 +			Matcher rowsNumberMatcher = rowsNumberPattern.matcher(limitString);
    3.47 +			rowsNumberMatcher.find();
    3.48 +			
    3.49 +			// if the initial limit is greater than the maximum, set it to the maximum
    3.50 +			if(Integer.valueOf(rowsNumberMatcher.group()) > MAX_LIMIT)			
    3.51 +				limitedQuery = limitMatcher.replaceAll("limit "+MAX_LIMIT);			
    3.52 +		}	
    3.53 +		else // add a limit to the query 
    3.54 +			limitedQuery = queryString+"limit "+MAX_LIMIT;
    3.55 +		
    3.56 +		return limitedQuery;
    3.57 +	}
    3.58 +
    3.59  }
    3.60