public Object getNewValue(Object oldValue) throws Exception {
    _type = null;
    _height = null;
    _width = null;
    _size = null;

    String text = (String) oldValue;

    byte[] bytes = (byte[]) Base64.stringToObject(text);

    try {
      Image image = ImageLocalServiceUtil.getImage(bytes);

      _type = image.getType();
      _height = new Integer(image.getHeight());
      _width = new Integer(image.getWidth());
      _size = new Integer(image.getSize());
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        String imageId = (String) _imageIdColumn.getOldValue();

        if (_log.isWarnEnabled()) {
          _log.warn("Unable to get image data for " + imageId + ": " + e.getMessage());
        }
      }

      _type = ImageConstants.TYPE_NOT_AVAILABLE;
      _height = null;
      _width = null;
      _size = new Integer(bytes.length);
    }

    return oldValue;
  }
예제 #2
0
  public byte[] getTextObj() {
    if (_textObj == null) {
      _textObj = (byte[]) Base64.stringToObject(getText());
    }

    return _textObj;
  }
  protected Event serializeEvent(Event event, ClassLoader portletClassLoader) {

    Serializable value = event.getValue();

    if (value == null) {
      return event;
    }

    Class<?> valueClass = value.getClass();

    String valueClassName = valueClass.getName();

    try {
      Class<?> loadedValueClass = portletClassLoader.loadClass(valueClassName);

      if (loadedValueClass.equals(valueClass)) {
        return event;
      }
    } catch (ClassNotFoundException cnfe) {
      if (_log.isWarnEnabled()) {
        _log.warn(portletClassLoader.toString() + " does not contain " + valueClassName, cnfe);
      }
    }

    EventImpl eventImpl = (EventImpl) event;

    String base64Value = eventImpl.getBase64Value();

    value = (Serializable) Base64.stringToObject(base64Value, portletClassLoader);

    return new EventImpl(event.getName(), event.getQName(), value);
  }
예제 #4
0
  private static byte[] _getSaltFromSSHA(String sshaString) throws PwdEncryptorException {

    byte[] saltBytes = new byte[8];

    if (Validator.isNull(sshaString)) {

      // Generate random salt

      Random random = new SecureRandom();

      random.nextBytes(saltBytes);
    } else {

      // Extract salt from encrypted password

      try {
        byte[] digestPlusSalt = Base64.decode(sshaString);
        byte[] digestBytes = new byte[digestPlusSalt.length - 8];

        System.arraycopy(digestPlusSalt, 0, digestBytes, 0, digestBytes.length);

        System.arraycopy(digestPlusSalt, digestBytes.length, saltBytes, 0, saltBytes.length);
      } catch (Exception e) {
        throw new PwdEncryptorException(
            "Unable to extract salt from encrypted password: " + e.getMessage());
      }
    }

    return saltBytes;
  }
예제 #5
0
  private static void _initKeys() {
    ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();

    if ((classLoader == null) || (_encryptedSymmetricKey != null)) {
      return;
    }

    try {
      URL url = classLoader.getResource("com/liferay/portal/license/public.key");

      byte[] bytes = IOUtils.toByteArray(url.openStream());

      X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(bytes);

      KeyFactory keyFactory = KeyFactory.getInstance("RSA");

      PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

      KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");

      keyGenerator.init(128, new SecureRandom());

      _symmetricKey = keyGenerator.generateKey();

      byte[] encryptedSymmetricKey =
          Encryptor.encryptUnencoded(publicKey, _symmetricKey.getEncoded());

      _encryptedSymmetricKey = Base64.objectToString(encryptedSymmetricKey);
    } catch (Exception e) {
      _log.error(e, e);
    }
  }
  public static String serialize(Address address) {
    Serializer serializer = new Serializer();

    serializer.writeObject(address);

    ByteBuffer byteBuffer = serializer.toByteBuffer();

    return Base64.encode(byteBuffer.array());
  }
  public static Address deserialize(String serializedAddress) {
    byte[] bytes = Base64.decode(serializedAddress);

    Deserializer deserializer = new Deserializer(ByteBuffer.wrap(bytes));

    try {
      return deserializer.readObject();
    } catch (ClassNotFoundException cnfe) {
      throw new RuntimeException("Unable to deserialize address " + serializedAddress, cnfe);
    }
  }
