<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: status code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 00:46:28 GMT</pubDate>
    <description>DZone Snippets: status code</description>
    <item>
      <title>Redirect a URL with Ruby CGI</title>
      <link>http://snippets.dzone.com/posts/show/5415</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;require 'cgi'&lt;br /&gt;&lt;br /&gt;cgi = CGI.new&lt;br /&gt;print cgi.header({'Status' =&gt; '302 Moved', 'location' =&gt;  'http://www.wired.com'})&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;or&lt;br /&gt;&lt;code&gt;&lt;br /&gt;url = 'http://www.wired.com/'&lt;br /&gt;print cgi.header({'status'=&gt;'REDIRECT', 'Location'=&gt;url})&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;References:&lt;br /&gt;&lt;a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/48889"&gt;RE: cgi redirect&lt;/a&gt; [nagaokaut.ac.jp]&lt;br /&gt;&lt;a href="http://www.mokehehe.com/assari/index.php?Ruby%2FCGI"&gt;Ruby/CGI - assari&lt;/a&gt; [mokehehe.com]&lt;br /&gt;&lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"&gt;HTTP/1.1: Status Code Definitions&lt;/a&gt; [w3.org]</description>
      <pubDate>Mon, 21 Apr 2008 12:25:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5415</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Overriding Tomcat Valve to return extended login failure status</title>
      <link>http://snippets.dzone.com/posts/show/3715</link>
      <description>See &lt;a href="http://shadegrowncode.blogspot.com/2007/03/returning-login-failure-reason-in.html"&gt;Shade Grown Code&lt;/a&gt; for more information.&lt;br /&gt;&lt;br /&gt;ExtendedStatusSetter.java&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package com.ofc.tomcat;&lt;br /&gt;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Interface flagging that the implementing Realm can set request&lt;br /&gt; * headers providing additional information about an authentication&lt;br /&gt; * failure.&lt;br /&gt; *&lt;br /&gt; * @author Nicholas Sushkin&lt;br /&gt; */&lt;br /&gt;public interface ExtendedStatusSetter&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * The request attribute under which we forward an extended failure status message&lt;br /&gt;     * (as an object of type String) to a login error page.&lt;br /&gt;     */&lt;br /&gt;    public static String LOGIN_FAILURE_MESSAGE_ATTR = &lt;br /&gt;        "com.ofc.tomcat.LOGIN_FAILURE_MESSAGE";&lt;br /&gt;    &lt;br /&gt;    public void setExtendedStatus(String username, HttpServletRequest request, HttpServletResponse response);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;ExtendedStatusFormAuthenticator.java&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package com.ofc.tomcat;&lt;br /&gt;&lt;br /&gt;import org.apache.catalina.authenticator.Constants;&lt;br /&gt;import org.apache.catalina.authenticator.FormAuthenticator;&lt;br /&gt;import org.apache.catalina.Realm;&lt;br /&gt;import org.apache.catalina.connector.Request;&lt;br /&gt;import org.apache.catalina.connector.Response;&lt;br /&gt;import org.apache.catalina.deploy.LoginConfig;&lt;br /&gt;import org.apache.commons.logging.Log;&lt;br /&gt;import org.apache.commons.logging.LogFactory;&lt;br /&gt;import javax.servlet.RequestDispatcher;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Adds extended authentication failure status to tomcat FormAuthenticator.&lt;br /&gt; *&lt;br /&gt; * @author Nicholas Sushkin&lt;br /&gt; */&lt;br /&gt;public class ExtendedStatusFormAuthenticator extends FormAuthenticator&lt;br /&gt;{&lt;br /&gt;    /**&lt;br /&gt;     * Descriptive information about this implementation.&lt;br /&gt;     */&lt;br /&gt;    protected static final String info =&lt;br /&gt;        "com.ofc.tomcat.ExtendedStatusFormAuthenticator/1.0";&lt;br /&gt;&lt;br /&gt;    private static Log log = LogFactory.getLog(ExtendedStatusFormAuthenticator.class);&lt;br /&gt;&lt;br /&gt;    // ------------------------------------------------------------- Properties&lt;br /&gt;    /**&lt;br /&gt;     * Return descriptive information about this Valve implementation.&lt;br /&gt;     */&lt;br /&gt;    @Override&lt;br /&gt;    public String getInfo() &lt;br /&gt;    {&lt;br /&gt;        return info;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // ------------------------------------------------------------- Overridden behavior&lt;br /&gt;    /**&lt;br /&gt;     * Called to forward to the error page&lt;br /&gt;     * &lt;br /&gt;     * @param request Request we are processing&lt;br /&gt;     * @param response Response we are creating&lt;br /&gt;     * @param config    Login configuration describing how authentication&lt;br /&gt;     *              should be performed&lt;br /&gt;     */&lt;br /&gt;    @Override&lt;br /&gt;    protected void forwardToErrorPage(Request request, Response response, LoginConfig config) &lt;br /&gt;    {&lt;br /&gt;        Realm realm = context.getRealm();&lt;br /&gt;&lt;br /&gt;        if (realm instanceof ExtendedStatusSetter)&lt;br /&gt;        {&lt;br /&gt;            log.debug("realm implements ExtendedStatusSetter, setting extended status for error page");&lt;br /&gt;            String username = request.getParameter(Constants.FORM_USERNAME);&lt;br /&gt;            ((ExtendedStatusSetter) realm).setExtendedStatus(username, request.getRequest(), response.getResponse());&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;            log.debug("realm does not implement ExtendedStatusSetter, NOT setting extended status for error page");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        RequestDispatcher disp =&lt;br /&gt;            context.getServletContext().getRequestDispatcher&lt;br /&gt;            (config.getErrorPage());&lt;br /&gt;        try {&lt;br /&gt;            disp.forward(request.getRequest(), response.getResponse());&lt;br /&gt;            response.finishResponse();&lt;br /&gt;        } catch (Throwable t) {&lt;br /&gt;            log.warn("Unexpected error forwarding to error page", t);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Realm implementation will include the following&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class AccountLockoutDatasourceRealm extends DataSourceRealm implements ExtendedStatusSetter&lt;br /&gt;{&lt;br /&gt;    // ...&lt;br /&gt;&lt;br /&gt;    public void setExtendedStatus(String username, HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;    {&lt;br /&gt;        setMessage(request, "Account locked");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    protected void setMessage(HttpServletRequest request, String message)&lt;br /&gt;    {&lt;br /&gt;        request.setAttribute(ExtendedStatusSetter.LOGIN_FAILURE_MESSAGE_ATTR, message);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 22 Mar 2007 22:25:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3715</guid>
      <author>NicholasSushkin (Nicholas Sushkin)</author>
    </item>
    <item>
      <title>Showing message on status bar</title>
      <link>http://snippets.dzone.com/posts/show/1642</link>
      <description>There's an empty space below app.title that we can&lt;br /&gt;use as a status bar. So, I modify this &lt;a href=http://bigbold.com/snippets/posts/show/839&gt;recipe&lt;/a&gt; and &lt;br /&gt;make it into a module. (need fgimage.pyd from &lt;a href=http://pymbian.sourceforge.net/misc/fgimage-v0.20051022.zip&gt;here&lt;/a&gt;)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# status.py&lt;br /&gt;from graphics import *&lt;br /&gt;import fgimage&lt;br /&gt;__all__ = ["status_on", "status_off"]&lt;br /&gt;&lt;br /&gt;bar = Image.new((119,13))&lt;br /&gt;bgcolor = screenshot().getpixel((0,0))[0]&lt;br /&gt;fg = fgimage.FGImage()&lt;br /&gt;&lt;br /&gt;def status_on(message=None):&lt;br /&gt;    if message is not None:&lt;br /&gt;        bar.clear(bgcolor)&lt;br /&gt;        bar.text((0,11), unicode(message), 0xffffff)&lt;br /&gt;    fg.set(57,30, bar._bitmapapi())&lt;br /&gt;&lt;br /&gt;def status_off():&lt;br /&gt;    fg.unset()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;You can use it easily this way&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; from status import *&lt;br /&gt;&gt;&gt;&gt; status_on('Hello world')  # show it&lt;br /&gt;&gt;&gt;&gt; status_off()              # hide it&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;See a &lt;a href=http://www.frappr.com/pys60/photo/1501182&gt;screenshot&lt;/a&gt;.</description>
      <pubDate>Mon, 06 Mar 2006 20:58:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1642</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
