From 8de840e829bd5e08fbc0f52608377fc3eb67382e Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 15 Mar 2012 21:22:49 +0000 Subject: Create 404s when row sets optionally don't return rows --- project2/cgi/cgiAppEngine.h | 1 + project2/cgi/cgiStagePresent.cpp | 8 ++++++-- project2/common/rowView.cpp | 8 +++++++- project2/common/rowView.h | 3 +++ project2/common/view.h | 1 + 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/project2/cgi/cgiAppEngine.h b/project2/cgi/cgiAppEngine.h index 7e081e6..854013a 100644 --- a/project2/cgi/cgiAppEngine.h +++ b/project2/cgi/cgiAppEngine.h @@ -103,6 +103,7 @@ class CgiApplicationEngine : public ApplicationEngine, public TransformChainLink virtual NextStage run(); virtual HttpHeaderPtr getHeader() const; protected: + HttpHeaderPtr header; mutable MultiRowSetPresenterPtr presenter; }; diff --git a/project2/cgi/cgiStagePresent.cpp b/project2/cgi/cgiStagePresent.cpp index 8ff1013..9c7ab7f 100644 --- a/project2/cgi/cgiStagePresent.cpp +++ b/project2/cgi/cgiStagePresent.cpp @@ -33,6 +33,11 @@ CgiApplicationEngine::PresentStage::run() } try { executeViews(); + header = HttpHeaderPtr(new Project2HttpHeader("200 OK")); + return NextStage(NULL, this, boost::dynamic_pointer_cast(presenter), presenter); + } + catch (EmptyRequiredRows) { + header = HttpHeaderPtr(new Project2HttpHeader("404 Not found")); return NextStage(NULL, this, boost::dynamic_pointer_cast(presenter), presenter); } catch (ResponseStagePtr p) { @@ -60,8 +65,7 @@ CgiApplicationEngine::ResponseStage::ResponseStage(ScriptNodePtr r) : CgiApplicationEngine::HttpHeaderPtr CgiApplicationEngine::PresentStage::getHeader() const { - Project2HttpHeader * header = new Project2HttpHeader("200 OK"); header->addHeader("Cache-control", "no-cache"); - return HttpHeaderPtr(header); + return header; } diff --git a/project2/common/rowView.cpp b/project2/common/rowView.cpp index e02ee23..efcf017 100644 --- a/project2/common/rowView.cpp +++ b/project2/common/rowView.cpp @@ -14,7 +14,8 @@ RowView::RowView(ScriptNodePtr p) : View(p), RowProcessor(p), rootName(p, "rootname"), - recordName(p, "recordname") + recordName(p, "recordname"), + required(p, "required", false) { BOOST_FOREACH(ScriptNodePtr node, p->childrenIn("columns")) { viewColumns.insert(Columns::value_type(node->get_name(), Variable(node))); @@ -37,6 +38,7 @@ RowView::loadComplete(const CommonObjects * co) void RowView::rowReady(const RowState * rs) const { + rowsFound = true; presenter->addNewRow(recordName()); if (viewColumns.empty()) { rs->foreachColumn(boost::bind(&RowSetPresenter::addNamedValue, presenter, _2, _3)); @@ -59,6 +61,7 @@ RowView::rowReady(const RowState * rs) const void RowView::execute(const MultiRowSetPresenter * p) const { + rowsFound = false; presenter = p; presenter->addNewRowSet(rootName()); ScopeObject pres(boost::bind(&MultiRowSetPresenter::finishRowSet, p)); @@ -67,6 +70,9 @@ RowView::execute(const MultiRowSetPresenter * p) const ScopeObject pres(boost::bind(&MultiRowSetPresenter::finishArray, p, true)); RowProcessor::execute(); } + if (required() && !rowsFound) { + throw EmptyRequiredRows(name); + } BOOST_FOREACH(SetAggregateCPtr s, setAggregates) { presenter->addNewArray(s->name, false); diff --git a/project2/common/rowView.h b/project2/common/rowView.h index 40ec8a9..250d54e 100644 --- a/project2/common/rowView.h +++ b/project2/common/rowView.h @@ -18,6 +18,7 @@ class RowView : public View, public RowProcessor { const Variable rootName; const Variable recordName; + const Variable required; protected: typedef std::map Columns; @@ -31,6 +32,8 @@ class RowView : public View, public RowProcessor { typedef ANONSTORAGEOF(SetAggregate) SetAggregates; SetAggregates setAggregates; mutable const MultiRowSetPresenter * presenter; + + mutable bool rowsFound; }; #endif diff --git a/project2/common/view.h b/project2/common/view.h index 5d13280..75a0f3c 100644 --- a/project2/common/view.h +++ b/project2/common/view.h @@ -5,6 +5,7 @@ #include "scriptStorage.h" class MultiRowSetPresenter; +SimpleMessageException(EmptyRequiredRows); /// Base class for Project2 components that output data class View : public virtual SourceObject { -- cgit v1.2.3