Author: markt
Date: Tue Dec 16 13:55:53 2014
New Revision: 1645953

URL: http://svn.apache.org/r1645953
Log:
Make GenericPrincipal Serializable.

Added:
    tomcat/trunk/test/org/apache/catalina/realm/TestGenericPrincipal.java   
(with props)
    tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipal.java   (with 
props)
    
tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipalNonSerializable.java 
  (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/realm/GenericPrincipal.java

Modified: tomcat/trunk/java/org/apache/catalina/realm/GenericPrincipal.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/GenericPrincipal.java?rev=1645953&r1=1645952&r2=1645953&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/GenericPrincipal.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/GenericPrincipal.java Tue Dec 
16 13:55:53 2014
@@ -16,6 +16,7 @@
  */
 package org.apache.catalina.realm;
 
+import java.io.Serializable;
 import java.security.Principal;
 import java.util.Arrays;
 import java.util.List;
@@ -31,7 +32,10 @@ import org.ietf.jgss.GSSCredential;
  *
  * @author Craig R. McClanahan
  */
-public class GenericPrincipal implements TomcatPrincipal {
+public class GenericPrincipal implements TomcatPrincipal, Serializable {
+
+    private static final long serialVersionUID = 1L;
+
 
     // ----------------------------------------------------------- Constructors
 
@@ -239,4 +243,36 @@ public class GenericPrincipal implements
             loginContext.logout();
         }
     }
+
+
+    // ----------------------------------------------------------- 
Serialization
+
+    private Object writeReplace() {
+        return new SerializablePrincipal(name, password, roles, userPrincipal);
+    }
+
+    private static class SerializablePrincipal implements Serializable {
+        private static final long serialVersionUID = 1L;
+
+        private final String name;
+        private final String password;
+        private final String[] roles;
+        private final Principal principal;
+
+        public SerializablePrincipal(String name, String password, String[] 
roles,
+                Principal principal) {
+            this.name = name;
+            this.password = password;
+            this.roles = roles;
+            if (principal instanceof Serializable) {
+                this.principal = principal;
+            } else {
+                this.principal = null;
+            }
+        }
+
+        private Object readResolve() {
+            return new GenericPrincipal(name, password, Arrays.asList(roles), 
principal);
+        }
+    }
 }

Added: tomcat/trunk/test/org/apache/catalina/realm/TestGenericPrincipal.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/realm/TestGenericPrincipal.java?rev=1645953&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/realm/TestGenericPrincipal.java 
(added)
+++ tomcat/trunk/test/org/apache/catalina/realm/TestGenericPrincipal.java Tue 
Dec 16 13:55:53 2014
@@ -0,0 +1,89 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.catalina.realm;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestGenericPrincipal {
+
+    private static final String USER = "user";
+    private static final String PASSWORD = "pwd";
+    private static final List<String> ROLES = Collections.unmodifiableList(
+            Arrays.asList(new String[] { "ROLE1", "ROLE2" }));
+    private static final TesterPrincipal PRINCIPAL = new 
TesterPrincipal("Principal");
+    private static final TesterPrincipalNonSerializable 
PRINCIPAL_NON_SERIALIZABLE =
+            new TesterPrincipalNonSerializable("PrincipalNonSerializable");
+
+    @Test
+    public void testSerialize01() throws ClassNotFoundException, IOException {
+        GenericPrincipal gpIn = new GenericPrincipal(USER, PASSWORD, ROLES);
+        doTest(gpIn);
+    }
+
+    @Test
+    public void testSerialize02() throws ClassNotFoundException, IOException {
+        GenericPrincipal gpIn = new GenericPrincipal(USER, PASSWORD, ROLES, 
PRINCIPAL);
+        doTest(gpIn);
+    }
+
+    @Test
+    public void testSerialize03() throws ClassNotFoundException, IOException {
+        GenericPrincipal gpIn = new GenericPrincipal(USER, PASSWORD, ROLES, 
PRINCIPAL_NON_SERIALIZABLE);
+        doTest(gpIn);
+    }
+
+    private void doTest(GenericPrincipal gpIn)
+            throws ClassNotFoundException, IOException {
+        GenericPrincipal gpOut = serializeAndDeserialize(gpIn);
+
+        Assert.assertNull(gpOut.getGssCredential());
+        Assert.assertEquals(gpIn.getName(), gpOut.getName());
+        Assert.assertEquals(gpIn.getPassword(), gpOut.getPassword());
+        Assert.assertArrayEquals(gpIn.getRoles(), gpOut.getRoles());
+        if (gpIn == gpIn.getUserPrincipal()) {
+            Assert.assertEquals(gpOut, gpOut.getUserPrincipal());
+        } else if (gpIn.getUserPrincipal() instanceof Serializable) {
+            Assert.assertEquals(gpIn.getUserPrincipal(), 
gpOut.getUserPrincipal());
+        } else {
+            Assert.assertEquals(gpOut, gpOut.getUserPrincipal());
+        }
+    }
+
+    private GenericPrincipal serializeAndDeserialize(GenericPrincipal gpIn)
+            throws IOException, ClassNotFoundException {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(bos);
+        oos.writeObject(gpIn);
+
+        byte[] data = bos.toByteArray();
+
+        ByteArrayInputStream bis = new ByteArrayInputStream(data);
+        ObjectInputStream ois = new ObjectInputStream(bis);
+        return (GenericPrincipal) ois.readObject();
+    }
+}

Propchange: 
tomcat/trunk/test/org/apache/catalina/realm/TestGenericPrincipal.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipal.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipal.java?rev=1645953&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipal.java (added)
+++ tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipal.java Tue Dec 16 
13:55:53 2014
@@ -0,0 +1,66 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.catalina.realm;
+
+import java.io.Serializable;
+import java.security.Principal;
+
+public class TesterPrincipal implements Principal, Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private final String name;
+
+    public TesterPrincipal(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        TesterPrincipal other = (TesterPrincipal) obj;
+        if (name == null) {
+            if (other.name != null) {
+                return false;
+            }
+        } else if (!name.equals(other.name)) {
+            return false;
+        }
+        return true;
+    }
+}

Propchange: tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipal.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipalNonSerializable.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipalNonSerializable.java?rev=1645953&view=auto
==============================================================================
--- 
tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipalNonSerializable.java 
(added)
+++ 
tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipalNonSerializable.java 
Tue Dec 16 13:55:53 2014
@@ -0,0 +1,63 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.catalina.realm;
+
+import java.security.Principal;
+
+public class TesterPrincipalNonSerializable implements Principal {
+
+    private final String name;
+
+    public TesterPrincipalNonSerializable(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        TesterPrincipalNonSerializable other = 
(TesterPrincipalNonSerializable) obj;
+        if (name == null) {
+            if (other.name != null) {
+                return false;
+            }
+        } else if (!name.equals(other.name)) {
+            return false;
+        }
+        return true;
+    }
+}

Propchange: 
tomcat/trunk/test/org/apache/catalina/realm/TesterPrincipalNonSerializable.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to