Java Code Examples for org.eclipse.core.filesystem.IFileStore#openInputStream()

The following examples show how to use org.eclipse.core.filesystem.IFileStore#openInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ManifestUtils.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Inner helper method parsing single manifests with additional semantic analysis.
 */
protected static ManifestParseTree parseManifest(IFileStore manifestFileStore, String targetBase, Analyzer analyzer) throws CoreException, IOException, ParserException, AnalyzerException {

	/* basic sanity checks */
	IFileInfo manifestFileInfo = manifestFileStore.fetchInfo();
	if (!manifestFileInfo.exists() || manifestFileInfo.isDirectory())
		throw new IOException(ManifestConstants.MISSING_OR_INVALID_MANIFEST);

	if (manifestFileInfo.getLength() == EFS.NONE)
		throw new IOException(ManifestConstants.EMPTY_MANIFEST);

	if (manifestFileInfo.getLength() > ManifestConstants.MANIFEST_SIZE_LIMIT)
		throw new IOException(ManifestConstants.MANIFEST_FILE_SIZE_EXCEEDED);

	InputStream inputStream = manifestFileStore.openInputStream(EFS.NONE, null);
	ManifestParseTree manifestTree = null;
	try {
		manifestTree = parseManifest(inputStream, targetBase, analyzer);
	} finally {
		if (inputStream != null) {
			inputStream.close();
		}
	}
	return manifestTree;
}
 
Example 2
Source File: NodeJSDeploymentPlanner.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Looks for a Procfile and parses the web command.
 * @return <code>null</code> iff there is no Procfile present or it does not contain a web command.
 */
protected String getProcfileCommand(IFileStore contentLocation) {
	IFileStore procfileStore = contentLocation.getChild(ManifestConstants.PROCFILE);
	if (!procfileStore.fetchInfo().exists())
		return null;

	InputStream is = null;
	try {

		is = procfileStore.openInputStream(EFS.NONE, null);
		Procfile procfile = Procfile.load(is);
		return procfile.get(ManifestConstants.WEB);

	} catch (Exception ex) {
		/* can't parse the file, fail */
		return null;

	} finally {
		IOUtilities.safeClose(is);
	}
}
 
Example 3
Source File: GenericDeploymentPlanner.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Looks for a Procfile and parses the web command.
 * @return <code>null</code> iff there is no Procfile present or it does not contain a web command.
 */
private String getProcfileCommand(IFileStore contentLocation) {
	IFileStore procfileStore = contentLocation.getChild(ManifestConstants.PROCFILE);
	if (!procfileStore.fetchInfo().exists())
		return null;

	InputStream is = null;
	try {

		is = procfileStore.openInputStream(EFS.NONE, null);
		Procfile procfile = Procfile.load(is);
		return procfile.get(ManifestConstants.WEB);

	} catch (Exception ex) {
		/* can't parse the file, fail */
		return null;

	} finally {
		IOUtilities.safeClose(is);
	}
}
 
Example 4
Source File: Packager.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
protected void writeZip(IFileStore source, IPath path, ZipOutputStream zos) throws CoreException, IOException {

		/* load .cfignore if present */
		IFileStore cfIgnoreFile = source.getChild(CF_IGNORE_FILE);
		if (cfIgnoreFile.fetchInfo().exists()) {

			IgnoreNode node = new IgnoreNode();
			InputStream is = null;

			try {

				is = cfIgnoreFile.openInputStream(EFS.NONE, null);
				node.parse(is);

				cfIgnore.put(source.toURI(), node);

			} finally {
				if (is != null)
					is.close();
			}
		}

		IFileInfo info = source.fetchInfo(EFS.NONE, null);

		if (info.isDirectory()) {

			if (!isIgnored(source, path, true))
				for (IFileStore child : source.childStores(EFS.NONE, null))
					writeZip(child, path.append(child.getName()), zos);

		} else {

			if (!isIgnored(source, path, false)) {
				ZipEntry entry = new ZipEntry(path.toString());
				zos.putNextEntry(entry);
				IOUtilities.pipe(source.openInputStream(EFS.NONE, null), zos, true, false);
			}
		}
	}
 
Example 5
Source File: NodeJSDeploymentPlanner.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Looks for the package.json and parses the start command.
 * @return <code>null</code> iff the package.json does not contain an explicit start command.
 */
protected String getPackageCommand(IFileStore contentLocation) {
	IFileStore packageStore = contentLocation.getChild(NodeJSConstants.PACKAGE_JSON);
	if (!packageStore.fetchInfo().exists())
		return null;

	InputStream is = null;
	try {

		is = packageStore.openInputStream(EFS.NONE, null);
		JSONObject packageJSON = new JSONObject(new JSONTokener(new InputStreamReader(is)));
		if (packageJSON.has(NodeJSConstants.SCRIPTS)) {
			JSONObject scripts = packageJSON.getJSONObject(NodeJSConstants.SCRIPTS);
			if (scripts.has(NodeJSConstants.START))
				return scripts.getString(NodeJSConstants.START);
		}

	} catch (Exception ex) {
		/* can't parse the file, fail */
		return null;

	} finally {
		IOUtilities.safeClose(is);
	}

	return null;
}
 
Example 6
Source File: ResourceUtils.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
public static String toString(IFileStore fileStore, IProgressMonitor monitor) throws CoreException {
  	try {
  		if (fileStore.fetchInfo().exists()) {
  			try(InputStream stream = fileStore.openInputStream(EFS.NONE , monitor)){
  				return IOUtils.toString(stream);
  			}
  		}
} catch (IOException e) {
	ExceptionHelper.rethrowAsCoreException(e);
}
  	return null;
  }
 
