Search in sources :

Example 96 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class SunSecurityProvider method createSignature.

@Override
public byte[] createSignature(byte[] privateKey, InputStream data) {
    Assertions.assertGreater(Assertions.assertNotNull(privateKey, "no private key provided").length, 0, "empty private key not allowed");
    if (data == null) {
        throw new AssertionException("no data provided");
    }
    try {
        // create private key from bytes
        KeyFactory keyFactory = KeyFactory.getInstance(getKeyPairGenerationAlgorithm(), getSignatureProvider());
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKey);
        PrivateKey priv = keyFactory.generatePrivate(privateKeySpec);
        // create signature
        Signature sig = Signature.getInstance(getSignatureAlgorithm(), getSignatureProvider());
        sig.initSign(priv, createSecureRandom());
        int n;
        byte[] buf = new byte[BUF_SIZE];
        while ((n = data.read(buf)) >= 0) {
            sig.update(buf, 0, n);
        }
        return sig.sign();
    } catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | SignatureException | IOException e) {
        throw new ProcessingException("unable to create signature.", e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Signature(java.security.Signature) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 97 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class RemoteFileService method getFiles.

private String[][] getFiles(String folderBase, FilenameFilter filter) {
    File root = new File(getRootPath());
    File path = null;
    if (folderBase == null || folderBase.length() == 0) {
        path = new File(getRootPath());
    } else {
        String tmp = folderBase;
        tmp = tmp.replaceAll("\\\\", "/");
        tmp = tmp.replaceAll("//", "/");
        path = new File(getRootPath(), tmp);
    }
    String canonicalRoot;
    String canonicalFolder;
    try {
        canonicalFolder = path.getCanonicalPath();
        canonicalRoot = root.getCanonicalPath();
    } catch (IOException e) {
        throw new ProcessingException("invalid path for file service for file: '" + folderBase + "'", e);
    }
    if (canonicalFolder == null || !canonicalFolder.startsWith(canonicalRoot)) {
        throw new SecurityException("invalid path for file service: path outside root-path");
    }
    ArrayList<String> dirList = new ArrayList<String>();
    ArrayList<String> fileList = new ArrayList<String>();
    String[] dir = path.list(filter);
    if (dir != null) {
        for (int i = 0; i < dir.length; i++) {
            try {
                File file = new File(path.getCanonicalPath() + "/" + dir[i]);
                if (!file.isHidden()) {
                    if (file.exists() && file.isDirectory()) {
                        String[][] tmp = getFiles((folderBase == null ? dir[i] : folderBase + "/" + dir[i]), filter);
                        for (String[] f : tmp) {
                            dirList.add(f[0]);
                            fileList.add(f[1]);
                        }
                    } else {
                        dirList.add(folderBase);
                        fileList.add(dir[i]);
                    }
                }
            } catch (IOException e) {
                throw new ProcessingException("FileService.getFiles:", e);
            }
        }
    }
    String[][] retVal = new String[dirList.size()][2];
    for (int i = 0; i < dirList.size(); i++) {
        retVal[i][0] = dirList.get(i);
        retVal[i][1] = fileList.get(i);
    }
    return retVal;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 98 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class RemoteFileService method getFileInternal.

private File getFileInternal(RemoteFile spec) {
    File root = new File(getRootPath());
    File folder = null;
    if (spec.getDirectory() == null || spec.getDirectory().length() == 0) {
        folder = new File(getRootPath());
    } else {
        String tmp = spec.getDirectory();
        tmp = tmp.replaceAll("\\\\", "/");
        tmp = tmp.replaceAll("//", "/");
        folder = new File(getRootPath(), tmp);
    }
    String canonicalRoot;
    String canonicalFolder;
    String canonicalSimpleName;
    try {
        canonicalRoot = root.getCanonicalPath();
        canonicalFolder = folder.getCanonicalPath();
        canonicalSimpleName = new File(canonicalFolder, spec.getName()).getName();
    } catch (IOException e) {
        throw new ProcessingException("invalid or unaccessible path", e);
    }
    if (canonicalFolder == null || !canonicalFolder.startsWith(canonicalRoot)) {
        throw new SecurityException("invalid or unaccessible path");
    }
    // if the remote file is requested from the RemoteFileServlet, spec.getName() will start with an "/"
    if (canonicalSimpleName == null || !canonicalSimpleName.equals(spec.getName().startsWith("/") ? spec.getName().substring(1) : spec.getName())) {
        throw new SecurityException("invalid or unaccessible path");
    }
    // 
    String filename = canonicalSimpleName;
    if (spec.getLocale() != null && filename.lastIndexOf('.') != -1) {
        // check locale string for hacking patterns (only allow
        String localeText = spec.getLocale().toString().replaceAll("__", "_");
        if (!LOCALE_SECURITY_PATTERN.matcher(localeText).matches()) {
            throw new SecurityException("invalid or unaccessible path");
        }
        String[] checkedLocaleParts = localeText.split("_", 3);
        String prefix = filename.substring(0, filename.lastIndexOf('.'));
        String suffix = filename.substring(filename.lastIndexOf('.'));
        for (int i = checkedLocaleParts.length - 1; i >= 0; i--) {
            if (prefix.toLowerCase().endsWith(checkedLocaleParts[i].toLowerCase())) {
                prefix = prefix.substring(0, prefix.length() - checkedLocaleParts[i].length());
                if (prefix.endsWith("_")) {
                    prefix = prefix.substring(0, prefix.length() - 1);
                }
            }
        }
        if (!prefix.endsWith("_")) {
            prefix = prefix + "_";
        }
        filename = prefix + localeText + suffix;
        File test = new File(canonicalFolder, filename);
        while (!test.exists()) {
            if (localeText.indexOf('_') == -1) {
                filename = canonicalSimpleName;
                break;
            }
            localeText = localeText.substring(0, localeText.lastIndexOf('_'));
            filename = prefix + localeText + suffix;
            test = new File(canonicalFolder, filename);
        }
    }
    File file = new File(canonicalFolder, filename);
    return file;
}
Also used : IOException(java.io.IOException) File(java.io.File) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 99 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class RemoteFileService method streamRemoteFile.

@Override
public void streamRemoteFile(RemoteFile spec, OutputStream out) {
    File file = getFileInternal(spec);
    if (!file.exists()) {
        throw new ProcessingException("remote file does not exist: " + spec.getPath());
    }
    int len = (int) file.length();
    byte[] buf = new byte[Math.min(102400, len)];
    int written = 0;
    int delta = 0;
    try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
        while (written < len) {
            delta = in.read(buf);
            out.write(buf, 0, delta);
            written += delta;
        }
    } catch (IOException e) {
        throw new ProcessingException("error streaming file: " + file.getAbsolutePath(), e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) File(java.io.File) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) FileInputStream(java.io.FileInputStream) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 100 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class AbstractIMAPService method openConnection.

public void openConnection(boolean useSSL) {
    try {
        Properties props = new Properties();
        props.put("mail.transport.protocol", "imap");
        if (m_host != null) {
            props.put("mail.imap.host", m_host);
        }
        if (m_port > 0) {
            props.put("mail.imap.port", "" + m_port);
        }
        if (StringUtility.hasText(m_sslProtocols)) {
            props.put("mail.imap.ssl.protocols", m_sslProtocols);
        }
        if (m_username != null) {
            props.put("mail.imap.user", m_username);
        }
        if (useSSL) {
            props.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.imap.socketFactory.fallback", "false");
            if (m_port > 0) {
                props.put("mail.imap.socketFactory.port", "" + m_port);
            }
        }
        Session session = Session.getInstance(props, null);
        m_store = session.getStore("imap");
        if (m_username != null && m_host != null) {
            m_store.connect(m_host, m_username, m_password);
        } else {
            m_store.connect();
        }
        if (m_mailbox != null) {
            m_folder = m_store.getFolder(m_mailbox);
        } else {
            m_folder = m_store.getDefaultFolder();
        }
        m_folder.open(Folder.READ_WRITE);
        m_opened = true;
    } catch (Exception e) {
        throw new ProcessingException("opening", e);
    }
}
Also used : Properties(java.util.Properties) MessagingException(javax.mail.MessagingException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) Session(javax.mail.Session) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)142 IOException (java.io.IOException)48 MessagingException (javax.mail.MessagingException)21 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)17 File (java.io.File)14 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)12 Folder (javax.mail.Folder)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 RemoteFile (org.eclipse.scout.rt.shared.services.common.file.RemoteFile)9 NoSuchProviderException (java.security.NoSuchProviderException)8 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 FileOutputStream (java.io.FileOutputStream)6 Message (javax.mail.Message)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStream (java.io.OutputStream)5 HashMap (java.util.HashMap)5