[seam-commits] Seam SVN: r12270 - in modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace: model and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Mar 25 05:45:28 EDT 2010


Author: shane.bryzak at jboss.com
Date: 2010-03-25 05:45:27 -0400 (Thu, 25 Mar 2010)
New Revision: 12270

Added:
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Authenticated.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Hex.java
Modified:
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/BlogAction.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ContentAction.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/FriendAction.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ImagePermission.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/PictureAction.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/PictureSearch.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ProfileAction.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/RegisterAction.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/model/Member.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/AuthenticationEvents.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/ContentServlet.java
   modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Hash.java
Log:
fix more compiler errors


Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/BlogAction.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/BlogAction.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/BlogAction.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -11,6 +11,10 @@
 import javax.persistence.EntityManager;
 import javax.persistence.NoResultException;
 
+import org.jboss.seam.security.examples.seamspace.model.BlogComment;
+import org.jboss.seam.security.examples.seamspace.model.Member;
+import org.jboss.seam.security.examples.seamspace.model.MemberBlog;
+
 @Named("blog")
 @ConversationScoped
 public class BlogAction
@@ -40,12 +44,15 @@
            .setParameter("memberName", name)
            .getSingleResult();
       }
-      catch (NoResultException ex) { }
+      catch (NoResultException ex) 
+      { 
+         return null;
+      }
    }   
    
-   @Begin
    public void createEntry()
    {
+      conversation.begin();
       selectedBlog = new MemberBlog();              
    }
    

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ContentAction.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ContentAction.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ContentAction.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -1,10 +1,11 @@
-package org.jboss.seam.security.examples.seamspace;
+package org.jboss.seam.security.examples.seamspace.action;
 
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.persistence.EntityManager;
 
 import org.jboss.seam.security.Identity;
+import org.jboss.seam.security.examples.seamspace.model.MemberImage;
 
 @Named
 public class ContentAction

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/FriendAction.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/FriendAction.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/FriendAction.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -50,7 +50,7 @@
          .getSingleResult();
                   
          Contexts.getMethodContext().set("friends", member.getFriends());
-         Identity.instance().checkPermission(member, "createFriendComment");
+         identity.checkPermission(member, "createFriendComment");
 
          friendComment = new FriendComment();
          friendComment.setFriend(authenticatedMember);

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ImagePermission.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ImagePermission.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ImagePermission.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -5,12 +5,16 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import javax.enterprise.context.Conversation;
 import javax.enterprise.inject.Model;
+import javax.inject.Inject;
 import javax.persistence.EntityManager;
 
 import org.jboss.seam.security.Role;
 import org.jboss.seam.security.SimplePrincipal;
 import org.jboss.seam.security.examples.seamspace.model.Member;
+import org.jboss.seam.security.examples.seamspace.model.MemberAccount;
+import org.jboss.seam.security.examples.seamspace.model.MemberImage;
 import org.jboss.seam.security.management.IdentityManager;
 import org.jboss.seam.security.permission.Permission;
 import org.jboss.seam.security.permission.PermissionManager;
@@ -29,21 +33,23 @@
    
    private List<Member> availableFriends;   
    
-   @In IdentityManager identityManager;
-   @In PermissionManager permissionManager;
+   @Inject IdentityManager identityManager;
+   @Inject PermissionManager permissionManager;
    
-   @In EntityManager entityManager;
+   @Inject EntityManager entityManager;
    
-   @In PermissionSearch permissionSearch;   
+   @Inject PermissionSearch permissionSearch;
    
+   @Inject Conversation conversation;
+   
    private MemberImage target; 
    
    private Principal recipient;
    
    @SuppressWarnings("unchecked")
-   @Begin(nested = true)
    public void createPermission()
    {
+      conversation.begin();
       target = (MemberImage) permissionSearch.getTarget();
       
       selectedFriends = new ArrayList<Member>();
@@ -54,9 +60,9 @@
             .getResultList();      
    }
    