예제 #8
0
  public Key getKeyObj() {
    if (_keyObj == null) {
      String key = getKey();

      if (Validator.isNotNull(key)) {
        _keyObj = (Key) Base64.stringToObject(key);
      }
    }

    return _keyObj;
  }
예제 #9
0
  protected static String encodePassword(
      String algorithm, String clearTextPassword, byte[] saltBytes) throws PwdEncryptorException {

    try {
      if (algorithm.equals(TYPE_BCRYPT)) {
        String salt = new String(saltBytes);

        return BCrypt.hashpw(clearTextPassword, salt);
      } else if (algorithm.equals(TYPE_CRYPT) || algorithm.equals(TYPE_UFC_CRYPT)) {

        return Crypt.crypt(saltBytes, clearTextPassword.getBytes(Digester.ENCODING));
      } else if (algorithm.equals(TYPE_SSHA)) {
        byte[] clearTextPasswordBytes = clearTextPassword.getBytes(Digester.ENCODING);

        // Create a byte array of salt bytes appended to password bytes

        byte[] pwdPlusSalt = new byte[clearTextPasswordBytes.length + saltBytes.length];

        System.arraycopy(clearTextPasswordBytes, 0, pwdPlusSalt, 0, clearTextPasswordBytes.length);

        System.arraycopy(
            saltBytes, 0, pwdPlusSalt, clearTextPasswordBytes.length, saltBytes.length);

        // Digest byte array

        MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");

        byte[] pwdPlusSaltHash = sha1Digest.digest(pwdPlusSalt);

        // Appends salt bytes to the SHA-1 digest.

        byte[] digestPlusSalt = new byte[pwdPlusSaltHash.length + saltBytes.length];

        System.arraycopy(pwdPlusSaltHash, 0, digestPlusSalt, 0, pwdPlusSaltHash.length);

        System.arraycopy(saltBytes, 0, digestPlusSalt, pwdPlusSaltHash.length, saltBytes.length);

        // Base64 encode and format string

        return Base64.encode(digestPlusSalt);
      } else {
        return DigesterUtil.digest(algorithm, clearTextPassword);
      }
    } catch (NoSuchAlgorithmException nsae) {
      throw new PwdEncryptorException(nsae.getMessage());
    } catch (UnsupportedEncodingException uee) {
      throw new PwdEncryptorException(uee.getMessage());
    }
  }
예제 #10
0
  private static HttpURLConnection _getConnection(HttpPrincipal httpPrincipal) throws IOException {

    if ((httpPrincipal == null) || (httpPrincipal.getUrl() == null)) {
      return null;
    }

    URL url = new URL(httpPrincipal.getUrl() + "/api/liferay/do");

    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);

    if (!_VERIFY_SSL_HOSTNAME && (httpURLConnection instanceof HttpsURLConnection)) {

      HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection;

      httpsURLConnection.setHostnameVerifier(
          new HostnameVerifier() {

            public boolean verify(String hostname, SSLSession session) {
              return true;
            }
          });
    }

    httpURLConnection.setRequestProperty(
        HttpHeaders.CONTENT_TYPE, ContentTypes.APPLICATION_X_JAVA_SERIALIZED_OBJECT);
    httpURLConnection.setUseCaches(false);

    httpURLConnection.setRequestMethod("POST");

    if (Validator.isNotNull(httpPrincipal.getLogin())
        && Validator.isNotNull(httpPrincipal.getPassword())) {

      String userNameAndPassword =
          httpPrincipal.getLogin() + StringPool.COLON + httpPrincipal.getPassword();

      httpURLConnection.setRequestProperty(
          HttpHeaders.AUTHORIZATION,
          HttpServletRequest.BASIC_AUTH
              + StringPool.SPACE
              + Base64.encode(userNameAndPassword.getBytes()));
    }

    return httpURLConnection;
  }
