portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r1635981 - in /portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso: JetspeedSSOUtils.java SSOInitHttpRequestCommand.java
Date Sat, 01 Nov 2014 16:04:33 GMT
Author: woonsan
Date: Sat Nov  1 16:04:32 2014
New Revision: 1635981

URL: http://svn.apache.org/r1635981
Log:
JS2-1302, JS2-1303, JS2-1304, JS2-1305

extract utility methods

Modified:
    portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/JetspeedSSOUtils.java
    portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/SSOInitHttpRequestCommand.java

Modified: portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/JetspeedSSOUtils.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/JetspeedSSOUtils.java?rev=1635981&r1=1635980&r2=1635981&view=diff
==============================================================================
--- portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/JetspeedSSOUtils.java
(original)
+++ portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/JetspeedSSOUtils.java
Sat Nov  1 16:04:32 2014
@@ -149,73 +149,157 @@ public class JetspeedSSOUtils
 
                 if (ssoSites != null)
                 {
-                    final URI inputURI = URI.create(url);
-                    URI siteURI = null;
+                    ssoSite = getBestMatchedSSOSite(ssoSites, URI.create(url));
+                }
+            }
+            catch (Exception e)
+            {
+                if (log.isDebugEnabled())
+                {
+                    log.warn("Failed to retrieve sso site.", e);
+                }
+                else
+                {
+                    log.warn("Failed to retrieve sso site. {}", e.toString());
+                }
+            }
+        }
+
+        return ssoSite;
+    }
+
+    public static SSOSite getBestMatchedSSOSite(Collection<SSOSite> ssoSites, final
URI uri)
+    {
+        if (ssoSites == null || ssoSites.isEmpty())
+        {
+            return null;
+        }
 
-                    List<SSOSite> candidates = new ArrayList<SSOSite>();
+        SSOSite ssoSite = null;
+        URI siteURI = null;
 
-                    for (SSOSite candidate : ssoSites)
+        List<SSOSite> candidates = new ArrayList<SSOSite>();
+
+        for (SSOSite candidate : ssoSites)
+        {
+            siteURI = URI.create(candidate.getURL());
+
+            if (isEachFromSameOrigin(uri, siteURI))
+            {
+                candidates.add(candidate);
+            }
+        }
+
+        if (!candidates.isEmpty())
+        {
+            if (candidates.size() == 1)
+            {
+                ssoSite = candidates.get(0);
+            }
+            else
+            {
+                Collections.sort(candidates, new Comparator<SSOSite>() {
+                    @Override
+                    public int compare(SSOSite site1, SSOSite site2)
                     {
-                        siteURI = URI.create(candidate.getURL());
+                        URI uri1 = URI.create(site1.getURL());
+                        URI uri2 = URI.create(site2.getURL());
+                        int di1 = StringUtils.indexOfDifference(uri.getPath(), uri1.getPath());
+                        int di2 = StringUtils.indexOfDifference(uri.getPath(), uri2.getPath());
 
-                        if (StringUtils.equals(inputURI.getScheme(), siteURI.getScheme())
&&
-                                        StringUtils.equals(inputURI.getHost(), siteURI.getHost())
&&
-                                        inputURI.getPort() == siteURI.getPort())
+                        if (di1 == di2)
                         {
-                            candidates.add(candidate);
+                            return 0;
+                        }
+                        else if (di1 < di2)
+                        {
+                            return -1;
+                        }
+                        else
+                        {
+                            return 1;
                         }
                     }
+                });
+
+                ssoSite = candidates.get(candidates.size() - 1);
+            }
+        }
+
+        return ssoSite;
+    }
+
+    public static JetspeedSSOSiteCredentials getBestMatchedSSOSiteCrendentials(Collection<JetspeedSSOSiteCredentials>
ssoSiteCredsList, final URI uri)
+    {
+        if (ssoSiteCredsList == null || ssoSiteCredsList.isEmpty())
+        {
+            return null;
+        }
+
+        JetspeedSSOSiteCredentials ssoSiteCreds = null;
+        URI siteURI = null;
 
-                    if (!candidates.isEmpty())
+        List<JetspeedSSOSiteCredentials> candidates = new ArrayList<JetspeedSSOSiteCredentials>();
+
+        for (JetspeedSSOSiteCredentials candidate : ssoSiteCredsList)
+        {
+            siteURI = candidate.getBaseURI();
+
+            if (isEachFromSameOrigin(uri, siteURI))
+            {
+                candidates.add(candidate);
+            }
+        }
+
+        if (!candidates.isEmpty())
+        {
+            if (candidates.size() == 1)
+            {
+                ssoSiteCreds = candidates.get(0);
+            }
+            else
+            {
+                Collections.sort(candidates, new Comparator<JetspeedSSOSiteCredentials>()
{
+                    @Override
+                    public int compare(JetspeedSSOSiteCredentials creds1, JetspeedSSOSiteCredentials
creds2)
                     {
-                        if (candidates.size() == 1)
+                        URI uri1 = creds1.getBaseURI();
+                        URI uri2 = creds2.getBaseURI();
+                        int di1 = StringUtils.indexOfDifference(uri.getPath(), uri1.getPath());
+                        int di2 = StringUtils.indexOfDifference(uri.getPath(), uri2.getPath());
+
+                        if (di1 == di2)
                         {
-                            ssoSite = candidates.get(0);
+                            return 0;
+                        }
+                        else if (di1 < di2)
+                        {
+                            return -1;
                         }
                         else
                         {
-                            Collections.sort(candidates, new Comparator<SSOSite>()
{
-                                @Override
-                                public int compare(SSOSite site1, SSOSite site2)
-                                {
-                                    URI uri1 = URI.create(site1.getURL());
-                                    URI uri2 = URI.create(site2.getURL());
-                                    int di1 = StringUtils.indexOfDifference(inputURI.getPath(),
uri1.getPath());
-                                    int di2 = StringUtils.indexOfDifference(inputURI.getPath(),
uri2.getPath());
-
-                                    if (di1 == di2)
-                                    {
-                                        return 0;
-                                    }
-                                    else if (di1 < di2)
-                                    {
-                                        return -1;
-                                    }
-                                    else
-                                    {
-                                        return 1;
-                                    }
-                                }
-                            });
-
-                            ssoSite = candidates.get(candidates.size() - 1);
+                            return 1;
                         }
                     }
-                }
-            }
-            catch (Exception e)
-            {
-                if (log.isDebugEnabled())
-                {
-                    log.warn("Failed to retrieve sso site.", e);
-                }
-                else
-                {
-                    log.warn("Failed to retrieve sso site. {}", e.toString());
-                }
+                });
+
+                ssoSiteCreds = candidates.get(candidates.size() - 1);
             }
         }
 