-   @Begin(nested = true)
    public void editPermission()
    {
+      conversation.begin();
       target = (MemberImage) permissionSearch.getTarget();
       recipient = permissionSearch.getSelectedRecipient();
             
@@ -140,7 +146,7 @@
       {
          if (selectedActions.size() == 0)
          {
-            FacesMessages.instance().add("You must select at least one action");
+            //FacesMessages.instance().add("You must select at least one action");
             return "failure";
          }
          
@@ -172,7 +178,7 @@
          
          permissionManager.grantPermissions(permissions);
       }
-      Conversation.instance().endBeforeRedirect();
+      conversation.end();
       return "success";
    }
    

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/PictureAction.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/PictureAction.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/PictureAction.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -1,22 +1,30 @@
 package org.jboss.seam.security.examples.seamspace.action;
 
 
-import javax.enterprise.inject.Model;
+import javax.enterprise.context.Conversation;
+import javax.enterprise.context.ConversationScoped;
+import javax.inject.Inject;
+import javax.inject.Named;
 import javax.persistence.EntityManager;
 
- at Model
+import org.jboss.seam.security.examples.seamspace.model.Member;
+import org.jboss.seam.security.examples.seamspace.model.MemberImage;
+
+ at Named
+ at ConversationScoped
 public class PictureAction
 {
    private MemberImage memberImage;
    
-   @In(required = false)
-   private Member authenticatedMember;
+   @Inject Member authenticatedMember;
    
-   @In EntityManager entityManager;
+   @Inject EntityManager entityManager;
    
-   @Begin
+   @Inject Conversation conversation;
+      
    public void uploadPicture()
    {
+      conversation.begin();
       memberImage = new MemberImage();
    }
    
@@ -24,7 +32,7 @@
    {
       memberImage.setMember(entityManager.find(Member.class, authenticatedMember.getMemberId()));
       entityManager.persist(memberImage);
-      Conversation.instance().end();
+      conversation.end();
    }
 
    public MemberImage getMemberImage()

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/PictureSearch.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/PictureSearch.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/PictureSearch.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -5,10 +5,13 @@
 import java.util.List;
 
 import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
 import javax.inject.Named;
 import javax.persistence.EntityManager;
 
 import org.jboss.seam.security.Identity;
+import org.jboss.seam.security.annotations.Delete;
+import org.jboss.seam.security.examples.seamspace.model.MemberImage;
 
 @RequestScoped
 @Named
@@ -18,9 +21,10 @@
    
    private String memberName;
    
-   @In
-   private EntityManager entityManager;
+   @Inject EntityManager entityManager;
    
+   @Inject Identity identity;
+   
    @Out(required = false)
    private List<MemberImage> memberImages;
    
@@ -54,6 +58,6 @@
             "select i from MemberImage i where i.member.memberName = :name and not i = i.member.picture")
             .setParameter("name", memberName)
             .getResultList();      
-      Identity.instance().filterByPermission(memberImages, "view");
+      identity.filterByPermission(memberImages, "view");
    }
 }

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ProfileAction.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ProfileAction.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/ProfileAction.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -4,12 +4,16 @@
 import java.util.List;
 import java.util.Random;
 
-import javax.ejb.Remove;
 import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
 import javax.inject.Named;
 import javax.persistence.EntityManager;
 import javax.persistence.NoResultException;
 
+import org.jboss.seam.security.examples.seamspace.model.FriendComment;
+import org.jboss.seam.security.examples.seamspace.model.Member;
+import org.jboss.seam.security.examples.seamspace.model.MemberBlog;
+
 @RequestScoped
 @Named
 public class ProfileAction
@@ -17,10 +21,10 @@
    @RequestParameter
    private String name;
 
-   @In(required = false) @Out(required = false, scope = CONVERSATION)
-   private Member selectedMember;
+   @Inject Member selectedMember;
    
-   @In(required = false)
+   
+   @Inject
    private Member authenticatedMember;
    
    @Out(required = false)
