/**
  * Decrypt a Base64-encoded string into a byte array using the given password.
  *
  * @param propertyContext
  * @param password encryption password
  * @param text string to decrypt
  * @return byte array containing the decoded data
  */
 public static byte[] decrypt(PropertyContext propertyContext, String password, String text) {
   try {
     Cache cache = ObjectCache.instance();
     Long validity = (long) 0;
     InternalCacheKey key = new InternalCacheKey("Decryption cipher", password);
     Cipher cipher = (Cipher) cache.findValid(propertyContext, key, validity);
     if (cipher == null) {
       cipher = Cipher.getInstance(CIPHER_TYPE);
       cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password), pbeParamSpec);
       cache.add(propertyContext, key, validity, cipher);
     }
     synchronized (cipher) {
       return cipher.doFinal(Base64.decode(text));
     }
   } catch (Exception e) {
     throw new OXFException(e);
   }
 }
 /**
  * Encrypt a byte array using the given password. The result is converted to Base64 encoding
  * without line breaks or spaces.
  *
  * @param propertyContext
  * @param password encryption password
  * @param bytes byte array to encrypt
  * @return string containing the encoding data as Base64
  */
 public static String encrypt(PropertyContext propertyContext, String password, byte[] bytes) {
   try {
     // Find cipher in cache
     final Cache cache = ObjectCache.instance();
     final Long validity = (long) 0;
     final InternalCacheKey key = new InternalCacheKey("Encryption cipher", password);
     Cipher cipher = (Cipher) cache.findValid(propertyContext, key, validity);
     if (cipher == null) {
       cipher = Cipher.getInstance(CIPHER_TYPE);
       cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password), pbeParamSpec);
       cache.add(propertyContext, key, validity, cipher);
     }
     // Encode with cipher
     // TODO: should probably use pool of cyphers instead of synchronization, which can cause
     // contention here.
     synchronized (cipher) {
       // Encode without putting line breaks between lines
       return Base64.encode(cipher.doFinal(bytes), false);
     }
   } catch (Exception e) {
     throw new OXFException(e);
   }
 }
예제 #3
0
  public static PooledXPathExpression getXPathExpression(
      PropertyContext propertyContext,
      List<Item> contextItems,
      int contextPosition,
      String xpathString,
      Map<String, String> prefixToURIMap,
      Map<String, ValueRepresentation> variableToValueMap,
      FunctionLibrary functionLibrary,
      String baseURI,
      boolean isAvt,
      boolean testNoCache,
      LocationData locationData) {

    try {
      // Find pool from cache
      final Long validity = (long) 0;
      final Cache cache = ObjectCache.instance(XPATH_CACHE_NAME, XPATH_CACHE_DEFAULT_SIZE);
      final FastStringBuffer cacheKeyString = new FastStringBuffer(xpathString);
      {
        if (functionLibrary != null) { // This is ok
          cacheKeyString.append('|');
          cacheKeyString.append(Integer.toString(functionLibrary.hashCode()));
        }
      }
      {
        // NOTE: Mike Kay confirms on 2007-07-04 that compilation depends on the namespace context,
        // so we need
        // to use it as part of the cache key.

        // TODO: PERF: It turns out that this takes a lot of time. Now that the namespace
        // information is computed statically, we can do better.
        if (DEBUG_TEST_KEY_OPTIMIZATION) {
          // PERF TEST ONLY
          cacheKeyString.append("|DUMMYNSVAR|");
        } else {

          if (prefixToURIMap != null) {
            final Map<String, String> sortedMap =
                (prefixToURIMap instanceof TreeMap)
                    ? prefixToURIMap
                    : new TreeMap<String, String>(
                        prefixToURIMap); // this should make sure we always get the keys in the same
                                         // order
            for (Map.Entry<String, String> currentEntry : sortedMap.entrySet()) {
              cacheKeyString.append('|');
              cacheKeyString.append(currentEntry.getKey());
              cacheKeyString.append('=');
              cacheKeyString.append(currentEntry.getValue());
            }
          }
        }
      }
      if (DEBUG_TEST_KEY_OPTIMIZATION) {
        // PERF TEST ONLY
        // NOP
      } else {

        if (variableToValueMap != null && variableToValueMap.size() > 0) {
          // There are some variables in scope. They must be part of the key
          // TODO: Put this in static state as this can be determined statically once and for all
          for (final String variableName : variableToValueMap.keySet()) {
            cacheKeyString.append('|');
            cacheKeyString.append(variableName);
          }
        }
      }
      {
        // Add this to the key as evaluating "name" as XPath or as AVT is very different!
        cacheKeyString.append('|');
        cacheKeyString.append(Boolean.toString(isAvt));
      }

      // TODO: Add baseURI to cache key (currently, baseURI is pretty much unused)

      final Set<String> variableNames =
          (variableToValueMap != null) ? variableToValueMap.keySet() : null;
      final PooledXPathExpression expr;
      if (testNoCache) {
        // For testing only: don't get expression from cache
        final Object o =
            new XFormsCachePoolableObjetFactory(
                    null,
                    xpathString,
                    prefixToURIMap,
                    variableNames,
                    functionLibrary,
                    baseURI,
                    isAvt,
                    false,
                    locationData)
                .makeObject();
        expr = (PooledXPathExpression) o;
      } else {
        // Get or create pool
        final InternalCacheKey cacheKey =
            new InternalCacheKey("XPath Expression2", cacheKeyString.toString());
        ObjectPool pool = (ObjectPool) cache.findValid(propertyContext, cacheKey, validity);
        if (pool == null) {
          pool =
              createXPathPool(
                  xpathString,
                  prefixToURIMap,
                  variableNames,
                  functionLibrary,
                  baseURI,
                  isAvt,
                  locationData);
          cache.add(propertyContext, cacheKey, validity, pool);
        }

        // Get object from pool
        final Object o = pool.borrowObject();
        expr = (PooledXPathExpression) o;
      }

      // Set context items and position
      expr.setContextItems(contextItems, contextPosition);

      // Set variables
      expr.setVariables(variableToValueMap);

      return expr;
    } catch (Exception e) {
      throw handleXPathException(e, xpathString, "preparing XPath expression", locationData);
    }
  }