@Override protected void getResource( String resourceName, File destination, TransferProgress transferProgress) throws TransferFailedException, ResourceDoesNotExistException { InputStream in = null; OutputStream out = null; try { S3Object s3Object = this.amazonS3.getObject(this.bucketName, getKey(resourceName)); in = s3Object.getObjectContent(); out = new TransferProgressFileOutputStream(destination, transferProgress); IoUtils.copy(in, out); } catch (AmazonServiceException e) { throw new ResourceDoesNotExistException( String.format("'%s' does not exist", resourceName), e); } catch (FileNotFoundException e) { throw new TransferFailedException(String.format("Cannot write file to '%s'", destination), e); } catch (IOException e) { throw new TransferFailedException( String.format("Cannot read from '%s' and write to '%s'", resourceName, destination), e); } finally { IoUtils.closeQuietly(in, out); } }
private void replace(Reader in, Writer out, boolean refs) throws IOException { final String template = IoUtils.read(in); final Matcher matcher = refStart.matcher(template); int matchPos = 0; int appendPos = 0; while (matcher.find(matchPos)) { final String name = matcher.group(1); if (!snippets.containsKey(name)) { throw new IllegalArgumentException("Snippet '" + name + "' not defined."); } if (refs) { out.write(template.substring(appendPos, matcher.start())); matchPos = appendPos = matcher.end(); } else { out.write(template.substring(appendPos, matcher.end())); appendPos = template.indexOf(refEnd, matcher.end()); if (appendPos < 0) { throw new IllegalArgumentException( "No refEnd marker found for refStart '" + template.substring(matcher.start(), matcher.end()) + "'"); } matchPos = appendPos + refEnd.length(); } out.write(prefix); out.write(snippets.get(name)); out.write(postfix); } out.write(template.substring(appendPos)); }
public static ExpandedResult processGzipEncoded(byte[] compressed, int sizeLimit) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_GZIP_COMPRESSION_RATIO * compressed.length); GZIPInputStream inStream = new GZIPInputStream(new ByteArrayInputStream(compressed)); boolean isTruncated = false; byte[] buf = new byte[BUF_SIZE]; int written = 0; while (true) { try { int size = inStream.read(buf); if (size == -1) { break; } if ((written + size) > sizeLimit) { isTruncated = true; outStream.write(buf, 0, sizeLimit - written); break; } outStream.write(buf, 0, size); written += size; } catch (Exception e) { LOGGER.trace("Exception unzipping content", e); break; } } IoUtils.safeClose(outStream); return new ExpandedResult(outStream.toByteArray(), isTruncated); }
public static byte[] processDeflateEncoded(byte[] compressed, int sizeLimit) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_DEFLATE_COMPRESSION_RATIO * compressed.length); // "true" because HTTP does not provide zlib headers Inflater inflater = new Inflater(true); InflaterInputStream inStream = new InflaterInputStream(new ByteArrayInputStream(compressed), inflater); byte[] buf = new byte[BUF_SIZE]; int written = 0; while (true) { try { int size = inStream.read(buf); if (size <= 0) { break; } if ((written + size) > sizeLimit) { outStream.write(buf, 0, sizeLimit - written); break; } outStream.write(buf, 0, size); written += size; } catch (Exception e) { LOGGER.trace("Exception inflating content", e); break; } } IoUtils.safeClose(outStream); return outStream.toByteArray(); }
@Override protected void putResource(File source, String destination, TransferProgress transferProgress) throws TransferFailedException, ResourceDoesNotExistException { String key = getKey(destination); mkdirs(key, 0); InputStream in = null; try { ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(source.length()); objectMetadata.setContentType(Mimetypes.getInstance().getMimetype(source)); in = new TransferProgressFileInputStream(source, transferProgress); this.amazonS3.putObject(new PutObjectRequest(this.bucketName, key, in, objectMetadata)); } catch (AmazonServiceException e) { throw new TransferFailedException(String.format("Cannot write file to '%s'", destination), e); } catch (FileNotFoundException e) { throw new ResourceDoesNotExistException( String.format("Cannot read file from '%s'", source), e); } finally { IoUtils.closeQuietly(in); } }
public <T> void sendObjectMessage(T object, String producerGroup, String topic, String tag) { Message message = new Message(); message.setTopic(topic); message.setTags(tag); try { if (object instanceof String) { message.setBody(((String) object).getBytes()); } else if (object instanceof Serializable) { message.setBody(IoUtils.objectToBtyeArray((Serializable) object)); } else { throw new IllegalArgumentException("not implements Serivalizable"); } } catch (IOException e) { // ignored } try { for (DefaultMQProducer defaultMQProducer : defaultMQProducers) { defaultMQProducer.setProducerGroup(producerGroup); SendResult sendResult = defaultMQProducer.send(message); infoLogger.info( "send " + object + " to " + defaultMQProducer.getNamesrvAddr() + ", " + sendResult); } // LogHelper.infoLog(infoLogger, null, "send {1} info to sbc {0}", // object.getClass().getName(), object.toString()); } catch (Exception e) { infoLogger.error("send " + object + " failed", e); } finally { // defaultMQProducer.shutdown(); } }
public void put(final Uri uri, final InputStream inputStream) { checkNotOnMainThread(); // First we need to save the stream contents to a temporary file, so it // can be read multiple times File tmpFile = null; try { tmpFile = File.createTempFile("bitmapcache_", null, mTempDir); // Pipe InputStream to file IoUtils.copy(inputStream, tmpFile); } catch (IOException e) { Log.e(Constants.LOG_TAG, "Error writing to saving stream to temp file: " + uri.toString(), e); } if (null != tmpFile) { // Try and decode File FileInputStreamProvider provider = new FileInputStreamProvider(tmpFile); if (provider != null) { if (null != mDiskCache) { final String key = transformUrlForDiskCacheKey(uri); final ReentrantLock lock = getLockForDiskCacheEdit(uri); lock.lock(); try { DiskLruCache.Editor editor = mDiskCache.edit(key); IoUtils.copy(tmpFile, editor.newOutputStream(0)); editor.commit(); } catch (IOException e) { Log.e(Constants.LOG_TAG, "Error writing to disk cache. URL: " + uri, e); } finally { lock.unlock(); scheduleDiskCacheFlush(); } } // Finally, delete the temporary file tmpFile.delete(); } } }
/** * Checks if a path has strict permissions * * <UL> * <LI> * <p>(For {@code Unix}) The path may not have group or others write permissions * <LI> * <p>The path must be owned by current user. * <LI> * <p>(For {@code Unix}) The path may be owned by root. * </UL> * * @param path The {@link Path} to be checked - ignored if {@code null} or does not exist * @param options The {@link LinkOption}s to use to query the file's permissions * @return The violated permission as {@link Pair} where {@link Pair#getClass()} is a loggable * message and {@link Pair#getSecond()} is the offending object - e.g., {@link * PosixFilePermission} or {@link String} for owner. Return value is {@code null} if no * violations detected * @throws IOException If failed to retrieve the permissions * @see #STRICTLY_PROHIBITED_FILE_PERMISSION */ public static Pair<String, Object> validateStrictConfigFilePermissions( Path path, LinkOption... options) throws IOException { if ((path == null) || (!Files.exists(path, options))) { return null; } Collection<PosixFilePermission> perms = IoUtils.getPermissions(path, options); if (GenericUtils.isEmpty(perms)) { return null; } if (OsUtils.isUNIX()) { PosixFilePermission p = IoUtils.validateExcludedPermissions(perms, STRICTLY_PROHIBITED_FILE_PERMISSION); if (p != null) { return new Pair<String, Object>(String.format("Permissions violation (%s)", p), p); } } String owner = IoUtils.getFileOwner(path, options); if (GenericUtils.isEmpty(owner)) { // we cannot get owner // general issue: jvm does not support permissions // security issue: specific filesystem does not support permissions return null; } String current = OsUtils.getCurrentUser(); Set<String> expected = new HashSet<>(); expected.add(current); if (OsUtils.isUNIX()) { // Windows "Administrator" was considered however in Windows most likely a group is used. expected.add(OsUtils.ROOT_USER); } if (!expected.contains(owner)) { return new Pair<String, Object>(String.format("Owner violation (%s)", owner), owner); } return null; }
private Map<String, String> parse(Reader in, Map<String, String> snippets) throws IOException { final String code = IoUtils.read(in); final Matcher matcher = snippetStart.matcher(code); int endPos = 0; while (matcher.find(endPos)) { endPos = code.indexOf(snippetEnd, matcher.end()); if (endPos < 0) { throw new IllegalArgumentException( "No snippetEnd marker found for snippetStart '" + code.substring(matcher.start(), matcher.end()) + "'"); } final String name = matcher.group(1); if (snippets.containsKey(name)) { throw new IllegalArgumentException("Snippet with name '" + name + "' already existing."); } snippets.put(name, trim(code.substring(matcher.end(), endPos))); endPos += snippetEnd.length(); } return snippets; }
/** * Processes the given {@code url} and adds all associated class files to the given {@code * classNames}. */ private void processUrl(Set<String> classNames, URL url) { if (url.getPath().endsWith(".jar")) { try { InputStream urlInput = url.openStream(); try { @SuppressWarnings("all") JarInputStream jarInput = new JarInputStream(urlInput); Manifest manifest = jarInput.getManifest(); if (manifest != null) { Attributes attributes = manifest.getMainAttributes(); if (attributes != null && Boolean.parseBoolean(attributes.getValue(INCLUDE_ATTRIBUTE))) { for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null; ) { String name = entry.getName(); if (name.endsWith(CLASS_FILE_SUFFIX)) { String className = name.substring(0, name.length() - CLASS_FILE_SUFFIX.length()); className = className.replace('/', '.'); classNames.add(className); } } } } } finally { urlInput.close(); } } catch (IOException ex) { } } else { File file = IoUtils.toFile(url, StringUtils.UTF_8); if (file != null && file.isDirectory()) { processFile(classNames, file, ""); } } }
public void testGetClasspathPath() { final String absUnixPath = "/one/two/three"; File f = new File(absUnixPath); assertTrue( "Path is wrong abs " + IoUtils.getClasspathPath(f), IoUtils.getClasspathPath(f).equals(absUnixPath)); final String relUnixPath = "one/two/three"; f = new File(relUnixPath); assertTrue( "Path is wrong rel " + IoUtils.getClasspathPath(f), IoUtils.getClasspathPath(f).equals(relUnixPath)); final String nameUnixPath = "three"; f = new File(nameUnixPath); assertTrue( "Path is wrong name " + IoUtils.getClasspathPath(f), IoUtils.getClasspathPath(f).equals(nameUnixPath)); }
private static File getFile(WebPageContext page) throws IOException { String file = page.param(String.class, "file"); if (file == null) { String servletPath = page.param(String.class, "servletPath"); if (servletPath != null) { file = page.getServletContext().getRealPath(servletPath); } } if (file == null) { return null; } File fileInstance = new File(file); if (!fileInstance.exists()) { IoUtils.createParentDirectories(fileInstance); } return fileInstance; }
private void doEdit(WebPageContext page) throws IOException, ServletException { final Type type = page.paramOrDefault(Type.class, "type", Type.JAVA); final File file = getFile(page); final StringBuilder codeBuilder = new StringBuilder(); if (file != null) { if (file.exists()) { if (file.isDirectory()) { // Can't edit a directory. } else { codeBuilder.append(IoUtils.toString(file, StandardCharsets.UTF_8)); } } else { String filePath = file.getPath(); if (filePath.endsWith(".java")) { filePath = filePath.substring(0, filePath.length() - 5); for (File sourceDirectory : CodeUtils.getSourceDirectories()) { String sourceDirectoryPath = sourceDirectory.getPath(); if (filePath.startsWith(sourceDirectoryPath)) { String classPath = filePath.substring(sourceDirectoryPath.length()); if (classPath.startsWith(File.separator)) { classPath = classPath.substring(1); } int lastSepAt = classPath.lastIndexOf(File.separatorChar); if (lastSepAt < 0) { codeBuilder.append("public class "); codeBuilder.append(classPath); } else { codeBuilder.append("package "); codeBuilder.append( classPath.substring(0, lastSepAt).replace(File.separatorChar, '.')); codeBuilder.append(";\n\npublic class "); codeBuilder.append(classPath.substring(lastSepAt + 1)); } codeBuilder.append(" {\n}"); break; } } } } } else { Set<String> imports = findImports(); imports.add("com.psddev.dari.db.*"); imports.add("com.psddev.dari.util.*"); imports.add("java.util.*"); String includes = Settings.get(String.class, INCLUDE_IMPORTS_SETTING); if (!ObjectUtils.isBlank(includes)) { Collections.addAll(imports, includes.trim().split("\\s*,?\\s+")); } String excludes = Settings.get(String.class, EXCLUDE_IMPORTS_SETTING); if (!ObjectUtils.isBlank(excludes)) { for (String exclude : excludes.trim().split("\\s*,?\\s+")) { imports.remove(exclude); } } for (String i : imports) { codeBuilder.append("import "); codeBuilder.append(i); codeBuilder.append(";\n"); } codeBuilder.append('\n'); codeBuilder.append("public class Code {\n"); codeBuilder.append(" public static Object main() throws Throwable {\n"); String query = page.param(String.class, "query"); String objectClass = page.paramOrDefault(String.class, "objectClass", "Object"); if (query == null) { codeBuilder.append(" return null;\n"); } else { codeBuilder .append(" Query<") .append(objectClass) .append("> query = ") .append(query) .append(";\n"); codeBuilder .append(" PaginatedResult<") .append(objectClass) .append("> result = query.select(0L, 10);\n"); codeBuilder.append(" return result;\n"); } codeBuilder.append(" }\n"); codeBuilder.append("}\n"); } new DebugFilter.PageWriter(page) { { List<Object> inputs = CodeDebugServlet.Static.getInputs(getServletContext()); Object input = inputs == null || inputs.isEmpty() ? null : inputs.get(0); String name; if (file == null) { name = null; } else { name = file.toString(); int slashAt = name.lastIndexOf('/'); if (slashAt > -1) { name = name.substring(slashAt + 1); } } startPage("Code Editor", name); writeStart("div", "class", "row-fluid"); if (input != null) { writeStart( "div", "class", "codeInput", "style", "bottom: 65px; position: fixed; top: 55px; width: 18%; z-index: 1000;"); writeStart("h2").writeHtml("Input").writeEnd(); writeStart( "div", "style", "bottom: 0; overflow: auto; position: absolute; top: 38px; width: 100%;"); writeObject(input); writeEnd(); writeEnd(); writeStart("style", "type", "text/css"); write(".codeInput pre { white-space: pre; word-break: normal; word-wrap: normal; }"); writeEnd(); writeStart("script", "type", "text/javascript"); write("$('.codeInput').hover(function() {"); write("$(this).css('width', '50%');"); write("}, function() {"); write("$(this).css('width', '18%');"); write("});"); writeEnd(); } writeStart( "div", "class", input != null ? "span9" : "span12", "style", input != null ? "margin-left: 20%" : null); writeStart( "form", "action", page.url(null), "class", "code", "method", "post", "style", "margin-bottom: 70px;", "target", "result"); writeElement("input", "name", "action", "type", "hidden", "value", "run"); writeElement("input", "name", "type", "type", "hidden", "value", type); writeElement("input", "name", "file", "type", "hidden", "value", file); writeElement( "input", "name", "jspPreviewUrl", "type", "hidden", "value", page.param(String.class, "jspPreviewUrl")); writeStart("textarea", "name", "code"); writeHtml(codeBuilder); writeEnd(); writeStart( "div", "class", "form-actions", "style", "bottom: 0; left: 0; margin: 0; padding: 10px 20px; position:fixed; right: 0; z-index: 1000;"); writeElement("input", "class", "btn btn-primary", "type", "submit", "value", "Run"); writeStart( "label", "class", "checkbox", "style", "display: inline-block; margin-left: 10px; white-space: nowrap;"); writeElement("input", "name", "isLiveResult", "type", "checkbox"); writeHtml("Live Result"); writeEnd(); writeStart( "label", "style", "display: inline-block; margin-left: 10px; white-space: nowrap;", "title", "Shortcut: ?_vim=true"); boolean vimMode = page.param(boolean.class, "_vim"); writeStart( "label", "class", "checkbox", "style", "display: inline-block; margin-left: 10px; white-space: nowrap;"); writeElement( "input", "name", "_vim", "type", "checkbox", "value", "true", vimMode ? "checked" : "_unchecked", "true"); writeHtml("Vim Mode"); writeEnd(); writeEnd(); writeElement( "input", "class", "btn btn-success pull-right", "name", "isSave", "type", "submit", "value", "Save"); writeElement( "input", "class", "btn pull-right", "style", "margin-right: 10px;", "name", "clearCode", "type", "submit", "value", "Clear"); writeEnd(); writeEnd(); writeStart( "div", "class", "resultContainer", "style", "background: rgba(255, 255, 255, 0.8);" + "border-color: rgba(0, 0, 0, 0.2);" + "border-style: solid;" + "border-width: 0 0 0 1px;" + "max-height: 45%;" + "top: 55px;" + "overflow: auto;" + "padding: 0px 20px 5px 10px;" + "position: fixed;" + "z-index: 3;" + "right: 0px;" + "width: 35%;"); writeStart("h2").writeHtml("Result").writeEnd(); writeStart("div", "class", "frame", "name", "result"); writeEnd(); writeEnd(); writeStart("script", "type", "text/javascript"); write("$('body').frame();"); write("var $codeForm = $('form.code');"); write("var lineMarkers = [ ];"); write("var columnMarkers = [ ];"); write("var codeMirror = CodeMirror.fromTextArea($('textarea')[0], {"); write("'indentUnit': 4,"); write("'lineNumbers': true,"); write("'lineWrapping': true,"); write("'matchBrackets': true,"); write("'mode': 'text/x-java'"); write("});"); // Save code to local storage when the user stops typing for 1 second write("if (window.localStorage !== undefined) {"); write("var baseCode = $('textarea[name=code]').text();"); write("var windowStorageCodeKey = 'bsp.codeDebugServlet.code';"); write( "if (window.localStorage.getItem(windowStorageCodeKey) !== null && window.localStorage.getItem(windowStorageCodeKey).trim()) {"); write("codeMirror.getDoc().setValue(window.localStorage.getItem(windowStorageCodeKey))"); write("}"); write("var typingTimer;"); write("codeMirror.on('keydown', function() {"); write("clearTimeout(typingTimer);"); write("});"); write("codeMirror.on('keyup', function() {"); write("clearTimeout(typingTimer);"); write("typingTimer = setTimeout(function(){"); write( "window.localStorage.setItem(windowStorageCodeKey, codeMirror.getDoc().getValue());},"); write("1000);"); write("});"); write("}"); // Reset code to original page load value and clear window storage write("$('.form-actions input[name=clearCode]').on('click', function(e) {"); write("e.preventDefault();"); write("if (baseCode !== undefined && baseCode.trim()) {"); write("codeMirror.getDoc().setValue(baseCode);"); write("if (window.localStorage !== undefined) {"); write("window.localStorage.removeItem(windowStorageCodeKey);"); write("}"); write("}"); write("});"); write("codeMirror.on('change', $.throttle(1000, function() {"); write("if ($codeForm.find(':checkbox[name=isLiveResult]').is(':checked')) {"); write("$codeForm.submit();"); write("}"); write("}));"); write("$('input[name=_vim]').change(function() {"); write("codeMirror.setOption('vimMode', $(this).is(':checked'));"); write("});"); write("$('input[name=_vim]').change();"); int line = page.param(int.class, "line"); if (line > 0) { write("var line = "); write(String.valueOf(line)); write(" - 1;"); write("codeMirror.setCursor(line);"); write("codeMirror.addLineClass(line, 'selected', 'selected');"); write("$(window).scrollTop(codeMirror.cursorCoords().top - $(window).height() / 2);"); } write("var $resultContainer = $('.resultContainer');"); write("$resultContainer.find('.frame').bind('load', function() {"); write( "$.each(lineMarkers, function() { codeMirror.clearMarker(this); codeMirror.setLineClass(this, null, null); });"); write("$.each(columnMarkers, function() { this.clear(); });"); write("var $frame = $(this).find('.syntaxErrors li').each(function() {"); write("var $error = $(this);"); write("var line = parseInt($error.attr('data-line')) - 1;"); write("var column = parseInt($error.attr('data-column')) - 1;"); write("if (line > -1 && column > -1) {"); write("lineMarkers.push(codeMirror.setMarker(line, '!'));"); write("codeMirror.setLineClass(line, 'errorLine', 'errorLine');"); write( "columnMarkers.push(codeMirror.markText({ 'line': line, 'ch': column }, { 'line': line, 'ch': column + 1 }, 'errorColumn'));"); write("}"); write("});"); write("});"); writeEnd(); writeEnd(); writeEnd(); endPage(); } @Override public void startBody(String... titles) throws IOException { writeStart("body"); writeStart("div", "class", "navbar navbar-fixed-top"); writeStart("div", "class", "navbar-inner"); writeStart("div", "class", "container-fluid"); writeStart("span", "class", "brand"); writeStart("a", "href", DebugFilter.Static.getServletPath(page.getRequest(), "")); writeHtml("Dari"); writeEnd(); writeHtml("Code Editor \u2192 "); writeEnd(); writeStart( "form", "action", page.url(null), "method", "get", "style", "float: left; height: 40px; line-height: 40px; margin: 0; padding-left: 10px;"); writeStart( "select", "class", "span6", "name", "file", "onchange", "$(this).closest('form').submit();"); writeStart("option", "value", ""); writeHtml("PLAYGROUND"); writeEnd(); for (File sourceDirectory : CodeUtils.getSourceDirectories()) { writeStart("optgroup", "label", sourceDirectory); writeStart( "option", "selected", sourceDirectory.equals(file) ? "selected" : null, "value", sourceDirectory); writeHtml("NEW CLASS IN ").writeHtml(sourceDirectory); writeEnd(); writeFileOption(file, sourceDirectory, sourceDirectory); writeEnd(); } writeEnd(); writeEnd(); includeStylesheet("/_resource/chosen/chosen.css"); includeScript("/_resource/chosen/chosen.jquery.min.js"); writeStart("script", "type", "text/javascript"); write("(function() {"); write("$('select[name=file]').chosen({ 'search_contains': true });"); write("})();"); writeEnd(); writeEnd(); writeEnd(); writeEnd(); writeStart("div", "class", "container-fluid", "style", "padding-top: 54px;"); } private void writeFileOption(File file, File sourceDirectory, File source) throws IOException { if (source.isDirectory()) { for (File child : source.listFiles()) { writeFileOption(file, sourceDirectory, child); } } else { writeStart( "option", "selected", source.equals(file) ? "selected" : null, "value", source); writeHtml(source.toString().substring(sourceDirectory.toString().length())); writeEnd(); } } }; }
@Test public void testFollowLinks() { assertTrue("Null ?", IoUtils.followLinks((LinkOption[]) null)); assertTrue("Empty ?", IoUtils.followLinks(IoUtils.EMPTY_LINK_OPTIONS)); assertFalse("No-follow ?", IoUtils.followLinks(IoUtils.getLinkOptions(false))); }
public ModifiableFileWatcher(Path file) { this(file, IoUtils.getLinkOptions(false)); }