Commits

tbrugz  committed 5667ed1

queryon: added header "Content-Location" (optional)

  • Participants
  • Parent commits 56fd77a

Comments (0)

Files changed (3)

File src/queryon.template.properties

 queryon.limit.default=100
 queryon.limit.max=1000
 
+# queryon xtra headers (defaults first)
+queryon.baseurl=http://localhost:9090/queryon/q/
+queryon.headers.addcontentlocation=false|true
+
 # rdf properties
 sqldump.rdf.base=http://localhost:9090/queryon/id/
 

File src/tbrugz/queryon/QueryOn.java

 import tbrugz.sqldump.def.DBMSResources;
 import tbrugz.sqldump.def.SchemaModelGrabber;
 import tbrugz.sqldump.util.ParametrizedProperties;
+import tbrugz.sqldump.util.Utils;
 
 /**
  * @see Web API Design - http://info.apigee.com/Portals/62317/docs/web%20api.pdf
 	static final String PROPERTIES_PATH = "properties-resource";
 	static final String DEFAULT_PROPERTIES_RESOURCE = "/queryon.properties";
 	static final String CONN_PROPS_PREFIX = "queryon";
+	
 	static final String PROP_DEFAULT_LIMIT = "queryon.limit.default";
 	static final String PROP_MAX_LIMIT = "queryon.limit.max";
+	static final String PROP_BASE_URL = "queryon.baseurl";
+	static final String PROP_HEADERS_ADDCONTENTLOCATION = "queryon.headers.addcontentlocation";
+	
+	static final String REQ_ATTR_CONTENTLOCATION = "attr.contentlocation";
 
 	static final String DEFAULT_OUTPUT_SYNTAX = "html";
 	
 			if(propertiesResource==null) { propertiesResource = DEFAULT_PROPERTIES_RESOURCE; }
 			
 			log.info("loading properties: "+propertiesResource);
-			//XXX: path: add host port? servlet mapping url-pattern? 
+			//XXX: path: add host port (request object needed?)? servlet mapping url-pattern? 
 			String path = "http://"+InetAddress.getLocalHost().getHostName()+getServletContext().getContextPath()+"/";
+			prop.setProperty(PROP_BASE_URL, path);
 			prop.setProperty(RDFAbstractSyntax.PROP_RDF_BASE, path);
 			prop.load(QueryOn.class.getResourceAsStream(propertiesResource));
 			
 
 		List<FK> fks = DBIdentifiable.getImportedKeys(relation, model.getForeignKeys());
 		List<Constraint> uks = DBIdentifiable.getUKs(relation);
+		
+		if(Utils.getPropBool(prop, PROP_HEADERS_ADDCONTENTLOCATION, false)) {
+			String contentLocation = reqspec.getCanonicalUrl(prop);
+			log.info("content-location header: "+contentLocation);
+			reqspec.request.setAttribute(REQ_ATTR_CONTENTLOCATION, contentLocation);
+		}
+		
 		dumpResultSet(rs, reqspec, relation.getName(), pk!=null?pk.uniqueColumns:null, fks, uks, applyLimitOffsetInResultSet, resp);
 		
 		}
 		
 		ds.initDump(queryName, uniqueColumns, rs.getMetaData());
 
-		resp.addHeader("Content-type", ds.getMimeType());
+		resp.addHeader("Content-Type", ds.getMimeType());
 		//XXX download? http://stackoverflow.com/questions/398237/how-to-use-the-csv-mime-type
 		//resp.addHeader("Content-disposition", "attachment;filename="+table.name+"."+ds.getDefaultFileExtension());
+		String contentLocation = (String) reqspec.request.getAttribute(REQ_ATTR_CONTENTLOCATION);
+		if(contentLocation!=null) {
+			resp.addHeader("Content-Location", contentLocation);
+		}
 		
 		ds.dumpHeader(resp.getWriter());
 		while(rs.next()) {

File src/tbrugz/queryon/RequestSpec.java

 //XXX: order by? 3a,1d,2d?
 public class RequestSpec {
 	static final Log log = LogFactory.getLog(RequestSpec.class);
+	
+	final HttpServletRequest request;
 
 	final String httpMethod;
 	final String object;
 	final List<String> orderAscDesc = new ArrayList<String>();
 	
 	public RequestSpec(DumpSyntaxUtils dsutils, HttpServletRequest req, Properties prop) throws ServletException {
+		this.request = req;
 		String method = req.getParameter("method");
 		//XXX: may method be changed? property?
 		if(method!=null) {
 			//XXX: warn unknown parameters
 		}
 	}
+	
+	
+	/*
+	 * reconstructs URL pased on RequestSpec
+	 * 
+	 * will not use: output syntax, method?, updateValues
+	 * XXX may use: columns, offset, limit, filters, order??
+	 */
+	public String getCanonicalUrl(Properties prop) {
+		String url = prop.getProperty(QueryOn.PROP_BASE_URL)+object+"/";
+		//url = url.replaceAll("\\/+", "\\/");
+		String params = Utils.join(this.params, "/");
+		return url+params;
+	}
+	
 }