/**
 * @author <a href="mailto:[email protected]">Andrey Parfonov</a>
 * @version $Id: $
 */
public class ConversationStateListener implements HttpSessionListener {

  /** Logger. */
  protected Logger log = Logger.getLogger(ConversationStateListener.class);

  /** {@inheritDoc} */
  public void sessionCreated(HttpSessionEvent event) {
    // nothing to do here
  }

  /** Remove {@link ConversationState}. {@inheritDoc} */
  public void sessionDestroyed(HttpSessionEvent event) {
    HttpSession httpSession = event.getSession();
    StateKey stateKey = new HttpSessionStateKey(httpSession);

    ConversationRegistry conversationRegistry =
        (ConversationRegistry)
            getContainer(httpSession.getServletContext())
                .getComponentInstanceOfType(ConversationRegistry.class);

    ConversationState conversationState = conversationRegistry.unregister(stateKey);

    if (conversationState != null)
      if (log.isDebugEnabled()) log.debug("Remove conversation state " + httpSession.getId());
  }

  /**
   * @return actual ExoContainer instance.
   * @deprecated use {@link #getContainer(ServletContext)} instead
   */
  protected KernelContainer getContainer() {
    KernelContainer container = KernelContainerContext.getCurrentContainer();
    if (container instanceof RootContainer) {
      container = RootContainer.getInstance().getPortalContainer("portal");
    }
    return container;
  }

  /**
   * @param sctx {@link ServletContext}
   * @return actual ExoContainer instance
   */
  protected KernelContainer getContainer(ServletContext sctx) {
    KernelContainer container = KernelContainerContext.getCurrentContainer();
    if (container instanceof RootContainer) {
      String containerName = null;
      // check attribute in servlet context first
      if (sctx.getAttribute(SetCurrentIdentityFilter.PORTAL_CONTAINER_NAME) != null)
        containerName = (String) sctx.getAttribute(SetCurrentIdentityFilter.PORTAL_CONTAINER_NAME);

      // if not set then use default name.
      if (containerName == null) containerName = "portal";
      container = RootContainer.getInstance().getPortalContainer(containerName);
    }
    return container;
  }
}
示例#2
0
/** Utility class for doing SHA-1/MD5 One-Way Hash Encryption */
public class HashCrypt {

  private static final Logger logger = Logger.getLogger(HashCrypt.class);

  public static String getDigestHash(String str) {
    return getDigestHash(str, "SHA");
  }

  public static String getDigestHash(String str, String hashType) {
    if (str == null) return null;
    try {
      MessageDigest messagedigest = MessageDigest.getInstance(hashType);
      byte[] strBytes = str.getBytes();

      messagedigest.update(strBytes);
      byte[] digestBytes = messagedigest.digest();
      char[] digestChars = Hex.encodeHex(digestBytes);

      StringBuilder sb = new StringBuilder();
      sb.append("{").append(hashType).append("}");
      sb.append(digestChars, 0, digestChars.length);
      return sb.toString();
    } catch (Exception e) {
      throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e);
    }
  }

  public static String getDigestHash(String str, String code, String hashType) {
    if (str == null) return null;
    try {
      byte codeBytes[] = null;

      if (code == null) codeBytes = str.getBytes();
      else codeBytes = str.getBytes(code);
      MessageDigest messagedigest = MessageDigest.getInstance(hashType);

      messagedigest.update(codeBytes);
      byte digestBytes[] = messagedigest.digest();
      char[] digestChars = Hex.encodeHex(digestBytes);
      StringBuilder sb = new StringBuilder();
      sb.append("{").append(hashType).append("}");
      sb.append(digestChars, 0, digestChars.length);
      return sb.toString();
    } catch (Exception e) {
      throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e);
    }
  }

  public static String getHashTypeFromPrefix(String hashString) {
    if (UtilValidate.isEmpty(hashString) || hashString.charAt(0) != '{') {
      return null;
    }

    return hashString.substring(1, hashString.indexOf('}'));
  }

  public static String removeHashTypePrefix(String hashString) {
    if (UtilValidate.isEmpty(hashString) || hashString.charAt(0) != '{') {
      return hashString;
    }

    return hashString.substring(hashString.indexOf('}') + 1);
  }

  public static String getDigestHashOldFunnyHexEncode(String str, String hashType) {
    if (UtilValidate.isEmpty(hashType)) hashType = "SHA";
    if (str == null) return null;
    try {
      MessageDigest messagedigest = MessageDigest.getInstance(hashType);
      byte strBytes[] = str.getBytes();

      messagedigest.update(strBytes);
      byte digestBytes[] = messagedigest.digest();
      int k = 0;
      char digestChars[] = new char[digestBytes.length * 2];

      for (int l = 0; l < digestBytes.length; l++) {
        int i1 = digestBytes[l];

        if (i1 < 0) {
          i1 = 127 + i1 * -1;
        }
        StringUtil.encodeInt(i1, k, digestChars);
        k += 2;
      }

      return new String(digestChars, 0, digestChars.length);
    } catch (Exception e) {
      logger.error("Error while computing hash of type " + hashType, e);
    }
    return str;
  }
}