Download a zip file through wicket - wicket

I am using wicket framework, and I have made a zip file by Java code, I want to have a link to download it, I don't know if it is possible or I should make the zip file by wicket (but not Java) and then have a link to download.

Take a look at ZipResourceStream. With this class you can generate zip contents of a directory on the fly, and use a org.apache.wicket.markup.html.link.ResourceLink with ResourceStreamResource to link to it.
File file = new File(path);
IResourceStream resStream = new ZipResourceStream(file);
ResourceStreamResource resource = new ResourceStreamResource(resStream);
ResourceLink link = new ResourceLink("link", resource);
add(link);
Alternatively, if you prefer to zip the file with another tool, you can use DownloadLink:
File zipFile = generateZipFile();
IModel fileModel = new Model(zipFile);
add(new DownloadLink("dllink", fileModel);
If you prefer to generate the File on the fly in the Link's onClick, take a look at this question: How to use Wicket's DownloadLink with a file generated on the fly?

Related

Dropbox API overwrite upload files instead of rename it

I am trying to upload some files to dropbox using their java API (version 2-beta-4), but some of these files have the same name.
What I would like to know is: What is the reason for I send a file (for instance "file.txt") to dropbox, this file is uploaded, but if I send another file with the same name (file.txt) dropbox overwrite the old file with this new one instead of renaming it to "file (1).txt", even I am setting autorename true and the WriteMode to add?
Code:
DbxRequestConfig config = new DbxRequestConfig("sample", "pt_BR");
String acessToken = "...";
client = new DbxClientV2(config, accessToken);
InputStream input = new ByteArrayInputStream(file.getBytes());
FileMetadata file = client.files.uploadBuilder(path).mode(WriteMode.add).autorename(true)
.mute(true).run(input);
Thanks.
WriteMode.add is what's causing this behavior. "Add" means "Add a new file with this name," so it never overwrites an existing file. If you want to overwrite the existing file, use WriteMode.overwrite.
(Also, isn't it WriteMode.add() and WriteMode.overwrite()? I thought those were methods.)

Filter file system file selection dialog by ContentTypeId

In the Eclipse plugins we're developing, we've defined a custom content type, using the org.eclipse.core.contenttype.contentTypes extension point. We're successfully using the content type to enable or disable UI components, based on, e.g., whether the user is editing a file of that type.
I'd like to take this ones step further and also use it to filter files in a file selection dialog, such that it only shows files that match the content type.
I have found that filtering a JFace Viewer this way is possible, so for files in the workspace, we could use an ElementTreeSelectionDialog and add a ViewerFilter.
Is there a way to do the same for a file selection dialog of the entire file system (instead of filtering by file extension)? Or is this impossible because it's restricted to the OS's filtering?
The standard SWT FileDialog can only be filtered by file extension and cannot be extended.
You could write your own file selection dialog using the normal Java File or Path APIs with a tree viewer and a viewer filter.
Because the files are outside of the workspace you can't use any of the IResource, IFile, IFolder APIs. However you can still use the IContentTypeManager interface which gives you access to your content types.
IContentTypeManager manager = Platform.getContentTypeManager();
If the file extension is enough to distinguish the files you can just use:
IContentType contentType = manager.findContentTypeFor("the file name");
If you need to use the content describers use:
InputStream stream = ... new FileInputStream(....
IContentType contentType = manager.findContentTypeFor(stream, "the file name");
stream.close();

Read AndroidManifest.xml in eclipse plugin code without opening it in IEditor

First I tried to open it in eclipse editor and then read it using org.eclipse.jface.text.IDocument API. But it is quite lengthy, so I tried to figure out way to directly read the file. Here are the pieces that I have found in this regard.
IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
IPath path = project.getFullPath();
String strr=path.toString();
IFile manifest = project.getFile("AndroidManifest.xml");
IPath file_path = manifest.getFullPath().makeAbsolute();
Now, I can use getContents() method of IFile interface. But how to read the contents as it appears in editor(Eclipse editor or IEditor) as I have no idea how AndroidManifest.xml is encoded.

Exporting an JAR file in Eclipse and referencing a file

I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers
You are almost there.
Images in a jar are treated as resources. You need to refer to them using the classpath
Just use getClass().getResource: something like:
getClass().getResource("/images/logo.jpg"));
where "images" is a package inside the jar file, with the path as above
see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct
Also see:
How to includes all images in jar file using eclipse
See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.

ResourceException when creating IMarker on IFile (linked resource)

I have some problems updating an "old" Eclipse plugin. Here is what I would like to do and what the original plugin did:
(parse compiler output on console with file name and error information --> still works)
--> set link to the location within the file
--> set marker to location in the file
What I did in the past was to get the IFile from the path String of the file and generated link and marker from it:
IFile ifile;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath path = new Path(fileName);
IFiles[] files = workspace.getRoot().findFilesForLocation(path);
...
ifile = iFiles[0];
Map attributes = new HashMap();
attributes.put(IMarker.SEVERITY, new Integer (severity));
MarkerUtilities.setLineNumber(attributes, lineNumber);
MarkerUtilities.setMessage(attributes, message);
MarkerUtilities.createMarker(ifile, attributes,
IMarker
Since findFilesForLocation is deprecated, I tried to find another way but I am not succeeding whatsoever. Using the changed code to get the IFile always results in a exception: org.eclipse.core.internal.resources.ResourceException: Resource '/path/to/file.c' does not exist.
Is it possible that this relates to the fact that the source file is only linked into the project, and not physically within the project?
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath location = new Path(fileName);
IFile ifile = workspace.getRoot().getFile(location);
Can anyone help?
Thank you!
I am guessing that fileName is the fully qualified path to the file you want to get. I'm also guessing that the file that you are looking for is already in the workspace, even if it is linked (if not, then this won't work. You will first need to add the file to a project before getting the IFile for it).
You need to do something like this:
IFiles[] files = workspace.getRoot().findFilesForLocationURI("file:" + fileName);
Then this will find all files in the workspace that correspond to the file in the file system.
The reason why your attempt above is giving you a ResourceException is that you are trying to pass in a file system path to get an IFile object from the workspace. The Eclipse workspace is an abstraction over the underlying filesystem and cannot directly work with absolute paths.
For the Resources APIs, Paths usually means a path in the workspace and Location usually refers to a place in the filesystem or outside the workspace. If you already have a workspace path to start with, just ask the IWorkspaceRoot for the IFile and get on with what you're doing.