[pal-cvs 3207] [944] added SSLRedirectValve to redirect to ssl or non-ssl page.

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2008年 6月 1日 (日) 08:19:26 JST


Revision: 944
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=pal&view=rev&rev=944
Author:   shinsuke
Date:     2008-06-01 08:19:25 +0900 (Sun, 01 Jun 2008)

Log Message:
-----------
added SSLRedirectValve to redirect to ssl or non-ssl page.

Added Paths:
-----------
    pal-portal/branches/pal-portal-1.x/portal/jetspeed-2/components/portal/src/java/jp/sf/pal/portal/redirect/
    pal-portal/branches/pal-portal-1.x/portal/jetspeed-2/components/portal/src/java/jp/sf/pal/portal/redirect/impl/
    pal-portal/branches/pal-portal-1.x/portal/jetspeed-2/components/portal/src/java/jp/sf/pal/portal/redirect/impl/SSLRedirectValveImpl.java


-------------- next part --------------
Added: pal-portal/branches/pal-portal-1.x/portal/jetspeed-2/components/portal/src/java/jp/sf/pal/portal/redirect/impl/SSLRedirectValveImpl.java
===================================================================
--- pal-portal/branches/pal-portal-1.x/portal/jetspeed-2/components/portal/src/java/jp/sf/pal/portal/redirect/impl/SSLRedirectValveImpl.java	                        (rev 0)
+++ pal-portal/branches/pal-portal-1.x/portal/jetspeed-2/components/portal/src/java/jp/sf/pal/portal/redirect/impl/SSLRedirectValveImpl.java	2008-05-31 23:19:25 UTC (rev 944)
@@ -0,0 +1,154 @@
+package jp.sf.pal.portal.redirect.impl;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jetspeed.om.page.ContentPage;
+import org.apache.jetspeed.om.page.Fragment;
+import org.apache.jetspeed.pipeline.PipelineException;
+import org.apache.jetspeed.pipeline.valve.AbstractValve;
+import org.apache.jetspeed.pipeline.valve.ValveContext;
+import org.apache.jetspeed.request.RequestContext;
+
+public class SSLRedirectValveImpl extends AbstractValve
+{
+
+    private static final String QUERY_SEPARATOR = "?";
+
+    private static final String REDIRECT_TO_SSL_PAGE_KEY = "redirect.to.ssl.page";
+
+    protected Log log = LogFactory.getLog(SSLRedirectValveImpl.class);
+
+    private static final String TRUE = "true";
+
+    private static final String FALSE = "false";
+
+    private static final String PORT_SEPARATOR = ":";
+
+    private static final String SCHEME_SEPARATOR = "://";
+
+    private static final String HTTP_SCHEME = "http";
+
+    private static final String HTTPS_SCHEME = "https";
+
+    private static final int DEFAULT_HTTPS_PORT = 443;
+
+    private static final int DEFAULT_HTTP_PORT = 80;
+
+    private String redirectToSSLPageKey;
+
+    private int httpPort;
+
+    private int httpsPort;
+
+    public SSLRedirectValveImpl()
+    {
+        redirectToSSLPageKey = REDIRECT_TO_SSL_PAGE_KEY;
+        httpPort = DEFAULT_HTTP_PORT;
+        httpsPort = DEFAULT_HTTPS_PORT;
+    }
+
+    public SSLRedirectValveImpl(String redirectToSSLPageKey, int httpPort,
+            int httpsPort)
+    {
+        this.redirectToSSLPageKey = redirectToSSLPageKey;
+        this.httpPort = httpPort;
+        this.httpsPort = httpsPort;
+    }
+
+    public void invoke(RequestContext request, ValveContext context)
+            throws PipelineException
+    {
+        ContentPage page = request.getPage();
+        if (page != null)
+        {
+            Fragment fragment = page.getRootFragment();
+            if (fragment != null)
+            {
+                String value = fragment.getProperty(redirectToSSLPageKey);
+                if (value != null)
+                {
+                    if (TRUE.equals(value))
+                    {
+                        // check if the page is redirected to SSL page
+                        String scheme = request.getRequest().getScheme();
+                        if (HTTP_SCHEME.equals(scheme))
+                        {
+                            try
+                            {
+                                sendRedirect(request, HTTPS_SCHEME, httpsPort,
+                                        DEFAULT_HTTPS_PORT);
+                                return;
+                            }
+                            catch (IOException e)
+                            {
+                                log
+                                        .warn(
+                                                "Could not redirect to a SSL page.",
+                                                e);
+                            }
+                        }
+                    }
+                    else if (FALSE.equals(value))
+                    {
+                        // check if the page is redirected to non-SSL page
+                        String scheme = request.getRequest().getScheme();
+                        if (HTTPS_SCHEME.equals(scheme))
+                        {
+                            try
+                            {
+                                sendRedirect(request, HTTP_SCHEME, httpPort,
+                                        DEFAULT_HTTP_PORT);
+                                return;
+                            }
+                            catch (IOException e)
+                            {
+                                log
+                                        .warn(
+                                                "Could not redirect to a non-SSL page.",
+                                                e);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        // Pass control to the next Valve in the Pipeline
+        context.invokeNext(request);
+    }
+
+    protected void sendRedirect(RequestContext request, String scheme,
+            int port, int defaultPort) throws IOException
+    {
+        HttpServletRequest servletRequest = request.getRequest();
+        StringBuffer url = new StringBuffer();
+        url.append(scheme).append(SCHEME_SEPARATOR).append(
+                servletRequest.getServerName());
+        if (port != defaultPort)
+        {
+            url.append(PORT_SEPARATOR).append(port);
+        }
+        url.append(servletRequest.getContextPath());
+        url.append(servletRequest.getServletPath());
+        if (servletRequest.getPathInfo() != null)
+        {
+            url.append(servletRequest.getPathInfo());
+        }
+        if (servletRequest.getQueryString() != null)
+        {
+            url.append(QUERY_SEPARATOR);
+            url.append(servletRequest.getQueryString());
+        }
+        request.getResponse().sendRedirect(url.toString());
+    }
+
+    public String toString()
+    {
+        return "SSLRedirectValve";
+    }
+
+}


Property changes on: pal-portal/branches/pal-portal-1.x/portal/jetspeed-2/components/portal/src/java/jp/sf/pal/portal/redirect/impl/SSLRedirectValveImpl.java
___________________________________________________________________
Name: svn:eol-style
   + native


pal-cvs メーリングリストの案内
Back to archive index