@@ -29,8 +33,7 @@
    @Out(required = false)
    List<MemberBlog> memberBlogs;   
    
-   @In
-   private EntityManager entityManager;
+   @Inject EntityManager entityManager;
 
    @Factory("selectedMember")
    public void display()
@@ -117,8 +120,5 @@
             "from FriendComment c where c.member = :member order by commentDate desc")
             .setParameter("member", selectedMember)
             .getResultList();
-   }
-   
-   @Remove @Destroy
-   public void destroy() { }   
+   }  
 }

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/RegisterAction.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/RegisterAction.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/RegisterAction.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -94,13 +94,12 @@
       member.setMemberSince(new Date());      
       entityManager.persist(member);      
       
-      new RunAsOperation() {
+      identity.runAs(new RunAsOperation() {
          public void execute() {
             identityManager.createUser(username, password);
             identityManager.grantRole(username, "user");            
          }         
-      }.addRole("admin")
-       .run();
+      }.addRole("admin"));
             
       newAccount.setMember(member);
       newAccount = entityManager.merge(newAccount);

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/model/Member.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/model/Member.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/model/Member.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -118,7 +118,7 @@
       this.lastName = lastName;
    }   
    
-   @NotNull @Email
+   @NotNull //@Email
    public String getEmail()
    {
       return email;

Added: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Authenticated.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Authenticated.java	                        (rev 0)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Authenticated.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -0,0 +1,18 @@
+package org.jboss.seam.security.examples.seamspace.util;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+ at Target({FIELD, METHOD})
+ at Retention(RUNTIME)
+ at Qualifier
+public @interface Authenticated
+{
+
+}

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/AuthenticationEvents.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/AuthenticationEvents.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/AuthenticationEvents.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -1,15 +1,24 @@
 package org.jboss.seam.security.examples.seamspace.util;
 
-import javax.inject.Named;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Produces;
 
-import org.jboss.seam.security.management.JpaIdentityStore;
+import org.jboss.seam.security.events.UserAuthenticatedEvent;
+import org.jboss.seam.security.examples.seamspace.model.MemberAccount;
 
- at Named
+ at SessionScoped
 public class AuthenticationEvents
 {
-   //@Observer(JpaIdentityStore.EVENT_USER_AUTHENTICATED)
-   public void loginSuccessful(MemberAccount account)
+   private MemberAccount account;
+   
+   @Produces @Authenticated MemberAccount getAuthenticatedAccount()
    {
-     // Contexts.getSessionContext().set("authenticatedMember", account.getMember());
+      return account;
    }
+   
+   public void loginSuccessful(@Observes UserAuthenticatedEvent event)
+   {
+      account = (MemberAccount) event.getUser();
+   }
 }

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/ContentServlet.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/ContentServlet.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/ContentServlet.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -7,6 +7,8 @@
 import java.io.IOException;
 import java.io.InputStream;
 
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.imageio.ImageIO;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -14,7 +16,10 @@
 import javax.servlet.http.HttpServletResponse;
 import javax.swing.ImageIcon;
 
+import org.jboss.seam.security.examples.seamspace.action.ContentAction;
+import org.jboss.seam.security.examples.seamspace.model.MemberImage;
 
+
 /**
  * Serves images and other member content
  * 
@@ -56,13 +61,19 @@
       
    }
 
+   @SuppressWarnings("unchecked")
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException
    {
       if (IMAGES_PATH.equals(request.getPathInfo()))
       {
-         ContentAction contentAction = (ContentAction) Component.getInstance(ContentAction.class);
+         BeanManager manager = (BeanManager) getServletContext().getAttribute(BeanManager.class.getName());
+         
+         Bean<ContentAction> bean = (Bean<ContentAction>) manager.getBeans(
+               ContentAction.class).iterator().next();
+         
+         ContentAction contentAction = bean.create(manager.createCreationalContext(bean));
 
          String id = request.getParameter("id");
          MemberImage mi = (id != null && !"".equals(id)) ? 

Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Hash.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Hash.java	2010-03-25 06:53:40 UTC (rev 12269)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Hash.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -2,37 +2,43 @@
 
 import java.security.MessageDigest;
 
-import org.jboss.seam.util.Hex;
+public class Hash
+{
+   String hashFunction = "MD5";
+   String charset = "UTF-8";
 
-public class Hash {
-    String hashFunction = "MD5";
-    String charset      = "UTF-8";
-    
-    public String hash(String password) {
-        try {
-            MessageDigest md = MessageDigest.getInstance(hashFunction);
-            md.update(password.getBytes(charset));
-            byte[] raw = md.digest();
-            return new String(Hex.encodeHex(raw));
-        } 
-        catch (Exception e) {
-            throw new RuntimeException(e);        
-        }
-    }
+   public String hash(String password)
+   {
+      try
+      {
+         MessageDigest md = MessageDigest.getInstance(hashFunction);
+         md.update(password.getBytes(charset));
+         byte[] raw = md.digest();
+         return new String(Hex.encodeHex(raw));
+      }
+      catch (Exception e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
 
-    public String getCharset() {
-        return charset;
-    }
+   public String getCharset()
+   {
+      return charset;
+   }
 
-    public void setCharset(String charset) {
-        this.charset = charset;
-    }
+   public void setCharset(String charset)
+   {
+      this.charset = charset;
+   }
 
-    public String getHashFunction() {
-        return hashFunction;
-    }
+   public String getHashFunction()
+   {
+      return hashFunction;
+   }
 
-    public void setHashFunction(String hashFunction) {
-        this.hashFunction = hashFunction;
-    }    
+   public void setHashFunction(String hashFunction)
+   {
+      this.hashFunction = hashFunction;
+   }
 }

Added: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Hex.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Hex.java	                        (rev 0)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/util/Hex.java	2010-03-25 09:45:27 UTC (rev 12270)
@@ -0,0 +1,187 @@
+package org.jboss.seam.security.examples.seamspace.util;
+
+/**
+ * Hex encoder and decoder, ripped off from commons-codec.  DecoderException and
+ * EncoderException have been replaced by RuntimeException
+ * 
+ * @author Apache Software Foundation
+ */
+public class Hex
+{
+
+   /** 
+    * Used building output as Hex 
+    */
+   private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
+            'b', 'c', 'd', 'e', 'f' };
+
+   /**
+    * Converts an array of characters representing hexidecimal values into an
+    * array of bytes of those same values. The returned array will be half the
+    * length of the passed array, as it takes two characters to represent any
+    * given byte. An exception is thrown if the passed char array has an odd
+    * number of elements.
+    * 
+    * @param data An array of characters containing hexidecimal digits
+    * @return A byte array containing binary data decoded from
+    *         the supplied char array.
+    * @throws RuntimeException Thrown if an odd number or illegal of characters 
+    *         is supplied
+    */
+   public static byte[] decodeHex(char[] data) throws RuntimeException
+   {
+
+      int len = data.length;
+
+      if ((len & 0x01) != 0)
+      {
+         throw new RuntimeException("Odd number of characters.");
+      }
+
+      byte[] out = new byte[len >> 1];
+
+      // two characters form the hex value.
+      for (int i = 0, j = 0; j < len; i++)
+      {
+         int f = toDigit(data[j], j) << 4;
+         j++;
+         f = f | toDigit(data[j], j);
+         j++;
+         out[i] = (byte) (f & 0xFF);
+      }
+
+      return out;
+   }
+
+   /**
+    * Converts a hexadecimal character to an integer.
+    *  
+    * @param ch A character to convert to an integer digit
+    * @param index The index of the character in the source
+    * @return An integer
+    * @throws RuntimeException Thrown if ch is an illegal hex character
+    */
+   protected static int toDigit(char ch, int index) throws RuntimeException
+   {
+      int digit = Character.digit(ch, 16);
+      if (digit == -1)
+      {
+         throw new RuntimeException("Illegal hexadecimal charcter " + ch + " at index " + index);
+      }
+      return digit;
+   }
+
+   /**
+    * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order.
+    * The returned array will be double the length of the passed array, as it takes two characters to represent any
+    * given byte.
+    * 
+    * @param data
+    *                  a byte[] to convert to Hex characters
+    * @return A char[] containing hexidecimal characters
+    */
+   public static char[] encodeHex(byte[] data)
+   {
+
+      int l = data.length;
+
+      char[] out = new char[l << 1];
+
+      // two characters form the hex value.
+      for (int i = 0, j = 0; i < l; i++)
+      {
+         out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
+         out[j++] = DIGITS[0x0F & data[i]];
+      }
+
+      return out;
+   }
+
+   /**
+    * Converts an array of character bytes representing hexidecimal values into an
+    * array of bytes of those same values. The returned array will be half the
+    * length of the passed array, as it takes two characters to represent any
+    * given byte. An exception is thrown if the passed char array has an odd
+    * number of elements.
+    * 
+    * @param array An array of character bytes containing hexidecimal digits
+    * @return A byte array containing binary data decoded from
+    *         the supplied byte array (representing characters).
+    * @throws RuntimeException Thrown if an odd number of characters is supplied
+    *                   to this function
+    * @see #decodeHex(char[])
+    */
+   public byte[] decode(byte[] array) throws RuntimeException
+   {
+      return decodeHex(new String(array).toCharArray());
+   }
+
+   /**
+    * Converts a String or an array of character bytes representing hexidecimal values into an
+    * array of bytes of those same values. The returned array will be half the
+    * length of the passed String or array, as it takes two characters to represent any
+    * given byte. An exception is thrown if the passed char array has an odd
+    * number of elements.
+    * 
+    * @param object A String or, an array of character bytes containing hexidecimal digits
+    * @return A byte array containing binary data decoded from
+    *         the supplied byte array (representing characters).
+    * @throws RuntimeException Thrown if an odd number of characters is supplied
+    *                   to this function or the object is not a String or char[]
+    * @see #decodeHex(char[])
+    */
+   public Object decode(Object object) throws RuntimeException
+   {
+      try
+      {
+         char[] charArray = object instanceof String ? ((String) object).toCharArray()
+                  : (char[]) object;
+         return decodeHex(charArray);
+      }
+      catch (ClassCastException e)
+      {
+         throw new RuntimeException(e.getMessage());
+      }
+   }
+
+   /**
+    * Converts an array of bytes into an array of bytes for the characters representing the
+    * hexidecimal values of each byte in order. The returned array will be
+    * double the length of the passed array, as it takes two characters to
+    * represent any given byte.
+    *
+    * @param array a byte[] to convert to Hex characters
+    * @return A byte[] containing the bytes of the hexidecimal characters
+    * @see #encodeHex(byte[])
+    */
+   public byte[] encode(byte[] array)
+   {
+      return new String(encodeHex(array)).getBytes();
+   }
+
+   /**
+    * Converts a String or an array of bytes into an array of characters representing the
+    * hexidecimal values of each byte in order. The returned array will be
+    * double the length of the passed String or array, as it takes two characters to
+    * represent any given byte.
+    *
+    * @param object a String, or byte[] to convert to Hex characters
+    * @return A char[] containing hexidecimal characters
+    * @throws RuntimeException Thrown if the given object is not a String or byte[]
+    * @see #encodeHex(byte[])
+    */
+   public Object encode(Object object) throws RuntimeException
+   {
+      try
+      {
+         byte[] byteArray = object instanceof String ? ((String) object).getBytes()
+                  : (byte[]) object;
+         return encodeHex(byteArray);
+      }
+      catch (ClassCastException e)
+      {
+         throw new RuntimeException(e.getMessage());
+      }
+   }
+
+}



More information about the seam-commits mailing list