-        return ssoSite;
+        return ssoSiteCreds;
     }
+
+    private static boolean isEachFromSameOrigin(URI uri1, URI uri2)
+    {
+        if (StringUtils.equals(uri1.getScheme(), uri2.getScheme()) &&
+                        StringUtils.equals(uri1.getHost(), uri2.getHost()) &&
+                        uri1.getPort() == uri2.getPort())
+        {
+            return true;
+        }
+
+        return false;
+    }
+
 }

Modified: portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/SSOInitHttpRequestCommand.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/SSOInitHttpRequestCommand.java?rev=1635981&r1=1635980&r2=1635981&view=diff
==============================================================================
--- portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/SSOInitHttpRequestCommand.java
(original)
+++ portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/sso/SSOInitHttpRequestCommand.java
Sat Nov  1 16:04:32 2014
@@ -40,33 +40,24 @@ public class SSOInitHttpRequestCommand e
     @Override
     protected HttpRequestBase createHttpRequest(final ProxyContext context) throws ReverseProxyException,
IOException
     {
-        List<JetspeedSSOSiteCredentials> ssoCredsList = getJetspeedSSOSiteCredentials(context);
+        List<JetspeedSSOSiteCredentials> ssoCredsList = getJetspeedFormSSOSiteCredentials(context);
+        URI remoteURI = context.getRemoteURI();
+        JetspeedSSOSiteCredentials ssoSiteCreds = JetspeedSSOUtils.getBestMatchedSSOSiteCrendentials(ssoCredsList,
remoteURI);
 
-        if (ssoCredsList != null && !ssoCredsList.isEmpty())
+        if (ssoSiteCreds != null)
         {
-            for (JetspeedSSOSiteCredentials ssoCreds : ssoCredsList)
-            {
-                if (ssoCreds.isFormAuthentication())
-                {
-                    URI remoteURI = context.getRemoteURI();
-
-                    if (remoteURI != null && remoteURI.equals(ssoCreds.getBaseURI()))
-                    {
-                        HttpRequestBase httpRequest = new HttpPost(remoteURI);
-                        List <NameValuePair> formParams = new ArrayList<NameValuePair>();
-                        formParams.add(new BasicNameValuePair(ssoCreds.getFormUserField(),
ssoCreds.getUsername()));
-                        formParams.add(new BasicNameValuePair(ssoCreds.getFormPwdField(),
ssoCreds.getPassword()));
-                        ((HttpPost) httpRequest).setEntity(new UrlEncodedFormEntity(formParams));
-                        return httpRequest;
-                    }
-                }
-            }
+            HttpRequestBase httpRequest = new HttpPost(remoteURI);
+            List <NameValuePair> formParams = new ArrayList<NameValuePair>();
+            formParams.add(new BasicNameValuePair(ssoSiteCreds.getFormUserField(), ssoSiteCreds.getUsername()));
+            formParams.add(new BasicNameValuePair(ssoSiteCreds.getFormPwdField(), ssoSiteCreds.getPassword()));
+            ((HttpPost) httpRequest).setEntity(new UrlEncodedFormEntity(formParams));
+            return httpRequest;
         }
 
         return super.createHttpRequest(context);
     }
 
-    private List<JetspeedSSOSiteCredentials> getJetspeedSSOSiteCredentials(final ProxyContext
context)
+    private List<JetspeedSSOSiteCredentials> getJetspeedFormSSOSiteCredentials(final
ProxyContext context)
     {
         List<JetspeedSSOSiteCredentials> ssoCredsList = null;
 
@@ -76,6 +67,21 @@ public class SSOInitHttpRequestCommand e
             ssoCredsList = (List<JetspeedSSOSiteCredentials>) session.getAttribute(SSOReverseProxyIFramePortlet.SUBJECT_SSO_SITE_CREDS);
         }
 
-        return ssoCredsList;
+        if (ssoCredsList == null)
+        {
+            return null;
+        }
+
+        List<JetspeedSSOSiteCredentials> formCredsList = new ArrayList<JetspeedSSOSiteCredentials>();
+
+        for (JetspeedSSOSiteCredentials ssoCreds : ssoCredsList)
+        {
+            if (ssoCreds.isFormAuthentication())
+            {
+                formCredsList.add(ssoCreds);
+            }
+        }
+
+        return formCredsList;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message