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);
}
}
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;
}
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;
}
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);
}
}
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);
}
}
Aggregations