Daniel Schmidt wrote: > > Hi David, > > That is great. I got it working with the following code:
Excellent progress. More discussion below. (BTW, i collapsed the indenting to assist with text wrap.) > <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0"> > <map:components> > ... > <map:selectors> > <map:selector name="exception" > src="org.apache.cocoon.selection.ExceptionSelector"> > <exception name="FileNotFound" > class="org.apache.cocoon.ResourceNotFoundException"/> > <exception name="ContentNotAvailableInCountry" > class="org.apache.cocoon.ProcessingException"/> > </map:selector> > </map:selectors> > </map:components> > <map:resources> > <map:resource name="404"> > <map:generate src="404.xml"/> > <map:serialize status-code="404"/> > </map:resource> > <map:resource name="404-notavailableinyourcountry"> > <map:generate src="404-notavailableinyourcountry.xml"/> > <map:serialize status-code="404"/> > </map:resource> > ... > </map:resources> > <map:pipelines> > <map:pipeline> > <map:handle-errors> > <map:select type="exception"> > <map:when test="ContentNotAvailableInCountry"> > <map:call resource="404-notavailableinyourcountry"/> > </map:when> > <map:when test="FileNotFound"> > <map:call resource="404"/> > </map:when> > <map:otherwise> > <!-- something else --> > </map:otherwise> > </map:select> > </map:handle-errors> > ... > </map:pipeline> > </map:pipelines> > </map:sitemap> > > > So I always get a "ContentNotAvailableInCountry" exception if I do a > <xsl:message terminate="yes"/> in XSLT. Otherwise I'm serving a "generic" > error page or a FileNotFound error page. Mine was a bit more specific to trap the <exception name="transformer" class="javax.xml.transform.TransformerException"/> > However, it would be great if I could throw different exceptions from within > XSLT with the following approach: > <xsl:message terminate="yes">ContentNotAvailableInCountry</xsl:message> Yes i have identified the same need. It does help a little to use a muti-purpose map:transform in the handle-errors to show different content, depending on the value of the message. See the <map:generate type="exception"/> which then makes the message available in the xml stream. See an example via the URL below, i.e. mine is like this: <map:handle-errors> <map:select type="exception"> <map:when test="resourceNotFound"> <map:generate type="exception"/> <map:transform src="{lm:transform.exception.text}"> <map:parameter name="type" value="404"/> </map:transform> <map:serialize type="text" status-code="404"/> </map:when> <map:when test="transformer"> <map:generate type="exception"/> <map:transform src="{lm:transform.exception.text}"> <map:parameter name="type" value="transformer"/> </map:transform> <map:serialize type="text" status-code="403"/> </map:when> </map:select> </map:handle-errors> That {lm:...} stuff is the brilliant Forrest Locationmap. By default it resolves to the stylesheet at: https://svn.apache.org/repos/asf/forrest/trunk/main/webapp/resources/stylesheets/exception-to-text.xsl However that only addresses part of the problem. We still need specific different exceptions from the initial transformer available in the sitemap so that we can set different serializer status-code values. Also we aree masking other stylesheet errors by just trapping a generic transformer or processing exception. Surely we are not the first to encounter this need, so i started searching the net. Not had time to do much yet. This is an amazing co-incidence. I am just now dealing with this same issue in a Forrest-based project, and added that FAQ as part of that work. Now here we are at Cocoon helping each other with the same issue. Good timing. So i hope that bit helps. Just in case you have not seen it, and to add more connections info to the mail archives: http://wiki.apache.org/cocoon/ErrorHandling Some of that is old, but there is a good thread linked in the section about Cocoon 2.1 that is relevant: Subject: Generators now allowed in <map:handle-errors> http://marc.info/?l=xml-cocoon-dev&m=104923290631009 -David > I tried using an XPathExceptionSelector instead of the ExceptionSelector (the > numbers 1/2 in the code below are only there to point out a line; they are > not part of the code): > > <map:selector name="exception" > src="org.apache.cocoon.selection.XPathExceptionSelector"> > <exception name="Transform" class="org.apache.cocoon.ProcessingException"> > (1) <xpath name="ContentNotAvailableInCountry" test="rawMessage='Failed to > process pipeline'"/> > (2) <xpath name="ContentNotAvailableInCountry" > test="message='ContentNotAvailableInCountry'"/> > </exception> > </map:selector> > > While (1) works fine (it's equal to the ExceptionSelector approach since I > can't say what's the content of <xsl:message/>), I can't get (2) working. > > How can I test if the content of <xsl:message terminate="yes"/> is a certain > string with the help of XPathExceptionSelector? > > Thank you! > > -----Original Message----- > From: David Crossley [mailto:[email protected]] > Sent: Freitag, 13. Dezember 2013 00:49 > To: [email protected] > Subject: Re: Trigger Cocoon http response code from within XSL code > > Daniel Schmidt wrote: > > Hi Thorsten, > > > > Thanks for pointing out actions. I will have a closer look at them. > > > > Setting the status code is no problem, we already do that. The problem is > > that we only know within the XSLT code if a page is available in a certain > > country, because this is defined in XML. > > Could use "stylesheet-directed termination" (xsl:message terminate="yes") > Then in the relevant sitemap use "map:handle-errors" to set the status-code > on the serializer. > http://forrest.apache.org/faq.html#handle-errors > > -David > > > Thanks. > > > > > > > > From: Thorsten Scherler [mailto:[email protected]] > > Sent: Donnerstag, 12. Dezember 2013 14:33 > > To: [email protected] > > Subject: Re: Trigger Cocoon http response code from within XSL code > > > > On 12/12/2013 02:21 PM, Daniel Schmidt wrote: > > Hi, > > > > In our project we have multiple languages and some pages are not available > > in a certain country. If that is the case, we just output a message. For > > Search engines it would be good to respond with a different status code > > then 200. > > > > The question is: Is it possible to trigger a different http status code > > from within the XSL-code to the sitemap? Is there a way of doing something > > like that? > > > > Thank you! > > Daniel > > > > > > Daniel Schmidt | Jr Web Developer | TomTom Business Solutions | > > [email protected]<mailto:[email protected]> | > > +49(0)341 24495-920 office | +49(0)341 24495-888 fax | > > www.tomtom.com/business<http://www.tomtom.com/business> > > > > > > Depending which version I would suggest an action to do that. There are > > examples how to set the status code, the question which one would you want > > to set? From within xslt I think it is a bit more complicated. > > > > salu2 > > > > > > -- > > > > Thorsten Scherler <scherler.at.gmail.com> > > > > codeBusters S.L. - web based systems > > > > <consulting, training and solutions> > > > > > > > > http://www.codebusters.es/ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