Example 7
Source File: RemoteImportTracesOperation.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
private IResource downloadFileTrace(IFileStore trace, IFolder traceFolder, IProgressMonitor monitor) throws CoreException, IOException, InterruptedException {

        IFolder folder = traceFolder;
        String traceName = trace.getName();

        traceName = TmfTraceCoreUtils.validateName(traceName);

        IResource resource = folder.findMember(traceName);
        if ((resource != null) && resource.exists()) {
            String newName = fConflictHandler.checkAndHandleNameClash(resource.getFullPath(), monitor);
            if (newName == null) {
                return null;
            }
            traceName = newName;
        }
        SubMonitor subMonitor = SubMonitor.convert(monitor, 1);
        subMonitor.beginTask(RemoteMessages.RemoteImportTracesOperation_DownloadTask, 1);

        IPath destination = folder.getLocation().addTrailingSeparator().append(traceName);
        IFileInfo info = trace.fetchInfo();
        subMonitor.setTaskName(RemoteMessages.RemoteImportTracesOperation_DownloadTask + ' ' + trace.getName() + '/' + trace.getName());
        try (InputStream in = trace.openInputStream(EFS.NONE, new NullProgressMonitor())) {
            copy(in, folder, destination, subMonitor, info.getLength());
        }
        folder.refreshLocal(IResource.DEPTH_INFINITE, null);
        return folder.findMember(traceName);
    }
 
Example 8
Source File: BuildSettingWizardPage.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static void copyFile(IFileStore source, File target) throws IOException, CoreException {
	try (InputStream is = source.openInputStream(EFS.NONE, null)) {
		try (FileOutputStream os = new FileOutputStream(target)) {
			unsecureCopyFile(is, os);
		}
	}
}
 
Example 9
Source File: FileHandlerV1.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
private void handlePatchContents(HttpServletRequest request, BufferedReader requestReader, HttpServletResponse response, IFileStore file)
		throws IOException, CoreException, NoSuchAlgorithmException, JSONException, ServletException {
	JSONObject changes = OrionServlet.readJSONRequest(request);
	// read file to memory
	Reader fileReader = new InputStreamReader(file.openInputStream(EFS.NONE, null));
	StringWriter oldFile = new StringWriter();
	IOUtilities.pipe(fileReader, oldFile, true, false);
	StringBuffer oldContents = oldFile.getBuffer();
	// Remove the BOM character if it exists
	if (oldContents.length() > 0) {
		char firstChar = oldContents.charAt(0);
		if (firstChar == '\uFEFF' || firstChar == '\uFFFE') {
			oldContents.replace(0, 1, "");
		}
	}
	JSONArray changeList = changes.getJSONArray("diff");
	for (int i = 0; i < changeList.length(); i++) {
		JSONObject change = changeList.getJSONObject(i);
		long start = change.getLong("start");
		long end = change.getLong("end");
		String text = change.getString("text");
		oldContents.replace((int) start, (int) end, text);
	}

	String newContents = oldContents.toString();
	boolean failed = false;
	if (changes.has("contents")) {
		String contents = changes.getString("contents");
		if (!newContents.equals(contents)) {
			failed = true;
			newContents = contents;
		}
	}
	Writer fileWriter = new OutputStreamWriter(file.openOutputStream(EFS.NONE, null), "UTF-8");
	IOUtilities.pipe(new StringReader(newContents), fileWriter, false, true);
	if (failed) {
		statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_ACCEPTABLE,
				"Bad File Diffs. Please paste this content in a bug report: \u00A0\u00A0 	" + changes.toString(), null));
		return;
	}

	// return metadata with the new Etag
	handleGetMetadata(request, response, file);
}
 
Example 10
Source File: RemoteImportTracesOperation.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
private IResource downloadDirectoryTrace(IFileStore trace, IFolder traceFolder, IProgressMonitor monitor) throws CoreException, IOException, InterruptedException {

        IFileStore[] sources = trace.childStores(EFS.NONE, monitor);

        // Don't import just the metadata file
        if (sources.length > 1) {
            String traceName = trace.getName();

            traceName = TmfTraceCoreUtils.validateName(traceName);

            IFolder folder = traceFolder.getFolder(traceName);
            String newName = fConflictHandler.checkAndHandleNameClash(folder.getFullPath(), monitor);
            if (newName == null) {
                return null;
            }

            folder = traceFolder.getFolder(newName);
            folder.create(true, true, null);

            SubMonitor subMonitor = SubMonitor.convert(monitor, sources.length);
            subMonitor.beginTask(RemoteMessages.RemoteImportTracesOperation_DownloadTask, sources.length);

            for (IFileStore source : sources) {
                if (subMonitor.isCanceled()) {
                    throw new InterruptedException();
                }

                IPath destination = folder.getLocation().addTrailingSeparator().append(source.getName());
                IFileInfo info = source.fetchInfo();
                // TODO allow for downloading index directory and files
                if (!info.isDirectory()) {
                    SubMonitor childMonitor = subMonitor.newChild(1);
                    childMonitor.setTaskName(RemoteMessages.RemoteImportTracesOperation_DownloadTask + ' ' + trace.getName() + '/' + source.getName());
                    try (InputStream in = source.openInputStream(EFS.NONE, new NullProgressMonitor())) {
                        copy(in, folder, destination, childMonitor, info.getLength());
                    }
                }
            }
            folder.refreshLocal(IResource.DEPTH_INFINITE, null);
            return folder;
        }
        return null;
    }
 
Example 11
Source File: NewJavaProjectWizardPageTwo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void copyFile(IFileStore source, File target) throws IOException, CoreException {
	InputStream is= source.openInputStream(EFS.NONE, null);
	FileOutputStream os= new FileOutputStream(target);
	copyFile(is, os);
}