// Path: server/war/src/main/java/eu/stratuslab/marketplace/server/MarketplaceException.java
// @SuppressWarnings("serial")
// public class MarketplaceException extends RuntimeException {
// 
//     public MarketplaceException() {
//     }
// 
//     public MarketplaceException(String message) {
//         super(message);
//     }
//     
//     public MarketplaceException(String message, Throwable cause){
//     	super(message, cause);
//     }
// 
// }


import static eu.stratuslab.marketplace.metadata.MetadataNamespaceContext.MARKETPLACE_URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.parser.QueryParser;
import org.openrdf.query.parser.QueryParserUtil;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.sparql.lang.ParserSPARQL11;
import com.hp.hpl.jena.sparql.syntax.Element;
import com.hp.hpl.jena.sparql.syntax.ElementGroup;
import com.hp.hpl.jena.sparql.syntax.ElementNamedGraph;
import eu.stratuslab.marketplace.server.MarketplaceException;
        	SparqlQuery query = new SparqlQuery(
        			getQueryFromRequest(), 
        			SparqlQuery.OUTPUT_JSON);
        	
        	String results = (String)executeQuery(query);
        	if(results == null){
        		results = "";
        	}
        	        	        	
            representation = new StringRepresentation(results,
                    MediaType.APPLICATION_SPARQL_RESULTS_JSON);
        } catch (MalformedQueryException e) {
        	throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e
                    .getMessage(), e);
        }
        
        return representation;
    }
       
	private Object executeQuery(SparqlQuery query){
		//check that query is allowed.
    	if(!query.isAllowed()){
    		throw new ResourceException(
    				Status.CLIENT_ERROR_BAD_REQUEST,
    				"query not allowed.");
    	}
         
    	Object results = null;
		try {
			results = getFormattedQueryResults(query);
// Path: server/war/src/main/java/eu/stratuslab/marketplace/server/MarketplaceException.java
// @SuppressWarnings("serial")
// public class MarketplaceException extends RuntimeException {
// 
//     public MarketplaceException() {
//     }
// 
//     public MarketplaceException(String message) {
//         super(message);
//     }
//     
//     public MarketplaceException(String message, Throwable cause){
//     	super(message, cause);
//     }
// 
// }


// Path: server/war/src/main/java/eu/stratuslab/marketplace/server/resources/QueryResource.java
import static eu.stratuslab.marketplace.metadata.MetadataNamespaceContext.MARKETPLACE_URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.parser.QueryParser;
import org.openrdf.query.parser.QueryParserUtil;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.sparql.lang.ParserSPARQL11;
import com.hp.hpl.jena.sparql.syntax.Element;
import com.hp.hpl.jena.sparql.syntax.ElementGroup;
import com.hp.hpl.jena.sparql.syntax.ElementNamedGraph;
import eu.stratuslab.marketplace.server.MarketplaceException;

        	SparqlQuery query = new SparqlQuery(
        			getQueryFromRequest(), 
        			SparqlQuery.OUTPUT_JSON);
        	
        	String results = (String)executeQuery(query);
        	if(results == null){
        		results = "";
        	}
        	        	        	
            representation = new StringRepresentation(results,
                    MediaType.APPLICATION_SPARQL_RESULTS_JSON);
        } catch (MalformedQueryException e) {
        	throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e
                    .getMessage(), e);
        }
        
        return representation;
    }
       
	private Object executeQuery(SparqlQuery query){
		//check that query is allowed.
    	if(!query.isAllowed()){
    		throw new ResourceException(
    				Status.CLIENT_ERROR_BAD_REQUEST,
    				"query not allowed.");
    	}
         
    	Object results = null;
		try {
			results = getFormattedQueryResults(query);
		} catch (MarketplaceException e) {