예제 #11
0
  private static byte[] _encryptRequest(String serverURL, String request) throws Exception {

    byte[] bytes = request.getBytes(StringPool.UTF8);

    if (serverURL.startsWith(Http.HTTPS)) {
      return bytes;
    } else {
      JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

      bytes = Encryptor.encryptUnencoded(_symmetricKey, bytes);

      jsonObject.put("content", Base64.objectToString(bytes));
      jsonObject.put("key", _encryptedSymmetricKey);

      return jsonObject.toString().getBytes(StringPool.UTF8);
    }
  }
  protected String getUID(long companyId, String languageId, String word, String... parameters) {

    StringBundler uidSB = new StringBundler();

    uidSB.append(String.valueOf(companyId));
    uidSB.append(StringPool.UNDERLINE);
    uidSB.append(PortletKeys.SEARCH);
    uidSB.append(_PORTLET_SEPARATOR);

    int length = 4;

    if (parameters != null) {
      length += parameters.length;
    }

    try {
      CharsetEncoder charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(StringPool.UTF8);

      StringBundler keySB = new StringBundler(length);

      keySB.append(languageId);
      keySB.append(StringPool.UNDERLINE);
      keySB.append(word);
      keySB.append(StringPool.UNDERLINE);

      keySB.append(StringUtil.toLowerCase(word));

      if (parameters != null) {
        for (String parameter : parameters) {
          keySB.append(parameter);
          keySB.append(StringPool.UNDERLINE);
        }
      }

      String key = keySB.toString();

      byte[] bytes =
          DigesterUtil.digestRaw(Digester.MD5, charsetEncoder.encode(CharBuffer.wrap(key)));

      uidSB.append(Base64.encode(bytes));
    } catch (Exception e) {
      throw new IllegalStateException(e);
    }

    return uidSB.toString();
  }
예제 #13
0
  public void setTextObj(byte[] textObj) {
    _textObj = textObj;

    super.setText(Base64.objectToString(textObj));
  }
예제 #14
0
  public void setKeyObj(Key keyObj) {
    _keyObj = keyObj;

    super.setKey(Base64.objectToString(keyObj));
  }
예제 #15
0
  public static String sendRequest(String request) throws Exception {
    InputStream inputStream = null;
    OutputStream outputStream = null;

    try {
      String serverURL = LICENSE_SERVER_URL;

      if (!serverURL.endsWith(StringPool.SLASH)) {
        serverURL += StringPool.SLASH;
      }

      serverURL += "osb-portlet/license";

      URL url = new URL(serverURL);

      URLConnection connection = null;

      if (Validator.isNotNull(_PROXY_URL)) {
        if (_log.isInfoEnabled()) {
          _log.info("Using proxy " + _PROXY_URL + StringPool.COLON + _PROXY_PORT);
        }

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(_PROXY_URL, _PROXY_PORT));

        connection = url.openConnection(proxy);

        if (Validator.isNotNull(_PROXY_USER_NAME)) {
          String login = _PROXY_USER_NAME + StringPool.COLON + _PROXY_PASSWORD;

          String encodedLogin = Base64.encode(login.getBytes());

          connection.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
        }
      } else {
        connection = url.openConnection();
      }

      connection.setDoOutput(true);

      outputStream = connection.getOutputStream();

      outputStream.write(_encryptRequest(serverURL, request));

      String response = _decryptResponse(serverURL, connection.getInputStream());

      if (_log.isDebugEnabled()) {
        _log.debug("Server response: " + response);
      }

      if (Validator.isNull(response)) {
        throw new Exception("Server response is null");
      }

      return response;
    } finally {
      try {
        if (inputStream != null) {
          inputStream.close();
        }
      } catch (Exception e) {
      }

      try {
        if (outputStream != null) {
          outputStream.close();
        }
      } catch (Exception e) {
      }
    }
  }