use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppFile method open.
@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN" })
@Override
public void open(String mode) throws ScriptException {
if ("r".equals(mode)) {
try {
file = new RandomAccessFile(realPath, "r");
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
readable = true;
} else if ("r+".equals(mode)) {
try {
file = new RandomAccessFile(realPath, "rw");
file.seek(0);
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
readable = true;
writable = true;
} else if ("w".equals(mode)) {
try {
file = new RandomAccessFile(realPath, "rw");
file.setLength(0);
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
writable = true;
} else if ("w+".equals(mode)) {
try {
file = new RandomAccessFile(realPath, "rw");
file.setLength(0);
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
readable = true;
writable = true;
} else if ("a".equals(mode)) {
try {
file = new RandomAccessFile(realPath, "rw");
file.seek(file.length());
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
writable = true;
} else if ("a+".equals(mode)) {
try {
file = new RandomAccessFile(realPath, "rw");
file.seek(file.length());
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
readable = true;
writable = true;
} else {
String msg = "Invalid or unsupported file mode, path : " + realPath + ", mode : " + mode;
log.error(msg);
throw new ScriptException(msg);
}
opened = true;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppFile method close.
@Override
public void close() throws ScriptException {
if (!opened) {
return;
}
try {
file.close();
opened = false;
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppFile method getFilePath.
private String getFilePath(String fileURL) throws ScriptException {
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
ServletContext context = (ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT);
String parent = includesCallstack.lastElement();
try {
String[] keys = WebAppManager.getKeys(context.getContextPath(), parent, fileURL);
fileURL = "/".equals(keys[1]) ? keys[2] : keys[1] + keys[2];
} catch (NullPointerException ne) {
throw new ScriptException("Invalid file path : " + fileURL, ne);
}
return fileURL;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class HostObjectUtil method readerToString.
public static String readerToString(Reader reader) throws ScriptException {
StringBuilder sb = new StringBuilder();
try {
int data = reader.read();
while (data != -1) {
sb.append((char) data);
data = reader.read();
}
return sb.toString();
} catch (IOException e) {
String msg = "Error while reading the content from the Reader";
log.error(msg, e);
throw new ScriptException(msg, e);
} finally {
try {
reader.close();
} catch (IOException e) {
log.warn(e);
}
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class JaggerySecurityDomain method getCodeSource.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public CodeSource getCodeSource() throws ScriptException {
if (codeSource != null) {
return codeSource;
}
URL url = null;
try {
String contextPath = servletContext.getRealPath("/");
if (contextPath == null) {
url = servletContext.getResource(scriptPath);
} else {
if (!contextPath.endsWith(File.separator)) {
contextPath += File.separator;
}
url = new File(contextPath + scriptPath).getCanonicalFile().toURI().toURL();
}
codeSource = new CodeSource(url, (Certificate[]) null);
return codeSource;
} catch (IOException e) {
throw new ScriptException(e);
}
}
Aggregations