Strabon

changeset 88:fe79e508b1a4

added UpdateBean for updates separating them from execution of SELECT queries in QueryBean, clean-up of QueryBean
author Babis Nikolaou <charnik@di.uoa.gr>
date Wed Apr 04 21:25:40 2012 +0300 (2012-04-04)
parents e1a54ea341b2
children d7e748ccd2eb
files endpoint/WebContent/WEB-INF/web.xml endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/UpdateBean.java
line diff
     1.1 --- a/endpoint/WebContent/WEB-INF/web.xml	Wed Apr 04 16:12:08 2012 +0300
     1.2 +++ b/endpoint/WebContent/WEB-INF/web.xml	Wed Apr 04 21:25:40 2012 +0300
     1.3 @@ -33,6 +33,17 @@
     1.4  		<url-pattern>/Store</url-pattern>
     1.5  	</servlet-mapping>
     1.6  	
     1.7 +	<servlet>
     1.8 +		<display-name>Update</display-name>
     1.9 +		<servlet-name>Update</servlet-name>
    1.10 +		<servlet-class>eu.earthobservatory.org.StrabonEndpoint.UpdateBean</servlet-class>
    1.11 +		<load-on-startup>1</load-on-startup>
    1.12 +	</servlet>
    1.13 +	<servlet-mapping>
    1.14 +		<servlet-name>Update</servlet-name>
    1.15 +		<url-pattern>/Update</url-pattern>
    1.16 +	</servlet-mapping>
    1.17 +		
    1.18  <!-- 	<servlet> -->
    1.19  <!-- 		<display-name>QueryServlet</display-name> -->
    1.20  <!-- 		<servlet-name>QueryServlet</servlet-name> -->
     2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Wed Apr 04 16:12:08 2012 +0300
     2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Wed Apr 04 21:25:40 2012 +0300
     2.3 @@ -30,7 +30,8 @@
     2.4  	
     2.5  	private static final long serialVersionUID = -378175118289907707L;
     2.6  	
     2.7 -	public QueryBean() {}
     2.8 +	private ServletContext context; 
     2.9 +	private StrabonBeanWrapper strabonWrapper;
    2.10  	
    2.11  	public void doGet(HttpServletRequest request, HttpServletResponse response)
    2.12  	throws ServletException, IOException
    2.13 @@ -86,7 +87,12 @@
    2.14  		String reqFuncionality = (request.getParameter("submit") == null) ? "" : request.getParameter("submit");
    2.15  		
    2.16  		// check whether Update submit button was fired
    2.17 -		boolean isUpdate = (reqFuncionality.equals("Update") ? true:false);
    2.18 +		if (reqFuncionality.equals("Update")) { // only for executions from web browsers
    2.19 +			response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
    2.20 +			response.sendRedirect("Update?SPARQLQuery=" + hive.getSPARQLQuery());
    2.21 +			
    2.22 +			return;
    2.23 +		}
    2.24  		
    2.25  		if ((reqFormat == "") && (reqAccept == "")) {
    2.26  			hive.setFormat("HTML");
    2.27 @@ -115,31 +121,6 @@
    2.28  		}
    2.29  		//System.out.println("\n\n\n\n\format='"+this.format+"'\n\n\n\n\n");
    2.30  		
    2.31 -		ServletContext context = getServletContext();
    2.32 -		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    2.33 -		StrabonBeanWrapper strabonWrapper = (StrabonBeanWrapper)applicationContext.getBean("strabonBean");
    2.34 -		
    2.35 -		/* Execution of UPDATE query */  
    2.36 -		if (isUpdate) {
    2.37 -			String answer = "";
    2.38 -			try {
    2.39 -				strabonWrapper.getStrabon().update(hive.getSPARQLQuery(), strabonWrapper.getStrabon().getSailRepoConnection());
    2.40 -				response.setStatus(HttpServletResponse.SC_OK);
    2.41 -				answer = "true";
    2.42 -				
    2.43 -			} catch(MalformedQueryException e) {
    2.44 -				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    2.45 -				answer = "<exception>\n"+e.getMessage()+"\n\t</exception>";
    2.46 -			}
    2.47 -			
    2.48 -			// write response to client
    2.49 -			response.getWriter().append(getUPDATEHeaderResponse());
    2.50 -			response.getWriter().append(answer);
    2.51 -			response.getWriter().append(getUPDATEFooterResponse());
    2.52 -			return;
    2.53 -		}
    2.54 -		/* Execution of UPDATE query */
    2.55 -
    2.56  		PrintWriter out = response.getWriter();
    2.57  		
    2.58  		if ((hive.getFormat().equalsIgnoreCase("KML"))) {
    2.59 @@ -177,9 +158,9 @@
    2.60   
    2.61  	            //System.out.println("Done");
    2.62   
    2.63 -    	     }catch(IOException e){
    2.64 +    	     } catch(IOException e) {
    2.65      		        e.printStackTrace();
    2.66 -    	           }
    2.67 +    	     }
    2.68      	     
    2.69   			 response.setContentType("application/vnd.google-earth.kml+xml; charset=UTF-8");
    2.70   			 response.setDateHeader("Expires", 0);
    2.71 @@ -258,26 +239,14 @@
    2.72  		out.flush();
    2.73  	}
    2.74  
    2.75 -	/**
    2.76 -	 * Used as the template answer for UPDATE queries.
    2.77 -	 * @return
    2.78 -	 */
    2.79 -	private String getUPDATEHeaderResponse() {
    2.80 -		return "<?xml version='1.0' encoding='UTF-8'?>\n" +
    2.81 -			   "<response>\n" +
    2.82 -			   "\t";
    2.83 -	}
    2.84 -	
    2.85 -	/**
    2.86 -	 * Used as the template answer for UPDATE queries.
    2.87 -	 * @return
    2.88 -	 */
    2.89 -	private String getUPDATEFooterResponse() {
    2.90 -		return "\n</response>\n";
    2.91 -	}
    2.92 -
    2.93  	public void init(ServletConfig servletConfig) throws ServletException {
    2.94  		super.init(servletConfig);
    2.95 +		
    2.96 +		// get StrabonWrapper
    2.97 +		context = getServletContext();
    2.98 +		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    2.99 +		
   2.100 +		strabonWrapper = (StrabonBeanWrapper)applicationContext.getBean("strabonBean");
   2.101  	}
   2.102  
   2.103  	public String evaluateQuery(StrabonBeanWrapper strabonWrapper, String resultFormat, String reqFunctionality, String SPARQLQuery, StringBuilder errorMessage) {		
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/UpdateBean.java	Wed Apr 04 21:25:40 2012 +0300
     3.3 @@ -0,0 +1,82 @@
     3.4 +/**
     3.5 + * 
     3.6 + */
     3.7 +package eu.earthobservatory.org.StrabonEndpoint;
     3.8 +
     3.9 +import java.io.IOException;
    3.10 +
    3.11 +import javax.servlet.ServletConfig;
    3.12 +import javax.servlet.ServletContext;
    3.13 +import javax.servlet.ServletException;
    3.14 +import javax.servlet.http.HttpServlet;
    3.15 +import javax.servlet.http.HttpServletRequest;
    3.16 +import javax.servlet.http.HttpServletResponse;
    3.17 +
    3.18 +import org.openrdf.query.MalformedQueryException;
    3.19 +import org.springframework.web.context.WebApplicationContext;
    3.20 +import org.springframework.web.context.support.WebApplicationContextUtils;
    3.21 +
    3.22 +/**
    3.23 + * @author charnik
    3.24 + *
    3.25 + */
    3.26 +public class UpdateBean extends HttpServlet {
    3.27 +
    3.28 +	private static final long serialVersionUID = -633279376188071670L;
    3.29 +
    3.30 +	private StrabonBeanWrapper strabonWrapper;
    3.31 +	
    3.32 +	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    3.33 +		doPost(request, response);
    3.34 +	}
    3.35 +			
    3.36 +	@Override
    3.37 +	public void init(ServletConfig servletConfig) throws ServletException {
    3.38 +		super.init(servletConfig);
    3.39 +		
    3.40 +		// get strabon wrapper
    3.41 +		ServletContext context = getServletContext();
    3.42 +		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    3.43 +		
    3.44 +		strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean");
    3.45 +	}
    3.46 +	
    3.47 +	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    3.48 +		String query = request.getParameter("SPARQLQuery");
    3.49 +		
    3.50 +		String answer = "";
    3.51 +		try {
    3.52 +			strabonWrapper.getStrabon().update(query, strabonWrapper.getStrabon().getSailRepoConnection());
    3.53 +			response.setStatus(HttpServletResponse.SC_OK);
    3.54 +			answer = "true";
    3.55 +			
    3.56 +		} catch(MalformedQueryException e) {
    3.57 +			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    3.58 +			answer = "<exception>\n"+e.getMessage()+"\n\t</exception>";
    3.59 +		}
    3.60 +		
    3.61 +		// write response to client
    3.62 +		response.getWriter().append(UpdateBean.getUPDATEHeaderResponse());
    3.63 +		response.getWriter().append(answer);
    3.64 +		response.getWriter().append(UpdateBean.getUPDATEFooterResponse());
    3.65 +	}
    3.66 +	
    3.67 +	/**
    3.68 +	 * Used as the template answer for UPDATE queries.
    3.69 +	 * @return
    3.70 +	 */
    3.71 +	public static String getUPDATEHeaderResponse() {
    3.72 +		return "<?xml version='1.0' encoding='UTF-8'?>\n" +
    3.73 +			   "<response>\n" +
    3.74 +			   "\t";
    3.75 +	}
    3.76 +	
    3.77 +	/**
    3.78 +	 * Used as the template answer for UPDATE queries.
    3.79 +	 * @return
    3.80 +	 */
    3.81 +	public static String getUPDATEFooterResponse() {
    3.82 +		return "\n</response>\n";
    3.83 +	}
    3.84 +
    3.85 +}